@isTest
private class TSRepairControllerTest {
    public static TS_Repair__c tsr;
    public static TS_Repair__c tsr2;
    public static BusinessActivity__c ba1;
    public static BusinessActivity__c ba2;
    public static BusinessActivity__c ba3;

    public static User_FaultInfo__c uf1;
    public static User_FaultInfo__c uf2;
    public static User_FaultInfo__c uf3;

    public static FailureAsset__c fa1;
    public static FailureAsset__c fa2;
    public static FailureAsset__c fa3;

    public static Contact contact;

    public static Account acc1;
    public static Account acc2;
    
    @TestSetup
    static void setup(){
        TestDataUtility.CreatePIPolicyConfigurations(new string[]{'Contact'});
    }

    static void basicData() {

        tsr = new TS_Repair__c();
        tsr.TSReportDate__c = Date.today();
        insert tsr;    
        tsr2 = new TS_Repair__c();
        tsr2.TSReportDate__c = Date.today();
        tsr2.TSReportStatus__c  = '申请中';
        insert tsr2;

        uf1 = new User_FaultInfo__c();
        insert uf1;
        FailureAsset__c fa1 = new FailureAsset__c();
        fa1.model__c = 'test1';
        fa1.FAUserFaultInfo__c = uf1.Id;
        insert fa1;
        ba1 = new BusinessActivity__c();
        ba1.BusinessATS__c = tsr.Id;
        // ba1.BusinessARepairDate__c =  Date.today();
        insert ba1;

        uf2 = new User_FaultInfo__c();
        insert uf2;
        fa2 = new FailureAsset__c();
        fa2.model__c = 'test2';
        fa2.FAUserFaultInfo__c = uf2.Id;
        insert fa2;
        ba2 = new BusinessActivity__c();
        ba2.BusinessATS__c = tsr2.Id;
        ba2.User_FaultInfo__c = uf2.Id;
        insert ba2;

        User_FaultInfo__c uf3 = new User_FaultInfo__c();
        insert uf3;
        fa3 = new FailureAsset__c();
        fa3.model__c = 'test3';
        fa3.FAUserFaultInfo__c = uf3.Id;
        insert fa3;
        ba3 = new BusinessActivity__c();
        ba3.BusinessATS__c = tsr2.Id;
        ba3.User_FaultInfo__c = uf3.Id;
        insert ba3;

        Attachment att = new Attachment(
            Name = 'ORDER-123456.pdf',
            parentId = ba1.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att;

        Attachment att2 = new Attachment(
            Name = 'not-ORDER-123456.pdf',
            parentId = ba2.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att2;


        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        acc1 = new Account(
            name = '*',
            RecordTypeId = rectIE[0].Id, 
            OwnerId = UserInfo.getUserId(),
            DivisionName__c = 'Service IE',
            FacilityName__c = 'abc',
            DivisionName_D__c = 'Service IE',
            FacilityNameD__c = 'abc',
            AccountStatus__c = 'Cancel',
            PostCode__c = '123456'
        );
        insert acc1;

        acc2 = new Account(
            name = '*',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c = 'Service IE2',
            FacilityName__c = 'abc2',
            DivisionName_D__c = 'Service IE2',
            FacilityNameD__c = 'abc2',
            AccountStatus__c = 'Cancel',
            PostCode__c = '123456'
        );
        insert acc2;
        List<Account> selectAccount = [ Select Id,Name from Account];
        contact = new Contact();
        contact = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=acc2.id);
        insert contact;

    }
    // 保存验证
    static testMethod void saveValidation() {
        basicData();
        ba1.serviceType__c = '硬件修理';
        ba1.BusinessAServiceCategory__c = 'Repair';
        ba1.User_FaultInfo__c = uf1.Id;
        ba1.BusinessADetermineDate__c = Date.today().addDays(-2);
        ba1.BusinessARepairDate__c = Date.today().addDays(-1);
        ba1.BusinessADateRequest__c = Date.today().addDays(-3);
        ba1.BusinessANatureService__c = '有偿';
        ba1.BusinessADateApproval__c = Date.today().addDays(1);
        ba1.BusinessADateQuotation__c = Date.today().addDays(-2);
        ba1.BusinessACompletionDate__c = Date.today().addDays(1);

        ba2.serviceType__c = '硬件修理';
        ba2.BusinessAServiceCategory__c = 'Repair';
        ba2.BusinessACustomerUnit__c = 'Test 01';
        ba2.BusinessAAccount__c = acc1.Id;
        ba2.BusinessAContact__c = contact.Id;
        ba2.BusinessANatureService__c = '有偿';
        ba2.BusinessADateApproval__c = null;
        ba2.BusinessADateQuotation__c  = null;

        ba3.serviceType__c = '硬件修理';
        ba3.BusinessAServiceCategory__c = 'Repair';
        ba3.BusinessACustomerUnit__c = '';
        // ba3.BusinessAAccount__c = acc1.Id;
        ba3.BusinessAContact__c = contact.Id;
        ba3.BusinessANatureService__c = '无偿';
        ba3.BusinessAWithoutReason__c = '';
        ba3.BusinessADateApproval__c = null;
        ba3.BusinessADateQuotation__c  = null;

        PageReference page = new PageReference('/apex/TSRepair?Id='+tsr.Id);
        System.Test.setCurrentPage(page);
        TSRepairController controller = new TSRepairController();
        controller.init(); 
        controller.RepairInfoList[0].ba = ba1;
        controller.RepairInfoList[1].ba = ba2;
        controller.RepairInfoList[2].ba = ba3;
        controller.RepairInfoList[1].ba.BusinessAServiceContentNew__c = 'Test';
        controller.RepairInfoList[1].failureAssetList[0].model__c = 'Test';
        controller.RepairInfoList[1].failureAssetList[1].model__c = 'Test';
        // controller.save();
        controller.baIds = ba3.Id + '/0';
        controller.FindTSRepair();
        controller.checkReportDate();
        controller.uploadingAttachment();
         
        controller.status  = '已批准';
        controller.setStatus();

        controller.addFailureAsset();
    }
    // 申请中 验证
    static testMethod void AuthenticationInformationValidation() {
        basicData();
        ba1.serviceType__c = '硬件修理';
        ba1.BusinessAServiceCategory__c = 'Repair';
        ba1.User_FaultInfo__c = uf1.Id;
        ba1.BusinessADetermineDate__c = Date.today().addDays(-2);
        ba1.BusinessARepairDate__c = Date.today().addDays(-1);
        ba1.BusinessADateRequest__c = Date.today().addDays(-3);
        ba1.BusinessANatureService__c = '有偿';
        ba1.BusinessADateApproval__c = Date.today().addDays(1);
        ba1.BusinessADateQuotation__c = Date.today().addDays(-2);
        ba1.BusinessACompletionDate__c = Date.today().addDays(1);

        ba2.serviceType__c = '硬件修理';
        ba2.BusinessAServiceCategory__c = 'Repair';
        ba2.BusinessACustomerUnit__c = 'Test 01';
        ba2.BusinessAAccount__c = acc1.Id;
        ba2.BusinessAContact__c = contact.Id;
        ba2.workingHours__c = 11.5;
        ba2.EndTimeHour__c = '20';
        ba2.EndTimeMinute__c = '00';

        ba3.serviceType__c = '硬件修理';
        ba3.BusinessAServiceCategory__c = 'Repair';
        ba3.BusinessACustomerUnit__c = '';
        ba3.BusinessAContact__c = contact.Id;

        PageReference page = new PageReference('/apex/TSRepair?Id='+tsr.Id);
        System.Test.setCurrentPage(page);
        TSRepairController controller = new TSRepairController();
        controller.init(); 
        controller.RepairInfoList[0].ba = ba1;
        controller.RepairInfoList[1].ba = ba2;
        controller.RepairInfoList[1].ba = ba3;
        controller.RepairInfoList[0].failureAssetList[0].model__c = 'Test';
        controller.RepairInfoList[0].failureAssetList[1].model__c = 'Test';

        controller.RepairInfoList[1].ba = ba2;
        controller.save();         
        controller.status  = '申请中';
        controller.setStatus();

        controller.addFailureAsset();
    }
    // 申请中
    static testMethod void save() {
        basicData();
        tsr2.TSReportStatus__c  = '申请中';
        // update tsr2;
        // 
        System.Test.startTest();
        BusinessActivity__c ba4 = new BusinessActivity__c();
        ba4.serviceType__c = '硬件修理';
        ba4.BusinessAServiceCategory__c = 'Repair';
        ba4.User_FaultInfo__c = uf1.Id;
        ba4.BusinessADetermineDate__c = Date.today().addDays(-1);
        ba4.BusinessARepairDate__c = Date.today().addDays(-1);
        ba4.BusinessANatureService__c = '有偿';
        ba4.BusinessACustomerUnit__c = 'abc2Service IE2';
        ba4.BusinessAAccount__c = acc2.Id;
        ba4.BusinessAContact__c = contact.Id;
        ba4.BusinessAHaveAttachment__c = true;
        ba4.firstInput__c = '否';
        ba4.workingHours__c = 10;
        insert ba4;
        BusinessActivity__c ba6 = new BusinessActivity__c();
        ba6.serviceType__c = '硬件修理';
        ba6.BusinessAServiceCategory__c = 'Repair';
        ba6.User_FaultInfo__c = uf1.Id;
        ba6.BusinessADetermineDate__c = Date.today().addDays(-1);
        ba6.BusinessARepairDate__c = Date.today().addDays(-1);
        ba6.BusinessANatureService__c = '有偿';
        ba6.BusinessACustomerUnit__c = 'abc2Service IE2';
        ba6.BusinessAAccount__c = acc2.Id;
        ba6.BusinessAContact__c = contact.Id;
        ba6.BusinessAHaveAttachment__c = true;
        ba6.firstInput__c = '是';
        ba6.workingHours__c = 10;
        insert ba6;
        PageReference page = new PageReference('/apex/TSRepair?Id='+tsr2.Id);
        System.Test.setCurrentPage(page);
        TSRepairController controller = new TSRepairController();
        controller.init(); 
        controller.status = '申请中';
        FailureAsset__c fa4 = new FailureAsset__c();
        fa4.model__c = 'test3';
        fa4.FAActivity__c = ba4.Id;
        insert fa4;
        controller.RepairInfoList[0].failureAssetList[0] = fa4;
        controller.RepairInfoList[0].ba = ba4;
        controller.save();
        System.Test.StopTest();
        controller.baIds = '/0';
        controller.FindTSRepair();
        controller.checkReportDate();
        controller.uploadingAttachment();

        controller.status  = '草案中';
        // controller.save();
        controller.setStatus();
    }
    // 草案中 / 驳回
    static testMethod void save1() {
        basicData();
        tsr2.TSReportStatus__c  = '申请中';

        BusinessActivity__c ba4 = new BusinessActivity__c();
        ba4.serviceType__c = '硬件修理';
        ba4.BusinessAServiceCategory__c = 'Repair';
        ba4.User_FaultInfo__c = uf1.Id;
        ba4.BusinessADetermineDate__c = Date.today().addDays(-1);
        ba4.BusinessARepairDate__c = Date.today().addDays(-1);
        ba4.BusinessANatureService__c = '有偿';
        ba4.BusinessACustomerUnit__c = 'abc2Service IE2';
        ba4.BusinessAAccount__c = acc2.Id;
        ba4.BusinessAContact__c = contact.Id;
        ba4.firstInput__c = '否';
        ba4.BusinessAHaveAttachment__c = true;
        ba4.workingHours__c = 10;
        TS_Repair__c tsr3 = new TS_Repair__c();
        PageReference page = new PageReference('/apex/TSRepair?Id='+tsr2.Id);
        System.Test.setCurrentPage(page);
        TSRepairController controller = new TSRepairController();
        controller.init(); 
        controller.status = '草案中';
        controller.RepairInfoList[0].ba = ba4;
        controller.lineno = '1';
        controller.fromUserFaultInfoId = uf1.Id;
        controller.initBusinessActivity();
        controller.save();
        controller.initPagePrompt('1');
        controller.initPagePrompt('2');
        controller.initPagePrompt('Submit');
        controller.initPagePrompt('Draft');
        controller.initPagePrompt('Approval');
        controller.initPagePrompt('Rejected');
        controller.ServiceContentNewAssembled('Test');
        controller.TSRepairAssembled(tsr3);
        page = new PageReference('/apex/TSRepair?Id='+tsr2.Id);
        System.Test.setCurrentPage(page);
        controller = new TSRepairController(new ApexPages.StandardController(tsr2));
        controller.init();
    }
    // 用户点击 新建TS日报按钮创建TS日报
    static testMethod void testMethod3() {
        basicData();
        PageReference page = new PageReference('/apex/TSRepair?Id=');
        System.Test.setCurrentPage(page);
        TSRepairController controller = new TSRepairController();
        controller.init(); 
        controller.FindTSRepair();
        controller.checkReportDate();
        controller.status  = '已批准';
        controller.setStatus();

        controller.addFailureAsset();
    }


}