public with sharing class BatchSelectRepairPageController {
    public List<RetrievalData> RevalInfoList { get; set; }
    public List<RepairData> RAInfoList { get; set; }
    public List<Repair__c> RepairList { get; set; }
    public List<String> repairIdList{ get; set; }
    public Integer RACount {
        get { return RAInfoList == null ? 0 : RAInfoList.size(); }
    }

    public BatchSelectRepairPageController() {

    }

    public void init(){
        RetrievalData reval = new RetrievalData();
        RevalInfoList = new List<RetrievalData>();
        reval.RepairName = null;
        reval.SAPRepairNo = null;
        reval.HospitalName = null;
        RevalInfoList.add(reval);
    }

    public PageReference RetrievalBtn() {
        RepairList = new List<Repair__c>();
        RAInfoList = new List<RepairData>();
        String RepairName;
        String SAPRepairNo;
        String HospitalName;
        for(RetrievalData reval : RevalInfoList){
            if(String.isNotBlank(reval.RepairName)){
                RepairName = reval.RepairName;
            }
            if(String.isNotBlank(reval.SAPRepairNo)){
                SAPRepairNo = reval.SAPRepairNo;
            }
            if(String.isNotBlank(reval.HospitalName)){
                HospitalName = reval.HospitalName;
            }
        }
        System.debug('RepairName1:' + RepairName);
        if(String.isBlank(RepairName) && String.isBlank(SAPRepairNo) && String.isBlank(HospitalName)){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '请至少添加一个检索条件!'));
            return null;
        }
        String sql = 'select Id, Name, SAP_Service_Repair_No__c, HP_Name__c, Department_Name__c from Repair__c ';
        if(String.isNotBlank(RepairName)){
            sql += ' where Name like \'%' + RepairName.trim() + '%\'';
            if(String.isNotBlank(SAPRepairNo)){
                sql += ' and SAP_Service_Repair_No__c like \'%' + SAPRepairNo.trim() + '%\'';
                if(String.isNotBlank(HospitalName)){
                    sql += ' and (HP_Name__c like \'%' + HospitalName.trim() + '%\' or Department_Name__c like \'%' + HospitalName.trim() + '%\')';
                }
            } else if(String.isNotBlank(HospitalName)) {
                sql += ' and (HP_Name__c like \'%' + HospitalName.trim() + '%\' or Department_Name__c like \'%' + HospitalName.trim() + '%\')';
            }
        } else if(String.isNotBlank(SAPRepairNo)){
            sql += ' where SAP_Service_Repair_No__c like \'%' + SAPRepairNo.trim() + '%\'';
            if(String.isNotBlank(HospitalName)){
                sql += ' and (HP_Name__c like \'%' + HospitalName.trim() + '%\' or Department_Name__c like \'%' + HospitalName.trim() + '%\')';
            }
        } else if(String.isNotBlank(HospitalName)){
            sql += ' where HP_Name__c like \'%' + HospitalName.trim() + '%\' or Department_Name__c like \'%' + HospitalName.trim() + '%\'';
        }
        sql += ' limit 5000';
        RepairList = Database.query(sql);
        if(RepairList.size() >= 5000 ){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '检索数据太多,请缩小检索范围'));
            return null;
        }
        if(RepairList.size() <= 0 ){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '没检索到任何修理'));
            return null;
        }
        for(Repair__c ra : RepairList){
            RepairData raData = new RepairData(ra);
            RAInfoList.add(raData);
        }
        return null;
    }

    public PageReference showPDF() {
        repairIdList = new List<String>();
        RepairList = new List<Repair__c>();
        String url = '=';
        if(RAInfoList == null){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '请先检索修理'));
            return null;
        }
        for(RepairData rd : RAInfoList) {
            if(rd.IFCheck){
                repairIdList.add(rd.repair.Id);
                RepairList.add(rd.repair);
            }
        }
        if(repairIdList.size() > 0){
            if(repairIdList.size() <= 50){
                if(RepairList.size() > 0){
                    generateAttachment(RepairList);
                }
                for(String Id : repairIdList){
                    url += Id + '=';
                }
                System.debug('url1:'+url);
                url = url.substring(0, url.lastIndexOf('='));
                System.debug('url2:'+url);
                PageReference pageRef = new PageReference('/apex/MaintenanceCommissionPDF?id' + url);
                pageRef.setRedirect(true);
                return pageRef;
            } else{
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '多单打印最大数量为50,请选择少于50个修理!'));
                return null;
            }
        } else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '请至少选择一个修理'));
            return null;
        }
    }

    // 生成pdf添加到对应的修理中
    public static PageReference generateAttachment(List<Repair__c> repList){
        PageReference pdfPage;
        List<Attachment> attachments = new List<Attachment>();
        for(Repair__c re : repList){       
            pdfPage = new PageReference('/apex/MaintenanceCommissionPDF?id=' + re.Id);
            Blob pdfBody;
            if(Test.isRunningTest()) { 
                pdfBody = blob.valueOf('Unit.Test');
            } else {
                pdfBody = pdfPage.getContentAsPDF();
            }
            Attachment attach = new Attachment();
            attach.Body = pdfBody;
            attach.Name = re.name + '_' + 'MaintenanceCommission.pdf';
            attach.ParentId = re.id;
            attachments.add(attach);
        }
        insert attachments;
        return null;
    }

    public class RetrievalData {
        public String RepairName{ get; set; }
        public String SAPRepairNo{ get; set; }
        public String HospitalName{ get; set; }
    }

    public class RepairData {
        public Boolean IFCheck { get; set; }
        public Repair__c repair { get; set; }
        public RepairData(Repair__c RepairInfo){
            IFCheck = null;
            repair = RepairInfo;
        }
    }
}