public with sharing class TransferApplyPDFController {

    private static Integer MAXLINEHRIGHT = 25;//maxLineHight 25
    private static Integer MAXPAGECOUNT = 26; //32 maxPageCount 22

    private String transferApplyId;

    public List<PDFInfo> records { get; set; }
    public Integer pageCnt { get; set; }
    //public Integer pageNum { get; set; }
    public Integer AllSum {get;set;}
    public TransferApply__c ApplyHeadShow { get; set; }
    public List<lineInfo> DetailsAllList { get; set;}
    //public List<List<String>> PageCutList { get; set; }
    public TransferApplyPDFController() {
        //pageNum = Integer.valueOf(ApexPages.currentPage().getParameters().get('page'));
        //Apexpages.currentPage().getHeaders().put('X-UA-Compatible', 'IE=8');
        TransferApplyId = ApexPages.currentPage().getParameters().get('raid');
        ApplyHeadShow = new TransferApply__c();
    }
    public Integer getWordCount(String s)
	{
		s = s.replaceAll('[^\\x00-\\xff]', '**');
		Integer length = s.length();
		return length;
	}
    // 画面初始化
    public void init() {
    	records = new List<PDFInfo>();
    	//PageCutList = new List<List<String>>();
        List<TransferApply__c> ApplyList = new List<TransferApply__c>();
        if(transferApplyId!=null){
            ApplyList = [SELECT Id,
                                Name,
                                RecordTypeName__c,
                                From_Location__c,
                                Destination_location__c,
                                Contact_Person__r.Name,
                                QRImg__c,
                                QRId__c
                        FROM  TransferApply__c
                        WHERE Id = :transferApplyId];
        }
        if(ApplyList.size()>0){
            ApplyHeadShow = ApplyList[0];
        }
        DetailsAllList = new List<lineInfo>();
        //计算行高总数，用于分页
        integer LineNum = 0;
        List<TransferApplyDetail__c> LineOrderList = [SELECT Id,Name,Fixture_Model_No__c,
        										TransferApplySummary__r.Fixture_Set__r.Name,
        										Internal_Asset_number_key__c,
										        SerialNumber__c,
										        EquipmentSet_Managment_Code_After__c,
										        Salesdepartment_After__c,
										        SalesProvince_After__c,
										        Equipment_Type_After__c,
										        Manage_type__c,
										        Loaner_accsessary__c,
										        TAESD_Status__c
										        FROM TransferApplyDetail__c 
										        WHERE TransferApply__c =:TransferApplyId 
									        	AND Cancel_Select__c = false 
                                                AND Approved_F__c = true 
									        	ORDER BY Name 
    											];	
    	//取消的、出库前检测NG（即不是最终发货明细的）的不显示											
		Map<String,integer> DLSM = new Map<String,integer>();
		Map<String,String> LoanerCodeSM = new Map<String,String>();
		Map<String,String> DetailsLineMap = new Map<String,String>();
		String DetailsLine = '';
        if (LineOrderList!=null) {
        	Integer i = 0;
            for (TransferApplyDetail__c LineOrder : LineOrderList) {
                    DetailsLine  = LineOrder.Name;
                    lineInfo Li = new lineInfo(LineOrder);
                    //载入行高 每行44
                    if (getWordCount(DetailsLine) != 0) {
                        Li.LineHeight = (getWordCount(DetailsLine)/56) + (Math.mod(getWordCount(DetailsLine), 56) > 0 ? 1 : 0);
                    } else {
                        Li.LineHeight = 1;
                    }

                    Li.height = Li.LineHeight * MAXLINEHRIGHT;

                    LineNum = LineNum + Li.LineHeight;
                    DetailsAllList.add(Li);
            }
        }
		//明细总页数
        integer PageAllNum = integer.valueOf(Math.ceil( LineNum / (MAXPAGECOUNT+1))+1+'');

        pageCnt = integer.valueOf(Math.ceil( DetailsAllList.size() / 27 ) +1+'');//17
        System.debug('pageCnt:'+pageCnt+':PageAllNum:'+PageAllNum);
        //新建一页
        PDFInfo info = new PDFInfo(new TransferApply__c());       
        integer PageCut = 0;
        if (PageAllNum>=7) {
           PageAllNum=7;
        }
        Integer tadCount = DetailsAllList.size();
        for (Integer K = 0; K < tadCount; K++) {
        	PageCut = PageCut +DetailsAllList[K].LineHeight;
        	if (PageCut > MAXPAGECOUNT) {
               records.add(info);
               system.debug('records内容::::::::'+records);
               info = new PDFInfo(new TransferApply__c());
               // List<String> G = new List<String>();
               // G.add('ANY element');
               // PageCutList.add(G);
               PageCut=0;
            }
        	info.lineList.add(DetailsAllList[K]);
            System.debug('PageCut:'+PageCut);
            if(K==tadCount-1){
        		for(Integer t = PageCut; t < MAXPAGECOUNT; t++){
            		info.lineList.add(new lineInfo());
               		PageCut = PageCut+1;		
            	}
            	if (PageCut >= MAXPAGECOUNT) {
					records.add(info);
					system.debug('records内容::::::::'+records);
					info = new PDFInfo(new TransferApply__c());
					// List<String> G = new List<String>();
					// G.add('ANY element');
					// PageCutList.add(G);
					PageCut=0;
	            } 
            } 
        }
        pageCnt = records.size();
        AllSum = DetailsAllList.size();
    }
    // Data Bean
    class PDFInfo {
        public String name { get; private set; } //调拨明细单号
        public String RecordTypeName { get; private set; }//调拨类型
        public String FromLocation { get; private set; }//存放地
        public String DestinationLocation { get; private set; }//调拨至地
        public String ContactPerson { get; private set; }//联系人
        public List<lineInfo> lineList { get; private set; }
        public PDFInfo(TransferApply__c ta) {
	        name = ta.name;
	        RecordTypeName = ta.RecordTypeName__c;
	        FromLocation = ta.From_Location__c;
	        DestinationLocation = ta.Destination_location__c;
	        ContactPerson = ta.Contact_Person__r.Name;
	        lineList = new List<lineInfo>();
        }
    }

    class lineInfo {
        public String Name { get; private set; }  //  调拨一览明细No
        public String Fixture_Model_No { get; private set; }  //  备品配套型号
        public String Fixture_Model_No_F { get; private set; }  //  备品配套明细型号
        public String Internal_Asset_number_key { get; private set; }  //  固定资产编号(key)
        public String SerialNumber { get; private set; }  //  机身编号
        public String EquipmentSet_Managment_Code { get; private set; }  //  备品管理编码
        public String Salesdepartment { get; private set; }  //  所在地区(本部)
        public String SalesProvince { get; private set; }  //  所在地区(省)
        public String Equipment_Type { get; private set; }  //  备品分类
        public String Manage_type { get; private set; }  //  管理种类
        public String Loaner_accsessary { get; private set; }  //  备品用途附属品
        public String TAESD_Status { get; private set; }  //  备品状态
        
        public integer LineHeight {get;private set;} //记录行高
        public integer height {get;private set;}
        public lineInfo() {
        	Loaner_accsessary = null;
           	height = height ==null ? MAXLINEHRIGHT: height;
        }
        public lineInfo(TransferApplyDetail__c tad) {
			Name = tad.Name;
			Fixture_Model_No = tad.Fixture_Model_No__c;
			Fixture_Model_No_F = tad.TransferApplySummary__r.Fixture_Set__r.Name;
			Internal_Asset_number_key = tad.Internal_Asset_number_key__c;
			SerialNumber = tad.SerialNumber__c;
			EquipmentSet_Managment_Code = tad.EquipmentSet_Managment_Code_After__c;
			Salesdepartment = tad.Salesdepartment_After__c;
			SalesProvince = tad.SalesProvince_After__c;
			Equipment_Type = tad.Equipment_Type_After__c;
			Manage_type = tad.Manage_type__c;
			Loaner_accsessary = String.valueOf(tad.Loaner_accsessary__c);
			TAESD_Status = tad.TAESD_Status__c;
           	height = height ==null ? MAXLINEHRIGHT: height;
        }
    }
}