var el = {
    form : '#form',
    useState: '#useState',
    attachment: '#attachment',
    shareFlag: '#shareFlag',
    deductionType: '#deductionType',
    taxCode: '#taxCode',
    poCodeAndItemNo: '#poCodeAndItemNo',
    qualityNotice: '#qualityNotice',
    claimOrder: '#claimOrder',
    poItemNo: '#poItemNo',
    emptyTd: '.empty-td',
    poData: '.po_data',
    qualityNoticeData: '.quality_notice_data',
    claimOrderData1: '.claim_order_data1',
    poCodeAndItemNo: '#poCodeAndItemNo',
    poCode: '#poCode',
    qualityNoticeCode: '#qualityNoticeCode',
    claimOrderCode: '#claimOrderCode',
    deductionAmount: '#deductionAmount',
    dntAmount: '#dntAmount',
    taxValue: '#taxValue',
    currency: '#currency',
    supReplyAttachment: '#supReplyAttachment',
    suppllierName: '#suppllierName',
}


/**
 * 初始化页面特殊输入框
 */
$(function () {
    if (window.localStorage) {
        localStorage.userName =$('#userName').val();
    }
    var deductionType = $(el.deductionType).combobox('getValue');
    showHiddenRow(deductionType);


    // 扣款类型关联框
    $(el.poCodeAndItemNo).textbox({
        tipPosition: 'bottom',
        prompt: '请输入采购订单数据',
        required: false,
        width: 200,
    });
    $(el.qualityNotice).textbox({
        tipPosition: 'bottom',
        prompt: '请输入质量通知单数据',
        required: false,
        width: 200,
    });
    $(el.claimOrder).textbox({
        tipPosition: 'bottom',
        prompt: '请输入索赔单数据',
        required: false,
        width: 200,
    });
    // 初始化是否分摊下拉框
    $(el.shareFlag).combobox({
        valueField: 'value',
        textField: 'label',
        data: [
            {label: '需要', value: 1},
            {label: '不需要', value: 0},

        ],
        editable: false,
        prompt: '-- 请选择 --',
        panelHeight: 'auto'
    });
    // 初始化扣款类型下拉框
    $(el.deductionType).combobox({
        url: g.ctx + 'pc/sys/dict/getDicByType?type=back_type',
        panelHeight: 260,
        method: 'GET',
        valueField: 'code',
        textField: 'name',
        prompt: '-- 请选择 --',
        editable: false,
        onSelect: function (data) {
            showHiddenRow(data.code);
        }
    });
    // 初始化公司下拉框
    $('#companyCode').combobox({
        valueField: 'code',
        textField: 'codeWithDesc',
        url: g.ctx + 'pc/sys/company/rest/listAll',
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
        panelWidth: 300,
        onSelect:function (data) {
            $('#companyName').val(data.companyName);
        }
    });
    // 初始化採購組织下拉框
    $('#purchaseOrg').combobox({
        valueField: 'code',
        textField: 'codeWithDesc',
        url: g.ctx + 'pc/sys/purchaseOrg/rest/listAll',
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
        panelWidth: 300,
        // onSelect:function (data) {
        //     $('#companyName').textbox('setValue',data.companyName);
        // }
    });
    // 初始化採購組织下拉框
    $('#purchaseGroup').combobox({
        valueField: 'code',
        textField: 'name',
        url: g.ctx + 'pc/sys/purchaseGroup/rest/listAll',
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
        panelWidth: 300,
        // onSelect:function (data) {
        //     $('#companyName').textbox('setValue',data.companyName);
        // }
    });
    // 初始化工厂下拉框
    $('#factory').combobox({
        valueField: 'code',
        textField: 'codeWithDesc',
        url: g.ctx + 'pc/sys/factory/rest/listAll',
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
        panelWidth: 300,
        // onSelect:function (data) {
        //     $('#companyName').textbox('setValue',data.companyName);
        // }
    });
    // 初始化币种下拉框
    $(el.currency).combobox({
        url: g.ctx + 'pc/sys/dict/getDicByType?type=currency',
        panelHeight: 260,
        method: 'GET',
        valueField: 'code',
        textField: 'name',
        prompt: '-- 请选择 --',
        editable: false
    });
    // 初始化价格单位下拉框
    $(el.priceUnit).combobox({
        url: g.ctx + 'pc/sys/dict/getDicByType?type=price_unit_id',
        panelHeight: 260,
        method: 'GET',
        valueField: 'code',
        textField: 'name',
        prompt: '-- 请选择 --',
        editable: false
    });
    // 初始化税率下拉框
    // $(el.taxCode).combobox({
    //     url: g.ctx + 'pc/sys/dict/getDicByType?type=tax',
    //     panelHeight: 260,
    //     method: 'GET',
    //     valueField: 'code',
    //     textField: 'name',
    //     prompt: '-- 请选择 --',
    //     editable: false
    // });
    $('#taxCode').combobox({
        valueField: 'taxCode',
        textField: 'taxName',
        url: g.ctx + 'pc/sys/tax/rest/listAll',
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
        panelWidth: 300,
        tipPosition: 'bottom',
        // onSelect: function (data) {
        //     var taxValue = data.taxName.match(/\d+/g)[0];
        //     $(el.taxValue).val(taxValue);
        //
        //     var deductionAmount = $(el.deductionAmount).numberbox('getValue');
        //     if(deductionAmount){
        //         var dntAmount = (Number(deductionAmount) / (Number(taxValue) / 100 + 1)).toFixed(2);
        //         $(el.dntAmount).numberbox('setValue', dntAmount);
        //     }
        // }
    });
    // 初始化採購組下拉框
    $('#deductionExecutionProcurementName').combobox({
        valueField: 'code',
        textField: 'codeWithDesc',
        url: g.ctx + 'pc/sys/purchaseGroup/rest/listAll',
        editable: false,
        panelMaxHeight:180,
        prompt: '--- 请选择 ---',
        // panelHeight: 'auto',
        panelWidth: 300,
        // onSelect:function (data) {
        //     $('#companyName').textbox('setValue',data.companyName);
        // }
    });
    // 含税扣款金额变更
    // $(el.deductionAmount).numberbox({
    //     onChange: function (value, oldValue) {
    //         var taxValue = $(el.taxValue).val();
    //         if(taxValue){
    //             var dntAmount = (Number(value) / (Number(taxValue) / 100 + 1)).toFixed(2);
    //             $(el.dntAmount).numberbox('setValue', dntAmount);
    //         }
    //     }
    // });
});

/**
 * @param call
 * 编辑和保存
 */
function saveOrUpdate(call) {
    if($(el.form).form('validate')){
        var formData = $(el.form).serializeJson();
        var idValue = formData.id;
        var flag = formData.flag;
        console.log(88888888888888888)
        console.log(idValue)
        var attachData = $(el.attachment).attr('data-fileIds')
        formData.attachment = attachData
        var supAttachData = $(el.supReplyAttachment).attr('data-fileIds')
        console.log(888888888888888)
        console.log(supAttachData)
        formData.supReplyAttachment = supAttachData
        if(formData.supReplyAttachment == null || formData.supReplyAttachment == ''){
            layer.msg("请上传供应商回传附件");
            return;
        }
        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        $.ajax({
            contentType: 'application/json',
            url: g.ctx + 'pc/finance/deduction/rest/saveOrUpdate?flag' + flag,
            method: 'post',
            data: JSON.stringify(formData),
            success: function (data) {
                // var url = g.ctx + 'pc/finance/deduction/rest/submitOa?id=' + idValue;
                // $.ajax({
                //     url: url,
                //     method: "post",
                //     data: JSON.stringify(formData),
                //     success: function (data) {
                //         // 处理成功后的逻辑
                //         $(el.tb).datagrid('reload');
                //     },
                //     complete: function(data) {
                //         top.layer.close(loading);
                //     },
                //     error: function (data) {
                //         layer.alert(JSON.parse(data.responseText).message);
                //     }
                // });
                call();
            },
            complete: function(data) {
                top.layer.close(loading);
            },
            error: function (data) {
                layer.alert(JSON.parse(data.responseText).message);
            }
        });
    }else{
        layer.msg("红色框为必输项，请填写！");
    }
}
// function saveOrUpdate(call) {
//     if($(el.form).form('validate')){
//         var formData = $(el.form).serializeJson();
//         var attachData = $(el.attachment).attr('data-fileIds')
//         formData.attachment = attachData
//         var supAttachData = $(el.supReplyAttachment).attr('data-fileIds')
//         formData.supReplyAttachment = supAttachData
//         var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
//         $.ajax({
//             contentType: 'application/json',
//             url: g.ctx + 'pc/finance/deduction/rest/saveOrUpdate',
//             method: 'post',
//             data: JSON.stringify(formData),
//             success: function (data) {
//                 call();
//             },
//             complete: function(data) {
//                 top.layer.close(loading);
//             },
//             error: function (data) {
//                 layer.alert(JSON.parse(data.responseText).message);
//             }
//         });
//     }else{
//         layer.msg("红色框为必输项，请填写！");
//     }
// }
/**
 * 选择用户
 */
function userSearch (type) {
    var userDialogElement = openDialog("userDialog", g.ctx + "pc/sys/user/index?select=1&single=0", {
        title: "用户选择",
        width: '80%',
        height: '80%',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            userDialogElement.dialog('destroy');
        },
        buttons: [{
            text: itablei8n.save,
            handler: function () {
                topWin.frames['userDialog'].chooseRow(function (rows) {
                    //数组得用rows[0]
                    if(type===1){
                        //应付会计
                        $('#apAccountantName').textbox('setValue', rows[0].username);
                        $('#apAccountant').val(rows[0].id);
                    }else if(type===2){
                        //扣款执行采购
                        $('#deductionExecutionProcurementName').textbox('setValue', rows[0].username);
                        $('#deductionExecutionProcurement').val(rows[0].id);
                    }
                    userDialogElement.dialog('close');
                });
            }
        }, {
            text: '关闭', iconCls: 'icon-cancel', handler: function () {
                userDialogElement.dialog('close');
            }
        }]
    })
}
function userSearch1 () {
    userSearch(1)
}
function userSearch2 () {
    userSearch(2)
}
/**
 * 选择不同扣款类型 隐藏显示不同输入框
 *
 * @param deductionType 扣款类型 1:质量扣款 2:延期扣款 3:索赔扣款 4:其他
 */
function showHiddenRow(deductionType) {
    if(1 === Number(deductionType) || 2 === Number(deductionType) || 3 === Number(deductionType) || 4 === Number(deductionType)){
        $(el.emptyTd).hide();
    }

    var poData = $(el.poData), qualityNoticeData = $(el.qualityNoticeData), claimOrderData1 = $(el.claimOrderData1);
    if (Number(deductionType) === 1) {
        $(el.poCode).val('');
        $(el.poItemNo).val('');

        qualityNoticeData.css('display', 'table-cell');
        poData.css('display', 'none');
        claimOrderData1.css('display', 'none');
    }
    if (Number(deductionType) === 2) {
        $(el.qualityNoticeCode).val('');

        poData.css('display', 'table-cell');
        qualityNoticeData.css('display', 'none');
        claimOrderData1.css('display', 'none');
    }
    if (Number(deductionType) === 3) {
        $(el.claimOrderCode).val('');
        claimOrderData1.css('display', 'table-cell');
        poData.css('display', 'none');
        qualityNoticeData.css('display', 'none');
    }
    if (Number(deductionType) === 4) {
        $(el.poCode).val('');
        $(el.poItemNo).val('');
        $(el.qualityNoticeCode).val('');
        $(el.claimOrderCode).val('');
        poData.css('display', 'none');
        qualityNoticeData.css('display', 'none');
        claimOrderData1.css('display', 'none');
        $(el.emptyTd).show();
    }
}
function searchSupplier(){
    var userOrgyDialog = openDialog('userDeductionDialog', g.ctx + 'pc/supplier/supplierSelect?single=1', {
        title: "选择供应商",
        width: '800px',
        height: '500px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            userOrgyDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.confirm,
                handler: function () {
                    topWin.frames['userDeductionDialog'].chooseRow(function (cbox) {console.log(cbox)
                        $("#supplierId").val(cbox.id);
                        $("#supplierCode").val(cbox.supplierCode);
                        $('#supplierName').textbox('setValue', cbox.supplierName)
                        userOrgyDialog.dialog('close');
                    });
                }
            },
            {
                text: itablei8n.close,
                handler: function () {
                    userOrgyDialog.dialog('close');
                }
            }
        ]
    });
}