public with sharing class LexInvalidDelegationController {
    public LexInvalidDelegationController() {
        
    }
    @AuraEnabled
    public static InitData init(String recordId){
        InitData res=new InitData();
        try{

        	Task task=[SELECT Status,Id,QuoteIraiId__c,Answer__c FROM Task where id = :recordId];
        	res.Id=task.Id;
        	res.Status=task.Status;
        	res.QuoteIraiIdC=task.QuoteIraiId__c;
        	res.AnswerC=task.Answer__c;
        	return res;
        }catch(Exception e){
            System.debug(LoggingLevel.INFO,'LexInvalidDelegationController init Error : ' + e);
        }
        return res;
    }


    @AuraEnabled
    public static QuoteIrai__c getQuoteIraiC(String recordId){
        try{

        	QuoteIrai__c res=[select id,Lead__r.LatestQuotationEntrustment__c,Lead__r.QuotationDelegationStatus__c, Lead__c, Lead__r.no_need_date__c  from QuoteIrai__c where id = :recordId];
        	return res;
        }catch(Exception e){
            System.debug(LoggingLevel.INFO,'LexInvalidDelegationController init Error : ' + e);
        }
        return null;
    }


    
    @AuraEnabled
    public static UpdateResult updateTask(       
        String recordId,
        String Status
    ) {
        UpdateResult result = new UpdateResult();
        result.recordId = recordId;
        try{
            // 更新记录并获取结果
            if(recordId==null) return null;
            Task rac = new Task( id=recordId);

            if(String.isNotBlank(Status)){
                rac.Status=Status;
            }

            rac.Quotation_request_completed_time__c =System.now();
            if(rac.id==null)return null;
            update rac;
            result.success = true;
            result.errors = new List<String>();
            return result;
        }catch(Exception e){
            result.success = false;
            result.errors = new List<String>();
            if(e.getMessage().contains(':')){
                String eMessage =e.getMessage();
                Integer left = eMessage.indexOf(',')+1;
                Integer right= eMessage.lastIndexOf('。 ')+1;
                if(right>eMessage.length()||right<=left){
                    right=eMessage.length();
                }
                String mes=eMessage.substring(left,right);
                if (System.Test.isrunningTest()) {
                    return null;
                }
                result.errors.add(mes);
            }else{
                result.errors.add(e.getMessage());
            }
            System.debug(LoggingLevel.INFO,'updateTask update Error : ' + e);
        }
        return result;
    }
    @AuraEnabled
    public static UpdateResult updateLead(       
        String recordId,
        String LatestQuotationEntrustmentC,
        String d
    ) {
        UpdateResult result = new UpdateResult();
        result.recordId = recordId;
        try{
            // 更新记录并获取结果
            if(recordId==null) return null;
            Lead rac = new Lead( id=recordId);

            if(String.isNotBlank(LatestQuotationEntrustmentC)){
                rac.LatestQuotationEntrustment__c=LatestQuotationEntrustmentC;
            }
            if(d==null||d!='dds'){
                rac.no_need_date__c=System.today();
            }
            if(rac.id==null)return null;
            update rac;
            result.success = true;
            result.errors = new List<String>();
            return result;
        }catch(Exception e){
            result.success = false;
            result.errors = new List<String>();
            if(e.getMessage().contains(':')){
                String eMessage =e.getMessage();
                Integer left = eMessage.indexOf(',')+1;
                Integer right= eMessage.lastIndexOf('。 ')+1;
                if(right>eMessage.length()||right<=left){
                    right=eMessage.length();
                }
                String mes=eMessage.substring(left,right);
                if (System.Test.isrunningTest()) {
                    return null;
                }
                result.errors.add(mes);
            }else{
                result.errors.add(e.getMessage());
            }
            System.debug(LoggingLevel.INFO,'updateLead update Error : ' + e);
        }
        return result;
    }
    @AuraEnabled
    public static UpdateResult updateQuoteIraiC(       
        String recordId,
        String QuoteIraiStatusC
    ) {
        UpdateResult result = new UpdateResult();
        result.recordId = recordId;
        try{
            // 更新记录并获取结果
            if(recordId==null) return null;
            QuoteIrai__c rac = new QuoteIrai__c( id=recordId);

            if(String.isNotBlank(QuoteIraiStatusC)){
                rac.QuoteIrai_Status__c=QuoteIraiStatusC;
            }
            if(rac.id==null)return null;
            update rac;
            result.success = true;
            result.errors = new List<String>();
            return result;
        }catch(Exception e){
            result.success = false;
            result.errors = new List<String>();
            if(e.getMessage().contains(':')){
                String eMessage =e.getMessage();
                Integer left = eMessage.indexOf(',')+1;
                Integer right= eMessage.lastIndexOf('。 ')+1;
                if(right>eMessage.length()||right<=left){
                    right=eMessage.length();
                }
                String mes=eMessage.substring(left,right);
                if (System.Test.isrunningTest()) {
                    return null;
                }
                result.errors.add(mes);
            }else{
                result.errors.add(e.getMessage());
            }
            System.debug(LoggingLevel.INFO,'updateQuoteIraiC update Error : ' + e);
        }
        return result;
    }

    @AuraEnabled
    public static String getProfileId(){
        return UserInfo.getProfileId();
    }
    public class UpdateResult {
        @AuraEnabled public String recordId {get;set;}
        @AuraEnabled public Boolean success {get;set;}
        @AuraEnabled public List<String> errors {get;set;}
    }
    public class InitData{
    	@AuraEnabled
    	public String Status{get;set;}
    	@AuraEnabled
    	public String Id{get;set;}
    	@AuraEnabled
    	public String QuoteIraiIdC{get;set;}
    	@AuraEnabled
    	public String AnswerC{get;set;}
    }
}