From d446b7cfc668672f4e5542eeb2cfbb9191468efb Mon Sep 17 00:00:00 2001
From: Li Jun <buli@deloitte.com.cn>
Date: 星期二, 19 四月 2022 22:13:24 +0800
Subject: [PATCH] Rental Apply Record Type issue fix

---
 force-app/main/default/pages/NewRentalApply.page |  208 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 176 insertions(+), 32 deletions(-)

diff --git a/force-app/main/default/pages/NewRentalApply.page b/force-app/main/default/pages/NewRentalApply.page
index 8d3ce75..62eb087 100644
--- a/force-app/main/default/pages/NewRentalApply.page
+++ b/force-app/main/default/pages/NewRentalApply.page
@@ -1,10 +1,3 @@
-<!--
-  @description       : 
-  @author            : ChangeMeIn@UserSettingsUnder.SFDoc
-  @group             : 
-  @last modified on  : 03-23-2022
-  @last modified by  : ChangeMeIn@UserSettingsUnder.SFDoc
--->
 <apex:page standardController="Rental_Apply__c" extensions="RentalApplyController" id="page">
     <apex:stylesheet value="{!URLFOR($Resource.blockUIcss)}"/>
     <apex:includeScript value="{! URLFOR($Resource.AWSService, 'AWSService.js') }" />
@@ -24,7 +17,7 @@
         var requiredFieldAPIList = JSON.parse('{!requiredFieldAPIListStr}');
         var fieldAPIToLabelMap = JSON.parse('{!fieldAPIToLabelMapStr}');
         var VLookUpFields = new Set(['OwnerId','OPDPlan__c','Zsq_Rental_Apply__c','Loaner_medical_Staff__c','Hospital__c','Strategic_dept__c','Account__c','Repair__c','Campaign__c','QIS_number__c','QISRepair__c','NewRepair__c','Follow_UP_Opp__c','Statu_Achievements__c','Shipment_address__c','Dealer__c','Old_Rental_Apply__c','OPD__c']);
-        var userVLookUpFields = ['Person_In_Charge__c','applyUser__c','Rental_Assistant__c','Assign_Person__c','ZongjianApprovalManager__c','BuchangApprovalManager__c','JingliApprovalManager__c','BuchangApprovalManagerSales__c','SalesManager__c'];
+        var userVLookUpFields = ['Person_In_Charge__c','Return_Trake_Staff__c','applyUser__c','Rental_Assistant__c','Assign_Person__c','ZongjianApprovalManager__c','BuchangApprovalManager__c','JingliApprovalManager__c','BuchangApprovalManagerSales__c','SalesManager__c'];
         var redirectMode = 'Save';
 
         //Redirect Required Parameter
@@ -40,6 +33,8 @@
                 alertErrorMessage(errorMessage);
             }
         }
+        
+        
 
         //Query Required Parameter
         var queryBack = function queryBack(data) {
@@ -47,6 +42,7 @@
             document.querySelector("[data-id='Phone_number__c']").value = data.object.phoneNumber;
             document.querySelector("[data-id='direct_shippment_address__c']").value = data.object.directShippmentAddress;
             unblockUI();
+            enableButtonStatus();//Add by Li Jun 20220418
         };
 
         //Check If Insert Or Update
@@ -61,24 +57,78 @@
             payloadJson.Phone_Number_Encrypt__c = r.object[0].phoneNumberEncrypt;
             payloadJson.Direct_Shippment_Address_Encrypt__c = r.object[0].directShippmentAddressEncrypt;
             payloadJson.AWS_Data_Id__c = '{!AWSDataId}';
-            if (isNewMode) {
+            if('{!rtTypeId}'){
+                payloadJson.RecordTypeId = '{!rtTypeId}';//Add by Li Jun for Record Type Issue 20220419
+            }
+           
+            if (isNewMode || {!isCloneMode}) {
                 payloadJson.AWS_Data_Id__c = r.object[0].dataId;
+                delete payloadJson.OwnerId;
             } else {
                 payloadJson.AWS_Data_Id__c = '{!AWSDataId}';
             }
             debugger
             return payloadJson;
         }
+        //Add Button status start 20220418
+        function enableButton(obj) {
+            obj.classList.remove("btnDisabled");
+        }
 
+        function disableButton(obj) {
+            obj.classList.add("btnDisabled");
+        }
+
+        function disableButtonStatus() {
+            let btnList = document.getElementsByClassName('btn');
+            for (let i = 0; i < btnList.length; i++) {
+                disableButton(btnList[i]);
+            }
+        }
+
+        function enableButtonStatus() {
+            let btnList = document.getElementsByClassName('btn');
+            for (let i = 0; i < btnList.length; i++) {
+                enableButton(btnList[i]);
+            }
+        }
+        disableButtonStatus();
+        //Add Button status end 20220418 
         // New Or Edit
         function ProcessPI(rentalApplyJson, payloadForNewPI) {
             blockme();
-            if ({!isNewMode
-            }) {
+            if ({!isNewMode} || {!isCloneMode}) {
                 NewPIToAWS(rentalApplyJson, payloadForNewPI)
             }else {
                 UpdatePIToAWS(rentalApplyJson, payloadForNewPI)
             }
+        }
+
+        var GetEleByClass = function(class_name){
+            let eles = document.getElementsByClassName(class_name);
+            if(eles.length > 0) return eles[0];
+            return null;
+        }
+
+        function GetEleApiName(ele){
+            for(let ci in ele.classList){
+                let c = ele.classList[ci];
+                if(c.indexOf('PIBackApi_')>-1){
+                    return c.replace('PIBackApi_','');
+                }
+            }
+            return '';
+        }
+
+        function IsFormTag(tag_name){
+            if (!tag_name) {
+                return false;
+            }
+            return ['input','select','textarea'].indexOf(tag_name.toLowerCase())>-1;
+        }
+        
+        function IsFormElement(e){
+            return IsFormTag(e.tagName);
         }
 
         //Get Sensitive Information
@@ -101,21 +151,36 @@
             return true;
         }
 
+        function getCKEinstance(api_name){
+            let t = jQuery("label[for$='textAreaDelegate_"+api_name+"']").closest("tr").find("textarea")[0];
+            if(t && t.id){
+                return CKEDITOR.instances[t.id]
+                
+            }
+            return null;
+        }
+
         //Get Page Information
         function getRentalApplyInformation() {
             let nodelist = document.querySelectorAll("[data-id]");
             let result = {}
             //瀵屾枃鏈�
-            if(document.querySelector("[aria-describedby = 'cke_34']")){
-                result.HP_received_sign_rich__c = document.querySelector("[aria-describedby = 'cke_34']").contentWindow.document.getElementsByTagName('body')[0].innerHTML
-            }           
+            // if(document.querySelector("[aria-describedby = 'cke_38']")){
+            //     result.HP_received_sign_rich__c = document.querySelector("[aria-describedby = 'cke_38']").contentWindow.document.getElementsByTagName('body')[0].innerHTML
+            // }
+            // result.HP_received_sign_rich__c = CKEDITOR.instances["page:form:block:j_id34:12:j_id35:j_id36:1:j_id37:textAreaDelegate_HP_received_sign_rich__c"].getData();
+            // if(document.querySelector("[aria-describedby = 'cke_71']")){
+            //     result.  Extension_List_RentalApply__c = document.querySelector("[aria-describedby = 'cke_71']").contentWindow.document.getElementsByTagName('body')[0].innerHTML
+            // }
             for (let index = 0; index < nodelist.length; index++) {
                 if (VLookUpFields.has(nodelist[index].getAttribute("data-id"))) {
                     console.log(nodelist[index].id.indexOf('lkwgt'));
                     if (nodelist[index].id.indexOf('lkwgt') == -1) {
                         let vlookUpNodeId = nodelist[index].id + '_lkid';
-                        let vlookUpNodeValue = document.getElementById(vlookUpNodeId).value;
-                        result[nodelist[index].getAttribute("data-id")] = vlookUpNodeValue;
+                        if(document.getElementById(vlookUpNodeId)){
+                            let vlookUpNodeValue = document.getElementById(vlookUpNodeId).value;
+                            result[nodelist[index].getAttribute("data-id")] = vlookUpNodeValue;
+                        }
                     }
                 } else if (nodelist[index].type == 'checkbox') {
                     result[nodelist[index].getAttribute("data-id")] = nodelist[index].checked;
@@ -143,15 +208,42 @@
                     }
                 }             
             }
-            //page:form:j_id30:j_id34:2:j_id35:j_id36:2:j_id37
-            //page:form:j_id27:j_id31:2:j_id32:j_id33:2:j_id34
-            result.demo_purpose2__c = document.querySelector("[id='page:form:block:j_id34:2:j_id35:j_id36:2:j_id37']").value;
-            result.ToAgency__c = document.querySelector("[id='page:form:block:j_id34:0:j_id35:j_id36:14:j_id37']").value;
-            result.Loaner_cancel_reason__c = document.querySelector("[id='page:form:block:j_id34:12:j_id35:j_id36:1:j_id37']").value;
-            console.log(JSON.stringify(result));
-            if(result.HP_received_sign_rich__c == '<br>'){
-                result.HP_received_sign_rich__c = '';
+            if(api_id_map['demo_purpose2__c']){
+                result.demo_purpose2__c = document.getElementById(api_id_map['demo_purpose2__c']).value!='_\x01_'?document.getElementById(api_id_map['demo_purpose2__c']).value:'';
+            }            
+            if(api_id_map['ToAgency__c']){
+                result.ToAgency__c = document.getElementById(api_id_map['ToAgency__c']).value!='_\x01_'?document.getElementById(api_id_map['ToAgency__c']).value:'';
+            }            
+            //document.querySelector("[id='page:form:block:j_id34:0:j_id35:j_id36:18:j_id37']").disabled =true; // Commented By Li Jun 20220304
+            if(api_id_map['Loaner_cancel_reason__c']){
+                result.Loaner_cancel_reason__c = document.getElementById(api_id_map['Loaner_cancel_reason__c']).value!='_\x01_'?document.getElementById(api_id_map['Loaner_cancel_reason__c']).value:'';
             }
+            // else{
+            //     if(document.querySelector("[id='page:form:block:j_id34:11:j_id35:j_id36:1:j_id37']")!=null){
+            //         result.Loaner_cancel_reason__c = document.querySelector("[id='page:form:block:j_id34:11:j_id35:j_id36:1:j_id37']").value;
+            //     }               
+            // }
+            // //鏍煎紡鍖栨椂闂�
+            // if(result.HP_received_sign_day__c){
+            //     result.HP_received_sign_day__c = result.HP_received_sign_day__c.replace(/\//g, '-')+':00';
+            // }
+            let e = getCKEinstance('HP_received_sign_rich__c');
+            if(e){
+                    result.HP_received_sign_rich__c = e.getData();
+                }
+                e = getCKEinstance('Extension_List_RentalApply__c');
+            if(e){
+                    result.Extension_List_RentalApply__c = e.getData();
+                }
+            if(document.getElementById(api_id_map['Office_Assistant1__c'] + '_lkid')!=null){
+                // result.ToAgency__c = document.getElementById('page:form:block:j_id34:0:j_id35:j_id36:18:j_id37').value;
+                result.Office_Assistant1__c = document.getElementById(api_id_map['Office_Assistant1__c'] + '_lkid').value;
+            }
+            if(document.getElementById(api_id_map['Office_Assistant2__c'] + '_lkid')!=null){
+                // result.ToAgency__c = document.getElementById('page:form:block:j_id34:0:j_id35:j_id36:18:j_id37').value;
+                result.Office_Assistant2__c = document.getElementById(api_id_map['Office_Assistant2__c'] + '_lkid').value;
+            }
+            console.log(JSON.stringify(result));
             return result;
         }
 
@@ -256,8 +348,7 @@
             return template.content.firstChild;
         }
         function queryContactName() {
-            function queryContactName() {
-            let sfId = document.getElementById(document.querySelector("[data-id='Account__c']").id + '_lkid').value;
+            let sfId = document.getElementById(document.querySelector("[data-id='Loaner_medical_Staff__c']").id + '_lkid').value;
             let dataId = '';
             if ('{!contactsInfo}' != '') {
                 let contactsInfo = JSON.parse('{!contactsInfo}');
@@ -269,7 +360,6 @@
                 document.querySelector("[data-id='Loaner_medical_Staff__c']").value = result.object.lastName;
             };
             AWSService.query(staticResourcesContact.queryUrl, dataId, queryBackContactName, staticResourcesContact.token);
-        }
         }
         //鑷畾涔塴ookup鏌ヨ
         function searchContact(contactNodeId){
@@ -337,7 +427,7 @@
                                 <img src="/img/s.gif" alt="" class="minWidth" title="" width="1" height="1" />
                                 <h2 class="mainTitle">澶囧搧鍊熷嚭鐢宠缂栬緫</h2>
                             </td>
-                            <td class="pbButton" id="topButtonRow">
+                            <td class="pbButton" id="topButtonRow" style="pointer-events: none; opacity: 0.4;">
                                 <input class="btn" type="Button" value="淇濆瓨" onclick="saveRentalApplyProcess('Save')" />
                                 <input class="btn" type="Button" value="淇濆瓨骞舵柊寤�" onclick="saveRentalApplyProcess('SaveAndNew')" />
                                 <apex:commandButton action="{!cancel}" value="鍙栨秷" />
@@ -358,8 +448,12 @@
 
                     <!--Each section has layoutFields, let's iterate them as well-->
                     <apex:repeat value="{!layoutSection.layoutFields}" var="layoutField">
-                        <apex:inputField styleClass="PIBackApi PIBackApi_{!layoutField.fieldAPI}" html-data-id="{!layoutField.fieldAPI}" value="{!Rental_Apply__c[layoutField.fieldAPI]}" rendered="{!not(layoutField.isPlaceHOlder)}"
+                        <apex:inputField styleClass="PIBackApi PIBackApi_{!layoutField.fieldAPI}" html-data-id="{!layoutField.fieldAPI}" value="{!Rental_Apply__c[layoutField.fieldAPI]}" rendered="{!not(layoutField.isPlaceHOlder)&&isNewMode}"
                             required="{!layoutField.isRequired}" />
+                        <apex:inputField styleClass="PIBackApi PIBackApi_{!layoutField.fieldAPI}" html-data-id="{!layoutField.fieldAPI}" value="{!Rental_Apply__c[layoutField.fieldAPI]}" rendered="{!not(layoutField.isPlaceHOlder)&&not(isNewMode)&&layoutField.fieldAPI!='RecordTypeId'}"
+                            required="{!layoutField.isRequired}" />
+                        <apex:outputField styleClass="PIBackApi PIBackApi_{!layoutField.fieldAPI}" html-data-id="{!layoutField.fieldAPI}" value="{!Rental_Apply__c[layoutField.fieldAPI]}" rendered="{!not(layoutField.isPlaceHOlder)&&not(isNewMode)&&layoutField.fieldAPI=='RecordTypeId'}"
+                       />
                         <apex:pageblocksectionitem rendered="{!layoutField.isPlaceHolder}">
                         </apex:pageblocksectionitem>
                     </apex:repeat>
@@ -367,6 +461,25 @@
                 </apex:pageBlockSection>
             </apex:repeat>
             <script>
+               var init_nodes = document.getElementsByClassName("PIBackApi");
+                var api_id_map={};
+                for(let ei in init_nodes){
+                    let e = init_nodes[ei];
+                    if(IsFormElement(e)){
+                        if(e.getAttribute("multiple") != 'multiple' || e.style.display == 'none' && e.id.indexOf('selected') < 0){
+                            api_id_map[GetEleApiName(e)] = e.id;
+                        }
+                    }else{
+                        let eid = jQuery(e).find(".lookupInput input").attr("id")
+                        if(eid){
+                            eid += '_lkid';
+                            if(document.getElementById(eid)){
+                                api_id_map[GetEleApiName(e)] = eid;
+                            }
+                        }
+                    }
+                }
+                console.log(api_id_map);
                 //Append Page
                 sfdcPage.appendToOnloadQueue(function () {
                     var layoutSections = JSON.parse('{!layoutSectionsStr}');
@@ -375,20 +488,48 @@
                         for (let n = 0; n < layoutSection.length; n++) {
                             let layoutField = layoutSection[n];
                             if (layoutField.fieldAPI != '' && document.querySelector("[data-id='"+layoutField.fieldAPI+"']") != null) {
-                                document.querySelector("[data-id='"+layoutField.fieldAPI+"']").disabled = !(layoutField.editableField);
+                                let e = document.querySelector("[data-id='"+layoutField.fieldAPI+"']");
+                                console.log('layoutField.fieldAPI= ' + layoutField.fieldAPI);
+                                e.disabled = !(layoutField.editableField);
+                                if (!(layoutField.editableField)) {
+                                    if (e.getAttribute("multiple") && e.getAttribute("multiple") == 'multiple') {
+                                        e.parentNode.classList.add("disabledbutton");
+                                    }
+                                    if (e.tagName == 'DIV') {
+                                        e.classList.add("disabledbutton");
+                                    }
+                                    if(document.querySelector('[data-id=Old_Rental_Apply__c] .lookupIcon')){
+                                        document.querySelector('[data-id=Old_Rental_Apply__c] .lookupIcon').classList.add("disabledbutton");
+                                    }
+                                    
+
+                                }
                             }
                         }
                     }
+                    var onlyReadFields = ['ExtensionStatus__c','ExtensionApplicationTime_Initial__c','ExtensionApprovalTime_Initial__c','ExtensionApplicationTime_Final__c','ExtensionApprovalTime_Final__c','ExtensionSuccessTimes__c','ZongjianApprovalManager__c','BuchangApprovalManager__c','JingliApprovalManager__c','BuchangApprovalManagerSales__c','SalesManager__c'];
+                    console.log(onlyReadFields);
+                    for(let i=0;i<onlyReadFields.length;i++){
+                        if(document.querySelector('[data-id='+onlyReadFields[i]+']')){
+                        	document.querySelector('[data-id='+onlyReadFields[i]+']').parentNode.remove();
+                        }
+                    }
+                    
                     //2. Query AWS Data by dataId 
                     console.log('Mode for rentalApply Page:' + {!isNewMode});
                     if (!{!isNewMode}) {
                         blockme();
                         QueryRentalApplyFromAWS();
+                    }else{
+                        enableButtonStatus();//Add by Li Jun 20220418
                     };
                     //Replace Vlookup Field
                     replaceSearchContactLookup();
                     document.querySelector("[data-id='OwnerId']").classList.add("disabledbutton");
-
+                    
+                    document.getElementById('topButtonRow').style = '';
+聽 聽 聽 聽 聽 聽 聽document.getElementById('bottomButtonRow').style = '';
+                    
                     let previous_value = {};
                     jQuery(".lookupInput input").each(function(i,e){
                         let je =jQuery(e);
@@ -406,6 +547,9 @@
                             }
                         })
                     });
+                                        
+
+
 
                     // jQuery(".lookupInput").each(function(i,e){
                     //     let je =jQuery(e).find('input');
@@ -445,7 +589,7 @@
                         <tr>
                             <td class="pbTitle">
                                 <img src="/img/s.gif" alt="" class="minWidth" title="" width="1" height="1" />&nbsp;</td>
-                            <td class="pbButtonb" id="bottomButtonRow">
+                            <td class="pbButtonb" id="bottomButtonRow" style="pointer-events: none; opacity: 0.4;">
                                 <input class="btn" type="Button" value="淇濆瓨" onclick="saveRentalApplyProcess('Save')" />
                                 <input class="btn" type="Button" value="淇濆瓨骞舵柊寤�" onclick="saveRentalApplyProcess('SaveAndNew')" />
                                 <apex:commandButton action="{!cancel}" value="鍙栨秷" />

--
Gitblit v1.9.1