public with sharing class DeliveryConfirmPDFController {
	
	//基础url
	public String baseUrl { get; private set; }
    public String agentName {get; private set; }
	//数据List
	public List<DataLineBean> dataLines {get; private set;}
	//订单Id
	public String laId {get; private set;}
	public Map<String,DataLineBean> numMap {get; private set; }
    public loaner_application__c la{get;private set;}
	public laDataLineBean lab {get; private set;}
    public List<UserDataLineBean> luData {get; private set;}
    public Integer rowNum {get; private set;}

    public boolean isSelf{get; private set; }
    public boolean isExpress{get; private set; }
    public boolean isRenew{get; private set; }
    public String OCM_Number{get; private set; }
    public String staticResource{get; private set; }// 20220221 PI改造 by Bright

	public DeliveryConfirmPDFController() {
		 laId = System.currentPageReference().getParameters().get('id');
         staticResource = JSON.serialize(PIHelper.getPIIntegrationInfo('loaner_application__c'));// 20220221 PI改造 by Bright
	}
    //初始化数据
	public PageReference init(){
        isSelf = false;
        isExpress = false;
        isRenew = false;
        rowNum = 0;
		dataLines = new List<DataLineBean>();
		numMap = new Map<String,DataLineBean>();
        luData = new List<UserDataLineBean>();
		baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        String path = URL.getCurrentRequestUrl().getPath();
        if (path.indexOf('/apex') > 0) {
            baseUrl += path.substring(0,path.indexOf('/apex'));
        } else if (path.indexOf('production/') > 0) {
            baseUrl += '/production';
        }
        //获取借用单信息
        List<loaner_application__c> laList = [select Id,Follow_Opp_text__c,Follow_Opp__r.InquiryNumber__c,Equipment_Type__c ,pickup_time__c,Loaner_Apply_Renewal_Date__c,loaner_Remark__c,Rental_End_Date__c,Loaner_Ser__c,Approval_Date__c,loaner_request_number__c,Agent__r.Name,Approver__r.Name,Rental_Start_Date__c,loaner_place__c,Demo_purpose__c,direct_shippment_address__c,Loaner_receive_staff__c,Loaner_receive_staff_phone__c,CreatorUserType__c,
        AWS_Data_Id__c,// 20220221 PI改造 by Bright
        Applicant_department__c from loaner_application__c where Id = :laId];
        if (laList.size() == 0) {
            return null;
        }
        la = laList[0];
        OCM_Number = la.Follow_Opp_text__c;
        agentName = la.Agent__r.Name;
        if(agentName != null){
            agentName = agentName.replace('(P)', '').replace('（P）','');
        }
        String dateString = String.valueOf(la.Rental_Start_Date__c) + ' 至 ' +String.valueOf(la.Rental_End_Date__c);
        dateString = dateString.replace('null', '');
        if(dateString.equals( ' 至 ')){
            dateString = '';
        }

        if(la.Loaner_Apply_Renewal_Date__c != null){
            isRenew = true;
        }else if(la.pickup_time__c != null){
            isSelf = true;
        }else{
            isExpress = true;
        }

        Integer dateNum = 0;
        String dateNumString = '';
        if(la.Rental_Start_Date__c != null && la.Rental_End_Date__c != null){
            dateNum = la.Rental_Start_Date__c.daysBetween(la.Rental_End_Date__c );
            dateNumString = String.valueOf(dateNum) + '天';
        }
        lab = new laDataLineBean(dateString,la,dateNumString);
        List<loaner_user__c> luList = [select id,Contact__r.Phone,Follow_UP_Opp__r.InquiryNumber__c,Customer__r.Name,Customer__r.DivisionName__c,Contact__r.Name,FromThePeriod__c,EndThePeriod__c from loaner_user__c where loaner_application__c = : laId];
        List<loaner_application_detail__c> ladList = [select  id,LOANER__r.Name,LOANER__r.Internal_Asset_number__c,LOANER__r.SerialNumber,LOANER__r.SerialNumber1__c,Remark__c,ECCode__c,SerialNumber__c,OTCODE__c,LOANER__c,LOANER__r.RecordType.Id,LOANER__r.RecordType.Name,LOANER__r.Description from loaner_application_detail__c where loaner_application__c = :laId];
		
        for(loaner_user__c lu :luList){
           
            String borrowDate = String.valueOf(lu.FromThePeriod__c) + ' 至 ' +String.valueOf(lu.EndThePeriod__c);
            borrowDate = borrowDate.replace('null', '');
            if(borrowDate.equals(' 至 ')){
                borrowDate = '';
            }
            String name = lu.Customer__r.Name == null?'':lu.Customer__r.Name;
            rowNum = rowNum+1+(name.length()/30);
            UserDataLineBean udlb = new UserDataLineBean(borrowDate,lu);
            luData.add(udlb);
        }

        for(loaner_application_detail__c lad : ladList){
  
        	/*String typeId = lad.LOANER__r.RecordType.id;
        	
        	if( typeId.substring(0, 15) == System.label.Asset_Record_Type2){
        		if(numMap.size() == 0){
        			DataLineBean dataBean = new DataLineBean(lad.LOANER__r.RecordType.Name,1,lad);
        			numMap.put(lad.LOANER__c, dataBean);
        		}else{
        			if(numMap.containsKey(lad.LOANER__c)){

						DataLineBean da = numMap.get(lad.LOANER__c);
						da.num = da.num+1;
        			}else{
        				DataLineBean dataBean = new DataLineBean(lad.LOANER__r.RecordType.Name,1,lad);
        				numMap.put(lad.LOANER__c, dataBean);
        			}
        		}
        	}else{
        		DataLineBean da = new DataLineBean(lad.LOANER__r.RecordType.Name,1,lad);
        		dataLines.add(da);
        	}*/

            DataLineBean da = new DataLineBean(lad.LOANER__r.RecordType.Name,1,lad);
                dataLines.add(da);
        	
        }
        if(numMap.size() > 0){
        	dataLines.addAll(numMap.values());
        }
       
        
        if(dataLines.size() < 12){
            Integer num = dataLines.size();
            for(Integer i =0; i<(12-num); i++){
            loaner_application_detail__c lac = new loaner_application_detail__c();
            DataLineBean demo = new DataLineBean(null,null,lac);
            System.debug(i);
            dataLines.add(demo);
            }  
        }
        rowNum = rowNum+dataLines.size();
		return null;
	}


	 public class DataLineBean {
        // 样机类型
        public String recordType { get;private set; }
        // 数量
        public Integer num { get; private set; }
        // 样机借出申请明细
        public loaner_application_detail__c lad { get; set; }

        public DataLineBean(String rtype,Integer in_no, loaner_application_detail__c in_lad) {
            recordType = rtype;
            num = in_no;
            lad = in_lad;
        }
    }

    public class UserDataLineBean{
        //借用日期
        public String borrowDate {get;private set;}

        public loaner_user__c lu {get;private set;}

        public UserDataLineBean(String rdate,loaner_user__c in_lu){
            borrowDate = rdate;
            lu = in_lu;
        }

    }

      public class laDataLineBean{
        //借用日期
        public String ladate {get;private set;}

        //借用期限
        public String ladateNumS {get; private set;}
        public loaner_application__c lala {get;private set;}

        public laDataLineBean(String in_ladate,loaner_application__c in_la,String in_laNum){
            ladate = in_ladate;
            lala = in_la;
            ladateNumS = in_laNum;
        }

    }
}