/**
 * author:zhangzhengmei
 * time:2023/4/19
 */

public with sharing class LexPaymentPlanLoadController {
    public LexPaymentPlanLoadController() {
        
    }
    @AuraEnabled
    public static InitData initData(String recordId) {
        InitData res = new InitData();
        try {
            res.statusSave = 'Fin';
            res.testDateTime = system.now();
            List<Maintenance_Contract__c> PageMCE = [select
                                                id,
                                                Name,
                                                Service_Contract_Staff__c,
                                                Payment_Plan_Sum_First__c,
                                                Payment_Plan_Date_First__c,
                                                Payment_Plan_Sum_Fifth__c,
                                                Payment_Plan_Date_Fifth__c,
                                                Payment_Plan_Sum_Third__c,
                                                Payment_Plan_Date_Third__c,
                                                Payment_Plan_Sum_Forth__c,
                                                Payment_Plan_Date_Forth__c,
                                                Payment_Plan_Sum_Second__c,
                                                Payment_Plan_Date_Second__c,
                                                Payment_Plan_Sum_Sixth__c,
                                                Payment_Plan_Date_Sixth__c,
                                                MainLeaderManager__c,
                                                Service_Contract_Staff__r.JingliApprovalManager__c,
                                                User_Salesdepartment__c,
                                                User_Salesdepartment_Sec__c,
                                                Service_Contract_Staff__r.Salesdepartment__c,
                                                Is_Standard_Contract__c,
                                                Small_Repair_Time__c,
                                                Big_Repair_Time__c,
                                                Live_Technical_Support_Time__c,
                                                Engineer_Name__c,
                                                Phone_Number__c,
                                                Tour_Check__c,
                                                Inspection_Time__c,
                                                Dedust__c,
                                                // fxk
                                                Contract_End_Date__c,
                                                Contract_Start_Date__c,
                                                Contract_Range__c,
                                                Contract_Limit_Times__c,
                                                Service_contract_target_number__c,
                                                // fxk
                                                Negotiate_Mean__c,
                                                First_Party_Contract_Number__c,
                                                Second_Party_Contract_Number__c,
                                                Term_Of_Payment__c,
                                                one_off_payment_agreed_period__c,
                                                Contract_Amount__c,
                                                Total_Contract_Amount_In_Words__c,
                                                Service_Contract_Staff__r.Name,
                                                Total_Payment_Time__c,
                                                Payment_Plan_Sum_First_in_Word__c,
                                                Payment_Plan_Sum_Second_in_Word__c,
                                                Payment_Plan_Sum_Third_in_Word__c,
                                                Payment_Plan_Sum_Forth_in_Word__c,
                                                Payment_Plan_Sum_Fifth_in_Word__c,
                                                Payment_Plan_Sum_Sixth_in_Word__c,
                                                Estimation_Id__c,
                                                Estimation_Id__r.Estimate_Target__c,
                                                Estimate_Target__c,
                                                Status__c,
                                                Decided_Estimation__c
                                                from
                                                Maintenance_Contract__c
                                                where
                                                id = :recordId];
        //权限管理-Denid
        if (PageMCE.size() == 1) {
            //if(PageMCE[0].Service_Contract_Staff__c != UserInfo.getUserId() && UserInfo.getProfileId() != System.Label.ProfileId_SystemAdmin && UserInfo.getProfileId() != System.Label.ProfileId_IThelp){
            //  ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, '没有付款计划的编辑权限'));
            //  PageMCEElement = new Maintenance_Contract__c();
            //}else{
            res.PageMCEElement = PageMCE[0];
            if (res.PageMCEElement.Small_Repair_Time__c == null) {
                res.PageMCEElement.Small_Repair_Time__c = '48';

            }
            if (res.PageMCEElement.Big_Repair_Time__c == null) {
                res.PageMCEElement.Big_Repair_Time__c = '15';

            }
            if (res.PageMCEElement.Live_Technical_Support_Time__c == null) {
                res.PageMCEElement.Live_Technical_Support_Time__c = '48';

            }
            if (res.PageMCEElement.Engineer_Name__c == null && res.PageMCEElement.Service_Contract_Staff__r.Name != null ) {
                res.PageMCEElement.Engineer_Name__c = res.PageMCEElement.Service_Contract_Staff__r.Name.replaceAll(' ', '');
            }
            if (res.PageMCEElement.First_Party_Contract_Number__c == null) {
                res.PageMCEElement.First_Party_Contract_Number__c = '2';
            }
            if (res.PageMCEElement.Second_Party_Contract_Number__c == null) {
                res.PageMCEElement.Second_Party_Contract_Number__c = '1';
            }

            //}

        } else {
            res.msg = '找不到对应的维修合同';
            // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, '找不到对应的维修合同'));
            res.statusSave = 'Denied';
        }
        //fxk 新加点检总次数 2021.9.2 Star
        if (res.PageMCEElement.Contract_End_Date__c != null || res.PageMCEElement.Contract_Start_Date__c != null) {
            res.Contract_validMonth = Integer.valueOf(res.PageMCEElement.Contract_Range__c);
        }
        //fxk 新加点检总次数 2021.9.2 End
        }catch (System.Exception e) {
            res.msg = e.getMessage();
        }
       
        return res;
    }


    public class InitData{
       
        @AuraEnabled
        public Maintenance_Contract__c PageMCEElement ;
        @AuraEnabled
        public Datetime testDateTime;
        @AuraEnabled
        public String statusSave ;
        @AuraEnabled
        public Boolean printContract ;
        @AuraEnabled
        public Boolean printTripartite ;
        @AuraEnabled
        public Boolean printAgent ;

        @AuraEnabled
        public Decimal Contract_validMonth;

        @AuraEnabled
        public String msg;
    }


    @AuraEnabled
    public static String save(Maintenance_Contract__c PageMCEElement) {
        /*验证是否输入付款计划
        *多次付款至少输入付款计划1和付款计划1金额
        *一次性付款需要输入一次性付款约定期限
        */
        InitData saveData = new InitData();
        saveData.PageMCEElement = PageMCEElement;
        if (PageMCEElement.Is_Standard_Contract__c == null  ) {
            saveData.statusSave = 'Denied';
            return '请选择是否标准合同！';
        }
        if (PageMCEElement.Term_Of_Payment__c != null &&
                '多次付款'.equals(PageMCEElement.Term_Of_Payment__c)) {
            if (PageMCEElement.Payment_Plan_Sum_First__c == null || PageMCEElement.Payment_Plan_Date_First__c == null || PageMCEElement.Payment_Plan_Sum_First__c == 0) {
                saveData.statusSave = 'Denied';
                return '请输入至少一个付款计划！';
            }

        } else if (PageMCEElement.Term_Of_Payment__c != null
                   && '一次性付款'.equals(PageMCEElement.Term_Of_Payment__c)
                   && PageMCEElement.one_off_payment_agreed_period__c == null ) {
            saveData.statusSave = 'Denied';
            return '请输入付款计划！';
        }
        // fxk 2021/8/31 点检次数必须填 Star
        // saveErrorflag = false;
        if (PageMCEElement.Is_Standard_Contract__c == '是') {
            if (PageMCEElement.Inspection_Time__c == null || ''.equals(PageMCEElement.Inspection_Time__c) || '0'.equals(PageMCEElement.Inspection_Time__c)) {
                System.debug('zzm' + PageMCEElement.Inspection_Time__c);
                // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,
                // '非标合同不需要打印合同文本！'));
                PageMCEElement.Inspection_Time__c.adderror('点检次数必选项');
                saveData.statusSave = 'Denied';
                return '点检次数必选项';
            }
            if (PageMCEElement.Contract_End_Date__c == null || PageMCEElement.Contract_Start_Date__c == null) {
                saveData.statusSave = 'Denied';
                return '请审批报价并decide';
            }
        }

        // fxk 2021/8/31 点检次数必须填 end

        // fxk 为维修合同点检总次数赋值 star
        if (PageMCEElement.Inspection_Time__c != null) {
            if (PageMCEElement.Contract_End_Date__c != null || PageMCEElement.Contract_Start_Date__c != null) {

                System.debug('PageMCEElement.Contract_End_Date__c.year():' + PageMCEElement.Contract_End_Date__c.year());
                
                Integer Contract_validMonth = Integer.valueOf(PageMCEElement.Contract_Range__c);
            
                if((Integer.valueOf(PageMCEElement.Inspection_Time__c) == 1 && Contract_validMonth < 9) 
                    || (Integer.valueOf(PageMCEElement.Inspection_Time__c) == 2 && Contract_validMonth < 6) 
                    || (Integer.valueOf(PageMCEElement.Inspection_Time__c) == 3 && Contract_validMonth < 4)
                    || (Integer.valueOf(PageMCEElement.Inspection_Time__c) == 4 && Contract_validMonth < 3)){
                    PageMCEElement.Service_contract_target_number__c = 1;
                }else{
                    Integer All_check;
                    if (Contract_validMonth - Integer.valueOf(Contract_validMonth / 12) * 12 >= 6
                            && Integer.valueOf(PageMCEElement.Inspection_Time__c) == 1) {
                        All_check = (Contract_validMonth * Integer.valueOf(PageMCEElement.Inspection_Time__c)) / 12 + 1;
                    } else {
                        All_check = (Contract_validMonth * Integer.valueOf(PageMCEElement.Inspection_Time__c)) / 12;
                    }
                    PageMCEElement.Service_contract_target_number__c = All_check;
                }
            }
        }
        // fxk 为维修合同点检总次数赋值 end

        Savepoint sp = Database.setSavepoint();
        try {
            if (PageMCEElement.Service_Contract_Staff__r.Salesdepartment__c == '1.华北') {
                PageMCEElement.User_Salesdepartment__c = System.Label.Payment_Block_Leader_HB;
                PageMCEElement.User_Salesdepartment_Sec__c = System.Label.Payment_Block_Leader_SD;
            } else if (PageMCEElement.Service_Contract_Staff__r.Salesdepartment__c == '2.东北') {
                PageMCEElement.User_Salesdepartment__c = System.Label.Payment_Block_Leader_DB;
            } else if (PageMCEElement.Service_Contract_Staff__r.Salesdepartment__c == '3.西北') {
                PageMCEElement.User_Salesdepartment__c = System.Label.Payment_Block_Leader_XB;
            } else if (PageMCEElement.Service_Contract_Staff__r.Salesdepartment__c == '4.西南') {
                PageMCEElement.User_Salesdepartment__c = System.Label.Payment_Block_Leader_XN;
            } else if (PageMCEElement.Service_Contract_Staff__r.Salesdepartment__c == '5.华东') {
                PageMCEElement.User_Salesdepartment__c = System.Label.Payment_Block_Leader_HD;
            } else if (PageMCEElement.Service_Contract_Staff__r.Salesdepartment__c == '6.华南') {
                PageMCEElement.User_Salesdepartment__c = System.Label.Payment_Block_Leader_HN;
                PageMCEElement.User_Salesdepartment_Sec__c = System.Label.Payment_Block_Leader_HNVice;
            } else if (PageMCEElement.Service_Contract_Staff__r.Salesdepartment__c == '7.能量') {

            }
            // 对于标准合同进行验证必填项
            string requiredInput = '';
            if (PageMCEElement.Is_Standard_Contract__c.equals('是')) {
                if (PageMCEElement.Engineer_Name__c == null || ''.equals(PageMCEElement.Engineer_Name__c)) {
                    requiredInput += '工程师姓名';
                }
                if (PageMCEElement.Phone_Number__c == null || ''.equals(PageMCEElement.Phone_Number__c)) {
                    if (!''.equals(requiredInput)) {
                        requiredInput += '、';
                    }
                    requiredInput += '手机号码';
                }
                if (PageMCEElement.Small_Repair_Time__c == null || ''.equals(PageMCEElement.Small_Repair_Time__c)) {
                    if (!''.equals(requiredInput)) {
                        requiredInput += '、';
                    }
                    requiredInput += '小修理时限';
                }
                if (PageMCEElement.Big_Repair_Time__c == null || ''.equals(PageMCEElement.Big_Repair_Time__c)) {
                    if (!''.equals(requiredInput)) {
                        requiredInput += '、';
                    }
                    requiredInput += '大修理时限';
                }
                if (PageMCEElement.Live_Technical_Support_Time__c == null || ''.equals(PageMCEElement.Live_Technical_Support_Time__c)) {
                    if (!''.equals(requiredInput)) {
                        requiredInput += '、';
                    }
                    requiredInput += '现场技术支持时限';
                }
                if (PageMCEElement.Tour_Check__c == null || ''.equals(PageMCEElement.Tour_Check__c)) {
                    if (!''.equals(requiredInput)) {
                        requiredInput += '、';
                    }
                    requiredInput += '巡回';
                }
                if (PageMCEElement.Inspection_Time__c == null || ''.equals(PageMCEElement.Inspection_Time__c)) {
                    if (!''.equals(requiredInput)) {
                        requiredInput += '、';
                    }
                    requiredInput += '点检';
                }
                if (PageMCEElement.Dedust__c == null || ''.equals(PageMCEElement.Dedust__c)) {
                    if (!''.equals(requiredInput)) {
                        requiredInput += '、';
                    }
                    requiredInput += '除尘';
                }

                if (PageMCEElement.First_Party_Contract_Number__c == null || ''.equals(PageMCEElement.First_Party_Contract_Number__c)) {
                    if (!''.equals(requiredInput)) {
                        requiredInput += '、';
                    }
                    requiredInput += '合同份数：甲方:';
                }
                if (PageMCEElement.Second_Party_Contract_Number__c == null || ''.equals(PageMCEElement.Second_Party_Contract_Number__c)) {
                    if (!''.equals(requiredInput)) {
                        requiredInput += '、';
                    }
                    requiredInput += '合同份数：乙方:';
                }
                if (!''.equals(requiredInput)) {
                    saveData.statusSave = 'Denied';
                    return '该合同是标准合同，请输入：' + requiredInput;
                }
            } else {
                if (PageMCEElement.Term_Of_Payment__c != null &&
                        '多次付款'.equals(PageMCEElement.Term_Of_Payment__c)) {
                    decimal PriceSum =  (PageMCEElement.Payment_Plan_Sum_First__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_First__c) +
                                        (PageMCEElement.Payment_Plan_Sum_Second__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Second__c) +
                                        (PageMCEElement.Payment_Plan_Sum_Third__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Third__c) +
                                        (PageMCEElement.Payment_Plan_Sum_Forth__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Forth__c) +
                                        (PageMCEElement.Payment_Plan_Sum_Fifth__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Fifth__c) +
                                        (PageMCEElement.Payment_Plan_Sum_Sixth__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Sixth__c);
                    if (PriceSum != PageMCEElement.Contract_Amount__c ) {
                        
                        saveData.statusSave = 'Denied';
                        return '付款总金额与合同金额不符。';
                    }
                }
            }

            if (PageMCEElement.Term_Of_Payment__c != null
                    && '一次性付款'.equals(PageMCEElement.Term_Of_Payment__c)) {
                PageMCEElement.Payment_Plan_Sum_First__c    = PageMCEElement.Contract_Amount__c;
                PageMCEElement.Payment_Plan_Sum_Second__c   = null;
                PageMCEElement.Payment_Plan_Sum_Third__c    = null;
                PageMCEElement.Payment_Plan_Sum_Forth__c    = null;
                PageMCEElement.Payment_Plan_Sum_Fifth__c    = null;
                PageMCEElement.Payment_Plan_Sum_Sixth__c    = null;

                PageMCEElement.Payment_Plan_Date_First__c   = null;
                PageMCEElement.Payment_Plan_Date_Second__c  = null;
                PageMCEElement.Payment_Plan_Date_Third__c   = null;
                PageMCEElement.Payment_Plan_Date_Forth__c   = null;
                PageMCEElement.Payment_Plan_Date_Fifth__c   = null;
                PageMCEElement.Payment_Plan_Date_Sixth__c   = null;
                PageMCEElement.Total_Payment_Time__c        = null;
            } else {
                PageMCEElement.one_off_payment_agreed_period__c        = null;

            }
            if (PageMCEElement.Is_Standard_Contract__c != null && PageMCEElement.Is_Standard_Contract__c.equals('否')) {
                PageMCEElement.Engineer_Name__c = null;
                PageMCEElement.Phone_Number__c = null;
                PageMCEElement.Small_Repair_Time__c = null;
                PageMCEElement.Big_Repair_Time__c = null;
                PageMCEElement.Live_Technical_Support_Time__c = null;
                PageMCEElement.Tour_Check__c = null;
                PageMCEElement.Inspection_Time__c = null;
                PageMCEElement.Dedust__c = null;
                PageMCEElement.First_Party_Contract_Number__c = null;
                PageMCEElement.Second_Party_Contract_Number__c = null;

            }

            PageMCEElement.MainLeaderManager__c = PageMCEElement.Service_Contract_Staff__r.JingliApprovalManager__c;
            update PageMCEElement;
            saveData.statusSave = 'Fin';
            return '1';
        } catch (exception ex) {
            saveData.statusSave = 'Denied';
            Database.rollback(sp);
            return ex.getMessage();
        }
    }




    @AuraEnabled
    public static InitData print(Maintenance_Contract__c PageMCEElement) {
        System.debug('print start');
        InitData printResult = new InitData();
        String saveRes = save(PageMCEElement);
        if(saveRes == '1'){
            printResult.statusSave = 'Fin';
        }

        if (PageMCEElement.Decided_Estimation__c == null) {
            printResult.statusSave = 'Denied';
            printResult.msg = '报价单没有决定之前不能打印合同文本!';
            return printResult;
        }

        if (PageMCEElement.Estimation_Id__r.Estimate_Target__c == null) {

            printResult.statusSave = 'Denied';
            printResult.msg = '请确定提交对象!';
            return printResult;
        } 


        if (PageMCEElement.Is_Standard_Contract__c != null && PageMCEElement.Is_Standard_Contract__c.equals('否')) {
            
            printResult.statusSave = 'Denied';
            printResult.msg = '非标合同不需要打印合同文本!';
            return printResult;
        }

        if (PageMCEElement.Term_Of_Payment__c != null &&
                '多次付款'.equals(PageMCEElement.Term_Of_Payment__c)) {
            decimal PriceSum =  (PageMCEElement.Payment_Plan_Sum_First__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_First__c) +
                                (PageMCEElement.Payment_Plan_Sum_Second__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Second__c) +
                                (PageMCEElement.Payment_Plan_Sum_Third__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Third__c) +
                                (PageMCEElement.Payment_Plan_Sum_Forth__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Forth__c) +
                                (PageMCEElement.Payment_Plan_Sum_Fifth__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Fifth__c) +
                                (PageMCEElement.Payment_Plan_Sum_Sixth__c == null ? 0 : PageMCEElement.Payment_Plan_Sum_Sixth__c);
            if (PriceSum != PageMCEElement.Contract_Amount__c ) {
                printResult.statusSave = 'Denied';
                printResult.msg = '付款总金额与合同金额不符。';
                return printResult;
            }
        }

        if (printResult.statusSave.equals('Fin') && PageMCEElement.Estimation_Id__r.Estimate_Target__c.equals('医院')) {
            printResult.printContract = true;
            printResult.printAgent = false;
        } else if (printResult.statusSave.equals('Fin')) {
            printResult.printContract = false;
            printResult.printAgent = true;
        }
        return printResult;
    }
}