var el = {
    form : '#form',
    useState: '#useState',
}
$(function () {
    //加载公司数据
    $('#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').textbox('setValue',data.companyName);
        }
    });
    // 初始化使用状态下拉框
    $('#useState').combobox({
        valueField: 'value',
        textField: 'label',
        value: -1,
        data: [
            {label: '', value: -1},
            {label: '使用', value: 0},
            {label: '未使用', value: 1}
        ],
        required: false,
        editable: false,
        panelHeight: 'auto',
    });
});
function searchSupplier(type){
    var userOrgyDialog = openDialog('userDeductionDialog', g.ctx + 'pc/supplier/supplierSelect?single=1', {
        title: "选择供应商",
        width: '800px',
        height: '500px',
        cache: false,
        modal: true,
        border: false,
        draggable: true,
        shadow: false,
        onClose: function () {
            userOrgyDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.confirm,
                handler: function () {
                    topWin.frames['userDeductionDialog'].chooseRow(function (cbox) {console.log(cbox)
                        if(type === 1){
                            // $("#supplierCode").val(cbox.supplierCode);
                            $('#supplierName').textbox('setValue', cbox.supplierName)
                            $('#supplierCode').textbox('setValue', cbox.supplierCode)
                        }
                        if(type === 2){
                            // $("#faSupplier").val(cbox.supplierCode);
                            $('#faSupplierName').textbox('setValue', cbox.supplierName)
                            $('#faSupplier').textbox('setValue', cbox.supplierCode)
                        }
                        userOrgyDialog.dialog('close');
                    });
                }
            },
            {
                text: itablei8n.close,
                handler: function () {
                    userOrgyDialog.dialog('close');
                }
            }
        ]
    });
}
function supplierSearch1(){
    searchSupplier(1);
}
function supplierSearch2(){
    searchSupplier(2);
}
function saveOrUpdate(call) {
    if($(el.form).form('validate')){
        var formData = $(el.form).serializeJson();
        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        $.ajax({
            url: g.ctx + 'pc/supplier/supRelationBid/rest/saveOrUpdate',
            method: 'post',
            data: 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("红色框为必输项，请填写！");
    }
}