/**
 * 发票管理
 */
var el = {
    id: '#id',
    companyCode: '#companyCode',
    invoiceTitle: '#invoiceTitle',
    invoiceNo: '#invoiceNo',
    invoiceTel: '#invoiceTel',
    invoiceAddress: '#invoiceAddress',
    openingBank: '#openingBank',
    bankAccount: '#bankAccount',
    bankTel: '#bankTel',
    bankAddress: '#bankAddress'
};


/**
 * 文档就绪
 */
$(function () {

    $(el.companyCode).combobox({
        valueField: 'code',
        textField: 'codeWithDesc',
        limitToList: 'true',
        method: 'POST',
        prompt: '--- 请选择 ---',
        url: g.ctx + 'pc/sys/company/rest/listAll',
        onSelect: function (data) {
            console.log(data)
            $(el.invoiceTitle).textbox('setValue', data.companyName);
        }
    });

});


/**
 * 数据保存
 *
 * @param callback 回调函数
 */
function dataSave(callback) {
    var checkMsg = verification();
    if (checkMsg){
        topWin.layer.alert(checkMsg, {icon: 0});
        return false;
    }

    var params = {};
    params.id = $(el.id).val();
    params.companyCode = $(el.companyCode).combobox('getValue');
    params.invoiceTitle = $(el.invoiceTitle).textbox('getValue');
    params.invoiceNo = $(el.invoiceNo).textbox('getValue');
    params.invoiceTel = $(el.invoiceTel).textbox('getValue');
    params.invoiceAddress = $(el.invoiceAddress).textbox('getValue');
    params.openingBank = $(el.openingBank).textbox('getValue');
    params.bankAccount = $(el.bankAccount).textbox('getValue');
    params.bankTel = $(el.bankTel).textbox('getValue');
    params.bankAddress = $(el.bankAddress).textbox('getValue');

    topWin.layer.confirm('您确定要保存吗?', {icon: 3}, function (index) {
        topWin.layer.close(index);

        var loading = topWin.layer.msg('数据保存中, 请耐心等待......', {icon: 16, time: 0, shade: 0.1});
        $.ajax({
            url: '/pc/mall/invoice/rest/data/save',
            type: 'POST',
            data: JSON.stringify(params),
            contentType: 'application/json',
            success: function () {
                topWin.layer.close(loading);
                callback();
            },
            error: function (data) {
                topWin.layer.close(loading);
                topWin.layer.alert(data.responseJSON, {icon: 2});
            }
        });
    });
}


/**
 * 表单数据校验
 *
 * @return {string} 结果
 */
function verification() {
    if ($(el.invoiceTitle).textbox('getValue') === '') {
        return '抬头不能为空';
    }
    if ($(el.invoiceNo).textbox('getValue') === '') {
        return '纳税人识别号不能为空';
    }
    if ($(el.invoiceTel).textbox('getValue') === '') {
        return '营业电话不能为空';
    }
    if ($(el.openingBank).textbox('getValue') === '') {
        return '开户行不能为空';
    }
    if ($(el.bankAccount).textbox('getValue') === '') {
        return '开户行账号不能为空';
    }else{
        var reg = /^(\d{1,50})$/;
        if(!reg.test($(el.bankAccount).textbox('getValue'))){
            return '请输入正确的开户行账号!';
        }
    }
    return '';
}

