public with sharing class RentalApplyControllerLWT {
    public RentalApplyControllerLWT() {
        
    }
    @AuraEnabled
    public static Rental_Apply__c initFromCancelSubmitButton(String recordId){
        InitData res = new InitData();
        try{
            Rental_Apply__c report=[select 
             Status__c,Yi_loaner_arranged__c,Id,RA_Status__c,
            demo_purpose2__c,Follow_UP_Opp__c,Statu_Achievements__c,Statu_Achievements_ID__c,
            Request_shipping_day__c,Demo_purpose1__c,Repair__c,RecordTypeId,SupplementCreated__c,
            OPDPlan__c,Campaign__c,QIS_number__c,OwnerId,
            // QIS_numberId__c,CampaignId__c,applyUserId__c，
            Re_repair__c,
            QIS_ID_Line__c,
            applyUser__c
             from Rental_Apply__c where Id= :recordId];
            return report;
            // res.StatusC=report.Status__c;
            // res.Id=report.Id;
            // res.YiLoanerArrangedC=report.Yi_loaner_arranged__c;
            // res.RAStatusC=report.RA_Status__c;
            // res.DemoPurpose2C=report.demo_purpose2__c;
            // res.FollowUPOppC=report.Follow_UP_Opp__c;
            // res.StatuAchievementsC=report.Statu_Achievements__c;
            // res.StatuAchievementsIDC=report.Statu_Achievements_ID__c;
            // res.RequestShippingDayC=report.Request_shipping_day__c;
            // res.DemoPurpose1C=report.Demo_purpose1__c;
            // res.RepairC=report.Repair__c;
            // res.RecordTypeId=report.RecordTypeId;
            // res.SupplementCreatedC=report.SupplementCreated__c;
            // res.OPDPlanC=report.OPDPlan__c;
            // res.CampaignC=report.Campaign__c;
            // res.QISNumberC=report.QIS_number__c;
            // res.QISNumberIdc=report.QIS_numberId__c;
        }catch(Exception e){
            System.debug(LoggingLevel.INFO,'Rental_Apply__c Cancel Error : ' + e);
        }
        return null;
    }


    @AuraEnabled
    public static list<Rental_Apply_Equipment_Set_Detail__c> selectRentalApplyEquipmentSetDetailByRacId(String recordId){
        InitData res = new InitData();
        try{
            list<Rental_Apply_Equipment_Set_Detail__c> report=[select 
            Id, Fixture_Model_No_F__c, Product_Status_Flag_F__c 
            from Rental_Apply_Equipment_Set_Detail__c where Rental_Apply__c = :recordId]; 
            return report;
        }catch(Exception e){
            System.debug(LoggingLevel.INFO,'Rental_Apply__c Cancel Error : ' + e);
        }
        return null;
    }

    @AuraEnabled
    public static list<QIS_Report__c> selectQISReportById(String recordId){
        InitData res = new InitData();
        try{
            list<QIS_Report__c> report=[select 
            Id, nonyushohin__r.Product2.Fixture_Model_No_T__c 
            from QIS_Report__c 
            where id = :recordId
            ]; 
            return report;
        }catch(Exception e){
            System.debug(LoggingLevel.INFO,'Rental_Apply__c Cancel Error : ' + e);
        }
        return null;
    }
    
    @AuraEnabled
    public static list<Repair__c> selectRepairById(String recordId){
        InitData res = new InitData();
        try{
            list<Repair__c> report=[select Id, Repair_Rank__c, DW_Sign_Txt__c, FSE_ApplyForRepair_Day__c, Delivered_Product__r.Product2.Fixture_Model_No_T__c
                 , ReRepairObject_F__c,Status1__c,Agreed_Date__c,Repair_Estimated_date_formula__c,Repair_Ordered_Date__c
                 , IfCheckFixture__c
                 , Repair_Final_Inspection_Date__c,Repair_Shipped_Date__c,Number_of_EffectiveContract__c, NewProductGuaranteeObject__c, Delivered_Product__r.Product2.Asset_Model_No__c
                 from Repair__c where id = :recordId
            ]; 
            return report;
        }catch(Exception e){
            System.debug(LoggingLevel.INFO,'Rental_Apply__c Cancel Error : ' + e);
        }
        return null;
    }

    @AuraEnabled
    public static list<Campaign> selectCampaignById(String recordId){
        InitData res = new InitData();
        try{
            list<Campaign> report=[select 
            Status, Rental_Apply_Flag__c ,IF_Approved__c,Meeting_Approved_No__c,Approved_Status__c 
            from Campaign where id =:recordId

            ]; 
            return report;
        }catch(Exception e){
            System.debug(LoggingLevel.INFO,'Rental_Apply__c Cancel Error : ' + e);
        }
        return null;
    }
    @AuraEnabled
    public static list<Rental_Apply_Equipment_Set__c> selectRentalApplyEquipmentSetByRacId(String recordId){
        InitData res = new InitData();
        try{
            list<Rental_Apply_Equipment_Set__c> report=[select Id 
                from Rental_Apply_Equipment_Set__c 
                where RetalFSetDetail_Cnt__c = 0 AND Rental_Apply__c = :recordId
            ]; 
            return report;
        }catch(Exception e){
            System.debug(LoggingLevel.INFO,'Rental_Apply__c Cancel Error : ' + e);
        }
        return null;
    }

    @AuraEnabled
    public static list<Rental_Apply__c> selectRentalApplyById(String recordId){
        InitData res = new InitData();
        try{
            list<Rental_Apply__c> report=[select 
            id,OPDPlan__c,OPDPlan__r.SalesManager_Txt__c,OPDPlan__r.BuchangApprovalManagerSales_Txt__c 
            from Rental_Apply__c where id = :recordId

            ]; 
            return report;
        }catch(Exception e){
            System.debug(LoggingLevel.INFO,'Rental_Apply__c Cancel Error : ' + e);
        }
        return null;
    }

    @AuraEnabled
    public static list<User> selectUserById(String recordId){
        InitData res = new InitData();
        try{
            list<User> report=[select 
            id,JingliEquipmentManager__c,JingliEquipmentManager__r.Name,Buzhang_Equipment_Manager__c,
            Buzhang_Equipment_Manager__r.Name 
            from User where id = :recordId
            ]; 
            return report;
        }catch(Exception e){
            System.debug(LoggingLevel.INFO,'Rental_Apply__c Cancel Error : ' + e);
        }
        return null;
    }

    @AuraEnabled
    public static list<QIS_report__c> selectQISreportById2(String recordId){
        InitData res = new InitData();
        try{
            list<QIS_report__c> report=[select id,next_action__c 
            from QIS_report__c where id =:recordId
            ]; 
            return report;
        }catch(Exception e){
            System.debug(LoggingLevel.INFO,'Rental_Apply__c Cancel Error : ' + e);
        }
        return null;
    }

         
    @AuraEnabled
    public static String getUserId(){
        
        return UserInfo.getUserId();
    }

    @AuraEnabled
    public static String getProfileId(){
        return UserInfo.getProfileId();
    }   

    @AuraEnabled
    public static String setSObjectShare(String sobjectName, String rowCause, String parentId, List<String> userAccess, String ownerId) {

        try {
            List<SObject> sObjList = new List<SObject>();
            for (String ua : userAccess) {
                String userid = ua.split('_')[0];
                String access = ua.split('_')[1];
                SObject sObj = Schema.getGlobalDescribe().get(sobjectName).newSObject();
                if (String.isBlank(userid) == false && userid.substring(0, 15) != ownerId.substring(0, 15)) {
                    sObj.put('RowCause', rowCause);
                    sObj.put('ParentId', parentId);
                    sObj.put('UserOrGroupId', userid);
                    sObj.put('AccessLevel', access);
                    sObjList.add(sObj);
                }
            }
            if (sObjList.size() > 0) insert sObjList;
            return 'OK';
        } catch (Exception e) {
            return e.getMessage();
        }   
    }

    @AuraEnabled
    public static UpdateResult updateRentalApplyC(        
        String recordId,
        String SalesManagerSubmitC,
        String StatusC,
        String OPDManagerApproverC,
        String BuchangApprovalManagerSalesSubmitC,
        String OPDBuchangApproverC
    ) {
        UpdateResult result = new UpdateResult();
        result.recordId = recordId;
        try{
            // 更新记录并获取结果
            if(recordId==null) return null;
            Rental_Apply__c rac = new Rental_Apply__c( id=recordId);

            if(SalesManagerSubmitC!=null&& SalesManagerSubmitC != ''){
                rac.SalesManagerSubmit__c=SalesManagerSubmitC;
            }
            if(StatusC!=null&&StatusC!=''){
                rac.Status__c=StatusC;
            }
            if(OPDManagerApproverC!=null&&OPDManagerApproverC!=''){
                rac.OPDManagerApprover__c=OPDManagerApproverC;
            }
            if(BuchangApprovalManagerSalesSubmitC!=null&&BuchangApprovalManagerSalesSubmitC!=''){
                rac.BuchangApprovalManagerSalesSubmit__c=BuchangApprovalManagerSalesSubmitC;
            }
            if(OPDBuchangApproverC!=null&&OPDBuchangApproverC!=''){
                rac.OPDBuchangApprover__c=OPDBuchangApproverC;
            }
            if(rac.id==null)return null;
            update rac;
            result.success = true;
            result.errors = new List<String>();
            // 定义字符串列表，其中包含若干 sObject 的 API 名称
            //List<String> objTypeList = new List<String>{'Account', 'ExampleCustomObj__c'};
            // 得到 Account 和 ExampleCustomObj__c 的信息
            //Schema.DescribeSobjectResult[] results = Schema.describeSObjects(objTypeList);
            return result;
        }catch(Exception e){
            result.success = false;
            result.errors = new List<String>();
            result.errors.add(e.getMessage());
            System.debug(LoggingLevel.INFO,'Rental_Apply__c update Error : ' + e);
        }
        return result;
    }

    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 StatusC;
        @AuraEnabled
        public Decimal YiLoanerArrangedC;
        @AuraEnabled
        public String Id;
        @AuraEnabled
        public String RAStatusC;

        @AuraEnabled
        public String DemoPurpose2C;
        @AuraEnabled
        public String FollowUPOppC;
        @AuraEnabled
        public String StatuAchievementsC;
        @AuraEnabled
        public String StatuAchievementsIDC;
        @AuraEnabled
        public Date RequestShippingDayC;
        @AuraEnabled
        public String DemoPurpose1C;
        @AuraEnabled
        public String RepairC;
        @AuraEnabled
        public String RecordTypeId;
        @AuraEnabled
        public Boolean SupplementCreatedC;
        @AuraEnabled
        public String OPDPlanC;
        @AuraEnabled
        public String CampaignC;
        @AuraEnabled
        public String QISNumberC;
        @AuraEnabled
        public String QISNumberIdc;
    }
}