/**
 * 供应商AVL清单列表
 * src/main/resources/view/supplier/avl/index.ftl
 */
var el = {
    id: '#id',
    table: '#tb',
    searchForm: '#searchForm',
    isSupplier: "#isSupplier",
    materialNameOrCode: '#materialNameOrCode',
    supplierlNameOrCode: '#supplierlNameOrCode',
    productName: '#productName',
    materialPurchaseGroup: '#materialPurchaseGroup',
    mpn: '#mpn',
    msl: '#msl',
    eccn: '#eccn',
    moq: '#moq',
    mpq: '#mpq',
    cif: '#cif',
    mslName: '#mslName',
    bodyHeight: document.documentElement.clientHeight
}, isSupplier = $(el.isSupplier).val() === "true";


$(function () {
    loadData();

});


/**
 * 初始化页面数据
 */
function loadData() {
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight, 37, 40);
    var toolbars = ["-",
        {
            hide: false,
            text: buttonI18n.add,
            iconCls: 'icon-add',
            id: 'btn_add',
            handler: function () {
                addAndUpdate("", 1);
            }
        }, "-",
        {
            hide: false,
            text: '全量新增',
            iconCls: 'icon-add',
            id: 'btn_add_all',
            handler: function () {
                addAndUpdateAll("", 1);
            }
        }, "-",
        {
            hide: false,
            text: buttonI18n.edit,
            id: 'btn_edit',
            iconCls: 'icon-edit',
            handler: function () {
                var rows = $(el.table).datagrid('getSelections');
                if (rows.length !== 1) {
                    layer.msg("请选择一条记录!");
                    return
                }
                addAndUpdate(rows[0].id, 2);
            }
        }, "-",
        {
            hide: false,
            text: '全量编辑',
            id: 'btn_edit_all',
            iconCls: 'icon-edit',
            handler: function () {
                var rows = $(el.table).datagrid('getSelections');
                if (rows.length !== 1) {
                    layer.msg("请选择一条记录!");
                    return
                }
                addAndUpdateAll(rows[0].id, 2);
            }
        }, "-",
        {
            hide: false,
            text: buttonI18n.check,
            id: 'btn_lookup',
            iconCls: 'icon-book',
            handler: function () {
                var rows = $(el.table).datagrid('getSelections');
                if (rows.length !== 1) {
                    layer.msg("请选择一条记录!");
                    return
                }
                examie(rows[0].id, 3);
            }
        }, "-",
        {
            hide: false,
            text: 'MES同步',
            id: 'btn_mes',
            iconCls: 'icon-reload',
            handler: function () {
                var rows = $(el.table).datagrid('getSelections');
                if (rows.length <= 0) {
                    layer.msg("请选择一条记录!");
                    return
                }
                synToMesDatas();
            }
        },
        "-",
        {
            text: '导入',
            id: 'btn-import',
            iconCls: 'icon-edit',
            handler: function () {
                importDicItem();
            }
        },
        "-",
        {
            hide: false,
            text: '删除',
            id: 'btn_delete',
            iconCls: 'icon-remove',
            handler: function () {
                deleteDatas();
            }
        },
        "-", {
            text: '导出',
            iconCls: 'icon-download',
            id: 'btn-export',
            handler: function () {
                exportAccData();
            }
        },
    ];
    var columns = [[
        {
            field: 'ck',
            checkbox: true
        },
        {field: 'id', hidden: true},
        {
            field: 'materialCode',
            title: "物料编码",
            align: 'left',
            width: '8%',
            sortable: true
        },
        {
            field: 'materialName',
            title: "物料名称",
            align: 'left',
            width: '15%',
            sortable: true
        },

        {
            field: "supplierCode",
            title: "供应商编码",
            align: 'left',
            width: '10%',
            sortable: true
        },
        {
            field: "supplierName",
            title: "供应商名称",
            align: 'left',
            width: '13%',
            sortable: true
        },
        {
            field: "productName",
            title: "制造商中文名",
            align: 'left',
            width: '8%',
            sortable: true
        },
        {
            field: "productNameEn",
            title: "制造商英文名",
            align: 'left',
            width: '8%',
            sortable: true
        },
        {
            field: "countryName",
            title: "国系",
            align: 'left',
            width: '4%',
            sortable: true
        },
        {
            field: "materialPurchaseGroup",
            title: "采购组编码",
            align: 'left',
            width: '5%',
            sortable: true
        },
        {
            field: "eccn",
            title: "ECCN",
            align: 'left',
            width: '5%',
            sortable: true
        },
        {
            field: "mpn",
            title: "MPN(厂家型号)",
            align: 'left',
            width: '7%',
            sortable: true
        },
        {
            field: "mslName",
            title: "MSL(湿敏等级)",
            align: 'left',
            width: '7%',
            sortable: true
        },
        {
            field: "moq",
            title: "MOQ",
            align: 'left',
            width: '5%',
            sortable: true
        },
        {
            field: "mpq",
            title: "MPQ",
            align: 'left',
            width: '5%',
            sortable: true
        },
        {
            field: "cif",
            title: "货期(L/T)",
            align: 'left',
            width: '4%',
            sortable: true
        },
        {
            field: "createTime",
            title: "创建时间",
            align: 'left',
            width: '7%',
            sortable: true
        },
        {
            field: "synToMes",
            title: "MES同步状态",
            align: 'left',
            width: '5%',
            sortable: true
        }
    ]];
    $(el.table).datagrid({
        iconCls: 'icon-list',
        height: el.bodyHeight - searchHeight,
        singleSelect: false,
        method: "GET",
        url: g.ctx + 'pc/supplier/avl/rest/list',
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        pagination: true,
        loadMsg: commonMsg.loadData,
        rownumbers: true,
        fitColumns: false,
        page: 1,
        queryParams: {},
        columns: columns,
        toolbar: toolbars,
        onBeforeLoad: function (param) {
            displayButtonByUserRole('supplier_avl');
        }
    });
}

/**
 * 导出
 */
function exportAccData() {
    openExportDialog('pc/supplier/avl/rest/exportExcel', "tb", searchParams())
}

/**
 * 导入
 */
function importDicItem() {
    var a = openImportExcelDialog("static/excel/supplier_avl_list.xlsx", '供应商AVL清单导入', function (file) {
        let formData = new FormData();
        formData.append("file", file);
        //formData.append("dictTypeId", $('#id').val())
        //取到fileId然后进行数据导入操作
        $.ajax({
            url: g.ctx + 'pc/supplier/avl/rest/import',
            method: "POST",
            contentType: false,
            processData: false,
            data: formData,
            success: function (data) {
                topWin.layer.closeAll('loading');
                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});
            }
        });
    });
}


/**
 * 新增编辑页面
 * @param id  主键id
 * @param type 页面类型 1：新增 2：编辑
 */
function addAndUpdate(id, type) {
    let btns = [
        {
            text: buttonI18n.close,
            iconCls: 'icon-cancel',
            handler: function () {
                addOrEditDialog.dialog('close');
            }
        }
    ];
    if (isSupplier) {
        btns.unshift(
            {
                text: '保存',
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['addOrEditDialog'].saveDemo(1, function (data) {
                        addOrEditDialog.dialog('close');
                        $(el.table).datagrid("reload");
                    });
                }
            }
            )
    } else {

        btns.unshift(
            {
            text: '保存',
            iconCls: 'icon-save',
            handler: function () {
                topWin.frames['addOrEditDialog'].saveDemo(1, function (data) {
                    addOrEditDialog.dialog('close');
                    $(el.table).datagrid("reload");
                });
            }
        }
        /*,
            {
            text: buttonI18n.submit,
            iconCls: 'icon-save',
            handler: function () {
                topWin.frames['addOrEditDialog'].saveDemo(2, function (data) {
                    addOrEditDialog.dialog('close');
                    $(el.table).datagrid("reload");
                });
            }
        }*/
        )

    }

    var title_str = "新增AVL物料"
    if (type == 2) {
        title_str = "编辑AVL物料";
    }


    var addOrEditDialog = openDialog("addOrEditDialog", g.ctx + "pc/supplier/avl/page/add?id=" + id + "&type=" + type, {
        title: title_str,
        width: '80%',
        height: '65%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            addOrEditDialog.dialog('destroy');
        },
        buttons: btns
    })
}

/**
 * 全量新增编辑页面
 * @param id  主键id
 * @param type 页面类型 1：新增 2：编辑
 */
function addAndUpdateAll(id, type) {
    let btns = [
        {
            text: buttonI18n.close,
            iconCls: 'icon-cancel',
            handler: function () {
                addOrEditDialog.dialog('close');
            }
        }
    ];
    if (isSupplier) {
        btns.unshift(
            {
                text: '保存',
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['addOrEditDialog'].saveDemo(1, function (data) {
                        addOrEditDialog.dialog('close');
                        $(el.table).datagrid("reload");
                    });
                }
            }
        )
    } else {

        btns.unshift(
            {
                text: '保存',
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['addOrEditDialog'].saveDemo(1, function (data) {
                        addOrEditDialog.dialog('close');
                        $(el.table).datagrid("reload");
                    });
                }
            }
            /*,
                {
                text: buttonI18n.submit,
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['addOrEditDialog'].saveDemo(2, function (data) {
                        addOrEditDialog.dialog('close');
                        $(el.table).datagrid("reload");
                    });
                }
            }*/
        )

    }

    var title_str = "全量新增AVL物料"
    if (type == 2) {
        title_str = "全量编辑AVL物料";
    }


    var addOrEditDialog = openDialog("addOrEditDialog", g.ctx + "pc/supplier/avl/page/addAll?id=" + id + "&type=" + type, {
        title: title_str,
        width: '80%',
        height: '65%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            addOrEditDialog.dialog('destroy');
        },
        buttons: btns
    })
}

/**
 * 审核 查看页面页面
 * @param id 样品的id
 * @param type 1审核 0查看
 */
function examie(id, type) {
    let btns = [{
        text: buttonI18n.close,
        iconCls: 'icon-cancel',
        handler: function () {
            examineDialog.dialog('close');
        }
    }]

    var examineDialog = openDialog("examineDialog", g.ctx + "pc/supplier/avl/page/detail?id=" + id + "&type=" + type, {
        title: type === 1 ? '审核页面' : '查看页面',
        width: '80%',
        height: '65%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            examineDialog.dialog('destroy');
        },
        buttons: btns
    })
}

/**
 * 批量删除
 */
function deleteDatas() {
    var notDel = [];
    var delIds = [];
    var rows = $(el.table).datagrid('getSelections');
    for (var i = 0; i < rows.length; i++) {
        if (rows[i].state !== 1 && rows[i].state !== 2) {
            //notDel.push((i + 1));
        }
        delIds.push(rows[i].id);
    }

    if (notDel.length > 0) {
        topWin.layer.msg('第' + notDel.join(";") + '行不能删除');
        return;
    }

    if (delIds.length <= 0) {
        topWin.layer.msg('请选择要删除的记录');
        return;
    }

    topWin.layer.confirm('是否确认删除该记录？', {icon: 3}, function (iid) {
        $.ajax({
            method: 'POST',
            url: g.ctx + 'pc/supplier/avl/rest/remove',
            data: {
                ids: delIds.join(",")
            },
            success: function (data) {
                topWin.layer.msg("操作成功", {icon: 1});
                $(el.table).datagrid('reload');
                topWin.layer.close(iid);
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
                topWin.layer.close(iid);
            }
        })
    })
}


/**
 * 批量同步至Mes
 */
function synToMesDatas() {
    var delIds = [];
    var rows = $(el.table).datagrid('getSelections');
    for (var i = 0; i < rows.length; i++) {
        if (rows[i].state !== 1 && rows[i].state !== 2) {
            //notDel.push((i + 1));
        }
        delIds.push(rows[i].id);
    }

    if (delIds.length <= 0) {
        topWin.layer.msg('请选择要同步的记录');
        return;
    }

    topWin.layer.confirm('是否确认同步该记录？',{icon: 3}, function (iid) {
        $.ajax({
            method: 'POST',
            url: g.ctx + 'pc/supplier/avl/rest/pushToMes',
            data: {
                ids: delIds.join(",")
            },
            success: function (data) {
                topWin.layer.msg("操作成功", {icon: 1});
                $(el.table).datagrid('reload');
                topWin.layer.close(iid);
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
                topWin.layer.close(iid);
            }
        })
    })
}








