
let el = {
    tb: '#tb',
    id: '#id',
    requiredFlag: '#requiredFlag',
    form: '#form',
}

$(function () {
    initPageSelect();
    loadGrid();
});

/**
 * 初始化页面下拉框数据
 */
function initPageSelect() {
    // 初始化出货检验报告是否必填下拉框
    $(el.requiredFlag).combobox({
        url: g.ctx + 'pc/sys/dict/getDicByType?type=required_flag',
        panelHeight: 'auto',
        method: 'GET',
        valueField: 'code',
        textField: 'name',
        prompt: '-- 请选择 --',
        editable: false
    });
}

function loadGrid() {
    $('#tb').datagrid({
        url: g.ctx + "pc/sys/supplierOrderFileSetting/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: 'requiredFlag',
                title: '出货检验报告是否必填',
                width: 140,
                align: 'center',
                exportEnum:[
                    {code:"0",codeWithDesc:'否'},
                    {code:"1",codeWithDesc:'是'},
                ],
                formatter: function (value) {
                    if (value === "0") {
                        return "否"
                    } else if (value === "1") {
                        return "是"
                    }
                }
            },
            {
                field: 'updateName',
                title: '最近修改人',
                width: 180
            },
            {
                field: 'updateTime',
                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/supplierOrderFileSetting/rest/export','tb',{supplierOrderFileSettingId:$(el.id).val()});
            }
        }],
        onLoadSuccess: function () {
            displayButtonByUserRole("emailConfig");
            $(el.tb).datagrid('clearSelections');
        },

    });
}

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

    const supplierEmailDialog = openDialog('supplierOrderFileSetting', g.ctx + 'pc/sys/supplierOrderFileSetting/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['supplierOrderFileSetting'].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() {
    const cbox = $('#tb').datagrid('getSelected');
    if (cbox === null || cbox === undefined) {
        layer.msg("请选中一条记录");
        return;
    }

    const supplierEmail = cbox.id;

    parent.layer.confirm('是否删除？', function (index) {
        const loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        const url = g.ctx + 'pc/sys/supplierOrderFileSetting/rest/remove';
        $.ajax({
            url: url,
            method: "post",
            data: {id: supplierEmail},
            success: function () {
                parent.layer.close(index);
                $(el.tb).datagrid('reload');
            },
            complete: function() {
                top.layer.close(loading);
            },
            error: function (data) {
                layer.close(index);
                layer.alert(JSON.parse(data.responseText).message);
            }
        });
    });
}

/**
 * 导入
 */
function importDicItem() {
    const a = openImportExcelDialog("static/excel/supplier_order_file_setting_list.xlsx", '出货检验报告必填设置导入', function (file) {
        let formData = new FormData();
        formData.append("file", file);
        //取到fileId然后进行数据导入操作
        $.ajax({
            url: g.ctx + 'pc/sys/supplierOrderFileSetting/rest/import',
            method: "POST",
            contentType: false,
            processData: false,
            data: formData,
            success: function () {
                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});
            }
        });
    });
}