/**
 * 供应商送货管控清单列表
 * src/main/resources/view/order/deliveryOrderControl/delivery_order_control_index.ftl
 */
var el = {
    id: '#id',
    table: '#tb',
    searchForm: '#searchForm',
    isSupplier: "#isSupplier",
    materialNameOrCode: '#materialNameOrCode',
    supplierlNameOrCode: '#supplierlNameOrCode',
    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: buttonI18n.edit,
            id: 'btn_edit',
            iconCls: 'icon-edit',
            handler: function () {
                var rows = $(el.table).datagrid('getSelections');
                if (rows.length !== 1) {
                    layer.msg("请选择一条记录!");
                    return
                }
                if (rows[0].deleteFlag === 1) {
                    layer.msg("已删除记录不能编辑!");
                    return
                }
                addAndUpdate(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);
            }
        }, "-",

        // {
        //     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: '20%',
            sortable: true
        },
        {
            field: "supplierCode",
            title: "供应商编码",
            align: 'left',
            width: '8%',
            sortable: true
        },
        {
            field: "supplierName",
            title: "供应商名称",
            align: 'left',
            width: '14%',
            sortable: true
        },
        {
            field: "controlNum",
            title: "管控数量",
            align: 'left',
            width: '6%',
            sortable: true
        },
        {
            field: "applyUser",
            title: "申请人(需求发起人)",
            align: 'left',
            width: '8%'
        },
        {
            field: "manageUser",
            title: "项目/研发负责人",
            align: 'left',
            width: '8%'
        },
        {
            field: "effectiveDate",
            title: "生效日期",
            align: 'left',
            width: '6%'
        },
        {
            field: "createTime",
            title: "创建时间",
            align: 'left',
            width: '8%',
            sortable: true
        },
        {
            field: "createUserName",
            title: "创建人",
            align: 'left',
            width: '8%'
        },
        {
            field: "deleteFlag",
            title: "删除标志",
            align: 'left',
            width: '6%',
            formatter: function (value) {
                console.log(value === 0)
                if (value === 0) {
                    return '未删除';
                } else if (value === 1) {
                    return '已删除(失效)'
                }
                return ''
            }
        }
    ]];
    $(el.table).datagrid({
        iconCls: 'icon-list',
        height: el.bodyHeight - searchHeight,
        singleSelect: false,
        method: "GET",
        url: g.ctx + 'pc/order/deliveryOrderControl/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('delivery_control_manage');
        }
    });
}

/**
 * 导出
 */
// 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 = "新增供应商物料送货单管控"
    if (type == 2) {
        title_str = "编辑供应商物料送货单管控";
    }


    var addOrEditDialog = openDialog("addOrEditDialog", g.ctx + "pc/order/deliveryOrderControl/page/add?id=" + id + "&type=" + type, {
        title: title_str,
        width: '60%',
        height: '60%',
        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/order/deliveryOrderControl/page/detail?id=" + id + "&type=" + type, {
        title: type === 1 ? '审核页面' : '查看页面',
        width: '60%',
        height: '40%',
        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].deleteFlag === 1) {
            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/order/deliveryOrderControl/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);
            }
        })
    })
}








