@isTest
private class StraightBackAddressControllerTest {

    private static Repair__c rpr { get; set; }
    @TestSetup
    static void makeData(){
        List<String> strList = new List<String>();
        strList.add('Address__c');
        strList.add('Contact');
        strList.add('Repair__c');
        strList.add('Agency_Contact__c');
        TestDataUtility.CreatePIPolicyConfigurations(strList);
    }
    @isTest
    static void testMethod1() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }
        //省
        Address_Level__c alc1 = new Address_Level__c(
            Name = '北京市'
        );
        insert new Address_Level__c[] {alc1};
        Address_Level2__c alc11 = new Address_Level2__c(
            Name = '顺义区',Address_Level__c = alc1.Id,Level1_Name__c='北京市'
        );
        Address_Level2__c alc12 = new Address_Level2__c(
            Name = '昌平区',Address_Level__c = alc1.Id,Level1_Name__c='北京市'
        );

        insert new Address_Level2__c[] {alc11,alc12};
        //
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = '北京医院';
        company.State_Master__c = alc1.id; 
        company.City_Master__c = alc11.id;
        upsert company;
        // Account section = [Select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];
        Account section = [Select Management_Code__c,  Name, Id from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];

        Account depart = new Account();
        depart.RecordTypeId        = rectDpt[0].Id;
        depart.Name                = '北京朝阳医院';
        depart.State_Master__c = alc1.id; 
        depart.City_Master__c = alc11.id;
        depart.Department_Name__c  = 'NFM104TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;

        // company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :company.Id];
        // depart = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart.Id];
        company = [select Management_Code__c,  Name, Id from Account where Id = :company.Id];
        depart = [select Management_Code__c,  Name, Id from Account where Id = :depart.Id];

        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'NFM104Prd1';
        prd1.ProductCode            = 'NFM104Prd1';
        prd1.Repair_Product_Code__c = 'NFM104Prd1_RP';
        prd1.Name                   = 'NFM104Prd1';
        prd1.Manual_Entry__c        = false;
        insert prd1;

        Asset ast = new Asset();
        ast.Name                             = 'NFM104Ast1';
        ast.AccountId                        = depart.Id;
        ast.Department_Class__c              = section.Id;
        ast.Hospital__c                      = company.Id;
        ast.Product2Id                       = prd1.Id;
        ast.SerialNumber                     = 'NFM104SerialNumber';
        ast.Guarantee_period_for_products__c = Date.today();
        ast.InstallDate                      = Date.today();
        insert ast;

        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];

        User us = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', Work_Location__c = '北京', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        us.Job_Category__c = '销售服务';
        insert us;

        rpr = new Repair__c();
        rpr.SAPRepairNo__c   = 'NFM104Rpr1';
        rpr.Account__c             = depart.Id;
        rpr.Department_Class__c    = section.Id;
        rpr.Hospital__c            = company.Id;
        rpr.Delivered_Product__c   = ast.Id;
        rpr.SalesOfficeCode_selection__c = '北京';
        rpr.Status__c              = '1.受理完毕';
        rpr.Incharge_Staff__c = us.Id;
        rpr.Repair_Detail__c = 'test';
        rpr.On_site_repair__c = '直送SORC修理';
        rpr.Returns_Product_way__c = '返送医院';
        rpr.Address_Type_Index__c=1;
        insert rpr;

        Contact contact2 = new Contact();
        contact2.AccountId = depart.Id;
        contact2.FirstName = '小明';
        contact2.LastName = 'test1经销商';
        insert contact2;

        Address__c ascc = new Address__c();
        ascc.Address_Classification__c = '医院';
        ascc.Customer__c = depart.Id;
        ascc.Contacts__c = contact2.id;
        ascc.Telephone__c = '123123';
        ascc.Province__c = alc1.id;
        ascc.City__c = alc11.id;
        ascc.Detailed_Address__c = '北京市顺义区西海洪村';
        insert ascc;


        //测试构造器里面的代码
        PageReference peg = new PageReference('/apex/StraightBackAddress?id='+rpr.id);
        System.Test.setCurrentPage(peg);
        
        StraightBackAddressController sbac = new StraightBackAddressController();
         sbac.txtName = '北京';
         sbac.txtAddress = '市';
         sbac.typeText = '医院';
         sbac.UpdId = ascc.id;
         sbac.adoptId = ascc.id;
         sbac.init();//页面初始化方法
         sbac.onEditor();//修改按钮方法
         sbac.onCopy();//复制按钮方法
         //sbac.optHospitalId = '0011000000V9Tcx';
         //sbac.onChJudge();
         sbac.insUpdData.Contacts__c = null;
         sbac.insUpdData.Create_Contacts__c = '王五';
         sbac.save();
         //sbac.adoptSave();
         sbac.searchBtn();
        sbac.insUpdData.Customer__c = depart.Id;
        sbac.queryRecordType();
        
        sbac.redirectPag();
        sbac.optionAddressClassification.Address_Classification__c = '经销商';
        sbac.searchBtn();
/*
         StraightBackAddressController  ss = new StraightBackAddressController();
         ss.txtName = '经销商';
         ss.txtAddress = '哈哈';
         ss.typeText = '经销商';
         ss.UpdId = ascc.id;
         ss.adoptId = ascc.id;
        ss.searchBtn();
         ss.init();//页面初始化方法
         
         ss.onEditor();//修改按钮方法
         ss.onCopy();//复制按钮方法
         ss.insUpdData.Address_Classification__c = '';
         ss.insUpdData.Province__c = null;
         ss.insUpdData.City__c = null;
         ss.insUpdData.Telephone__c = null;
         ss.insUpdData.Detailed_Address__c = null;
         ss.insUpdData.Customer__c = null;
         ss.save();
*/         
    }

    @isTest
    static void testSaveEx() {
        StraightBackAddressController.SaveExCore(null,null,null);
        StraightBackAddressController.SaveExCore('{"attributes":{"type":"Repair__c"}}','{"attributes":{"type":"Asset"}}','a4R1m000000Lfme');
        
    }
    
     /**
     * 为了方便前段table获取值
     */
    class AddressData {
        //数据
        public Address__c address { get; set; }
        //编辑按钮是否展示
        public String canEdit { get; private set; }
        
        public AddressData(Address__c record) {
            address = record;
            canEdit = '';
        }
        //主要是用于第一个展示和复选框是否显示
        public AddressData(Account record,String disCanEdit,String AccType) {
            this.address = new Address__c(
                Address_Classification__c = AccType,
                Customer__c= record.id,
                Telephone__c = record.phone,
                Province__c = record.State_Master__c,
                City__c = record.City_Master__c,
                Detailed_Address__c = record.Address__c);
            canEdit = disCanEdit;
        }
    }
}