var el = {
    form : '#form',
    useState: '#useState',
    attachment: '#attachment',
    useTimes: '#useTimes',
}
$(function () {
    //加载公司数据
    $('#companyCode').combobox({
        valueField: 'companyCode',
        textField: 'dataPrimary',
        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({
        url: g.ctx + 'pc/sys/dict/getDicByType?type=moldform_status',
        panelHeight: 100,
        method: 'GET',
        valueField: 'code',
        textField: 'name',
        prompt: '-- 请选择 --',
        editable: false,
    });
    // $('#useState').combobox({
    //     valueField: 'value',
    //     textField: 'label',
    //     data: [
    //         {label: '未使用', value: 1},
    //         {label: '使用', value: 0},
    //
    //     ],
    //     editable: false,
    //     prompt: '-- 请选择 --',
    //     panelHeight: 'auto'
    // });

});
function saveOrUpdate(call) {
    if($(el.form).form('validate')){
        // debugger
        // if (Number.isInteger(el.useTimes)) {

        var formData = $(el.form).serializeJson();
        console.log(JSON.stringify(formData))
        //var attachData = $(el.attachment).data('fileIds');
        var attachData = $(el.attachment).attr('data-fileIds')
        formData.attachment = attachData
        //console.log(JSON.stringify(formData))
        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        $.ajax({
            url: g.ctx + 'pc/sys/mold/moldForm/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("红色框为必输项，请填写！");
    }
}

// function delFactory() {
//     var cbox = $('#tb').datagrid('getSelected');
//     if (cbox === null || cbox === undefined) {
//         layer.msg("请选中一条记录");
//         return;
//     }
//
//     var moldForm = cbox.moldCode;
//
//     parent.layer.confirm('是否删除？', function (index) {
//         var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
//         var url = g.ctx + 'pc/sys/mold/moldForm/rest/remove';
//         $.ajax({
//             url: url,
//             method: "post",
//             data: {moldCode: moldForm},
//             success: function (data) {
//                 parent.layer.close(index);
//                 $(el.tb).datagrid('reload');
//             },
//             complete: function(data) {
//                 top.layer.close(loading);
//             },
//             error: function (data) {
//                 layer.close(index);
//                 layer.alert(JSON.parse(data.responseText).message);
//             }
//         });
//     });
// }

function searchSupplier(){
    var userOrgyDialog = openDialog('userOrgyDialog', 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['userOrgyDialog'].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');
                }
            }
        ]
    });
}