/** 
 * SLA信息更新页面
 * 接收日报页面传来的访问场所、访问人员
 * 查询访问人员的电话
 */
public without sharing class SLAReportDetailsController {
    public SLAReportInfo__c slaInfo {get; set;}
    public String slaInfoId {get; set;}
    public String index {get; set;}
    public Boolean error {get; set;}
    public String eventcId;
    public String visitor1;
    public String visitor2;
    public String visitor3;
    public String visitor4;
    public String visitor5;
    public String serviceId;
    public String accId;
    // deloitte-zhj 20231115 PIPL还原
    public SLAReportDetailsController(){
        eventcId = ApexPages.currentPage().getParameters().get('eventCId') == null ? '' : ApexPages.currentPage().getParameters().get('eventCId');
        visitor1 = ApexPages.currentPage().getParameters().get('visitor1') == null ? '' : ApexPages.currentPage().getParameters().get('visitor1');
        visitor2 = ApexPages.currentPage().getParameters().get('visitor2') == null ? '' : ApexPages.currentPage().getParameters().get('visitor2');
        visitor3 = ApexPages.currentPage().getParameters().get('visitor3') == null ? '' : ApexPages.currentPage().getParameters().get('visitor3');
        visitor4 = ApexPages.currentPage().getParameters().get('visitor4') == null ? '' : ApexPages.currentPage().getParameters().get('visitor4');
        visitor5 = ApexPages.currentPage().getParameters().get('visitor5') == null ? '' : ApexPages.currentPage().getParameters().get('visitor5');
        accId = ApexPages.currentPage().getParameters().get('accountId') == null ? '' : ApexPages.currentPage().getParameters().get('accountId');
        serviceId = ApexPages.currentPage().getParameters().get('serviceId') == null ? '' : ApexPages.currentPage().getParameters().get('serviceId');
        index = ApexPages.currentPage().getParameters().get('index') == null ? '' : ApexPages.currentPage().getParameters().get('index');
        slaInfoId = ApexPages.currentPage().getParameters().get('id') == null ? '' : ApexPages.currentPage().getParameters().get('id');
    }
    public void init(){
        slaInfo = new SLAReportInfo__c();
        List<String> conIds = new List<String>();
        error = true;
        if (String.isNotBlank(slaInfoId)) { 
            List<SLAReportInfo__c>  slaInfos = [SELECT Id,Account_ID__c,Distribution_Person__c,Distribution_Method__c,Event__c,
                                        Maintenance_Contract__c,Visitor1__c,Visitor2__c,Visitor3__c,Visitor4__c,Visitor5__c,
                                        Visitor1__r.Phone,SLATask__c,Distribution_Consumption_Rate__c 
                                         FROM SLAReportInfo__c 
                                         WHERE Id = :slaInfoId];
            if (slaInfos.size() > 0) {
                slaInfo = slaInfos[0];
                if (String.isNotBlank(eventcId)) {
                    slaInfo.Event__c = eventcId;
                }
            }
        }else if (String.isNotBlank(eventcId)) { //Id为空  报告一览不为空
            List<SLAReportInfo__c>  slaInfos = [SELECT Id,Account_ID__c,Distribution_Person__c,Distribution_Method__c,Event__c,
                                        Maintenance_Contract__c,Visitor1__c,Visitor2__c,Visitor3__c,Visitor4__c,Visitor5__c,
                                        Visitor1__r.Phone,Distribution_Consumption_Rate__c 
                                         FROM SLAReportInfo__c 
                                         WHERE Event__c = :eventcId OR Id = :slaInfoId];
            if (slaInfos.size() > 0) {
                slaInfo = slaInfos[0];
                slaInfoId = slaInfos[0].Id;
            }else{ //没有报告书 创建
                slaInfo.Event__c = eventcId;
                if (String.isNotBlank(serviceId)) {
                    slaInfo.Maintenance_Contract__c = serviceId;
                    List<NewMaintenanceReport_Task__c> reportTasks = [SELECT Id,ActualDistributionTimes_Quarter__c,NewMaintenance_Contract__c,Distribution_Start_Date__c,Distribution_End_Date__c,Distribution_Person__c,NewMaintenance_Contract__r.Contract_Consumption_rate__c FROM NewMaintenanceReport_Task__c WHERE NewMaintenance_Contract__c =:serviceId];
                    Date today = Date.today();
                    for (NewMaintenanceReport_Task__c reportTask : reportTasks) {
                        if (today >= reportTask.Distribution_Start_Date__c && today <= reportTask.Distribution_End_Date__c) {
                            slaInfo.SLATask__c = reportTask.Id;
                            slaInfo.Distribution_Consumption_Rate__c = reportTask.NewMaintenance_Contract__r.Contract_Consumption_rate__c;
                        }
                    }
                }
                if (String.isNotBlank(accId)) {
                    slaInfo.Account_ID__c = accId;
                }
            }
        }else{ //没有报告一览，新建报告书
            if (String.isNotBlank(serviceId)) {
                slaInfo.Maintenance_Contract__c = serviceId;
                List<NewMaintenanceReport_Task__c> reportTasks = [SELECT Id,ActualDistributionTimes_Quarter__c,NewMaintenance_Contract__c,Distribution_Start_Date__c,Distribution_End_Date__c,Distribution_Person__c,NewMaintenance_Contract__r.Contract_Consumption_rate__c FROM NewMaintenanceReport_Task__c WHERE NewMaintenance_Contract__c =:serviceId];
                Date today = Date.today();
                for (NewMaintenanceReport_Task__c reportTask : reportTasks) {
                    if (today >= reportTask.Distribution_Start_Date__c && today <= reportTask.Distribution_End_Date__c) {
                        slaInfo.SLATask__c = reportTask.Id;
                        slaInfo.Distribution_Consumption_Rate__c = reportTask.NewMaintenance_Contract__r.Contract_Consumption_rate__c;
                    }
                }
            }
            if (String.isNotBlank(accId)) {
                slaInfo.Account_ID__c = accId;
            }
        }
        if (String.isNotBlank(visitor1)) {
            slaInfo.Visitor1__c = visitor1;
        }else{
            slaInfo.Visitor1__c = null; //日报页面清空的对应的报告书信息里的也要清空
        }
        if (String.isNotBlank(visitor2)) {
            slaInfo.Visitor2__c = visitor2;
        }else{
            slaInfo.Visitor2__c = null; 
        }
        if (String.isNotBlank(visitor3)) {
            slaInfo.Visitor3__c = visitor3;
        }else{
            slaInfo.Visitor3__c = null; 
        }
        if (String.isNotBlank(visitor4)) {
            slaInfo.Visitor4__c = visitor4;
        }else{
            slaInfo.Visitor4__c = null; 
        }
        if (String.isNotBlank(visitor5)) {
            slaInfo.Visitor5__c = visitor5;
        }else{
            slaInfo.Visitor5__c = null; 
        }
        // deloitte-zhj 20231115 PIPL还原
    }
    public void save(){
        try {
            if (String.isBlank(slaInfo.Distribution_Person__c) || String.isBlank(slaInfo.Distribution_Method__c)) {
                error = true;

                return;
            }
            if (slaInfo.Visitor1__c == null) {
                slaInfo.Visitor1__c.addError('请先选择访问人员');
                error = true;
                return;
            }

            if (String.isBlank(slaInfoId)) {
                insert slaInfo;
                this.slaInfoId = slaInfo.Id;
                error = false;
            }else{
                update slaInfo;
                error = false;
            }
            
        } catch (Exception e) {
            error = true;
            ApexPages.addMessages(e);
        }
    }
}