'use strict';
var EditableData = false;
var DefaultState = parent.top.OrgID === 'SG' ? 'CHN' : 'TWN';
var fnPageInit = function () {
    var sLang = g_ul.GetLang(),
        sAction = getUrlParam('Action') || 'Add',
        oAuditFlag = { 'N': 'P', 'Z': 'A', 'Q': 'A' },
        canDo = new CanDo({
            /**
             * 是否返回查詢頁面
             */
            goBack: false,
            /**
             * 當前程式所有ID名稱集合
             */
            idKeys: ['OrgID', 'guid'],
            /**
             * 當前程式所有參數名稱集合
             */
            paramKeys: ['guid', 'From', 'FromId', 'Flag'],
            /**
             * 頁簽回調函數
             */
            tabAction: function (el, pargs) {
                switch (el.id) {
                    case 'litab2':
                        if (!$(el).data('action')) {
                            pargs.Grid.loadData();
                            $(el).data('action', true);
                        }
                        break;
                }
            },
            /**
             * 查詢當前資料
             * @param  {Object} pargs CanDo 對象
             * @param  {Object} data 當前資料實體
             */
            getOneBack: function (pargs, data) {
                var oRes = data,
                    sText = '',
                    elAuditReason = $('#AuditReason');
                oRes.Contactors = $.parseJSON(oRes.Contactors || '[]');
                $('.NotPassReason').show();
                switch (oRes.IsAudit) {
                    case 'N':// ╠common.NotAudit⇒未提交審核╣
                        EditableData = true;
                        sText = i18next.t("common.NotAudit");
                        elAuditReason.css('color', 'red');
                        break;
                    case 'Y':// ╠common.Audited⇒已審核╣
                        sText = i18next.t("common.Audited");
                        elAuditReason.css('color', 'green');
                        break;
                    case 'P':// ╠common.InAudit⇒提交審核中╣
                        sText = i18next.t("common.InAudit");
                        elAuditReason.css('color', 'blue');
                        break;
                    case 'A':// ╠common.AuditAgain⇒重新提交審核中╣
                        sText = i18next.t("common.AuditAgain") + '    (' + (oRes.NotPassReason || '') + ')';
                        elAuditReason.css('color', 'blue');
                        break;
                    case 'Z':// ╠common.ApplyforUpdateing⇒申請修改中╣
                        EditableData = true;
                        sText = i18next.t("common.ApplyforUpdateing") + '    (' + (oRes.NotPassReason || '') + ')';
                        elAuditReason.css('color', 'blue');
                        break;
                    case 'Q':// ╠common.NotPass⇒不通過╣
                        sText = i18next.t("common.NotPass") + '    (' + (oRes.NotPassReason || '') + ')';
                        elAuditReason.css('color', 'red');
                        break;
                }
                elAuditReason.text(sText);
                if ('Y,P,A'.indexOf(oRes.IsAudit) > -1) {
                    disableInput(pargs._form, '.plustemplkey,[data-input],#WebsiteAdress,#State,#Memo');
                    if ('P,A'.indexOf(oRes.IsAudit) > -1 && parent.top.SysSet.CustomersAuditUsers.indexOf(parent.top.UserInfo.MemberID) > -1) {
                        $('#Toolbar_Audit').show();
                    }
                    if (oRes.IsAudit === 'Y') {
                        $('#Toolbar_ApplyforUpdate').show();
                    }
                }
                else {
                    disableInput(pargs._form, '#CustomerNO', false);
                    $('#Toolbar_ToAudit').show();
                }
                if (pargs.params.Flag === 'Fit') {
                    $('#Toolbar_Leave,#Toolbar_Del').hide();
                }

                $('#Toolbar_CopySync').hide();
                if (data.IsAudit === 'Y' && 'TE,TG'.indexOf(parent.UserInfo.OrgID) > -1) {
                    //只有已審核才顯示
                    $('#Toolbar_CopySync').show();
                }
            },
            /**
             * 處理新增資料參數
             * @param  {Object} pargs CanDo 對象
             * @param  {Object} data 當前表單資料
             */
            getInsertParams: function (pargs, data) {
                var sCustomerFirstChart = '',
                    sCustomerNo = '';
                //若失敗後,資料已經變成字串了。需要再次轉回陣列,才能刪除
                if (typeof pargs.data.Contactors === 'string') {
                    pargs.data.Contactors = JSON.parse(pargs.data.Contactors || '[]');
                }
                data.Contactors = JSON.stringify(pargs.data.Contactors);
                sCustomerFirstChart = getCustomerFirstChart(data.TransactionType, data.CustomerEName, data.CustomerCName);
                sCustomerNo = parent.top.OrgID + data.TransactionType + sCustomerFirstChart;

                if (sAction === 'Upd') {
                    if (data.CustomerNO.substr(0, 4) !== sCustomerNo) {
                        //與原先不相同,重新產生編碼
                        data.CustomerNO = sCustomerNo;
                    }
                }
                else {
                    if (!data.CustomerNO) {
                        data.CustomerNO = sCustomerNo;
                    }
                }

                return data;
            },
            /**
             * 處理修改資料參數
             * @param  {Object} pargs CanDo 對象
             * @param  {Object} data 當前表單資料
             */
            getUpdateParams: function (pargs, data) {
                return pargs.options.getInsertParams(pargs, data);
            },
            /**
             * 新增資料
             * @param  {Object} pargs CanDo 對象
             * @param  {Object} data 當前新增的資料
             * @param {String} flag 新增 or 儲存后新增
             */
            getInsertBack: function (pargs, data, flag) {

                if (pargs.params.From === 'Appoint') {
                    fnReEditCustomer($('body').attr('PopId') || '', data.guid).done(function () {
                        if (pargs.params.Flag === 'Pop') {
                            $('#Toolbar button').prop('disabled', true);
                            parent.fnReFresh(data.guid);
                            showMsg(i18next.t("message.Save_Success"), 'success'); // ╠message.Save_Success⇒新增成功╣
                        }
                        else {
                            showMsgAndGo(i18next.t("message.Save_Success"), pargs.ProgramId, '?Action=Upd&guid=' + data.guid); // ╠message.Save_Success⇒新增成功╣
                        }
                    });
                }
                else {
                    if (flag == 'add') {
                        showMsgAndGo(i18next.t("message.Save_Success"), pargs.ProgramId, '?Action=Upd&guid=' + data.guid); // ╠message.Save_Success⇒新增成功╣
                    }
                    else {
                        showMsgAndGo(i18next.t("message.Save_Success"), pargs.ProgramId, '?Action=Add'); // ╠message.Save_Success⇒新增成功╣
                    }
                }
            },
            /**
             * 修改資料
             * @param  {Object} pargs CanDo 對象
             * @param  {Object} data 當前修改的資料
             */
            getUpdateBack: function (pargs, data) {

                if (typeof pargs.data.Contactors === 'string') {
                    pargs.data.Contactors = $.parseJSON(pargs.data.Contactors || '[]');
                }
                showMsg(i18next.t("message.Modify_Success"), 'success'); //╠message.Modify_Success⇒修改成功╣
                if (window.bLeavePage) {
                    setTimeout(function () {
                        pargs._pageLeave();
                    }, 1000);
                }
                else {
                    pargs._setFormVal(data);
                    //location.reload();
                }
            },
            /**
             * 客製化驗證規則
             * @param  {Object} pargs CanDo 對象
             */
            validRulesCus: function (pargs) {
                $.validator.addMethod("unicoderequired", function (value, element, parms) {
                    var sTransactionType = $('[name="TransactionType"]:checked').val();
                    if ((sTransactionType === 'A' || sTransactionType === 'D') && value === '') {
                        return false;
                    }
                    return true;
                });
                $.validator.addMethod("unicodelen", function (value) {
                    if (value !== '' && value.length != 8) {
                        return false;
                    }
                    return true;
                });
                $.validator.addMethod("customercnamerequired", function (value, element, parms) {
                    var sTransactionType = $('[name="TransactionType"]:checked').val();
                    if (!(sTransactionType === 'B' || sTransactionType === 'C') && value === '') {
                        return false;
                    }
                    return true;
                });
                $.validator.addMethod("customerenamecus", function (value, element, parms) {
                    var p = /[a-z]/i,
                        b = p.test(value.substr(0, 1));
                    if (!b && value !== '') {
                        return false;
                    }
                    return true;
                });
                $.validator.addMethod("customerenamerequired", function (value, element, parms) {
                    var sTransactionType = $('[name="TransactionType"]:checked').val();
                    if ((sTransactionType === 'B' || sTransactionType === 'C') && value === '') {
                        return false;
                    }
                    return true;
                });


                $.validator.addMethod("customershortnamerule", function (value) {
                    var bRetn = true;
                    g_api.ConnectLite(pargs.ProgramId, pargs._api.getcout,
                        {
                            guid: pargs.ids.guid,
                            CustomerShotCName: value
                        },
                        function (res) {
                            if (res.RESULT && res.DATA.rel > 0) {
                                bRetn = false;
                            }
                        }, null, false);
                    return bRetn;
                });

                $.validator.addMethod("unicoderule", function (value) {
                    var bRetn = true;
                    if (value) {
                        g_api.ConnectLite(pargs.ProgramId, pargs._api.getcout,
                            {
                                guid: pargs.ids.guid,
                                UniCode: value
                            },
                            function (res) {
                                if (res.RESULT && res.DATA.rel > 0) {
                                    bRetn = false;
                                }
                            }, null, false);
                    }
                    return bRetn;
                });
            },
            /**
             * 驗證規則
             */
            validRules: {
                //若是換頁新增會出錯,必須驗證隱藏的tab
                //ignore: 'disabled',
                onfocusout: false,
                rules: {
                    Email: {
                        email: true
                    },
                    UniCode: {
                        unicodelen: true,
                        unicoderule: true

                    },
                    TransactionType: { required: true },
                    CustomerShotCName: { customershortnamerule: true }
                },
                messages: {
                    Email: i18next.t("message.IncorrectEmail"),// ╠message.IncorrectEmail⇒郵箱格式不正確╣
                    TransactionType: { required: i18next.t("Customers_Upd.TransactionType_required") },// ╠Customers_Upd.TransactionType_required⇒請選擇交易類型╣
                    UniCode: {
                        unicodelen: i18next.t("message.UniCodeLength"),// ╠message.UniCodeLength⇒客戶統一編號必須是8碼╣
                        unicoderule: i18next.t("message.Data_Repeat")// ╠message.Data_Repeat⇒此筆資料已建檔╣
                    },
                    CustomerShotCName: { customershortnamerule: i18next.t("message.ShotNameExist") }// ╠message.ShotNameExist⇒該簡稱已被使用╣
                }
            },
            /**
             * 客製化按鈕
             * @param  {Object} pargs CanDo 對象
             */
            cusBtns: function (pargs) {
                var saCusBtns = [];
                if (pargs.action !== 'add') {
                    saCusBtns.push({
                        id: 'Toolbar_ToAudit',
                        value: 'common.SubmitAudit',// ╠common.SubmitAudit⇒提交審核╣
                        /**
                         * 業務提交審核
                         */
                        action: function (pargs) {
                            if (pargs.data.Effective === 'N') {
                                showMsg(i18next.t('message.DataHasInvalid')); // ╠message.DataHasInvalid⇒該資料已無效╣
                                return false;
                            }
                            var sIsAudit = oAuditFlag[pargs.data.IsAudit];
                            var sCustomerShotCName = $('#CustomerShotCName').val();
                            var sUniCode = $('#UniCode').val();
                            var sTransactionType = $('[name="TransactionType"]:checked').val();
                            var sTaxpayerOrgID = $('#TaxpayerOrgID').val();

                            g_api.ConnectLite(pargs.ProgramId, 'ToAudit', {
                                guid: pargs.ids.guid,
                                IsAudit: sIsAudit,
                                CustomerShotCName: sCustomerShotCName,
                                UniCode: sUniCode,
                                TransactionType: sTransactionType,
                                TaxpayerOrgID: sTaxpayerOrgID
                            }, function (res) {
                                if (res.RESULT) {
                                    $('#Toolbar_ToAudit').hide();
                                    pargs._getOne();
                                    showMsg(i18next.t("message.ToAudit_Success"), 'success'); // ╠message.ToAudit_Success⇒提交審核成功╣
                                    parent.top.msgs.server.pushTips(parent.top.fnReleaseUsers(res.DATA.rel));
                                }
                                else {
                                    showMsg(i18next.t('message.ToAudit_Failed') + '<br>' + res.MSG, 'error'); // ╠message.ToAudit_Failed⇒提交審核失敗╣
                                }
                            }, function () {
                                showMsg(i18next.t('message.ToAudit_Failed'), 'error'); // ╠message.ToAudit_Failed⇒提交審核失敗╣
                            });
                        }
                    }, {
                            id: 'Toolbar_CopySync',
                            value: 'Customers_Upd.CopySync', // 複製同步,
                            action: function (parg) {

                            },
                            'data-toggle': 'modal',
                            'data-target': '#CopySync'
                        });

                    if (parent.top.SysSet.CustomersAuditUsers.indexOf(parent.top.UserInfo.MemberID) > -1) {
                        saCusBtns.push({
                            id: 'Toolbar_Audit',
                            value: 'common.Audit',// ╠common.Audit⇒審核╣
                            /**
                             * 主管審核
                             */
                            action: function (pargs) {
                                layer.open({
                                    type: 1,
                                    title: i18next.t('common.Audit'),// ╠common.Audit⇒審核╣
                                    area: ['400px', '260px'],//寬度
                                    shade: 0.75,//遮罩
                                    shadeClose: true,
                                    btn: [i18next.t('common.Cancel')],// ╠common.Cancel⇒取消╣// ╠common.Cancel⇒取消╣
                                    content: '<div class="pop-box">\
                                                 <textarea name="NotPassReason" id="NotPassReason" style="min-width:300px;" class="form-control" rows="5" cols="20" placeholderid="common.NotPassReason" placeholder="不通過原因..."></textarea><br>\
                                                 <button type="button" data-i18n="common.Pass" id="audit_pass" class="btn-custom green">通過</button>\
                                                 <button type="button" data-i18n="common.NotPass" id="audit_notpass" class="btn-custom red">不通過</button>\
                                              </div>',
                                    success: function (layero, idx) {
                                        $('.pop-box :button').click(function () {
                                            var oAudit = {},
                                                sNotPassReason = $('#NotPassReason').val();
                                            if (this.id === 'audit_pass') {
                                                oAudit.IsAudit = 'Y';
                                                oAudit.NotPassReason = '';
                                            }
                                            else {
                                                oAudit.IsAudit = 'Q';
                                                oAudit.NotPassReason = sNotPassReason;
                                                if (!sNotPassReason) {
                                                    showMsg(i18next.t("message.NotPassReason_Required")); // ╠message.NotPassReason_Required⇒請填寫不通過原因╣
                                                    return false;
                                                }
                                            }

                                            g_api.ConnectLite(pargs.ProgramId, 'Audit', {
                                                IsAudit: oAudit.IsAudit,
                                                NotPassReason: oAudit.NotPassReason,
                                                guid: pargs.ids.guid
                                            }, function (res) {
                                                if (res.RESULT) {
                                                    $('#Toolbar_Audit').hide();
                                                    layer.close(idx);
                                                    pargs._getOne();
                                                    showMsg(i18next.t("message.Audit_Completed"), 'success'); // ╠message.Audit_Completed⇒審核完成╣
                                                    parent.top.msgs.server.pushTip(parent.top.OrgID, res.DATA.rel);
                                                    if (oAudit.IsAudit === 'Y') {
                                                        parent.top.msgs.server.pushTransfer(parent.top.OrgID, parent.top.UserID, pargs.data.CustomerNO, 2);
                                                    }
                                                }
                                                else {
                                                    showMsg(i18next.t('message.Audit_Failed') + '<br>' + res.MSG, 'error'); // ╠message.Audit_Failed⇒審核失敗╣
                                                }
                                            }, function () {
                                                showMsg(i18next.t('message.Audit_Failed'), 'error'); // ╠message.Audit_Failed⇒審核失敗╣
                                            });
                                        });
                                        pargs._transLang(layero);
                                    }
                                });
                            }
                        });
                    }

                    saCusBtns.push({
                        id: 'Toolbar_ApplyforUpdate',
                        value: 'common.ApplyforUpdate',// ╠common.ApplyforUpdate⇒申請修改╣
                        /**
                         * 申請修改
                         */
                        action: function (pargs) {
                            layer.open({
                                type: 1,
                                title: i18next.t('common.ApplyforUpdate'),// ╠common.ApplyforUpdate⇒申請修改╣
                                area: ['400px', '260px'],//寬度
                                shade: 0.75,//遮罩
                                shadeClose: true,
                                btn: [i18next.t('common.Confirm'), i18next.t('common.Cancel')],//╠common.Confirm⇒確定╣╠common.Cancel⇒取消╣
                                content: '<div class="pop-box"><textarea name="Reason" id="Reason" style="min-width:300px;" class="form-control" rows="5" cols="20" placeholderid="common.ApplyforUpdateReason" placeholder="申請修改原因..."></textarea></div>',
                                success: function (layero, idx) {
                                    pargs._transLang(layero);
                                },
                                yes: function (index, layero) {
                                    var sReason = $('#Reason').val();
                                    if (!sReason) {
                                        showMsg(i18next.t("message.Reason_Required")); // ╠message.Reason_Required⇒請填寫原因╣
                                        return false;
                                    }
                                    g_api.ConnectLite(pargs.ProgramId, 'ApplyforUpdate', {
                                        Guid: pargs.ids.guid,
                                        NotPassReason: sReason
                                    }, function (res) {
                                        if (res.RESULT) {
                                            layer.close(index);
                                            $('#Toolbar_ApplyforUpdate').hide();
                                            pargs._getOne();
                                            showMsg(i18next.t("message.HasNoticeAditor"), 'success'); // ╠message.HasNoticeAditor⇒已通知審核人員╣
                                            parent.top.msgs.server.pushTips(parent.top.fnReleaseUsers(res.DATA.rel));
                                        }
                                        else {
                                            showMsg(i18next.t('message.ToApplyFailed') + '<br>' + res.MSG, 'error'); // ╠message.ToApplyFailed⇒提交申請失敗╣
                                        }
                                    }, function () {
                                        showMsg(i18next.t('message.ToApplyFailed'), 'error');// ╠message.ToApplyFailed⇒提交申請失敗╣
                                    });
                                }
                            });
                        }
                    });
                }
                return saCusBtns;
            },
            /**
             * 頁面初始化
             * @param  {Object} pargs CanDo 對象
             */
            pageInit: function (pargs) {
                var postArray = [];
                if (pargs.action === 'add') {
                    $("#ui-id-2").hide();
                }

                $('#CopySync').find('label[OrgID=\"' + g_db.GetItem("orgid").toUpperCase() + '\"]').hide();  //隱藏自己組織別

                $('#Toolbar_CopySync').click(function () {
                    $('#CopySync').find('label[OrgID] > :checkbox').prop('checked', false);  //清除已勾選項目
                });

                $('#CopySycnOK').click(function () {
                    var aryCheckOrgID = [];
                    //取得勾選組織別
                    $('#CopySync').find('label[OrgID]').each(function (idx, element) {
                        var sOrgID = $(element).attr('OrgID');
                        if ($(element).find(':checkbox').is(':checked')) {
                            aryCheckOrgID.push(sOrgID);
                        }
                    });

                    if (aryCheckOrgID.length <= 0) {
                        showMsg(i18next.t("請先選擇要複製同步的公司別"), 'error');
                    }
                    else {
                        canDo.data.currOrgID = g_db.GetItem("orgid").toUpperCase();
                        canDo.data.OrgID = aryCheckOrgID[0];

                        var sCustomerFirstChart = getCustomerFirstChart(canDo.data.TransactionType, canDo.data.CustomerEName, canDo.data.CustomerCName),
                            sCustomerNo = canDo.data.OrgID + canDo.data.TransactionType + sCustomerFirstChart;

                        canDo.data.CustomerNO = sCustomerNo;

                        g_api.ConnectLite(pargs.ProgramId, 'CopySync', canDo.data, function (res) {
                            if (res.RESULT) {
                                var data = res.DATA.rel;
                                showMsg(i18next.t("執行成功,同步新客戶編號為" + data.CustomerNO), 'success');
                                parent.top.msgs.server.pushTips(parent.top.fnReleaseUsers(data));
                            }
                            else {
                                showMsg(i18next.t('message.ToAudit_Failed') + '<br>' + res.MSG, 'error'); // ╠message.ToAudit_Failed⇒提交審核失敗╣
                            }
                        }, function () {
                            showMsg(i18next.t('message.ToAudit_Failed'), 'error'); // ╠message.ToAudit_Failed⇒提交審核失敗╣
                        });

                    }

                });

                if (pargs.action === 'upd') {
                    postArray.push(pargs._getOne());
                }
                else {
                    if (g_db.GetItem("orgid").toUpperCase() === 'SG')  // if (parent.OrgID.toUpperCase() === 'SG')
                        $('.APIImport').hide();
                    else
                        $('.APIImport').show();
                    $('.CustomerEName').hide();
                    pargs.data.Contactors = [];
                }
                postArray.push(fnSetArgDrop([
                    {
                        ArgClassID: 'TranType',
                        CallBack: function (data) {
                            $('#transactiontype').html(createRadios(data, 'id', 'text', 'TransactionType', '', true)).find('label:first').click();
                            pargs._uniformInit($('#transactiontype'));
                        }
                    }
                ]));
                //postArray return 要是function 才會生效
                postArray.push(setStateDrop());
                if (parent.top.OrgID === 'SG') {
                    $('.unicode-box').hide();
                    $('.sg-box').show();
                }

                //加載報關類別,加載報價頁簽,加載運輸方式, 加載機場, 加載貨棧場, 加載倉庫
                $.whenArray(postArray).done(function (res) {
                    let resState = '';
                    if (pargs.action === 'upd' && res[0].RESULT) {
                        var oRes = res[0].DATA.rel;
                        pargs._setFormVal(oRes);
                        fnBindContactors();
                        pargs._getPageVal();//緩存頁面值,用於清除
                        resState = oRes.State;
                        switch (pargs.data.IsAudit) {
                            case 'N'://未提交
                            case 'Q'://退件
                            case 'Z'://申請修改中
                                $('#Toolbar_ApplyforUpdate,#Toolbar_Audit').hide();
                                break;
                            case 'Y':// ╠common.Audited⇒已審核╣
                                $('#Toolbar_ToAudit,#Toolbar_Audit').hide();
                                break;
                            case 'P':// ╠common.InAudit⇒提交審核中╣
                            case 'A'://重新提交審核中
                                $('#Toolbar_ApplyforUpdate').hide();
                                $('#Toolbar_ToAudit').hide();
                                break;
                        }
                    }

                    if (pargs.params.Flag === 'Pop') {
                        $('#Toolbar_Leave,#Toolbar_ReAdd,#APIImport').hide();
                    }
                    else if (pargs.params.Flag === 'Fit') {
                        $('#Toolbar_Leave,#Toolbar_Del').hide();
                    }
                    if (canDo.params.FromId) {
                        fnGetImportCustomersByAppointNO();
                    }
                    if ('Y,P,A'.indexOf(pargs.data.IsAudit) > -1) {
                        disableInput(pargs._form, '.plustemplkey,[data-input],#WebsiteAdress,#State,#Memo');
                    }
                    $('[name="TransactionType"]').click(function () {
                        if (this.value === 'A' || this.value === 'D') {
                            $('.innercol,.address,.telephone').show();
                            $('#BankName,#BankAcount,#TaxpayerOrgID,#Telephone,#Address').attr('required', true);
                        }
                        else {
                            $('.innercol,.address,.telephone').hide();
                            $('#BankName,#BankAcount,#TaxpayerOrgID,#Telephone,#Address').removeAttr('required');
                        }
                        if (this.value === 'B' || this.value === 'C') {
                            $('.CustomerCName').hide();
                            $('.CustomerEName').show();

                        }
                        else {
                            $('.CustomerCName').show();
                            $('.CustomerEName').hide();
                        }
                        if (EditableData || sAction === 'Add') {
                            var DefaultChangeState = 'A,D,E,F'.indexOf(this.value) > -1;
                            if (DefaultChangeState) {
                                $('#State').val(DefaultState).trigger("change");
                            }
                            else {
                                $('#State').val('').trigger("change");
                            }
                        }
                    });
                    $('[name="TransactionType"]:checked').click();


                    if (!!resState) {
                        $('#State').val(resState);
                    }
                    $('.plustemplkey').on('click', function () {
                        var oNewKey = {};
                        oNewKey.guid = guid();
                        oNewKey.FullName = '';
                        oNewKey.JobtitleName = '';
                        oNewKey.TEL1 = '';
                        oNewKey.TEL2 = '';
                        oNewKey.FAX = '';
                        oNewKey.Email = '';
                        oNewKey.Memo = '';
                        //若失敗後,資料已經變成字串了。需要再次轉回陣列,才能刪除
                        if (typeof pargs.data.Contactors === 'string') {
                            pargs.data.Contactors = JSON.parse(pargs.data.Contactors || '[]');
                        }
                        pargs.data.Contactors.push(oNewKey);
                        fnBindContactors();
                    });
                });

                var iHeight = $('body').height() - $('.page-title').height() - $('#searchbar').height() - 120;
                $("#jsGrid").jsGrid({
                    width: "100%",
                    height: "auto",
                    autoload: false,
                    filtering: false,
                    pageLoading: true,
                    inserting: false,
                    editing: false,
                    sorting: false,
                    paging: false,
                    pageIndex: 1,
                    pageSize: parent.top.SysSet.GridRecords || 10,
                    confirmDeleting: true,
                    deleteConfirm: i18next.t('message.ConfirmToDelete'),// ╠message.ConfirmToDelete⇒確定要刪除嗎 ?╣
                    pagePrevText: "<",
                    pageNextText: ">",
                    pageFirstText: "<<",
                    pageLastText: ">>",
                    fields: [
                        {
                            name: "RowIndex", title: 'common.RowNumber', width: 50, align: "center"
                        },
                        {
                            name: "ExhibitionCode", title: 'Exhibition_Upd.ExhibitionCode', width: 150, align: "center"
                        },
                        {
                            name: "Exhibitioname_TW", title: 'Exhibition_Upd.Exhibitioname_TW', width: 600,
                            itemTemplate: function (val, item) {
                                var oA = $('<a>', {
                                    html: sLang === 'zh' ? item.Exhibitioname_CN : val,
                                    class: 'a-url',
                                    click: function () {
                                        parent.top.openPageTab('Exhibition_Upd', '?Action=Upd&SN=' + item.SN);
                                    }
                                });
                                return oA;
                            }
                        }
                    ],
                    controller: {
                        loadData: function (args) {
                            return fnGetGridData();
                        },
                        insertItem: function (args) {
                        },
                        updateItem: function (args) {
                        },
                        deleteItem: function (args) {
                        }
                    },
                    onInit: function (args) {
                        pargs.setGrid(args.grid);
                    }
                });

                $('#UniCode').on('blur', function () {
                    this.value = $.trim(this.value);
                });
                $('#CustomerEName,#CustomerShotCName,#Address,#InvoiceAddress').on('blur', function () {
                    this.value = this.value.toUpperCase();
                });

                $('#APIImport').on('click', function () {
                    var sUniCode = $('#UniCode').val(),
                        sCustomerCName = $('#CustomerCName').val();
                    if (!sUniCode && !sCustomerCName) {
                        showMsg(i18next.t('message.UniCodeOrCustomerCName_Required')); // ╠message.UniCodeOrCustomerCName_Required⇒請輸入客戶統編或者客戶全稱╣
                        return false;
                    }

                    if (sUniCode && sUniCode.length !== 8) {
                        showMsg(i18next.t('message.UniCodeInvalid')); // ╠message.UniCodeInvalid⇒統編號碼不合法╣
                        return false;
                    }
                    g_api.ConnectLite(pargs.ProgramId, 'GetCrmBaseDataByUniCode', {
                        UniCode: sUniCode,
                        KeyWords: sCustomerCName
                    }, function (res) {
                        if (res.RESULT) {
                            var oBase = res.DATA.rel,
                                oFirst = {};
                            if (oBase) {
                                var saBase = $.parseJSON(oBase);
                                if ($.isArray(saBase)) {
                                    oFirst = saBase[0];
                                    if (!$.isEmptyObject(oFirst)) {
                                        $('#UniCode').val(oFirst.Business_Accounting_NO);
                                        $('#CustomerCName').val(oFirst.Company_Name);
                                        $('#Address').val(oFirst.Company_Location);
                                        $('#InvoiceAddress').val(oFirst.Company_Location);
                                    }
                                }
                            }
                        }
                        else {
                            showMsg(i18next.t('message.NotFindData') + '<br>' + res.MSG, 'error'); // ╠message.NotFindData⇒查不到對應的資料╣
                        }
                    }, function () {
                        showMsg(i18next.t('message.NotFindData'), 'error');
                    });
                });
            }

        }),
        /**
         * 設定國家下拉選單
         */
        setStateDrop = function () {
            return g_api.ConnectLite(Service.com, ComFn.GetArguments, {
                OrgID: 'TE',
                ArgClassID: 'Area',
                LevelOfArgument: 1
            }, function (res) {
                if (res.RESULT) {
                    let saState = res.DATA.rel;
                    if (saState.length > 0) {
                        $('#State').append(createOptions(saState, 'id', 'text', true)).select2();
                    }
                }
            });
        },

        /**
         * 客戶轉為正式資料后回寫動作
         * @param {String} preid 轉正前的客戶id
         * @param {String} afterid 轉正后的客戶id
         */
        fnReEditCustomer = function (previd, afterid) {
            return g_api.ConnectLite('Exhibition_Upd', 'UpdateCustomerTag', {//匯入費用項目
                PrevId: previd,
                AfterId: afterid
            }, function (res) { });
        },
        /**
         * 綁定模版參數
         */
        fnBindContactors = function () {
            var sKeysHtml = '';
            $.each(canDo.data.Contactors, function (idx, item) {
                sKeysHtml += '<tr data-id="' + item.guid + '">\
                              <td class="wcenter">' + (idx + 1) + '</td>\
                              <td><input type="text" data-input="FullName" class="form-control w100p" value="' + item.FullName + '"></td>\
                              <td><input type="text" data-input="JobtitleName" class="form-control w100p" value="' + (item.JobtitleName || '') + '"></td>\
                              <td><input type="text" data-input="TEL1" class="form-control w100p" value="' + item.TEL1 + '" placeholderid="Customers_Upd.Instruction_ContactorTEL" placeholder="含分機"></td>\
                              <td><input type="text" data-input="TEL2" class="form-control w100p" value="' + item.TEL2 + '" placeholderid="Customers_Upd.Instruction_ContactorTEL" placeholder="含分機"></td>\
                              <td><input type="text" data-input="FAX" class="form-control w100p" value="' + item.FAX + '"></td>\
                              <td><input type="text" data-input="Email" class="form-control w100p" value="' + item.Email + '"></td>\
                              <td><input type="text" data-input="Memo" class="form-control w100p" value="' + item.Memo + '"></td>\
                              <td><input type="number" data-input="RowID" class="form-control w100p" data-OriValue="' + (idx + 1) + '" value="' + (idx + 1) + '"></td>\
                              <td class="wcenter">\
                                 <i class="glyphicon glyphicon-trash" data-value="' + item.guid + '" title="刪除"></i>\
                              </td>\
                             </tr>';
            });
            $('#table_box').html(sKeysHtml).find('.glyphicon-trash').on('click', function () {
                var sId = $(this).attr('data-value'),
                    saNewList = [];
                //若失敗後,資料已經變成字串了。需要再次轉回陣列,才能刪除
                if (typeof canDo.data.Contactors === 'string') {
                    canDo.data.Contactors = JSON.parse(canDo.data.Contactors || '[]');
                }

                $.each(canDo.data.Contactors, function (idx, item) {
                    if (sId !== item.guid) {
                        saNewList.push(item);
                    }
                });
                $(this).parents('tr').remove();
                canDo.data.Contactors = saNewList;
            });
            canDo._transLang($('#table_box'));
            $('#table_box [data-input]').on('change click', function () {
                var sKey = $(this).attr('data-input'),
                    sId = $(this).parents('tr').attr('data-id'),
                    sVal = this.value;
                if (sKey === "RowID") {
                    let OriValue = $(this).attr('data-OriValue');
                    let FromIndex = Number(OriValue) - 1;
                    let ToIndex = Number(sVal) - 1;
                    let TempTarget = canDo.data.Contactors[ToIndex];
                    let TempOrigin = canDo.data.Contactors[FromIndex];
                    canDo.data.Contactors.splice(FromIndex, 1);
                    canDo.data.Contactors.splice(ToIndex, 0,TempOrigin);
                    fnBindContactors();
                }
                else {
                    $.each(canDo.data.Contactors, function (idx, item) {
                        if (sId === item.guid) {
                            item[sKey] = sVal;
                            return false;
                        }
                    });
                }
                
            });
        },
        /**
         * 設定交易型態下拉選單
         * @param {String} type 公司類別
         * @param {String} egname 英文名稱
         * @param {String} cnname 中文名稱
         */
        getCustomerFirstChart = function (type, egname, cnname) {
            var sFirstChart = '';
            if (type === 'E') {
                if (egname) {
                    if (egname.indexOf('.') > -1) {
                        if (egname.split('.')[1]) {
                            sFirstChart = egname.split('.')[1].substr(0, 1);
                        }
                        else {
                            sFirstChart = egname.split('.')[0].substr(0, 1);
                        }
                    }
                    else {
                        sFirstChart = egname.substr(0, 1);
                    }
                }
                else {
                    sFirstChart = cnname.substr(0, 1);
                }
            }
            else {
                if (egname) {
                    sFirstChart = egname.substr(0, 1);
                }
                else {
                    sFirstChart = cnname.substr(0, 1);
                }
            }
            sFirstChart = makePy(sFirstChart[0]);
            return sFirstChart[0].toUpperCase();
        },
        /**
         * 依據預約單號查詢匯入廠商并預設
         */
        fnGetImportCustomersByAppointNO = function () {
            g_api.ConnectLite(canDo.ProgramId, 'GetImportCustomersByAppointNO', {
                AppointNO: canDo.params.FromId
            }, function (res) {
                if (res.RESULT) {
                    var oRes = res.DATA.rel;
                    if (oRes) {
                        $('#CustomerCName').val(oRes.CustomerCName);
                        $('#Telephone').val(oRes.Telephone);
                        $('#Email').val(oRes.Email);
                        $('#Address').val(oRes.Address);
                        $('#InvoiceAddress').val(oRes.InvoiceAddress);
                        $('body').attr('PopId', oRes.guid);
                        setTimeout(function () {
                            $('.plustemplkey ').trigger('click');
                            var _input = $('#table_box').find(':input');
                            _input.eq(0).val(oRes.Contactor);
                            _input.eq(2).val(oRes.Telephone);
                            _input.eq(5).val(oRes.Email);
                            setTimeout(function () {
                                _input.eq(0).trigger('click');
                                _input.eq(2).trigger('click');
                                _input.eq(5).trigger('click');
                            }, 500);
                        }, 1000);
                    }
                }
            });
        },
        /**
         * 抓去參加展覽列表資料
         * @return {Object} ajax物件
         */
        fnGetGridData = function () {
            if (canDo.ids.guid) {
                return g_api.ConnectLite(canDo.ProgramId, 'GetExhibitionlist', {
                    Guid: canDo.ids.guid
                });
            }
            else {
                return $.Deferred().resolve().promise();
            }
        };


};


require(['base', 'select2', 'jsgrid', 'cando'], fnPageInit);