/**
 * 物料样品申请单列表
 * src/main/resources/view/supplier/product/demo/index.ftl
 */
var el = {
    id: '#id',
    table: '#tb',
    searchForm: '#searchForm',
    isSupplier: "#isSupplier",
    code: '#code',
    purchaseCode: '#purchaseCode',
    startTime: '#startTime',
    endTime: "#endTime",
    state: '#state',
    examineState: '#examineState',
    materialNameOrCode: '#materialNameOrCode',
    supplierlNameOrCode: '#supplierlNameOrCode',
    bodyHeight: document.documentElement.clientHeight
}, isSupplier = $(el.isSupplier).val() === "true";

$(function () {
    $(el.purchaseCode).combobox({
        valueField: 'purchaseGroupCode',
        textField: 'codeWithDesc',
        limitToList: 'true',
        method: 'POST',
        prompt: '--- 请选择 ---',
        url: g.ctx + 'pc/sys/purchaseGroup/rest/listAll',
    });
    initNecessaryDatas('pc/sys/purchaseGroup/rest/listAll')
    initDict("sample_type,material_life_cycle_state")
    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) {
                    topWin.layer.msg("请选择一条记录!");
                    return
                }
                addAndUpdate(rows[0].id, 2);
            }
        }, "-",
        {
            hide: false,
            text: "补充材料",
            id: 'btn_supplement',
            iconCls: 'icon-edit',
            handler: function () {
                var rows = $(el.table).datagrid('getSelections');
                if (rows.length !== 1) {
                    topWin.layer.msg("请选择一条记录!");
                    return
                }
                if (rows[0].state === 2 || rows[0].state === 6) {
                    addAndUpdate(rows[0].id, 2);
                } else {
                    topWin.layer.msg("请选中待处理或者不通过的数据进行补充材料!");
                    return
                }

            }
        }, "-",
        {
            hide: false,
            text: buttonI18n.check,
            id: 'btn_lookup',
            iconCls: 'icon-book',
            handler: function () {
                var rows = $(el.table).datagrid('getSelections');
                if (rows.length !== 1) {
                    topWin.layer.msg("请选择一条记录!");
                    return
                }
                examie(rows[0].id, 0);
            }
        },
        "-", {
            hide: false,
            text: buttonI18n.audit,
            id: 'btn_check',
            iconCls: 'icon-storage-move',
            handler: function () {
                var rows = $(el.table).datagrid('getSelections');
                if (rows.length !== 1) {
                    topWin.layer.msg("请选择一条记录!");
                    return
                }
                examie(rows[0].id, 1);
            }
        },
        "-", {
            hide: false,
            text: "确认",
            id: 'btn_confirm',
            iconCls: 'icon-storage-move',
            handler: function () {
                var rows = $(el.table).datagrid('getSelections');
                if (rows.length !== 1) {
                    topWin.layer.msg("请选择一条记录!");
                    return
                }
                if (rows[0].state === 5) {
                    examie(rows[0].id, 2);
                } else {
                    topWin.layer.msg("请选中待供方确认的数据!");
                }
            }
        },
        "-",
        {
            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: 'state',
            title: "状态",
            align: 'left',
            width: 90,
            formatter: function (value) {
                switch (value) {
                    case 1:
                        return '<font color="red">' + "新建" + '</font>';
                    case 2:
                        return '<font color="#FF5722">' + "待供应商处理" + '</font>';
                    case 3:
                        return '<font color="green">' + "待采购处理" + '</font>';
                    case 4:
                        return '<font color="green">' + "待审核" + '</font>';
                    case 5:
                        return '<font color="#ff8c00">' + "待供方确认" + '</font>';
                    case 6:
                        return '<font color="red">' + "审核不通过" + '</font>';
                    case 7:
                        return '<font color="#8b0000">' + "OA审批中" + '</font>';
                    case 8:
                        return '<font color="#006400">' + "供方已确认" + '</font>';
                }
            }
        },

        {
            field: 'sampleType',
            title: "签样类型",
            align: 'left',
            width: 110,
            formatter: function (value) {
                return getDicName("sample_type", value)
            }
        },
        {
            field: "purchaseGroupCode",
            title: "采购组",
            align: 'left',
            width: 75,
            formatter: function (value) {
                return getNecessaryDataName('pc/sys/purchaseGroup/rest/listAll',value)
            }
        },
        {
            field: "supplierCode",
            title: "供应商编码",
            align: 'left',
            width: 70
        },
        {
            field: "supplierName",
            title: "供应商名称",
            align: 'left',
            width: 185
        },
        {
            field: 'returnFlag',
            title: "是否需回签样品",
            align: 'left',
            width: 100,
            formatter: function (value) {
                return value === 1 ? "是" : "否"
            }
        },
        {
            field: 'pcnNo',
            title: "PCN单号",
            align: 'left',
            width: 80
        },
        {
            field: "remark",
            title: "特殊需求说明",
            align: 'left',
            width: 200
        },

        {
            field: 'code',
            title: "申请单号",
            align: 'left',
            width: 105
        },
        {
            field: "expressNumber",
            title: "快递单号",
            align: 'left',
            width: 200
        },
        {
            field: 'oaId',
            title: "OA审批单号",
            align: 'left',
            width: 235,
            formatter: function (value, row) {
                return oaTrack(value, OaProcessType.MATERIAL_DEMO)
            }
        },
        {
            field: "createTime",
            title: "创建时间",
            align: 'left',
            width: 120
        },
        {
            field: "submitTime",
            title: "提交时间",
            align: 'left',
            width: 120
        }
    ]];
    var hiddenState = $('#hiddenState').val();

    $(el.table).datagrid({
        iconCls: 'icon-list',
        height: el.bodyHeight - searchHeight,
        singleSelect: false,
        method: "GET",
        url: g.ctx + 'pc/supplier/materialDemo/rest/list',
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        pagination: true,
        loadMsg: commonMsg.loadData,
        rownumbers: true,
        fitColumns: false,
        page: 1,
        queryParams: {state:hiddenState},
        columns: columns,
        toolbar: toolbars,
        onBeforeLoad: function (param) {
            displayButtonByUserRole('material_demo');
        },
        onCheck: function (index, row) {
            //审核状态 1.新建 2.供应商补充资料 3.采购审核 4.待审核 5:审核通过 6:审核不通过
            $("#btn_check").linkbutton((row.state === 3) ? "enable" : "disable");
            $("#btn_edit").linkbutton((row.state === 1) || (row.state === 4) ? "enable" : "disable");
            $("#btn_delete").linkbutton((row.state === 1) || (row.state === 2) ? "enable" : "disable");
        }
    });
}

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


/**
 * 新增编辑页面
 * @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: buttonI18n.submit,
            iconCls: 'icon-save',
            handler: function () {
                topWin.frames['addOrEditDialog'].saveDemo(3, 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 = ''
    if (type == 1) {
        title = '新增'
    } else {
        if (isSupplier) {
            title = '补充资料'
        } else {
            title = '编辑'
        }
    }
    var addOrEditDialog = openDialog("addOrEditDialog", g.ctx + "pc/supplier/materialDemo/page/add?id=" + id + "&type=" + type, {
        title: title,
        width: '90%',
        height: '85%',
        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');
        }
    }]
    let title = '查看页面'
    if (type === 1) {
        title = '审核页面'
        btns.unshift({
            text: '通过',
            iconCls: 'icon-ok',
            handler: function () {
                topWin.frames['examineDialog'].examine(7, function (data) {
                    examineDialog.dialog('close');
                    $(el.table).datagrid("reload");
                });
            }
        }, {
            text: '不通过',
            iconCls: 'icon-undo',
            handler: function () {
                topWin.frames['examineDialog'].examine(6, function (data) {
                    examineDialog.dialog('close');
                    $(el.table).datagrid("reload");
                });
            }
        })
    } else if (type === 2) {
        title = '确认页面'
        btns.unshift({
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
                topWin.frames['examineDialog'].confirm(function (data) {
                    examineDialog.dialog('close');
                    $(el.table).datagrid("reload");
                });
            }
        })
    }
    var examineDialog = openDialog("examineDialog", g.ctx + "pc/supplier/materialDemo/page/detail?id=" + id + "&type=" + type, {
        title: title,
        width: '90%',
        height: '100%',
        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;
    }

    topWin.layer.confirm('是否确认删除该记录？',{icon: 3}, function (iid) {
        $.ajax({
            method: 'POST',
            url: g.ctx + 'pc/supplier/materialDemo/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);
            }
        })
    })
}








