/**
 * 数据管理-税率管理新增编辑页面JS
 *
 * Created by li.kui on 20203/17.
 */
'use strict';
var el = {
    form: '#form',
    id: '#id',
    purchaseGroupCode: '#purchaseGroupCode',
    purchaseGroupName: '#purchaseGroupName',
    orgCode: '#orgCode',
    supBankName: '#supBankName',
    currencyCode: '#currencyCode',
    supBankCode: '#supBankCode',
    supBankAccount: '#supBankAccount',
    bankCode: '#bankCode',
    bankInterCode: '#bankInterCode',
    paymentCompanyName: '#paymentCompanyName',
    bankAddress: '#bankAddress',
    orgName: '#orgName'
};

$(function () {
    $(el.supBankName).textbox({
        prompt: '请选择供应商开户行',
        required: true,
        icons: [
            {
                iconCls: 'icon-search',
                handler: function () {
                    choseBank();
                }
            }
        ]
    });
});
/**
 * 选择供应商开户行
 */
function choseBank() {
    const supplierId = $('#supplierId').val();
    if (!supplierId || supplierId === '') {
        topWin.layer.alert('请先选择供应商', {icon: 0});
        return false;
    }
    const url = g.ctx + "pc/supplier/bankInfo/page/selectorOfSupplierId?supplierId=" + supplierId;
    let supplierBank = openDialog('supplierBank', url, {
        title: "选择供应商开户行",
        width: '80%',
        height: '70%',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            supplierBank.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.confirm,
                handler: function () {
                    topWin.frames['supplierBank'].chooseRow(function (cbox) {
                        setBankInfo(cbox);
                        supplierBank.dialog('close');
                    });
                }
            },
            {
                text: itablei8n.close,
                handler: function () {
                    supplierBank.dialog('close');
                }
            }
        ]
    });
}

/**
 * 设置银行信息
 * @param chosenRow 选择的银行信息
 */
function setBankInfo(chosenRow) {
    if (!chosenRow) return;
    const currencyCode = $(el.currencyCode).val();
    let subBankCode;
    if (currencyCode === 'CNY') {
        subBankCode = chosenRow.bankCode;
    } else {
        if (chosenRow.bankCode === "" || chosenRow.bankCode == null || typeof (chosenRow.bankCode) == "undefined") {
            subBankCode = chosenRow.bankInterCode;
        } else {
            subBankCode = chosenRow.bankCode;
        }
    }
    $(el.supBankName).textbox('setValue', chosenRow.openingBankName);
    $(el.supBankCode).textbox('setValue',subBankCode);
    $(el.supBankAccount).textbox('setValue', chosenRow.bankAccount);
    $(el.bankCode).textbox('setValue', chosenRow.bankCode);
    $(el.bankInterCode).textbox('setValue', chosenRow.bankInterCode);
    $(el.paymentCompanyName).textbox('setValue', chosenRow.companyName);
    $(el.bankAddress).textbox('setValue', chosenRow.bankAddress);
}


/**
 * 数据提交
 *
 * @param callback 回调函数
 */
function saveOrUpdate(call) {
    if($(el.form).form('validate')){
        var formData = $(el.form).serializeJson();
        var isChange = false;
        if (formData.supBankNameOld!==formData.supBankName){
            isChange = true;
        }
        if (formData.paymentCompanyNameOld!==formData.paymentCompanyName){
            isChange = true;
        }
        if (formData.supBankAccountOld!==formData.supBankAccount){
            isChange = true;
        }
        if (formData.supBankCodeOld!==formData.supBankCode){
            isChange = true;
        }
        if (formData.bankInterCodeOld!==formData.bankInterCode){
            isChange = true;
        }
        if (formData.bankAddressOld!==formData.bankAddress){
            isChange = true;
        }
        if (!isChange){
            layer.confirm('银行信息无变化，提交后，付款申请单会推送共享，请确认是否提交？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                layer.closeAll()
                saveOrUpdateAjax(formData,call)
                return false;
            }, function(){
                layer.closeAll()
                return false;
            });
        }else {
            layer.confirm('提交后，付款申请单会推送共享，请确认是否提交？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                layer.closeAll()
                saveOrUpdateAjax(formData,call)
                return false;
            }, function(){
                layer.closeAll()
                return false;
            });
        }

    }else{
        layer.msg("红色框为必输项，请填写！");
    }
}


function saveOrUpdateAjax(formData,call) {
    var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
    $.ajax({
        url: g.ctx + 'pc/finance/payment/change/rest/saveOrUpdate',
        method: 'post',
        data: JSON.stringify(formData),
        contentType: 'application/json',
        success: function (data) {
            call();
        },
        complete: function(data) {
            top.layer.close(loading);
        },
        error: function (data) {
            layer.alert(JSON.parse(data.responseText).message);
        }
    });
}