public with sharing class BatchSelectRepairPageController {
    public RetrievalData RevalInfo { 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(){
        RevalInfo = new RetrievalData();
    }

    public List<SelectOption> getStatusItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('','--无--'));
        options.add(new SelectOption('草案中','草案中'));
        options.add(new SelectOption('1.受理完毕','1.受理完毕'));
        options.add(new SelectOption('2.维修报价阶段','2.维修报价阶段'));
        options.add(new SelectOption('3.维修阶段','3.维修阶段'));
        options.add(new SelectOption('4.修理品返送阶段','4.修理品返送阶段'));
        options.add(new SelectOption('5.完毕','5.完毕'));
        options.add(new SelectOption('0.取消','0.取消'));
        options.add(new SelectOption('0.删除','0.删除'));
        options.add(new SelectOption('0.申请完毕','0.申请完毕'));
        return options;
    }

    public List<SelectOption> getSiteRepairItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('','--无--'));
        options.add(new SelectOption('RC修理','RC修理'));
        options.add(new SelectOption('直送SORC修理','直送SORC修理'));
        options.add(new SelectOption('直送OGZ修理','直送OGZ修理'));
        options.add(new SelectOption('办事处修理','办事处修理'));
        options.add(new SelectOption('现场修理','现场修理'));
        return options;
    }

    public PageReference RetrievalBtn() {
        RepairList = new List<Repair__c>();
        RAInfoList = new List<RepairData>();
        if(String.isBlank(RevalInfo.Status1) && String.isBlank(RevalInfo.RepairName) && String.isBlank(RevalInfo.SAPRepairNo) 
            && String.isBlank(RevalInfo.SerialNumber) && String.isBlank(RevalInfo.repair.Incharge_Staff__c) && String.isBlank(RevalInfo.onSiteRepair)
            && String.isBlank(String.valueOf(RevalInfo.repair.Aware_date__c)) && String.isBlank(RevalInfo.workLocationSelect)){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '请至少添加一个检索条件!'));
            return null;
        }
        String sql = 'select Id, Name, SAP_Service_Repair_No__c, Status1__c, Status2__c, Delivered_Product__c, SerialNumber__c, ';
        sql += 'HP_Name__c, State_Hospital__c, Incharge_Staff__c, FSE_ApplyForRepair_Day__c, work_location_select__c, On_site_repair__c,';
        sql += 'Number_of_EffectiveContract__c, NewProductGuaranteeObject__c from Repair__c where Id != null ';
        if(String.isNotBlank(RevalInfo.Status1)){
            sql += ' and Status1__c = \'' + RevalInfo.Status1 + '\'';
        }
        if(String.isNotBlank(RevalInfo.RepairName)){
            sql += ' and Name like \'%' + RevalInfo.RepairName + '%\'';
        }
        if(String.isNotBlank(RevalInfo.SAPRepairNo)){
            sql += ' and SAP_Service_Repair_No__c like \'%' + RevalInfo.SAPRepairNo + '%\'';
        }
        if(String.isNotBlank(RevalInfo.SerialNumber)){
            sql += ' and SerialNumber__c like \'%' + RevalInfo.SerialNumber + '%\'';
        }
        if(String.isNotBlank(RevalInfo.repair.Incharge_Staff__c)){
            sql += ' and Incharge_Staff__c = \'' + RevalInfo.repair.Incharge_Staff__c + '\'';
        }
        if(String.isNotBlank(String.valueof(RevalInfo.repair.Aware_date__c))){
            String day = String.valueof(RevalInfo.repair.Aware_date__c);
            day = day.substring(0,10);
            sql += ' and FSE_ApplyForRepair_Day__c = ' + day;
        }
        if(String.isNotBlank(RevalInfo.onSiteRepair)){
            sql += ' and On_site_repair__c = \'' + RevalInfo.onSiteRepair + '\'';
        }
        if(String.isNotBlank(RevalInfo.workLocationSelect)){
            sql += ' and work_location_select__c like \'%' + RevalInfo.workLocationSelect + '%\'';
        }
        sql += ' limit 200';
        System.debug('sql1:' + sql);
        if(String.isNotBlank(sql)){
            RepairList = Database.query(sql);
        }
        if(RepairList.size() == 200){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '检索数据太多,请缩小检索范围！'));
        }
        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 = '=';
        System.debug('RAInfoList:' + RAInfoList);
        if(RAInfoList == null || RAInfoList.isEmpty()){
            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 + '=';
                }
                url = url.substring(0, url.lastIndexOf('='));
                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>();
        List<String> attachmentNameList = new List<String>();
        List<Attachment> isGeneratedPDFList = new List<Attachment>();
        //删除已存在的附件，生成新的附件
        for(Repair__c re : repList){   
            attachmentNameList.add(re.name + '_' + 'MaintenanceCommission.pdf');
        }
        isGeneratedPDFList = [select id from Attachment where name IN:attachmentNameList];
        if(isGeneratedPDFList.size() > 0){
            delete isGeneratedPDFList;
        }
        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 Status1{ get; set; }
        public String RepairName{ get; set; }
        public String SAPRepairNo{ get; set; }
        public String SerialNumber{ get; set; }
        public String onSiteRepair{ get; set; }
        public String workLocationSelect{ get; set; }
        public Repair__c repair{ get; set; }
        public RetrievalData(){
            repair = new Repair__c();
        }
    }

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