@isTest
private class LexOPDPlanProdControllerTest {
    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
    static testMethod void init() {
        RecordType rectCam =
            [SELECT Id FROM RecordType
             WHERE IsActive = true AND SobjectType = 'Campaign'
                              AND DeveloperName = 'ServiceEngineerTraining'];
        Opportunity opp = new Opportunity(
            Name='Opp',
            StageName='引合',
            CloseDate=Date.today().addDays(10),
            Close_Forecasted_Date__c=Date.today(),
            CurrencyIsoCode = 'CNY'
        );
        insert opp;
        
        Quote q = new Quote(
            Name = 'quote',
            OpportunityId = opp.Id,
            Pricebook2Id = pricebookId
        );
        insert q;

        opp.Estimation_Id__c = q.Id;
        // opp.Estimation_Id__c = String.valueOf(Date d)
        update opp;
        Product2 prd1 = new Product2();
        prd1.Name     = 'Prd1';
        prd1.ProductCode_Ext__c     = 'Prd1';
        prd1.ProductCode            = 'N4480080';
        prd1.Asset_Model_No__c = '1001';
        prd1.Important_product__c = true;
        prd1.Important_Rroduct_1GI__c = true;
        prd1.RentalSubject__c = true;
        prd1.Manual_Entry__c = true;
        insert prd1;  

        PricebookEntry entry = new PricebookEntry( Pricebook2Id = pricebookId, Product2Id = prd1.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        insert entry;

        QuoteLineItem quoteItem = new QuoteLineItem();
        quoteItem.Name__c = 'テスト商品';
        quoteItem.QuoteId = q.id;
        quoteItem.Quantity = 1;
        quoteItem.Cost_Subtotal__c = 0;
        quoteItem.Cost__c = 200;
        quoteItem.Item_Order__c  = 3;
        quoteItem.UnitPrice = 10;
        quoteItem.Product2Id = prd1.id;
        quoteItem.PricebookEntryId = entry.Id;
        insert quoteItem;

        //OPD计划
        OPDPlan__c oPDPlan = new OPDPlan__c();
        oPDPlan.Status__c = '草案中';
        oPDPlan.OPDPlan_ImplementDate__c =  Date.today().addMonths(1);
        oPDPlan.NoOpp_Reason__c = 'HCP对应';     //无询价理由
        oPDPlan.OPDLendSortDraft__c = 1;       //备品借出优先度
        oPDPlan.OPDType__c = '询价';
        oPDPlan.RentalReson__c = 'OPD';
        insert oPDPlan;

        oPDPlan.Related_Opportunity1_ID__c = opp.Id;
        oPDPlan.NoOpp_Reason__c = null;
        update oPDPlan;
        OPDPlan__c oPDPlan1 = new OPDPlan__c();
        oPDPlan1.Status__c = '计划中';
        oPDPlan1.OPDPlan_ImplementDate__c =  Date.today().addMonths(1);
        oPDPlan1.NoOpp_Reason__c = 'HCP对应';     //无询价理由
        oPDPlan1.OPDLendSortDraft__c = 1;       //备品借出优先度
        oPDPlan1.OPDType__c = '事件';

        oPDPlan1.Related_Opportunity1_ID__c = opp.Id;
        oPDPlan1.NoOpp_Reason__c = null;
        insert oPDPlan1;

        Plan_Rental_Equipment__c planRE = new Plan_Rental_Equipment__c();
        planRE.OPD_Plan__c = oPDPlan.Id;
        planRE.Rental_Equipment__c = 'CF-H170I';
        planRE.Name = 'CF-H170I';
        planRE.Rental_Quantity__c = '10';
        planRE.ProductCode__c = 'N4480080';
        planRE.MDM_Model_No__c = 'CF-H170I';
        insert planRE;

        Campaign cam = new Campaign();
        cam.Name = 'cam';
        cam.Name2__c = '1234';
        cam.RecordTypeId = rectCam.Id;
        cam.StartDate = Date.today().addDays(-15);
        cam.EndDate = Date.today().addDays(18);
        cam.Mailflg_after45__c = true;
        cam.Mailflg_cancel__c = true;
        cam.Mailflg_before15__c = true;
        cam.Mailflg_before7__c = true;
        cam.Mailflg_after3__c = true;
        insert cam;


        List<ProLine> proLines = new List<ProLine>();
        ProLine proLine1 = new ProLine();
        proLine1.Id = planRE.Id;
        proLine1.Quantity = 10;
        proLine1.Name = 'CF-H170I11';
        proLine1.ProductCode= 'N44800801';
        proLine1.ProductModel= 'CF-H170I11';
        ProLine proLine2 = new ProLine();
        // proLine2.Id = planRE.Id;
        proLine2.Quantity = 10;
        proLine2.Name = 'CF-H170I1';
        proLine2.ProductCode= 'N4480080';
        proLine2.ProductModel= 'CF-H170I';
        proLines.add(proLine2);

        String jsonRecords = System.JSON.serialize(proLines);

        List<ProLine> proLines1 = new List<ProLine>();
        ProLine proLine3 = new ProLine();
        // proLine3.Id = planRE.Id;
        proLine3.Quantity = 10;
        proLine3.Name = 'CF-H170I11';
        proLine3.ProductCode= '12334';
        proLine3.ProductModel= '12334';
        proLines1.add(proLine3);

        String jsonRecords1 = System.JSON.serialize(proLines1);
        Test.startTest();
        LexOPDPlanProdController.init(oPDPlan.Id);
        LexOPDPlanProdController.initPRE(oPDPlan.Id);
        LexOPDPlanProdController.initPRE(cam.Id);
        LexOPDPlanProdController.getEditPermission(oPDPlan.Id);
        LexOPDPlanProdController.getEditPermission(cam.Id);
        LexOPDPlanProdController.saveProductData(oPDPlan.Id,jsonRecords);
        LexOPDPlanProdController.saveProductData(oPDPlan.Id,jsonRecords1);
        LexOPDPlanProdController.getProductData(oPDPlan.Id,'',jsonRecords,'N448');
        LexOPDPlanProdController.getProductData(oPDPlan.Id,'',jsonRecords,'');
        LexOPDPlanProdController.initGetQuoteProLine(oPDPlan.Id);
        oPDPlan.RentalReson__c = '模型出借';
        update oPDPlan;
        LexOPDPlanProdController.getProductData(oPDPlan.Id,'',jsonRecords,'');
        LexOPDPlanProdController.getProductData(oPDPlan.Id,'event',jsonRecords,'');
        LexOPDPlanProdController.initGetQuoteProLine(oPDPlan.Id);
        LexOPDPlanProdController.createQuotePREData(jsonRecords,oPDPlan.Id);
        LexOPDPlanProdController.getPicklistValues('Event','Activity_PurposeEscFSE__c');
        LexOPDPlanProdController.initOriginal(oPDPlan.Id);
        //LexOPDPlanProdController.initGetQuoteProLine(oPDPlan1.Id);
        Test.stopTest();
    }

    public class ProLine {
        @AuraEnabled
        public Id Id { get; set; } 
        @AuraEnabled
        public Id QuoteId { get; set; } 
        @AuraEnabled
        public Integer Quantity { get; set; } 
        @AuraEnabled
        public String Name { get; set; } 
        @AuraEnabled
        public String ProductCode { get; set; }  
        @AuraEnabled
        public String ProductModel { get; set; } 
        @AuraEnabled
        public String QuoteNo { get; set; } 
        @AuraEnabled
        public String FixtureModel { get; set; } 
        @AuraEnabled
        public Boolean Selected { get; set; } 
        
    } 

}