@isTest
private class AgencyHospitalLinkControllerTest
{
	static Account agency1;
	static Account agency2;
	static void initAgencyHospital() {
		//医院
		RecordType hospitP = [select id from RecordType where IsActive = true and SobjectType= 'Account' and Name='病院' limit 1];
		//销售商
		RecordType agencyP = [select Id from RecordType where IsActive = true and SobjectType= 'Account' and Name = '販売店' limit 1];
		//省
        Address_Level__c alc1 = new Address_Level__c(
            Name = '安徽省'
        );
        Address_Level__c alc2 = new Address_Level__c(
            Name = '河南省'
        );
        insert new Address_Level__c[] {alc1,alc2};

        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='安徽省'
        );

        Address_Level2__c alc21 = new Address_Level2__c(
        	Name = '郑州市',Address_Level__c = alc2.Id,Level1_Name__c='河南省'
        );
        insert new Address_Level2__c[] {alc11,alc12,alc21};


		//安徽
		Account hospital1 = new Account();
        hospital1.Name = 'test hospital1';
        hospital1.RecordTypeId = hospitP.id; 
        hospital1.State_Master__c = alc1.id; 
        hospital1.OCM_Category__c = 'SLTV'; 
        hospital1.City_Master__c = alc11.id;
        hospital1.Is_Active__c = '有効';
        
		Account hospital2 = new Account();
        hospital2.Name = 'test hospital2';
        hospital2.RecordTypeId = hospitP.id;
        hospital2.State_Master__c = alc1.id; 
        hospital2.OCM_Category__c = 'LTV'; 
        hospital2.City_Master__c = alc12.id;
        hospital2.Is_Active__c = '有効';
        
        //河南
		Account hospital3 = new Account();
        hospital3.Name = 'test hospital3';
        hospital3.RecordTypeId = hospitP.id; 
        hospital3.State_Master__c = alc2.id; 
        hospital3.OCM_Category__c = 'SLTV'; 
        hospital3.City_Master__c = alc21.id;
        hospital3.Is_Active__c = '有効';
        
        Account hospital4 = new Account();
        hospital4.Name = 'test hospital4';
        hospital4.RecordTypeId = hospitP.id; 
        hospital4.State_Master__c = alc2.id; 
        hospital4.OCM_Category__c = 'SLTV'; 
        hospital4.City_Master__c = alc21.id;
        hospital4.Is_Active__c = '有効';
        
        insert new Account[] {hospital1,hospital2,hospital3,hospital4};
        //安徽
        agency1 = new Account();
        agency1.Name = 'test agency1'; 
        agency1.State_Master__c = alc1.id; 
        agency1.RecordTypeId = agencyP.id; 
        insert agency1;
        agency1 = [select Id from Account where Id = :agency1.Id];
        //河南
        agency2 = new Account();
        agency2.Name = 'test agency2'; 
        agency2.State_Master__c = alc2.id; 
        agency2.RecordTypeId = agencyP.id;
        insert agency2;
        agency2 = [select Id from Account where Id = :agency2.Id];
	}


	@isTest
	static void testSearch() {
		initAgencyHospital();
		PageReference page = new PageReference('/apex/AgencyHospitalLink?userid=' + agency1.Id);
		System.Test.setCurrentPage(page);
		AgencyHospitalLinkController controllTest = new AgencyHospitalLinkController();
		controllTest.init();
        controllTest.allSave = false;
		controllTest.refreshPageSize();
		controllTest.searchHospital();
		System.assertEquals(4, controllTest.hospitalInfoRecordsview.size());

		controllTest.moveToNext();
		List<SelectOption> cityOpts1 = new List<SelectOption>();
        cityOpts1.add(new SelectOption('', '--无--'));
        cityOpts1.add(new SelectOption('黄山市', '黄山市'));
        cityOpts1.add(new SelectOption('合肥市', '合肥市'));

        
		controllTest.area ='安徽省';
		controllTest.cityload();
		System.assertEquals(cityOpts1, controllTest.cityOpts);
		controllTest.city ='黄山市';
		controllTest.searchHospital();
        System.assertEquals(1, controllTest.hospitalInfoRecoedsviewCount);
        System.assertEquals(1, controllTest.hospitalInfoRecordsview.size());
        System.assertEquals('test hospital1', controllTest.hospitalInfoRecordsview[0].acc1.Name);
        controllTest.allSave = false;
        controllTest.refreshPageSize();

        controllTest.init();
		controllTest.searchHospital();
		controllTest.category1 = 'test';
		controllTest.searchHospital();
		System.assertEquals(1, controllTest.hospitalInfoRecordsview.size());
        //System.assertEquals('test hospital1', controllTest.hospitalInfoRecordsview[0].acc1.Name);
        
        

	}



	@isTest
	static void testinit()
	{
		initAgencyHospital();
		PageReference page = new PageReference('/apex/AgencyHospitalLink?userid=' + agency1.Id);
		System.Test.setCurrentPage(page);
		AgencyHospitalLinkController controllTest = new AgencyHospitalLinkController();
		controllTest.init();


		System.assertEquals(2, controllTest.hospitalInfoRecordsview.size());
        //System.assertEquals('test hospital1', controllTest.hospitalInfoRecordsview[0].acc1.Name);
        //System.assertEquals('test hospital2', controllTest.hospitalInfoRecordsview[1].acc1.Name);

        controllTest.searchHospital();
        System.assertEquals(4, controllTest.hospitalInfoRecordsview.size());
        //System.assertEquals('test hospital2', controllTest.hospitalInfoRecordsview[0].acc1.Name);
        //System.assertEquals('test hospital1', controllTest.hospitalInfoRecordsview[1].acc1.Name);
        //System.assertEquals('test hospital3', controllTest.hospitalInfoRecordsview[2].acc1.Name);
        //System.assertEquals('test hospital4', controllTest.hospitalInfoRecordsview[3].acc1.Name);
        
        controllTest.area ='河南省';
        controllTest.cityload();
        controllTest.searchHospital();
        System.assertEquals(2, controllTest.hospitalInfoRecordsview.size());
        //System.assertEquals('test hospital3', controllTest.hospitalInfoRecordsview[0].acc1.Name);
        //System.assertEquals('test hospital4', controllTest.hospitalInfoRecordsview[1].acc1.Name);
        controllTest.hospitalInfoRecordsview[0].check= true;
        controllTest.allSave = false;
        controllTest.CheckHospitalInfo();
        //System.assertEquals(0, controllTest.delAgencyLinkSize);
        controllTest.save();
        List<Agency_Hospital_Link__c> ahlList = [select Id,Name,Hospital__c,Hospital__r.Id,Hospital__r.Name,Hospital__r.Management_Code__c,Agency__c
												from Agency_Hospital_Link__c
												WHERE Agency__c = :agency1.Id];
		System.assertEquals(1, ahlList.size());
		System.assertEquals('test hospital4', ahlList[0].Hospital__r.Name);

        page = new PageReference('/apex/AgencyHospitalLink?userid=' + agency1.Id);
        System.Test.setCurrentPage(page);
        controllTest.init();

        controllTest.refreshPageSize(); 
        System.assertEquals(2, controllTest.hospitalInfoRecordsview.size());
        controllTest.searchHospital();
        System.assertEquals(4, controllTest.hospitalInfoRecordsview.size());
        controllTest.hospitalInfoRecordsview[0].check= false;
        controllTest.hospitalInfoRecordsview[1].check= true;
        controllTest.allSave = true;
        controllTest.CheckHospitalInfo();
        //System.assertEquals(0, controllTest.delAgencyLinkSize);
        controllTest.save();
        controllTest.refreshPageSize();
        controllTest.area ='河南省';
        controllTest.searchHospital();
        System.assertEquals(2, controllTest.hospitalInfoRecordsview.size());
        controllTest.ToPage();
	}

	@isTest
	static void testSave() {
		initAgencyHospital();
        PageReference page = new PageReference('/apex/AgencyHospitalLink?userid=' + agency2.Id);
        System.Test.setCurrentPage(page);
        
        AgencyHospitalLinkController controller = new AgencyHospitalLinkController();
        //初始化、
        controller.init();
        System.assertEquals(2, controller.hospitalInfoRecordsview.size());
        //System.assertEquals('test hospital3', controller.hospitalInfoRecordsview[0].acc1.Name);
        //System.assertEquals('test hospital4', controller.hospitalInfoRecordsview[1].acc1.Name);
        controller.searchHospital();
        System.assertEquals(4, controller.hospitalInfoRecordsview.size());
        //System.assertEquals('test hospital1', controller.hospitalInfoRecordsview[0].acc1.Name);
        //System.assertEquals('test hospital2', controller.hospitalInfoRecordsview[1].acc1.Name);
        //System.assertEquals('test hospital3', controller.hospitalInfoRecordsview[2].acc1.Name);
        //System.assertEquals('test hospital4', controller.hospitalInfoRecordsview[3].acc1.Name);
        controller.hospitalInfoRecordsview[0].check= true;
        controller.hospitalInfoRecordsview[2].check= true;
        controller.allSave = false;
        controller.CheckHospitalInfo();
        //System.assertEquals(0, controller.delAgencyLinkSize);
        controller.save();
        
        controller.init();
        controller.searchHospital();
        controller.makepagerecords();
        System.assertEquals(4, controller.hospitalInfoRecordsview.size());
        //System.assertEquals('test hospital1', controller.hospitalInfoRecordsview[0].esd.Hospital__r.Name);
        //System.assertEquals('test hospital3', controller.hospitalInfoRecordsview[1].esd.Hospital__r.Name);
        //System.assertEquals('test hospital2', controller.hospitalInfoRecordsview[2].acc1.Name);
        //System.assertEquals('test hospital4', controller.hospitalInfoRecordsview[3].acc1.Name);
        
        List<Agency_Hospital_Link__c> ahlList = [select Id,Name,Hospital__c,Hospital__r.Id,Hospital__r.Name,Hospital__r.Management_Code__c,Agency__c
												from Agency_Hospital_Link__c
												WHERE Agency__c = :agency2.Id];
		System.assertEquals(2, ahlList.size());
		//System.assertEquals('test hospital2', ahlList[0].Hospital__r.Name);


        PageReference page1 = new PageReference('/apex/AgencyHospitalLink?userid=' + agency1.Id);
        System.Test.setCurrentPage(page1);
        
        AgencyHospitalLinkController controller1 = new AgencyHospitalLinkController();
        //初始化、
        controller1.init();
        AgencyHospitalLinkController.getAreaOpts();
        AgencyHospitalLinkController.getOcmOpts();
        System.assertEquals(2, controller1.hospitalInfoRecordsview.size());
        
        //System.assertEquals('test hospital1', controller1.hospitalInfoRecordsview[0].acc1.Name);
        //System.assertEquals('test hospital2', controller1.hospitalInfoRecordsview[1].acc1.Name);

        controller1.searchHospital();
        controller1.hospitalInfoRecordsview[2].check= true;
        controller1.allSave = false;
        controller1.CheckHospitalInfo();
        //System.assertEquals(1, controller1.delAgencyLinkSize);
        controller1.save();

        List<Agency_Hospital_Link__c> ahlcList1 = [select Id,Name,Hospital__c,Hospital__r.Id,Hospital__r.Name,Hospital__r.Management_Code__c,Agency__c
												from Agency_Hospital_Link__c
												WHERE Agency__c = :agency1.Id];
		System.assertEquals(1, ahlcList1.size());

		/*List<Agency_Hospital_Link__c> ahlcList2 = [select Id,Name,Hospital__c,Hospital__r.Id,Hospital__r.Name,Hospital__r.Management_Code__c,Agency__c
												from Agency_Hospital_Link__c
												WHERE Agency__c = :agency2.Id];
		System.assertEquals(1, ahlcList2.size());*/
	}
}