'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') + '
' + 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: '