var el = {
    form : '#form',
    useState: '#useState',
    supplierType: '#supplierType',
}
$(function () {
    // 初始化供应商邮箱类型下拉框
    $(el.supplierType).combobox({
        url: g.ctx + 'pc/sys/dict/getDicByType?type=email_config',
        panelHeight: 260,
        method: 'GET',
        valueField: 'code',
        textField: 'name',
        prompt: '-- 请选择 --',
        editable: false
    });
    // 初始化使用状态下拉框
    $('#emailType').combobox({
        valueField: 'value',
        textField: 'label',
        data: [
            {label: '', value: ''},
            {label: '内部', value: "0"},
            {label: '外部', value: "1"}
        ],
        required: true,
        editable: false,
        panelHeight: 'auto',
    });
});
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/sys/supplierEmail01/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 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").textbox('setValue',cbox.supplierCode);
                        $('#supplierName').textbox('setValue', cbox.supplierName)
                        userOrgyDialog.dialog('close');
                    });
                }
            },
            {
                text: itablei8n.close,
                handler: function () {
                    userOrgyDialog.dialog('close');
                }
            }
        ]
    });
}

/**
 * 选择用户
 */
function userSearch () {
    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) {
                    $('#puserName').textbox('setValue',rows[0].username);
                    $('#puserCode').val(rows[0].accountName);
                    userDialogElement.dialog('close');
                });
            }
        }, {
            text: '关闭', iconCls: 'icon-cancel', handler: function () {
                userDialogElement.dialog('close');
            }
        }]
    })
}



/**
 * 选择用户
 */
function userSearchPlus (type) {
    if(type == 1){
        var roleCodes = "role_sqe";
    }else if(type == 2){
        var roleCodes = "CE";
    }
    var userDialogElement = openDialog("userDialog", g.ctx + "pc/sys/user/index?select=1&single=0&roleCodes=" + roleCodes, {
        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){
                        //SQE
                        $('#sqeName').textbox('setValue', rows[0].username);
                        $('#sqe').val(rows[0].id);
                    }else if(type===2){
                        //CE
                        $('#ceName').textbox('setValue', rows[0].username);
                        $('#ce').val(rows[0].id);
                    }
                    userDialogElement.dialog('close');
                });
            }
        }, {
            text: '关闭', iconCls: 'icon-cancel', handler: function () {
                userDialogElement.dialog('close');
            }
        }]
    })
}
function userSearch1 () {
    userSearchPlus(1)
}
function userSearch2 () {
    userSearchPlus(2)
}