
let el = {
    tb: '#tb',
    id: '#id',
    supplierType: '#supplierType',
    form: '#form',

}

$(function () {
    initDict('email_config')
    initPageSelect();
    loadGrid();
});



/**
 * 初始化页面下拉框数据
 */
function initPageSelect() {
    // 初始化供应商邮箱类型下拉框
    $(el.supplierType).combobox({
        url: g.ctx + 'pc/sys/dict/getDicByType?type=email_config',
        panelHeight: 260,
        method: 'GET',
        valueField: 'code',
        textField: 'name',
        prompt: '-- 请选择 --',
        editable: false
    });
}

function loadGrid() {
    $('#tb').datagrid({
        url: g.ctx + "pc/sys/supplierEmail/rest/list",
        iconCls: 'icon-list',
        title: '',
        height: gridheight,
        width: gridwidth,
        singleSelect: true,
        pagination: true,
        idField: 'id',
        autoHeight: true,
        loadMsg: "数据加载中......",
        rownumbers: true,
        fitColumns: false,
        remoteSort: true,
        queryParams: {
            supplierEmailId:$(el.id).val(),
        },
        columns: [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'supplierCode',
                title: '供应商编码',
                width: 120
            },
            {
                field: 'supplierName',
                title: '供应商名称',
                width: 320
            },
            {
                field: 'puserName',
                title: '采购用户',
                width: 70
            },
            {
                field: 'puserCode',
                title: '采购用户编码',
                width: 90
            },
            // {
            //     field: 'supplierType',
            //     title: '供应商邮箱类型',
            //     width: 120,
            //     exportEnum:getDictList('email_config'),
            //     formatter: function (value) {
            //         return getDicName('email_config',value);
            //     }
            // },
            {
                field: 'email',
                title: '邮箱',
                width: 260
            },
            {
                field: 'sqeName',
                title: 'SQE人员',
                width: 100
            },
            {
                field: 'ceName',
                title: 'CE人员',
                width: 100
            },
            {
                field: 'purchaseCategoryName',
                title: '物料类别',
                width: 150
            },

            {
                field: 'emailType',
                title: '邮箱类型',
                width: 100,
                exportEnum:[
                    {code:"0",codeWithDesc:'内部'},
                    {code:"1",codeWithDesc:'外部'},
                ],
                formatter: function (value) {
                    if (value == "0") {
                        return "内部"
                    } else if (value == "1") {
                        return "外部"
                    }
                }
            },
            // {
            //     field: 'defaultFlag',
            //     title: '是否默认',
            //     width: 100,
            //     exportEnum:[
            //         {code:"0",codeWithDesc:'不默认'},
            //         {code:"1",codeWithDesc:'默认'},
            //     ],
            //     formatter: function (value) {
            //         if (value == 0) {
            //             return "不默认"
            //         } else if (value == 1) {
            //             return "默认"
            //         }
            //     }
            // },
            {
                field: 'createUserName',
                title: '创建人',
                width: 180
            },
            {
                field: 'createTime',
                title: '创建时间',
                width: 140,
                formatter: function (createTime) {
                    if (!createTime)
                        return '';
                    return moment(createTime).format('YYYY-MM-DD HH:mm');
                }

            }
        ]],
        toolbar: ["-", {
            text: '新增',
            id: 'add',
            iconCls: 'icon-add',
            hidden: true,
            handler: function () {
                addOrEdit(0);
            }
        }, "-", {
            text: '编辑',
            id: 'edit',
            iconCls: 'icon-edit',
            handler: function () {
                addOrEdit(1);
            }
        }, "-", {
            text: '删除',
            id: 'delete',
            iconCls: 'icon-remove',
            handler: function () {
                delFactory();
            }
        }, "-", {
            text: '查看',
            id: 'look',
            iconCls: 'icon-edit',
            handler: function () {
                look(1);
            }
        }, "-", {
                id: 'import',
                text: '导入',
                iconCls: 'icon-import',
                handler: function () {
                    importDicItem();
                }
        }, "-", {
            id: 'export',
            text: '导出',
            iconCls: 'icon-undo',
            handler: function () {
                openExportDialog('pc/sys/supplierEmail/rest/export','tb',{supplierEmailId:$(el.id).val()});
            }
        }],
        onLoadSuccess: function () {
            displayButtonByUserRole("emailConfig");
            $(el.tb).datagrid('clearSelections');
        },

    });
}


/**
 *新增编辑
 */
function addOrEdit(flag) {
    if($(el.form).form('validate')){
    var id= '';
    if(flag===1){
        var cbox = $('#tb').datagrid('getSelected');
        if (cbox === null || cbox === undefined) {
            layer.msg("请选中一条记录");
            return;
        }
        id = cbox.id;
    }

    var supplierEmailDialog = openDialog('supplierEmail', g.ctx + 'pc/sys/supplierEmail/detail?id=' + id, {
        title: flag===0?'新增':'编辑',
        width: '60%',
        height: '60%',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            supplierEmailDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.save,
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['supplierEmail'].saveOrUpdate(function () {
                        topWin.layer.msg('数据保存成功', {icon: 1});
                        supplierEmailDialog.dialog('close');
                        $(el.tb).datagrid('reload');
                    });
                }
            },
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    supplierEmailDialog.dialog('close');
                }
            }
        ]
    });
    }else{
        layer.msg("红色框为必输项，请填写！");
    }
}

//删除
function delFactory() {
    var cbox = $('#tb').datagrid('getSelected');
    if (cbox === null || cbox === undefined) {
        layer.msg("请选中一条记录");
        return;
    }

    var supplierEmail = cbox.id;

    parent.layer.confirm('是否删除？', function (index) {
        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        var url = g.ctx + 'pc/sys/supplierEmail/rest/remove';
        $.ajax({
            url: url,
            method: "post",
            data: {id: supplierEmail},
            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 importDicItem() {
    var a = openImportExcelDialog("static/excel/supplier_email_list.xlsx", '供应商邮箱配置导入', function (file) {
        let formData = new FormData();
        formData.append("file", file);
        //formData.append("dictTypeId", $('#id').val())
        //取到fileId然后进行数据导入操作
        $.ajax({
            url: g.ctx + 'pc/sys/supplierEmail/rest/import',
            method: "POST",
            contentType: false,
            processData: false,
            data: formData,
            success: function (data) {
                topWin.layer.alert("导入成功", {icon: 1}, function (idx) {
                    topWin.layer.close(idx);
                    a.dialog('close');
                    $('#tb').datagrid('reload');
                });
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    });
}