public with sharing class ISO_NewDemandOAD_PDFController {
	// si询价需求表id
	public String ISodcIdStr {get;set;}
	// PDF文件名称
	public String fileName {get;set;} 
	public String NowDate{get;set;}
	public IS_Opportunity_Demand__c ISOppDemandList{ get; set; }
	public List<IS_Opportunity_Demand__c> ISOpptunityDemandList{ get; set; }

	// si询价手术间
	public List<IS_Opportunity_Demand_Operating__c> ISOppDemandOperList{ get; set; }
	public IS_Opportunity_Demand_Operating__c ISOppDemandOper1{ get; set; }
	public IS_Opportunity_Demand_Operating__c ISOppDemandOper2{ get; set; }
	public IS_Opportunity_Demand_Operating__c ISOppDemandOper3{ get; set; }
	public IS_Opportunity_Demand_Operating__c ISOppDemandOperMore{ get; set; }
	// si询价示教点
	public List<IS_Opportunity_Demand_Demonstration__c> ISOppDemandDemonList{ get; set; }
	public IS_Opportunity_Demand_Demonstration__c ISOppDemandDemon1{ get; set; }
	public IS_Opportunity_Demand_Demonstration__c ISOppDemandDemon2{ get; set; }
	public IS_Opportunity_Demand_Demonstration__c ISOppDemandDemonMore{ get; set; }
	// si询价术间对应的配置单
	public List<IS_Opportunity_Demand_Configuration__c> ISOppDemandConfiguateList{ get; set; }
	public List<IS_Opportunity_Demand_Configuration__c> ISOppDemandConfiguate1{get; set; }
	public List<IS_Opportunity_Demand_Configuration__c> ISOppDemandConfiguateMatch1{get; set; }
	public List<IS_Opportunity_Demand_Configuration__c> ISOppDemandConfiguate2{get; set; }
	public List<IS_Opportunity_Demand_Configuration__c> ISOppDemandConfiguateMatch2{get; set; }
	public List<IS_Opportunity_Demand_Configuration__c> ISOppDemandConfiguate3{get; set; }
	public List<IS_Opportunity_Demand_Configuration__c> ISOppDemandConfiguateMatch3{get; set; }

	// 控制术间的显示
	public Boolean showISOdo1{ get; set; }
	public Boolean showISOdo2{ get; set; }
	public Boolean showISOdo3{ get; set; }
	public Boolean showISOdoMore{ get; set; }
	// 控制示教点的显示
	public Boolean showISOdd1{ get; set; }
	public Boolean showISOdd2{ get; set; }
	public Boolean showISOddMore{ get; set; }

    public ISO_NewDemandOAD_PDFController() {
    	ISodcIdStr = ApexPages.currentPage().getParameters().get('id');
		fileName = ApexPages.currentPage().getParameters().get('fileName');
		ApexPages.currentPage().getHeaders().put('content-disposition','inline;filename='+fileName+'.pdf');    
    }

    public void init(){
    	showISOdo1 = false;
    	showISOdo2 = false;
    	showISOdo3 = false;
    	showISOdoMore = false;
    	showISOdd1 = false;
    	showISOdd2 = false;
    	showISOddMore = false;
    	ISOppDemandConfiguate1 = new List<IS_Opportunity_Demand_Configuration__c>();
    	ISOppDemandConfiguateMatch1 = new List<IS_Opportunity_Demand_Configuration__c>();
		ISOppDemandConfiguate2 = new List<IS_Opportunity_Demand_Configuration__c>();
		ISOppDemandConfiguateMatch2 = new List<IS_Opportunity_Demand_Configuration__c>();
		ISOppDemandConfiguate3 = new List<IS_Opportunity_Demand_Configuration__c>();
		ISOppDemandConfiguateMatch3 = new List<IS_Opportunity_Demand_Configuration__c>();
    	NowDate = System.now().format('yyyy-MM-dd');
    	// 查询si询价需求表的信息
    	ISOpptunityDemandList = [select id,Opportunity_ID__r.State__c,Opportunity_ID__r.Opportunity_No__c,Demonstration_Number__c,Opportunity_ID__c,Transmission_Mode__c,Operating_Room_Count__c,Demand_DemonstrationType__c,OperatingRoomName__c,Operating_Room_Position_Area__c,Operating_Room_Position_Building__c,Operating_Room_Position_Stage__c from IS_Opportunity_Demand__c where id = :ISodcIdStr];
    	if(ISOpptunityDemandList.size() > 0){
    		ISOppDemandList = ISOpptunityDemandList[0];
    	}
    	// 查询si询价手术间
    	ISOppDemandOperList = [select id,Project_Information__c,Name,Name__c,IS_Opportunity_Demand__c,Operating_Room_Area__c,Remarks_Text__c,Operating_Room_Type__c,Olympus_LightSource_Host__c,Olympus_CD_Writer__c,Olympus_Ultrasound_Knife__c,Olympus_Insufflator__c,Olympus_Camera_Host__c,Olympus_Electric_Knife__c,ThirdParty_OperatingTable__c,ThirdParty_OperationLamp__c,ThirdParty_Camera__c from IS_Opportunity_Demand_Operating__c where IS_Opportunity_Demand__c = :ISodcIdStr];
		// 查询si询价示教点
		ISOppDemandDemonList = [select id,Name,IS_Opportunity_Demand__c,Teaching_Needs__c,Other_Require_Text__c,Demonstration_Type__c,Other_Relative_Position__c,Name__c from IS_Opportunity_Demand_Demonstration__c where IS_Opportunity_Demand__c = :ISodcIdStr];
		// 查询si询价配置单
		ISOppDemandConfiguateList = [select id,name,Configuration_Type__c,Selection__c,Matching__c,SerialNumber__c,Number__c,Standard_Configuration__c,IS_Opportunity_Demand_Operating__c,Brand__c,Company__c,IS_Opportunity_Demand_Operating__r.Name__c from IS_Opportunity_Demand_Configuration__c where IS_Opportunity_Demand_Operating__c = :ISOppDemandOperList order by SerialNumber__c];
		// 拆分si询价手术间
		for(IS_Opportunity_Demand_Operating__c isOdo :ISOppDemandOperList){
			if(isOdo.Name__c == '手术间1'){
				ISOppDemandOper1 = isOdo;
				ISOppDemandOper1.Remarks_Text__c = reStringAll(isOdo.Remarks_Text__c);
				// System.debug(isOdo.Remarks_Text__c+'======');
				// String str = isOdo.Remarks_Text__c;
				// System.debug(str+'======');
				// String[] strlist =  str.split('\n');
				// System.debug(strlist+'======strlist');
				if(ISOppDemandOper1 != null){
					System.debug('======是否显示');
					showISOdo1 = true;
					showISOdo2 = false;
					showISOdo3 = false;
					showISOdoMore = false;
				}
			}
			if(isOdo.Name__c == '手术间2'){
				ISOppDemandOper2 = isOdo;
				ISOppDemandOper2.Remarks_Text__c = reStringAll(isOdo.Remarks_Text__c);
				if(ISOppDemandOper2 != null){
					showISOdo2 = true;
					showISOdo1 = true;
					showISOdo3 = false;
					showISOdoMore = false;
				}
			}
			if(isOdo.Name__c == '手术间3'){
				ISOppDemandOper3 = isOdo;
				ISOppDemandOper3.Remarks_Text__c = reStringAll(isOdo.Remarks_Text__c);
				if(ISOppDemandOper3 != null){
					showISOdo3 = true;
					showISOdo2 = true;
					showISOdo1 = true;
					showISOdoMore = false;
				}
			}
			if(isOdo.Name__c == '术间数量>3间'){
				ISOppDemandOperMore = isOdo;
				ISOppDemandOperMore.Project_Information__c = reStringAll(isOdo.Project_Information__c);
				if(ISOppDemandOperMore != null){
					showISOdoMore = true;
					showISOdo2 = false;
					showISOdo1 = false;
					showISOdo3 = false;
				}
			}
		}
		// 控制术间和示教点的显示
		if(ISOppDemandList.Operating_Room_Count__c == '1'){
			showISOdo1 = true;
			showISOdo2 = false;
			showISOdo3 = false;
			showISOdoMore = false;
		}
		if(ISOppDemandList.Operating_Room_Count__c == '2'){
			showISOdo1 = true;
			showISOdo2 = true;
			showISOdo3 = false;
			showISOdoMore = false;
		}
		if(ISOppDemandList.Operating_Room_Count__c == '3'){
			showISOdo1 = true;
			showISOdo2 = true;
			showISOdo3 = true;
			showISOdoMore = false;
		}
		if(ISOppDemandList.Operating_Room_Count__c == '>3'){
			showISOdo1 = false;
			showISOdo2 = false;
			showISOdo3 = false;
			showISOdoMore = true;
		}
		if(ISOppDemandList.Demonstration_Number__c == '1'){
			showISOdd1 = true;
			showISOdd2 = false;
			showISOddMore = false;
		}
		if(ISOppDemandList.Demonstration_Number__c == '2'){
			showISOdd1 = true;
			showISOdd2 = true;
			showISOddMore = false;
		}
		if(ISOppDemandList.Demonstration_Number__c == '>2'){
			showISOdd1 = false;
			showISOdd2 = false;
			showISOddMore = true;
		}
		// 拆分si询价示教点
		for(IS_Opportunity_Demand_Demonstration__c isOdd : ISOppDemandDemonList){
			if(isOdd.Name__c == '示教点1'){
				ISOppDemandDemon1 = isOdd;
				ISOppDemandDemon1.Other_Require_Text__c = reStringAll(isOdd.Other_Require_Text__c);
				if(ISOppDemandDemon1 != null){
					showISOdd1 = true;
					showISOdd2 = false;
					showISOddMore = false;
				}
			}
			if(isOdd.Name__c == '示教点2'){
				ISOppDemandDemon2 = isOdd;
				ISOppDemandDemon2.Other_Require_Text__c = reStringAll(isOdd.Other_Require_Text__c);
				if(ISOppDemandDemon2 != null){
					showISOdd2 = true;
					showISOdd1 = true;
					showISOddMore = false;
				}
			}
			if(isOdd.Name__c == '示教点数量>2'){
				ISOppDemandDemonMore = isOdd;
				ISOppDemandDemonMore.Teaching_Needs__c = reStringAll(isOdd.Teaching_Needs__c);
				if(ISOppDemandDemonMore != null){
					showISOddMore = true;
					showISOdd2 = false;
					showISOdd1 = false;
				}
			}
		}
		// 拆分si询价手术间的配置单
		for(IS_Opportunity_Demand_Configuration__c isOdc : ISOppDemandConfiguateList){
			String conType = isOdc.Configuration_Type__c;
			System.debug(isOdc.IS_Opportunity_Demand_Operating__r.Name__c +'配置单对应类型:'+conType.substring(5,conType.length()-3)+'术间对应类型：'+ISOppDemandOper1.Operating_Room_Type__c);
			if(isOdc.IS_Opportunity_Demand_Operating__r.Name__c == '手术间1' && conType.substring(5,conType.length()-3) == ISOppDemandOper1.Operating_Room_Type__c){
				if(!isOdc.Matching__c){
					ISOppDemandConfiguate1.add(isOdc);
				}
				if(isOdc.Matching__c){
					ISOppDemandConfiguateMatch1.add(isOdc);
				}
				
			}
			if(isOdc.IS_Opportunity_Demand_Operating__r.Name__c == '手术间2' && conType.substring(5,conType.length()-3) == ISOppDemandOper2.Operating_Room_Type__c){
				// ISOppDemandConfiguate2.add(isOdc);
				if(!isOdc.Matching__c){
					ISOppDemandConfiguate2.add(isOdc);
				}
				if(isOdc.Matching__c){
					ISOppDemandConfiguateMatch2.add(isOdc);
				}
			}
			if(isOdc.IS_Opportunity_Demand_Operating__r.Name__c == '手术间3' && conType.substring(5,conType.length()-3) == ISOppDemandOper3.Operating_Room_Type__c){
				// ISOppDemandConfiguate3.add(isOdc);
				if(!isOdc.Matching__c){
					ISOppDemandConfiguate3.add(isOdc);
				}
				if(isOdc.Matching__c){
					ISOppDemandConfiguateMatch3.add(isOdc);
				}
			}
		}
		System.debug(ISOppDemandConfiguate1+'======');
    }

    // 为长文本格式化打印
    public String reStringAll(String str){
    	if(str != '' && str != null){
    		String[] strList = str.split('\n');
	    	Integer lineL = 40;
			String reStrAll = '';
			for(Integer i = 0 ; i < strList.size() ; i++){
			    String reStr = strList[i];
			    String reStrM = '';
			    for(Integer j = 0 ; j <= reStr.length()/lineL ; j++ ){
			        if(reStr.length() > (lineL*(j+1))){
			            reStrM += reStr.substring(j*lineL,(j+1)*lineL);
			            reStrM += '\n';
			        }else{
			            reStrM += reStr.substring(j*lineL,reStr.length());
			            reStrM += '\n';
			        }
			    }
			    reStrAll += reStrM;
			}
			return reStrAll;
    	}
    	return null;
    }
}