public with sharing class lexLostCancelReportController {
    @AuraEnabled
    public static InitData initSubmitReportButton(String recordId){
        InitData res = new InitData();
        try {
            Lost_Cancel_Report__c report = [
                select
                Opportunity__c
                from Lost_Cancel_Report__c where Id =: recordId
            ];
            Opportunity opp = [
                select
                Cnt_Lost_cancel_report__c,
                Cnt_Lost_cancel_Draft__c
                from Opportunity where Id =: report.Opportunity__c
            ];
            //生产环境bug修改 陈京武 20231105 Start
            RecordType recordType1 = [select Id from RecordType where Name = '询价取消报告'];
            List<Lost_cancel_report__c> repList = [select Id from Lost_cancel_report__c where Opportunity__c =: opp.Id and RecordTypeId !=: recordType1.Id and Report_Status__c != '草案'];
            if(repList.size() > 0){
                res.error = '已经有提交的失单报告，不可提交取消报告';
                return res;
            }
            //生产环境bug修改 陈京武 20231105 End
            res.cntLostCancelDraft = opp.Cnt_Lost_cancel_Draft__c;
            res.cntLostCancelReport = opp.Cnt_Lost_cancel_report__c;
        } catch (Exception e) {
        }
        return res;
    }
    @AuraEnabled
    public static InitData initForEditCancelReport(String recordId){
        InitData res = new InitData();
        try {
            Lost_cancel_report__c report = [
                select Id,
                RecordTypeId, 
                Opportunity__c, 
                LostType__c , 
                Report_Status__c from Lost_cancel_report__c  where id =: recordId
            ];
            res.recordTypeId = report.RecordTypeId;
            res.lostType = report.LostType__c;
            res.reportStatus = report.Report_Status__c;
        } catch (Exception e) {
        }
        return res;
    }
    @AuraEnabled
    public static string updateSubmitReportButton(String recordId){
        Savepoint sp = Database.setSavepoint();
        Lost_cancel_report__c report = new Lost_cancel_report__c();
        try { 
            report.Id = recordId;
            report.Report_Status__c = '提交';
            update report;
            // Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();
            // psr.setObjectId(report.Id);
            // Approval.ProcessResult submitResult = Approval.process(psr);
            return null;
        } catch (Exception e) {
            Database.rollback(sp);
            if (e.getMessage().contains(',')) {
				System.debug(LoggingLevel.INFO, '*** e: ' + e);
				String exc = '' + e.getMessage();
				Integer left = exc.indexOf(':') + 1;
				Integer right = exc.lastIndexOf(':');
				String str = exc.substring(left,right);
				left = str.indexOf(',') +  1;
				String newStr = str.substring(left);
				return newStr;
			}else {
				return e.getMessage();   
			}
        }
    }
    public class InitData {
        @AuraEnabled
        public Decimal cntLostCancelReport;
        @AuraEnabled
        public Decimal cntLostCancelDraft;
        @AuraEnabled
        public String recordTypeId;
        @AuraEnabled
        public String lostType;
        @AuraEnabled
        public String reportStatus;
        //生产环境bug修改 陈京武 20231105 Start
        @AuraEnabled
        public String error;
        //生产环境bug修改 陈京武 20231105 End
    }
}