Li Jun
2022-04-06 fb04e7c01d119c60632b4298d18fd93f3ccb3d79
force-app/main/default/pages/NewRentalApply.page
@@ -1,8 +1,8 @@
<!--
  @description       : 
  @author            : ChangeMeIn@UserSettingsUnder.SFDoc
  @author            : Dennis
  @group             : 
  @last modified on  : 03-17-2022
  @last modified on  : 04-01-2022
  @last modified by  : ChangeMeIn@UserSettingsUnder.SFDoc
-->
<apex:page standardController="Rental_Apply__c" extensions="RentalApplyController" id="page">
@@ -24,7 +24,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 +40,8 @@
                alertErrorMessage(errorMessage);
            }
        }
        //Query Required Parameter
        var queryBack = function queryBack(data) {
@@ -81,6 +83,33 @@
            }
        }
        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
        function getPIPayload(rentalApplyJson) {
            let rentalApplyPayloadList = [];
@@ -101,14 +130,27 @@
            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'));
@@ -135,17 +177,44 @@
                }
            }
            for (let i = 0; i < userVLookUpFields.length; i++) {
                let userVlookUpNodeValue = document.querySelector("[data-id='" + userVLookUpFields[i] + "']").children[1].value;
                console.log('userVlookUpNodeValue:'+userVlookUpNodeValue);
                if(userVlookUpNodeValue!='000000000000000'){
                    result[userVLookUpFields[i]] = userVlookUpNodeValue;
                if(document.querySelector("[data-id='" + userVLookUpFields[i] + "']")!=null){
                    let userVlookUpNodeValue = document.querySelector("[data-id='" + userVLookUpFields[i] + "']").children[1].value;
                    console.log('userVlookUpNodeValue:'+userVlookUpNodeValue);
                    if(userVlookUpNodeValue!='000000000000000'){
                        result[userVLookUpFields[i]] = userVlookUpNodeValue;
                    }
                }
            }
            if(document.querySelector("[id='page:form:block:j_id34:2:j_id35:j_id36:2:j_id37']")!=null){
                result.demo_purpose2__c = document.querySelector("[id='page:form:block:j_id34:2:j_id35:j_id36:2:j_id37']").value;
            }
            if(document.querySelector("[id='page:form:block:j_id34:0:j_id35:j_id36:18:j_id37']")!=null){
                result.ToAgency__c = document.querySelector("[id='page:form:block:j_id34:0:j_id35:j_id36:18:j_id37']").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(document.querySelector("[id='page:form:block:j_id34:13:j_id35:j_id36:1:j_id37']")!=null){
                result.Loaner_cancel_reason__c = document.querySelector("[id='page:form:block:j_id34:13:j_id35:j_id36:1:j_id37']").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;
                }               
            }
            //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:j_id30:j_id34:2:j_id35:j_id36:2:j_id37']").value;
            result.ToAgency__c = document.querySelector("[id='page:form:j_id30:j_id34:0:j_id35:j_id36:14:j_id37']").value;
            result.Loaner_cancel_reason__c = document.querySelector("[id='page:form:j_id30:j_id34:0:j_id35:j_id36:14: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('page:form:block:j_id34:0:j_id35:j_id36:13:j_id37' + '_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('page:form:block:j_id34:0:j_id35:j_id36:13:j_id37' + '_lkid').value;
            }
            console.log(JSON.stringify(result));
            return result;
        }
@@ -218,7 +287,7 @@
        //Alert Error Message
        function alertErrorMessage(errorMsg) {
            let errorMsgNode = document.getElementById("page:form:msgContent");
            let errorMsgNode = document.getElementById("page:form:block:msgContent");
            errorMsg = '错误:无效数据。' + '\n' + errorMsg;
            errorMsgNode.innerText = errorMsg;
            errorMsgNode.className = 'pbError';
@@ -227,7 +296,7 @@
        //Hide Error Message
        function hiddenErrorMsgNode() {
            let errorMsgNode = document.getElementById("page:form:msgContent");
            let errorMsgNode = document.getElementById("page:form:block:msgContent");
            errorMsgNode.innerText = '';
            errorMsgNode.className = '';
        }
@@ -251,22 +320,18 @@
            return template.content.firstChild;
        }
        function queryContactName() {
            // let sfId = document.getElementById(document.querySelector("[data-id='Loaner_medical_Staff__c']").id + '_lkid').value;
            let sfId = document.getElementById(document.querySelector("[data-id='Account__c']").id + '_lkid').value;
            let contactsInfo = JSON.parse('{!contactsInfo}');
            let dataId = contactsInfo[sfId];
            let url = staticResourcesContact.queryUrl + '?dataId=' + dataId;
            fetch(url, {
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json',
                    'pi-token': staticResources.token
                }
            }).then((data) => {
                return data.json();
            }).then((result) => {
            let sfId = document.getElementById(document.querySelector("[data-id='Loaner_medical_Staff__c']").id + '_lkid').value;
            let dataId = '';
            if ('{!contactsInfo}' != '') {
                let contactsInfo = JSON.parse('{!contactsInfo}');
                dataId = contactsInfo[sfId];
            } else if ('{!AWSDataId}' != '') {
                dataId = '{!AWSDataId}';
            }
            let queryBackContactName = function queryBackContactName(result){
                document.querySelector("[data-id='Loaner_medical_Staff__c']").value = result.object.lastName;
            })
            };
            AWSService.query(staticResourcesContact.queryUrl, dataId, queryBackContactName, staticResourcesContact.token);
        }
        //自定义lookup查询
        function searchContact(contactNodeId){
@@ -275,10 +340,11 @@
                let accountNodeId = document.querySelector("[data-id='Account__c']").id + '_lkid';
                accountValue = document.getElementById(accountNodeId).value;   
            } 
            let searchContactKeyWord = document.querySelector("[data-id='Loaner_medical_Staff__c']").value;
            console.log(accountValue);
            if(accountValue !='000000000000000'){
                let baseUrl = "/apex/SearchContactPage";
                let suffixUrl = "?contactId="+contactNodeId+"&accountId="+accountValue;
                let suffixUrl = "?contactId="+contactNodeId+"&accountId="+accountValue+"&searchContactKeyWord=" + searchContactKeyWord;
                let newSearchContactParam = 'height=600,width=800,left=100,top=100,dialogHide=true,resizable=no,scrollbars=yes,toolbar=no,status=no';
                newSearchContactWindow = window.open(baseUrl+suffixUrl, 'Popup', newSearchContactParam);
                if (window.focus) {
@@ -324,11 +390,7 @@
    </div>
    <apex:form id="form">
        <apex:inputHidden value="{!contactId}" id="contactId"/>
        <!-- Error Msg-->
        <apex:outputPanel id="errorMsg">
            <apex:pageMessages id="msgContent" escape="false" />
        </apex:outputPanel>
        <apex:pageblock >
        <apex:pageblock id="block">
            <div class="pbHeader">
                <table cellspacing="0" cellpadding="0" border="0">
                    <tbody>
@@ -346,13 +408,19 @@
                    </tbody>
                </table>
            </div>
            <!-- Error Msg-->
            <div style="text-align: center;">
                <apex:outputPanel id="errorMsg">
                    <apex:pageMessages id="msgContent" escape="false" />
                </apex:outputPanel>
            </div>
            <!-- Iterate the layoutSections, which is a list of sections -->
            <apex:repeat value="{!layoutSections}" var="layoutSection">
                <apex:pageBlockSection title="{!layoutSection.name}" collapsible="{!layoutSection.allowCollapse}" columns="{!layoutSection.columns}">
                    <!--Each section has layoutFields, let's iterate them as well-->
                    <apex:repeat value="{!layoutSection.layoutFields}" var="layoutField">
                        <apex:inputField 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)}"
                            required="{!layoutField.isRequired}" />
                        <apex:pageblocksectionitem rendered="{!layoutField.isPlaceHolder}">
                        </apex:pageblocksectionitem>
@@ -361,6 +429,18 @@
                </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;
                        }
                    }
                }
                console.log(api_id_map);
                //Append Page
                sfdcPage.appendToOnloadQueue(function () {
                    var layoutSections = JSON.parse('{!layoutSectionsStr}');
@@ -369,10 +449,31 @@
                        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++){
                        document.querySelector('[data-id='+onlyReadFields[i]+']').parentNode.remove();
                    }
                    //2. Query AWS Data by dataId 
                    console.log('Mode for rentalApply Page:' + {!isNewMode});
                    if (!{!isNewMode}) {
@@ -382,6 +483,58 @@
                    //Replace Vlookup Field
                    replaceSearchContactLookup();
                    document.querySelector("[data-id='OwnerId']").classList.add("disabledbutton");
                    let previous_value = {};
                    jQuery(".lookupInput input").each(function(i,e){
                        let je =jQuery(e);
                        let dataid = je.attr('data-id');
                        if(['Loaner_medical_Staff__c'].indexOf(dataid) < 0) return;
                        jQuery(e).focus(function(){
                            previous_value[this.id] = this.value;
                        })
                        jQuery(e).change(function(){
                            if (previous_value[this.id] != jQuery(this).val()) {
                                document.getElementById(this.id+'_lkid').value = '';
                            }
                        })
                    });
                    // jQuery(".lookupInput").each(function(i,e){
                    //     let je =jQuery(e).find('input');
                    //     je.attr("readonly","");
                    //     je.css("background","unset");
                    //     let dataid = je.attr('data-id');
                    //     if(['Hospital_Name__c','Department_Class__c','OwnerId'].indexOf(dataid) > -1) return;
                    //     jQuery(e).children(":last-child").before('<img class="closeIcon" data-id="'+dataid+'" generate="" alt="Clear" src="/s.gif" style="display: inline-block;">');
                    // })
                    // jQuery(".lookupInput").on("mouseenter","img[generate]",function(e){
                    //     this.className = "closeIconOn";
                    // });
                    // jQuery(".lookupInput").on("mouseleave","img[generate]",function(e){
                    //     this.className = "closeIcon"
                    // });
                    // jQuery(".lookupInput").on("click","img[generate]",function(e){
                    //     let id = jQuery("input[data-id='"+jQuery(this).attr("data-id")+ "']").attr("id");
                    //     let input = document.getElementById(id);
                    //     if(input){
                    //         input.value = '';
                    //         let hidden = document.getElementById(id+'_lkid');
                    //         if(hidden){
                    //             hidden.value = '';
                    //         }
                    //     }
                    // });
                });
            </script>
            <div class="pbBottomButtons">