/**************************************************************************************************
 * Name: ApplyJsCtl
 * Purpose: excute the java script of rental apply 
 * Author: Prec-tech team
 * Create Date: 2021-11-17
 * Modify History:
 * 2021-11-27       Create this class
 **************************************************************************************************/
global with sharing class ApplyJsCtl{
    @AuraEnabled(cacheable=false)
    webService static String queueTop(String applyId) {
        try {
            System.debug(LoggingLevel.INFO, '*** applyId: ' + applyId);
            RentalFixtureSetAssignController.applysetMap = new Map<String,String>();
            List<Rental_Apply_Equipment_Set_Detail__c> raesdObjs = [SELECT Id,Rental_Apply_Equipment_Set__r.Name,  Rental_Apply_Equipment_Set__c, Asset__c,Rental_Apply_Equipment_Set__r.Fixture_Set__r.Product_Type__c,
                                                                   FSD_Fixture_Model_No__c, Fixture_Model_No_text__c,Rental_Apply__r.demo_purpose2__c,Queue_Day__c,ExternalKey__c,
                                                                   Is_Body__c, FSD_Is_OneToOne__c, Select_Time__c,Rental_Apply__r.EquipmentGuaranteeFlg__c,Allow_Adjust_Queue_Flag__c,
                                                                   Cancel_Select__c, Fixture_Model_No_F__c, Queue_Number__c, Internal_asset_location__c,Fixture_Model_No__c,
                                                                   Internal_asset_location_before__c,Salesdepartment__c, Product_category_F__c, Product_category_text__c,Equipment_Type_text__c,
                                                                   Salesdepartment_before__c,
                                                                   Equipment_Type__c, Rental_Apply__r.Internal_asset_location_F__c,Cancel_Reason__c,Loaner_cancel_reason__c,Loaner_cancel_Remarks__c 
                                                                    FROM Rental_Apply_Equipment_Set_Detail__c 
                                                                    WHERE Rental_Apply__c =:applyId
                                                                    AND Queue_Number__c > 0
                                                                    AND Cancel_Select__c = false
                                                                    AND Is_Body__c = true];
            System.debug(LoggingLevel.INFO, '*** raesdObjs: ' + raesdObjs);
            for(Rental_Apply_Equipment_Set_Detail__c detail:raesdObjs){
                RentalFixtureSetAssignController.applysetMap.put(detail.Rental_Apply_Equipment_Set__c,detail.Rental_Apply_Equipment_Set__r.Name);
            }
            RentalFixtureSetAssignController.saveAgdustQueue(raesdObjs,'backEnd');
        }
        catch (Exception e) {
            System.debug(LoggingLevel.INFO, '*** e.getMessage()+e.getLineNumber(): ' + e.getMessage()+e.getLineNumber());
            return e.getMessage();
        }
        
        return 'Success';
    }
}