public without sharing class FixtureRentalPDFController {

    private static Integer MAXLINEHRIGHT = 25;
    private static Integer MAXPAGECOUNT = 22; //32

    private String rentalApplyIdIMG;
    // private String rentalApplyId;//20201120 ljh 

    private List<String> rentalApplyId;
    // private List<String> raesidList;  確認

    public List<PDFInfo> records { get; set; }
    public Integer pageCnt { get; set; }
    public Integer pageNum { get; set; }
    public integer AllSum {get;set;}
    public String RentalApplyName  { get; set; }
    public String BorrowTimeString { get; set; }
    public Rental_Apply__c ApplyHeadShow { get; set; }
    public List<lineInfo> DetailsAllList { get; set;}
    public Map<String,String> AssetModelNoMap {get;set;}
    public List<PDFInfoIMG> recordsIMG { get; set; }
    public List<PDFInfoIMG> recordsIMGfist { get; set; }
    public Integer pageCntIMG { get; set; }
    public String headerLength {get;set;}
    public List<List<String>> PageCutList { get; set; }
    public String HospitalName { get; set;}//医疗机构
    public String Shippmentadress { get; set;}//发货地址
    public Integer mainCnt { get; private set;}//主体件数
    public Integer accessoryCnt { get; private set;}//附属品件数
    public String centreAddress { get; set; }
    public Boolean IsShowLU { get; set; } //是否显示签收单左上角文字 true显示 20201119 LJH OCSM_BP5-61 add
    public Boolean IsMain; //是否是现地分配的主单 20201120 LJH OCSM_BP5-61 add
    
    public string staticResource { get; private set; }
    public string staticResourceFile { get; private set; }
    public Boolean addFlag { get; private set; }//敏感地址使用标记
    public String qrcode { get; private set; }
    public String barcode { get; private set; }
    // 20220413 ljh 抬头显示 add start
    public String AgencyAddressName { get; private set; } 
    public Boolean isAgencyOrCenter { get; private set; } 
    public String AgencyOrCenterAddress { get; private set; }
    // 20220413 ljh 抬头显示 add end

    public FixtureRentalPDFController() {
        pageNum = Integer.valueOf(ApexPages.currentPage().getParameters().get('page'));
        rentalApplyIdIMG = ApexPages.currentPage().getParameters().get('raid');
        //Apexpages.currentPage().getHeaders().put('X-UA-Compatible', 'IE=8');
        // rentalApplyId = ApexPages.currentPage().getParameters().get('raid');//20201120 ljh 
        String tempStr = ApexPages.currentPage().getParameters().get('raid');
        
        addFlag = false;
        rentalApplyId = new List<String>();
        if(tempStr != null){
           rentalApplyId.add(tempStr); 
        }
        
        ApplyHeadShow = new Rental_Apply__c();
        IsShowLU = true;//20201119 LJH OCSM_BP5-61 add
        IsMain = false;//20201123 LJH OCSM_BP5-61 add
    }

    // 画面初始化
    public void init() {
        staticResource = JSON.serialize(PIHelper.getPIIntegrationInfo('Rental_Apply__c'));
        staticResourceFile = JSON.serialize(PIHelper.getPIIntegrationInfo('Document'));
        // 新规奥林巴斯备品签收单PDF画面初始化
        PageCutList = new List<List<String>>();
        List<Rental_Apply__c> ApplyList = new List<Rental_Apply__c>();
        Map<String,String>  AgencyAddressIdMap = new Map<String,String>(); //20201124 LJH OCSM_BP5-61 add 出借Id,地址Id
        if(rentalApplyId!=null){
            ApplyList = [select id,
                                name,
                                Name_No__c, // 20211209 ljh SFDC-C923SR add 
                                Shippment_loaner_time__c,
                                WorkPlace__c,
                                Salesdept__c,
                                Demo_purpose1__c,
                                Demo_purpose2__c,
                                Bollow_Date__c,
                                Campaign__r.EndDate,
                                Follow_UP_Opp__c,
                                Repair__r.Repair_Shipped_Date__c,
                                Return_dadeline_final__c,
                                Shipment_address__r.Post_Code__c,
                                Person_In_Charge__r.Name,Post_Code__c,
                                Shippment_adress_detail__c,
                                Person_In_Charge__r.Phone,
                                Phone_number__c,
                                Hospital__r.Name,Account__r.Department_name__c,
                                Account__r.Name,
                                Direct_shippment_address__c,
                                Dealer__r.Postal_Code__c,
                                ApplyUser__r.Name,
                                applyUser__r.Phone,
                                Loaner_medical_Staff__r.Name,
                                Loaner_medical_Staff__r.Phone,
                                Loaner_received_staff__c,
                                Loaner_received_staff_phone__c,
                                Asset_loaner_start_date__c,
                                Asset_loaner_start_day__c,
                                ApplyPerson_Phone__c,
                                Loaner_centre_mail_address__c,
                              // Main_Cnt__c,
                              // Accessory_Cnt__c,
                                Old_Rental_Apply__c,//20201120 LJH OCSM_BP5-61 add
                                Split_Apply_Reason__c,//20201120 LJH OCSM_BP5-61 add
                                RecordType.DeveloperName,//20201120 LJH OCSM_BP5-61 add
                                Request_shipping_day__c,//20201120 LJH OCSM_BP5-61 add
                                ToAgency__c,//20201123 LJH OCSM_BP5-61 add
                                Agency_Address_Id__c,//20201124 LJH OCSM_BP5-61 add
                                Root_Rental_Apply__c,//20210617 ljh 1732 add
                                BRId__c, // 20211209 ljh SFDC-C923SR add 
                         		direct_send__c,
                                QRImg__c,
                                QRId__c,
                                AWS_Data_Id__c,//20220304 
                                Direct_Shippment_Address_Encrypt__c,//20220304
                                Phone_Number_Encrypt__c//20220304
                        from  Rental_Apply__c
                        where id = :rentalApplyId];
        }

        if(ApplyList.size()>0){
            ApplyHeadShow = ApplyList[0];
            qrcode = ConsumTrialPDFController.GetImageBase64(ApplyHeadShow.QRId__c);
            barcode = ConsumTrialPDFController.GetImageBase64(ApplyHeadShow.BRId__c);
        }
        //增加借出备品SET一览List，为了获取借出备品SET一览明细
        List<String> RAESSearchList = New List<String>();
        //20201201 ljh  OCSM_BP5-76 add start
        String centerAddressInfo;
        bp3_Setting__c conf = bp3_Setting__c.getOrgDefaults();
        //20201201 ljh  OCSM_BP5-76 add end
        //根据备品中心邮箱判断备品中心
        if(ApplyHeadShow.Loaner_centre_mail_address__c=='OCM_Asset@olympus.com.cn'){
          centreAddress = '请将备品回寄至北京备品中心';
          centerAddressInfo = 'BeiJingCenter';
        }else if(ApplyHeadShow.Loaner_centre_mail_address__c=='OCM_AssetSH@olympus.com.cn'){
          centreAddress = '请将备品回寄至上海备品中心';
          centerAddressInfo = 'ShangHaiCenter';
        }else if(ApplyHeadShow.Loaner_centre_mail_address__c=='OCM_AssetGZ@olympus.com.cn'){
          centreAddress = '请将备品回寄至广州备品中心';
          centerAddressInfo = 'GuangZhouCenter';
        }
        //20201120 LJH OCSM_BP5-61 add start
        //现地管理分单 主单&从单
        //20210617 ljh 1732 update start
        if(ApplyHeadShow.RecordType.DeveloperName == 'AgencyRequest'){
            //记录类型办事处
            IsShowLU = false;
            String tempStr = ApplyHeadShow.Agency_Address_Id__c;
            if(String.isNotBlank(tempStr)){
                AgencyAddressIdMap.put(ApplyHeadShow.Id,tempStr.substring(0,15));
            }
            if(String.isBlank(ApplyHeadShow.Old_Rental_Apply__c)){
                List<Rental_Apply__c> ApplyCopyList = [select Id, Name from  Rental_Apply__c where Root_Rental_Apply__c = :ApplyHeadShow.id  and Split_Apply_Reason__c = '现地管理分单']; //20210611 ljh 1732 update Root_Rental_Apply__c
                if(ApplyCopyList.size()>0){
                    for(Rental_Apply__c ac:ApplyCopyList){
                        String tempId = ac.Id;
                        rentalApplyId.add(tempId);
                    }  
                }

            }else{
                List<Rental_Apply__c> ApplyCopyList = [select Id, Name from  Rental_Apply__c where ((Root_Rental_Apply__c = :ApplyHeadShow.Root_Rental_Apply__c and id != :ApplyHeadShow.id and Split_Apply_Reason__c = '现地管理分单')  OR id = :ApplyHeadShow.Root_Rental_Apply__c) ]; //20210611 ljh 1732 update Root_Rental_Apply__c
                if(ApplyCopyList.size()>0){
                    for(Rental_Apply__c ac:ApplyCopyList){
                        String tempId = ac.Id;
                        rentalApplyId.add(tempId); 
                    }   
                }
            }
        }else{
            if(String.isBlank(ApplyHeadShow.Old_Rental_Apply__c)){
                //是否 现地管理分单 主单
                List<Rental_Apply__c> ApplyCopyList = [select Id, Name from  Rental_Apply__c where Root_Rental_Apply__c = :ApplyList[0].id  and Split_Apply_Reason__c = '现地管理分单']; //20210611 ljh 1732 update Root_Rental_Apply__c
                if(ApplyCopyList.size()>0){
                    IsShowLU = false;
                    IsMain = true;
                    for(Rental_Apply__c ac:ApplyCopyList){
                        String tempId = ac.Id;
                        rentalApplyId.add(tempId);
                    }    
                }
            }
        }
        List<Shipment_address__c> ShipmentAddressList = [select Id,Name,Address__c from Shipment_address__c where Id in :AgencyAddressIdMap.values()];
        Map<String,String> ShipmentAddressMap = new Map<String,String>();
        for(Shipment_address__c sa : ShipmentAddressList){
            String tempStr = sa.Id;
            ShipmentAddressMap.put(tempStr.substring(0,15),sa.Address__c);
        }
        Set<String> keySet = AgencyAddressIdMap.keySet();
        for(String ks:keySet){
            AgencyAddressIdMap.put(ks,ShipmentAddressMap.get(AgencyAddressIdMap.get(ks)));
        }
        //20201120 LJH OCSM_BP5-61 add end
        //医疗机构名分行显示
        if(ApplyHeadShow.Hospital__r.Name != null){
            String HospOrgan = ApplyHeadShow.Hospital__r.Name;
            Integer HospSize = HospOrgan == null ? 0 : HospOrgan.length();
            if(HospSize <=19){
                HospitalName = HospOrgan;
            }else{
                HospitalName = HospOrgan.substring(0,19)+'\n'+HospOrgan.substring(19,HospSize);
            }
        }

        //发货地址分行显示
        String ShippMentAdr = null;
        if (ApplyHeadShow.Shippment_adress_detail__c != null) {
            ShippMentAdr = ApplyHeadShow.Shippment_adress_detail__c;
        } else {
            ShippMentAdr = ApplyHeadShow.Direct_shippment_address__c;
            addFlag = true;
        }

        Integer ShipAdrSize = ShippMentAdr == null ? 0 : ShippMentAdr.length();
        if (ShipAdrSize <= 26) {
            Shippmentadress = ShippMentAdr;
        } else if (ShipAdrSize > 26 && ShipAdrSize <= 52) {
            Shippmentadress = ShippMentAdr.substring(0,26)+ '\n' +ShippMentAdr.substring(26,ShipAdrSize);
        } else {
            Shippmentadress = ShippMentAdr.substring(0,26)+'\n'+ShippMentAdr.substring(26,52) + '\n'+ShippMentAdr.substring(52,ShipAdrSize);
        }

        // 增加医院借用期限，输出字段格式化
        if (ApplyHeadShow.Asset_loaner_start_date__c != null) {
            if (ApplyHeadShow.Demo_purpose2__c      == '产品试用'
                ||  ApplyHeadShow.Demo_purpose2__c  ==  '新产品评价'
                ||  ApplyHeadShow.Demo_purpose2__c  ==  FixtureUtil.raDemo_purpose2MAP.get('shiyongwuxunjia')
                ||  ApplyHeadShow.Demo_purpose2__c  ==  FixtureUtil.raDemo_purpose2MAP.get('shiyongyouxunjia')
                ||  ApplyHeadShow.Demo_purpose2__c  ==  '其他'
                ||  ApplyHeadShow.Demo_purpose2__c  ==  '协议借用') {
                BorrowTimeString = String.valueOf(ApplyHeadShow.Asset_loaner_start_date__c) + ' ~ '
                                 + String.valueOf(ApplyHeadShow.Return_dadeline_final__c)
                                 + '前到达奥林巴斯备品中心';
            } else if (ApplyHeadShow.Demo_purpose2__c == '学会展会') {
                if (ApplyHeadShow.Campaign__r.EndDate != null){
                    BorrowTimeString = String.valueOf(ApplyHeadShow.Asset_loaner_start_date__c) + ' ~ '
                                     + String.valueOf(ApplyHeadShow.Campaign__r.EndDate.addDays(5))
                                     + '前到达奥林巴斯备品中心';
                                     // 2018年7月31日 SWAG-B36CY6 by 张玉山 start
                    } else if (ApplyHeadShow.Campaign__r.EndDate == null) {
                       // 2018年7月31日 SWAG-B36CY6 by 张玉山 end
                        BorrowTimeString = '学会结束日为空，无法确定医院借用期限';
                    }else{
                        BorrowTimeString = 'ApexClass104:无法获取数据，请联系开发组';
                    }

            } else if (ApplyHeadShow.Demo_purpose2__c == '已购待货') {
                BorrowTimeString = String.valueOf(ApplyHeadShow.Asset_loaner_start_date__c) + ' ~ '
                                 + '新品发货日起14天内'
                                 + '到达奥林巴斯备品中心';
            } else if (ApplyHeadShow.Demo_purpose2__c == '故障排查') {
                BorrowTimeString = '判定有故障时，' + String.valueOf(ApplyHeadShow.Asset_loaner_start_date__c)
                                 + '~修理品返送日起14天内到达奥林巴斯备品中心\n'
                                 + '判定无故障时，' + String.valueOf(ApplyHeadShow.Asset_loaner_start_date__c)
                                 + '~出库日起10个工作日内到达奥林巴斯备品中心';
            } else if (ApplyHeadShow.Demo_purpose1__c == '维修代用') {
                BorrowTimeString = String.valueOf(ApplyHeadShow.Asset_loaner_start_date__c) + ' ~ '
                                 + '修理品返送日起14天内'
                                 + '到达奥林巴斯备品中心';
            } else {
                BorrowTimeString = '未设定归还期限';
            }

        } else {
            BorrowTimeString = '备品预计出货日为空，无法确定医院借用期限';
        }

        system.debug(' ApplyHeadShow.Demo_purpose1__c'+ ApplyHeadShow.Demo_purpose1__c);

        // 增加医院借用期限，输出字段格式化
        // system.debug('测试位置1+raesidList'+raesidList);
        records = new List<PDFInfo>();
        List<Rental_Apply_Equipment_Set__c> raesList = new List<Rental_Apply_Equipment_Set__c>();
        // List<String> esidList = new List<String>();
        //ToDo Where 確認
        String soql =  'select Id, Name, Rental_Apply__c, Fixture_Set__r.Name'
               + ', Rental_Apply__r.Name, Request_owner__c, Rental_Apply__r.Salesdept__c'
               + ', Rental_Apply__r.WorkPlace__c, Account__c, Rental_Apply__r.Campaign__r.Name'
               + ', Rental_Apply__r.Request_shipping_day__c, Rental_Apply__r.Loaner_received_staff__c'
               + ', Rental_Apply__r.Loaner_received_staff_phone__c, Rental_Apply__r.Shippment_adress_detail__c'
               + ', Rental_End_Date__c, Rental_Apply__r.direct_shippment_address__c'
               + ' from Rental_Apply_Equipment_Set__c'
               + ' where Rental_Apply__c = :rentalApplyId'
               + ' AND Cancel_Select__c = false'
               + ' AND Yi_Shipment_request__c > 0';

        //ToDo条件確認
        //soql += 'and RAES_Status__c != \'引当済\' and RAES_Status__c != \'取消分配\'';

        //今回個別の表示はしないのでコメントアウトします
        // if (raesidListIMG.size() > 0) {
        //     soql += ' AND Id in : raesidListIMG ';
        // }

        // if(ApplyHeadShow.Shippment_loaner_time__c != null) {
        //     soql += ' AND Shippment_loaner_time__c != null';
        // }

        soql += ' order by Name';
        raesList = Database.query(soql);
        //20201120 LJH OCSM_BP5-61 add start
        //申请单-申请一览List
        Map<Id,List<Rental_Apply_Equipment_Set__c>> raRaesMap = new  Map<Id,List<Rental_Apply_Equipment_Set__c>>();
        //20201120 LJH OCSM_BP5-61 add end
        if (raesList.size() > 0) {
            for (Rental_Apply_Equipment_Set__c raes : raesList) {
                //ToDo RentalApplyName取り方が変
                //RentalApplyName = raes.Rental_Apply__r.Name;
                //循环获得RAES_id
                RAESSearchList.add(raes.id);
                //20201120 LJH OCSM_BP5-61 add start
                if(!raRaesMap.containsKey(raes.Rental_Apply__c)){
                    raRaesMap.put(raes.Rental_Apply__c,new Rental_Apply_Equipment_Set__c[]{raes});
                }else{
                    raRaesMap.get(raes.Rental_Apply__c).add(raes);
                }
                //20201120 LJH OCSM_BP5-61 add end
            }
            RentalApplyName = raesList[0].Rental_Apply__r.Name;
        }

        Map<String, List<Rental_Apply_Equipment_Set_Detail__c>> raesdMap = new Map<String, List<Rental_Apply_Equipment_Set_Detail__c>>();

        String NextName = '';
        if(RAESSearchList.size()>0){
            List<Rental_Apply_Equipment_Set_Detail__c> RAESDLine = New List<Rental_Apply_Equipment_Set_Detail__c>();
            RAESDLine = [
                    select id, Equipment_SetD_Borrowed__c,
                        EquipmentSet_Managment_Code_formula__c,
                        Rental_Apply_Equipment_Set__c,
                        Rental_Apply_Equipment_Set__r.Name,
                        Rental_Apply_Equipment_Set__r.Fixture_Set__r.Name,
                        Rental_Apply_Equipment_Set__r.Loaner_code__c,
                        FSD_Name_CHN__c,
                        Fixture_Name_text__c,
                        Fixture_Model_No_F__c,
                        Fixture_Model_No_text__c,
                        Loaner_asset_no__c,
                        Internal_Asset_number_c__c,
                        ProductName__c,
                        Asset__r.Product2.Image_DocumentID__c,
                        Asset__r.OwnershipMachine_No__c,
                        Asset__r.Name,
                        Asset__r.Loaner_accsessary__c,
                        Asset__r.WH_location__c,
                        SerialNumber__c,
                        SerialNumber_text__c,
                        Is_Body__c,
                        // Rental_Apply_Equipment_Set__r.SerialNumber_text__c,
                        Rental_Apply_Equipment_Set__r.Loaner_code_text__c
                    from Rental_Apply_Equipment_Set_Detail__c
                    where Rental_Apply_Equipment_Set__r.Id in :RAESSearchList
                and Cancel_Select__c = False
                AND asset__c != null
                AND Cancel_Select__c = false
                AND Shipment_request_time2__c != null
                    order by Rental_Apply_Equipment_Set__c, Name, ProductName__c asc nulls last, Asset__r.Loaner_accsessary__c];
            mainCnt = 0;
            accessoryCnt = 0;
            for (Rental_Apply_Equipment_Set_Detail__c ra : RAESDLine) {
                if (ra.Is_Body__c) {
                    mainCnt += 1;
                } else {
                    accessoryCnt += 1;
                }
                if (raesdMap.containsKey(ra.Rental_Apply_Equipment_Set__c)) {
                    raesdMap.get(ra.Rental_Apply_Equipment_Set__c).add(ra);
                } else {
                    List<Rental_Apply_Equipment_Set_Detail__c> rsd = new List<Rental_Apply_Equipment_Set_Detail__c>();
                    rsd.add(ra);
                    raesdMap.put(ra.Rental_Apply_Equipment_Set__c,rsd);
                }
            }
            //20201125 LJH OCSM_BP5-61 add start
            raesList = null;
            raesList = raRaesMap.get(rentalApplyId[0]);
            //20201125 LJH OCSM_BP5-61 add end
        }
        DetailsAllList = new List<lineInfo>();
        recordsIMG = new List<PDFInfoIMG>();
        //计算行高总数，用于分页
        integer LineNum = 0;

        System.debug('raesListsize is' + raesList.size());

        for (Integer i = 0; i < raesList.size(); i++) {
            Rental_Apply_Equipment_Set__c raes = raesList[i];
            Map<String,integer> DLSM = new Map<String,integer>();
            Map<String,String> LoanerCodeSM = new Map<String,String>();
            Map<String,String> DetailsLineMap = new Map<String,String>();
            Set<String> AllSet = new Set<String>();
            String DetailsLine = '';
            List<Rental_Apply_Equipment_Set_Detail__c> LineOrderList = raesdMap.get(raes.Id);
            // system.debug('LineOrderList测试'+LineOrderList.size());

            if (LineOrderList!=null) {
                boolean LotFlag = true;
                for (Rental_Apply_Equipment_Set_Detail__c Ec : LineOrderList) {
                    // LoanerCodeSM.put(Ec.Equipment_Set__r.Loaner_code__c, Ec.Equipment_Set__r.Loaner_code__c);
                    system.debug('LoanerCodeSM显示'+LoanerCodeSM);
                    //先数出来有多少重复的
                    //DetailsLine = DetailsLine + Ec.Asset__r.OwnershipMachine_No__c+' ; ';
                    if (DLSM.containsKey(Ec.Asset__r.OwnershipMachine_No__c)) {
                       //OCM内部产品型号 OwnershipMachine_No__c
                       integer j = DLSM.get(Ec.Asset__r.OwnershipMachine_No__c)+1;
                       DLSM.put(Ec.Asset__r.OwnershipMachine_No__c, j);
                    } else {
                       DLSM.put(Ec.Asset__r.OwnershipMachine_No__c, 1);
                    }

                    if (Ec.Asset__r.Loaner_accsessary__c == false) {
                       //有主机
                       LotFlag = false;
                    }

                    //去掉重复的 ToDo条件确认
                    // if (Ec.Asset__r.Loaner_accsessary__c == true) {
                        //if(Ec.SerialNumber__c == ''||Ec.SerialNumber__c==null){
                       AllSet.add(Ec.Asset__r.OwnershipMachine_No__c);
                    // }
                }

                system.debug('DLSM:::::'+DLSM);
                system.debug('AllSet:::::'+AllSet);
                for (String Ecc : AllSet) {
                    //然后做成字符串
                    if (LotFlag) {
                        //都为附属品
                        if(DLSM.get(Ecc)==1) {
                            DetailsLine = DetailsLine + Ecc + ' ; ';
                        } else {
                            DetailsLine = DetailsLine + Ecc + '*' + DLSM.get(Ecc) + ' ; ';
                        }
                    } else {
                        if (DLSM.get(Ecc)==1&&!LoanerCodeSM.containsKey(Ecc)) {
                            DetailsLine = DetailsLine + Ecc+' ; ';
                        } else if (DLSM.get(Ecc)!=1) {
                            DetailsLine = DetailsLine + Ecc+'*'+DLSM.get(Ecc)+' ; ';
                        }
                    }
                }

                if (DetailsLine.length()>0) {
                    DetailsLine = DetailsLine.substring(0, DetailsLine.length()-2);
                }

                // List<Rental_Apply_Equipment_Set_Detail__c> LinJar = raesdMap.get(raes.Equipment_Set__c);
                // Rental_Apply_Equipment_Set_Detail__c LineOrder = new Rental_Apply_Equipment_Set_Detail__c();
                // if (LinJar.size()>0) {
                Integer k = i;
                for (Rental_Apply_Equipment_Set_Detail__c LineOrder : LineOrderList) {
                        // LineOrder = raesdMap.get(raes.Equipment_Set__c)[0];
                        // system.debug('LineOrder::::::'+LineOrder);
                        lineInfo Li = new lineInfo(LineOrder, LotFlag, k);
                        Li.AssetModelNo = DetailsLine;
                        //载入行高 每行44
                        if (DetailsLine.length() != 0) {
                            Li.LineHeight = (DetailsLine.length()/44) + (Math.mod(DetailsLine.length(), 44) > 0 ? 1 : 0);
                        } else {
                            Li.LineHeight = 1;
                        }

                        Li.height = Li.LineHeight * MAXLINEHRIGHT;

                        LineNum = LineNum + Li.LineHeight;
                        DetailsAllList.add(Li);
                        k = null;
                }

                // }
                // for (Rental_Apply_Equipment_Set_Detail__c raesd : LineOrderList) {
                    PDFInfoIMG infoIMG = new PDFInfoIMG(raes, LineOrderList);
                    recordsIMG.add(infoIMG);
                // }
            }
        }

  //      //分页 15行一页，实际显示14行，判断第14行是否为双倍高度
  //      //明细总页数
        integer PageAllNum = integer.valueOf(Math.ceil( LineNum / (MAXPAGECOUNT+1))+1+'');

        pageCnt = integer.valueOf(Math.ceil( DetailsAllList.size() / 17 ) +1+'');
        //for(Integer j = 0 ; j< PageAllNum ;j++){
        //新建一页
        
        PDFInfo info = new PDFInfo(new Rental_Apply__c(),New List<Rental_Apply_Equipment_Set_Detail__c>());
        // 20220413 ljh  抬头显示 update start
        //20201120 LJH OCSM_BP5-61 update start
        // if(!IsShowLU){
        //     if(!IsMain){
        //         info.isAgencyOrCenter = true;//办事处
        //         info.AgencyAddressName = '请将备品回寄至';
        //         info.AgencyAddressName += ApplyHeadShow.ToAgency__c == null ? '' : ApplyHeadShow.ToAgency__c;
        //         info.AgencyAddressName += '办事处';
        //         info.AgencyOrCenterAddress = AgencyAddressIdMap.get(ApplyHeadShow.Id);
        //     }else{
        //         info.isAgencyOrCenter = false;//备品中心
        //         info.AgencyOrCenterAddress = centerAddressInfo;
        //     }   
        // }else{
        //     info.isAgencyOrCenter = false;//备品中心
        //     info.AgencyOrCenterAddress = centerAddressInfo;
        // }
        //20201120 LJH OCSM_BP5-61 update  end
        if(!IsShowLU){
            if(!IsMain){
                isAgencyOrCenter = true;//办事处
                AgencyAddressName = '请将备品回寄至';
                AgencyAddressName += ApplyHeadShow.ToAgency__c == null ? '' : ApplyHeadShow.ToAgency__c;
                AgencyAddressName += '办事处';
                AgencyOrCenterAddress = AgencyAddressIdMap.get(ApplyHeadShow.Id);
            }else{
                isAgencyOrCenter = false;//备品中心
                AgencyOrCenterAddress = centerAddressInfo;
            }   
        }else{
            isAgencyOrCenter = false;//备品中心
            AgencyOrCenterAddress = centerAddressInfo;
        }
        // 20220413 ljh  抬头显示 update end
        integer PageCut = 0;
        if (PageAllNum>=7) {
           PageAllNum=7;
        }

        Integer count = DetailsAllList.size() / MAXPAGECOUNT;
        if (Math.mod(DetailsAllList.size(), MAXPAGECOUNT) > 0) {
        count += 1;
        }


        for (Integer K = 0; K < count*MAXPAGECOUNT ; K++) {
            if (DetailsAllList.size()>k) {
               info.lineList.add(DetailsAllList[K]);
               PageCut = PageCut +DetailsAllList[K].LineHeight-1;

            } else {
               info.lineList.add(new lineInfo(new Rental_Apply_Equipment_Set_Detail__c(), false, null));
            }
            System.debug('info.lineList is' + info.lineList);
            if (records.size() < PageAllNum) {
               PageCut = PageCut+1;
            }
            system.debug('PageCut[K]' + PageCut);

            if (info.lineList.size() >= MAXPAGECOUNT) {
               records.add(info);
               system.debug('records内容::::::::'+records);
               info = new PDFInfo(new Rental_Apply__c(),New List<Rental_Apply_Equipment_Set_Detail__c>());
               List<String> G = new List<String>();
               G.add('ANY element');
               PageCutList.add(G);
               PageCut=0;
            }
        }

       // for (Integer K = 0; K < PageAllNum*MAXPAGECOUNT ; K++) {
       //     if (DetailsAllList.size()>k) {
       //         info.lineList.add(DetailsAllList[K]);
       //         PageCut = PageCut +DetailsAllList[K].LineHeight-1;

       //     } else {
       //         info.lineList.add(new lineInfo(new Rental_Apply_Equipment_Set_Detail__c(), false, null));
       //     }
       //     System.debug('info.lineList is' + info.lineList);
       //     if (records.size() < PageAllNum) {
       //         PageCut = PageCut+1;
       //     }
       //     system.debug('PageCut[K]' + PageCut);

       //     if (PageCut >= MAXPAGECOUNT) {
       //         records.add(info);
       //         system.debug('records内容::::::::'+records);
       //         info = new PDFInfo(new Rental_Apply__c(),New List<Rental_Apply_Equipment_Set_Detail__c>());
       //         List<String> G = new List<String>();
       //         G.add('ANY element');
       //         PageCutList.add(G);
       //         PageCut=0;
       //     }
       // }

  //      system.debug('records内容'+records);
        pageCnt = records.size();
        AllSum = DetailsAllList.size();
  
  //      //pageNum
        recordsIMGfist = new List<PDFInfoIMG>();

        for (integer J = pageNum * 10; J<pageNum * 10 +10; J++) {
            if (J < recordsIMG.size()) {
               recordsIMGfist.add(recordsIMG.get(J));
            }
        }

        pageCntIMG = recordsIMGfist.size();
    }
    // Data Bean
    class PDFInfo {
        public String eSetName { get; private set; } //备品中心管理单号
        // 20220413 ljh  抬头显示 update start
        // public String AgencyAddressName { get; private set; } //回寄地址 20201123 LJH OCSM_BP5-61 add
        // public Boolean isAgencyOrCenter { get; private set; } //备品中心？办事处 20201123 LJH OCSM_BP5-61 add
        // public String AgencyOrCenterAddress { get; private set; }//20201124 LJH OCSM_BP5-61 add
        // 20220413 ljh  抬头显示 update end
        public String requestOwner { get; private set; } //申请人
        public String salesDept { get; private set; } //所属本部
        public String workPlace { get; private set; } //所属办事处
        public String acc { get; private set; } //借用单位
        public String shippingDay { get; private set; } //希望到货日
        public String receivedStaff { get; private set; } //收件人
        public String receivedStaffPhone { get; private set; } //收件人电话
        public String shippmentAdress { get; private set; } //发送地址
        public String HospitalEndDate { get; private set; } //在医院使用期限
        public String LoanerCode {get;private set;} //備品型番
        public List<lineInfo> lineList { get; private set; }
        public String AssetModelNo {get;private set;} //OCM内部产品型号(略称)
        public PDFInfo(Rental_Apply__c raes, List<Rental_Apply_Equipment_Set_Detail__c> raesdList) {
            eSetName = raes.Name;
            requestOwner = raes.Person_In_Charge__c;
            salesDept = raes.Salesdept__c;
            workPlace = raes.WorkPlace__c;
            acc = raes.Account__c == null && raes.Campaign__c != null ? raes.Campaign__c : raes.Account__c;
            shippingDay = raes.Request_shipping_day__c == null ? '' : raes.Request_shipping_day__c.format();
            receivedStaff = raes.Loaner_received_staff__c;
            receivedStaffPhone = raes.Loaner_received_staff_phone__c;
            shippmentAdress = raes.Shippment_adress_detail__c == null ? raes.direct_shippment_address__c : raes.Shippment_adress_detail__c;
            if (raesdList == null) {
                raesdList = new List<Rental_Apply_Equipment_Set_Detail__c>();
            }
            lineList = new List<lineInfo>();
        }
    }

    class lineInfo {
        //bp2
        public String index { get; private set; }  //  序号
        public String setName { get; private set; }  //  备品set型号
        public String dModelNo { get; private set; }  //  备品配套明细型号
        public String dChinaName { get; private set; }  //  中文名称
        public String dSerialNumber { get; private set; }  //  机身号
        public String dLoaner_asset_no { get; private set; }  //  固定资产号码
        public String dManagementnumber { get; private set; }  //  管理号码
        public String dQRCode { get; private set; }  //  二维码
        private Integer maxLineContact;
        public String wh_location { get; private set; }  //  货位号


        public String name1 { get; private set; }
        public String productName1 { get; private set; }
        public String serialNumber1 { get; private set; }
        public String name2 { get; private set; }
        public String productName2 { get; private set; }
        public String serialNumber2 { get; private set; }
        public String LoanerCode {get;private set;} //備品型番
        public String AssetModelNo {get;private set;} //OCM内部产品型号(略称)
        public List<String> AssetModelNoList {get;private set;}//OCM内部产品型号(略称) 集合
        public integer LineHeight {get;private set;} //记录行高
        public integer height {get;private set;}
        // public lineInfo(Equipment_Set_Detail__c esd1, Equipment_Set_Detail__c esd2) {
        //    AssetModelNoList = new List<String>();
        //    name1 = esd1.Equipment_Set__r.Name == null ? '　' : esd1.Equipment_Set__r.Name;
        //    productName1 = esd1.Asset__r.Name;
        //    serialNumber1 = esd1.SerialNumber__c;
        //    name2 = esd2.Name == null ? '　' : esd2.Name;
        //    productName2 = esd2.Asset__r.Name;
        //    serialNumber2 = esd2.SerialNumber__c;
        //    height = height ==null ? 25: height;
        //    LoanerCode = esd1.Equipment_Set__r.Loaner_code__c;
        // }
// MAXINDEXCOUNT = 26;
// MAXDCHINNAMECOUNT = 2
// MAXDMODELNOCOUNT = 26
// MAXDSERIALNUMBERCOUNT
// MAXDLANCOUNT = 26;
// MAXDMMCOUNT = 26;
        public lineInfo(Rental_Apply_Equipment_Set_Detail__c raesd, boolean LotFlag, Integer inde) {
           AssetModelNoList = new List<String>();
            //bp2
            maxLineContact = 0;
            index = inde == null ? '' : String.valueOf(inde + 1);
            dChinaName = raesd.Fixture_Name_text__c;
            // dChinaName = '457100: 孔口探头，COTTLE型，带工业金刚石，20.5 cm（工业金刚石研磨表面取代了传统的钢纹，这使得操作省力，更简易，同时获得了更高的精确度。它能以极高的精准度切割或研磨较大骨组织）。';
            // dChinaName = '孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口探头孔口';
            dModelNo = raesd.Fixture_Model_No_text__c;
            dSerialNumber = raesd.SerialNumber_text__c;
            // dLoaner_asset_no = raesd.Loaner_asset_no__c;
            // OLY_OCM-321 发货确认单：固定资产号字段修改显示为固定资产编号(Key) _c__c?
            dLoaner_asset_no = raesd.Internal_Asset_number_c__c;
            dManagementnumber = raesd.EquipmentSet_Managment_Code_formula__c == null ? '　' : raesd.EquipmentSet_Managment_Code_formula__c;




           name1 = raesd.Equipment_SetD_Borrowed__c == null ? '　' : raesd.Equipment_SetD_Borrowed__c;
           productName1 = raesd.Asset__r.Name;
           setName = raesd.Rental_Apply_Equipment_Set__r.Fixture_Set__r.Name;
           if(LotFlag){
               serialNumber1 = ' ';
               LoanerCode = ' ';
           }else{
               // serialNumber1 = raesd.Rental_Apply_Equipment_Set__r.SerialNumber_text__c;
               LoanerCode = raesd.Rental_Apply_Equipment_Set__r.Loaner_code_text__c;
           }
           //name2 = esd2.Name == null ? '　' : esd2.Name;
           //productName2 = esd2.Asset__r.Name;
           //serialNumber2 = esd2.SerialNumber__c;
           height = height ==null ? MAXLINEHRIGHT: height;
           wh_location = raesd.Asset__r.WH_location__c;
        }
    }
    // Data Bean
    class PDFInfoIMG {
        public String eSetName { get; private set; }
        // public Equipment_Set__c es { get; private set; }
        public Fixture_Set__c es { get; private set; }

        public Rental_Apply_Equipment_Set__c raesc { get; private set; }
        public List<lineInfoIMG> lineList { get; private set; }
        public List<lineInfoIMGrase> lineListIMG { get; private set; }

        //public PDFInfoIMG(Rental_Apply_Equipment_Set__c raes, List<Equipment_Set_Detail__c> esdl) {
        //    eSetName = raes.Equipment_Set__r.Name;
        //    es = raes.Equipment_Set__r;
        //    raesc = raes;
        //    if (esdl == null) {
        //        esdl = new List<Equipment_Set_Detail__c>();
        //    }
        //    lineList = new List<lineInfoIMG>();
        //    if (esdl.Size() >= 20) {
        //        for (Integer i = 0; i < 5; i++) {
        //            lineList.add(new lineInfoIMG(esdl[i*4],esdl[i*4+1],esdl[i*4+2],esdl[i*4+3]));
        //        }
        //    } else {
        //        Integer size = esdl.Size();
        //        for (Integer i = 0; i < 20 - size; i++) {
        //            esdl.add(new Equipment_Set_Detail__c(Name='　'));
        //        }
        //        for (Integer i = 0; i < 5; i++) {
        //            lineList.add(new lineInfoIMG(esdl[i*4],esdl[i*4+1],esdl[i*4+2],esdl[i*4+3]));
        //        }
        //    }
        // }
        public PDFInfoIMG(Rental_Apply_Equipment_Set__c raes, List<Rental_Apply_Equipment_Set_Detail__c> raesdcl) {
           eSetName = raes.Fixture_Set__r.Name;
           es = raes.Fixture_Set__r;
           if (raesdcl == null) {
               raesdcl = new List<Rental_Apply_Equipment_Set_Detail__c>();
           }
           lineListIMG = new List<lineInfoIMGrase>();
           if (raesdcl.Size() >= 20) {
               for (Integer i = 0; i < 5; i++) {
                   lineListIMG.add(new lineInfoIMGrase(raesdcl[i*4],raesdcl[i*4+1],raesdcl[i*4+2],raesdcl[i*4+3]));
               }
           } else {
               Integer size = raesdcl.Size();
               for (Integer i = 0; i < 20 - size; i++) {
                   raesdcl.add(new Rental_Apply_Equipment_Set_Detail__c());
               }
               for (Integer i = 0; i < 5; i++) {
                   lineListIMG.add(new lineInfoIMGrase(raesdcl[i*4],raesdcl[i*4+1],raesdcl[i*4+2],raesdcl[i*4+3]));
               }
           }
        }
    }

    class lineInfoIMG {
        //public List<esdInfo> esdl { get; private set; }

        //public lineInfoIMG(Equipment_Set_Detail__c e1, Equipment_Set_Detail__c e2, Equipment_Set_Detail__c e3, Equipment_Set_Detail__c e4) {
        //    esdl = new List<esdInfo>();
        //    esdl.add(new esdInfo(e1));
        //    esdl.add(new esdInfo(e2));
        //    esdl.add(new esdInfo(e3));
        //    esdl.add(new esdInfo(e4));
        //    //esdl.add(e5);
        //}
    }
    class lineInfoIMGrase {
        public List<esdInfo> esdl { get; private set; }

        public lineInfoIMGrase(Rental_Apply_Equipment_Set_Detail__c e1, Rental_Apply_Equipment_Set_Detail__c e2, Rental_Apply_Equipment_Set_Detail__c e3, Rental_Apply_Equipment_Set_Detail__c e4) {
           esdl = new List<esdInfo>();
           esdl.add(new esdInfo(e1));
           esdl.add(new esdInfo(e2));
           esdl.add(new esdInfo(e3));
           esdl.add(new esdInfo(e4));
           //esdl.add(e5);
        }
    }

    class esdInfo {
        // public Rental_Apply_Equipment_Set_Detail__c esd { get; private set; }
        public Rental_Apply_Equipment_Set_Detail__c raesl { get; private set; }
        public Integer nameLength { get; private set; }
        public String ProductName { get; private set; }


        public esdInfo(Rental_Apply_Equipment_Set_Detail__c e) {
            raesl = e;
            String name = e.ProductName__c;
            nameLength = name == null ? 0 : name.length();

            if(nameLength <= 21){
               //一行
               ProductName = name;
            }else if(nameLength > 21 && nameLength <= 74){
               //两行
               if(nameLength <= 42){
                   //7px
                   //for(Integer i = 0; i < 2 ; i++){
                       ProductName = name.substring(0,21)+'<br/>'+name.substring(21,nameLength);
                   //}
               }else{
                   //4px
                   //for(Integer i = 0; i < 2 ; i++){
                       ProductName = name.substring(0,37)+'<br/>'+name.substring(37,nameLength);
                   //}
               }
                
            }else{
               //三行
               //for(Integer i = 0; i < 3 ; i++){
                       ProductName = name.substring(0,37)+'<br/>' + name.substring(37,74)+'<br/>'+name.substring(74,nameLength);
               //}
            }
        }
    }
}