@isTest
private class buttonMaintenanceContractEstimateCtlTest {
    @TestSetup
    public static void setup() {
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        User MacOwner = new User(Test_staff__c = true, LastName = 'hp1', FirstName = 'owner1', Alias = 'hp', CommunityNickname = 'hpOwner1', Email = 'olympus_hpowner@sunbridge.com',Job_Category__c = '销售服务', Username = 'olympus_hpowner@sunbridge.com1', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert MacOwner;
        Account hospital = new Account();
        hospital.recordtypeId = 
            Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
            
        hospital.Name = 'test hospita/l';
        insert hospital;
        
        // 戦略科室を得る
        List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
        
        // 診療科を作る
        Account dep = new Account();
        dep.recordtypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_GI').getRecordTypeId();
        dep.Name = 'test de/p';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;
        
        // 製品を作る
        Product2 productA = new Product2( Name='テスト商品', Maintenance_Price_Year__c = 12000, Manual_Entry__c = false,IsActive=true,Family='GI',
                Fixture_Model_No__c='n01',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n01',
                ProductCode_Ext__c='pc01');
        insert productA;
        
        // 维修合同を作成する
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep[0].Id;
        contract.Department__c = dep.Id;
        contract.Service_Contract_Staff__c = MacOwner.Id;
        contract.Payment_Plan_Sum_First__c = 1;
        contract.Status__c = '契約';
        contract.Maintenance_Contract_No__c = '11123';
         contract.recordtypeId = 
             Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Contract').getRecordTypeId();
        contract.Not_Upper_limit_reason__c = ' 1';
        contract.Contract_Start_Date__c = Date.today().addDays( -10);   
        contract.Contract_End_Date__c = Date.today().addDays( 5);       
        contract.SalesOfficeCode_selection__c = '北京RC';
        Maintenance_Contract__c contract2 = contract.clone();
        contract2.Name = 'tect contract2';
           contract2.Contract_Start_Date__c = Date.today().addDays( -100);   
        contract2.Contract_End_Date__c = Date.today().addDays( -10);       
         contract2.Maintenance_Contract_No__c = '11a 123';
        contract2.First_Estimate_Date__c = Date.today().addDays( -80);
        insert new list<Maintenance_Contract__c> {contract,contract2};
        
        Maintenance_Contract_Estimate__c contactEsti1 = new Maintenance_Contract_Estimate__c(
                Name = 'contract estimate 1',
                RecordtypeId = 
                         Schema.SObjectType.Maintenance_Contract_Estimate__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Quote').getRecordTypeId(),
                Contract_Esti_Start_Date__c = Date.today(),
                Contract_Range__c = 2,
                Maintenance_Contract__c = contract.Id,
            Asset_Sum_Price__c = 1000,
            mainTalksTime__c = 1,
            talksStartDate__c = date.today(),
             Discount_reason__c ='1',
            Improve_ConsumptionRate_Idea__c = '1',
            NewEstimation_Amount__c = 100
            );
        insert contactEsti1;
        contract.Estimation_Id__c = contactEsti1.id;
        update contract;
        
    }

    @isTest
    public static void testInit() {
        Test.startTest();
        List<Maintenance_Contract_Estimate__c> mces = [select id from Maintenance_Contract_Estimate__c limit 1];
        if(mces.size()>0) {
            buttonMaintenanceContractEstimateCtl.init(mces[0].Id);
        }
        Test.stopTest();
    }

    @isTest
    public static void test_updateMaintenanceContract() {
        Test.startTest();
        List<Maintenance_Contract_Estimate__c> mces = [select id from Maintenance_Contract_Estimate__c limit 1];
        if(mces.size()>0) {
            buttonMaintenanceContractEstimateCtl.updateMaintenanceContract(mces[0].Id);
        }
        Test.stopTest();
    }
}