/**
 *
 * @author BOGON
 * @version 1.0
 */

var page = {
    submitFlag: 0,
    initDataGrid: function () {
        let toolbar = [
            {
                text: '导出信息',
                id: 'btn-export',
                iconCls: 'icon-print',
                handler: function () {
                    page.exportExcel();
                    setOperationLog("导出信息按钮", "小批量供应商");
                }
            },
            {
                text: '信息查看',
                id: 'btn-info-look',
                iconCls: 'icon-book',
                handler: function () {
                    page.information();
                }
            },
            {
                text: '信息查看(品质)',
                id: 'btn-info-look',
                iconCls: 'icon-book',
                handler: function () {
                    page.information1();
                }
            },
            {
                text: '转合格',
                id: 'btn-info-look',
                iconCls: 'icon-confirm',
                handler: function () {
                    page.convertQualified();
                }
            },
            {
                text: '内部审批附件',
                id: 'btn-qualified—attachment',
                iconCls: 'icon-redo',
                handler: function () {
                    var row = $("#tb").datagrid("getChecked");
                    if (row.length == 0 || row.length != 1) {
                        topWin.layer.msg("请选择一条数据");
                        return;
                    }

                    var fileIds = row[0].onSiteAuthenticationFiles
                    var id = row[0].id
                    page.onSiteAuthenticationFilesLookupDialog(id, fileIds);
                }
            }
        ]
        let column = [[
            {
                field: 'ck',
                checkbox: 'true'
            },
            {
                field: 'state',
                title: '供应商状态',
                width: '100',
                align: 'left',
                exportEnum: [
                    {code: 1, codeWithDesc: '注册'},
                    {code: 2, codeWithDesc: '潜在'},
                    {code: 3, codeWithDesc: '正式'},
                    {code: 4, codeWithDesc: '临时'},
                    {code: 5, codeWithDesc: '退出'},
                    {code: 6, codeWithDesc: '合格'},
                    {code: 7, codeWithDesc: '小批量'},
                    {code: "", codeWithDesc: '初始'}
                ],
                formatter: function (value) {
                    switch (value) {
                        case 1:
                            return '注册';
                        case 2:
                            return '潜在';
                        case 3:
                            return '正式';
                        case 4:
                            return '临时';
                        case 5:
                            return '退出';
                        case 6:
                            return '合格';
                        case 7:
                            return '小批量';
                        default:
                            return '';
                    }
                }
            },
            {
                field: 'sqeState',
                title: '处理状态',
                width: '100',
                align: 'left',
                exportEnum: [
                    {code: 0, codeWithDesc: '待供应商处理'},
                    {code: 1, codeWithDesc: '审批中'},
                    {code: 2, codeWithDesc: '通过'},
                    {code: 3, codeWithDesc: '商务审批中'},
                    {code: 4, codeWithDesc: '驳回'},
                    {code: 5, codeWithDesc: 'SQE审批中'},
                    {code: '', codeWithDesc: '待转合格'}
                ],
                formatter: function (value) {
                    if (value === '') {
                        return '待转合格'
                    }
                    if (value === 0) {
                        return '<span style="color: grey;">待供应商处理</span>';
                    } else if (value === 1) {
                        return '<span style="color: blue;">审批中</span>';
                    } else if (value === 2) {
                        return '<span style="color: green;">' + statusi18n.tg + '</span>';
                    } else if (value === 3) {
                        return '<span style="color: blue;">商务审批中</span>';
                    } else if (value === 5) {
                        return '<span style="color: blue;">SQE审批中</span>';
                    } else if (value === 4) {
                        return '<span style="color: red;">' + statusi18n.bh + '</span>';
                    }
                }
            },
            {
                field: 'supplierCode',
                title: '供应商编号',
                width: '100',
                align: 'left',
                sortable: true
            },
            {
                field: 'supplierErpCode',
                title: '供应商ERP编号',
                width: '100',
                align: 'left',
                sortable: true
            },
            {
                field: 'supplierName',
                title: '供应商名称',
                width: '260',
                align: 'left'

            },
            {
                field: 'purchaseGroupCode',
                title: '对账采购组',
                width: '120',
                align: 'left',
                exportEnum: getNecessaryDataList('pc/sys/purchaseGroup/rest/listAll'),
                formatter: function (value) {
                    return getNecessaryDataName('pc/sys/purchaseGroup/rest/listAll', value)
                }
            },
            {
                field: 'purchaseCategoryCode',
                title: '采购品类编码',
                width: '120',
                align: 'left'
            },
            {
                field: 'purchaseCategoryName',
                title: '采购品类名称',
                width: '120',
                align: 'left'
            },
            {
                field: 'remark',
                title: '备注',
                width: '60',
                align: 'left'

            },
            {
                field: 'supplierGenre',
                title: '供应商类型',
                width: 120,
                exportEnum: getDictList('supplier_classify'),
                formatter: function (value) {
                    return getDicName('supplier_classify', value)
                }
            },
            {
                field: 'isFreeze',
                title: '冻结状态',
                width: '60',
                align: 'left',
                exportEnum: [
                    {code: 1, codeWithDesc: '冻结'},
                    {code: 0, codeWithDesc: '正常'},
                    {code: '', codeWithDesc: '正常'},
                ],
                formatter: function (value) {
                    if (value == 1) {
                        return '<span style="color: red">冻结</span>'
                    } else {
                        return '<span style="color: green">正常</span>'
                    }
                }
            },
            {
                field: 'freezeRemark',
                title: '冻结原因',
                width: '60',
                align: 'left'
            },
            {
                field: 'effectiveTime',
                title: '有效期',
                width: 135,
                exportDateFormatter: 'date',
                formatter: function (value) {
                    return dateColor(value)
                }
            },
            {
                field: 'apprState',
                title: '审批状态',
                width: '60',
                align: 'left',
                exportEnum: [
                    {code: 1, codeWithDesc: '审批中'},
                    {code: 2, codeWithDesc: '同意'},
                    {code: 3, codeWithDesc: '驳回'},
                    {code: 0, codeWithDesc: '新建'},
                    {code: '', codeWithDesc: '新建'}
                ],
                formatter: function (value) {
                    if (value == 3) {
                        return '<span style="color: red">驳回</span>'
                    } else if (value == 2) {
                        return '<span style="color: green">同意</span>'
                    } else if (value == 1) {
                        return '<span style="color: grey">审批中</span>'
                    } else if (value == 0) {
                        return '<span style="color: grey">新建</span>'
                    }
                }
            },
            {
                field: 'oaId',
                title: 'OA流程编号',
                width: '245',
                align: 'left',
                formatter: function (value) {
                    return oaTrack(value, OaProcessType.SUPPLIER_ACCESS);
                }
            },
            {
                field: 'ttt',
                title: '现场认证报告',
                width: '110',
                align: 'left',
                exportHidden: true,
                formatter: function (value, row) {
                    return "<a href='javascript:void(0)' onclick=\"page.onSiteAuthenticationFilesLookupDialog('" + row.supplierId + "','" + row.purchaseCategoryCode + "',1)\">" + "点击查看" + "</a>";
                }
            }
        ]]
        $("#tb").datagrid({
            height: document.documentElement.clientHeight - $('#searchForm').outerHeight(),
            width: gridwidth,
            url: g.ctx + 'pc/supplier/purchaseCategory/rest/smallbatchList',
            singleSelect: false,
            method: "GET",
            pagination: true,
            autoHeight: true,
            loadMsg: "数据加载中......",
            rownumbers: true,
            fitColumns: false,
            striped: true,
            page: 1,
            pageSize: 10,
            columns: column,
            toolbar: toolbar,
            onBeforeLoad: function () {
                displayButtonByUserRole('smallbatchsupplier');
            }
        });
    },
    postData: function (id, callback, ids) {
        page.submitFlag = 1;
        var load = topWin.layer.load(1);
        let data = {
            id: id
        }
        if (ids !== undefined) {
            data['demoIds'] = ids.join(",")
        }
        $.ajax({
            url: g.ctx + 'pc/supplier/purchaseCategory/rest/convertQualified',
            type: 'POST',
            data: data,
            complete: function () {
                topWin.layer.close(load);
                page.submitFlag = 0;
            },
            success: function (data) {
                topWin.layer.alert("操作成功!", {icon: 1})
                callback()
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        })
    },
    /**
     * 查看供应商注册信息
     */
    information: function () {
        let row = $("#tb").datagrid('getSelected');
        if (!row) {
            topWin.layer.msg("请选中一条记录");
            return;
        }
        let id = row.supplierId;
        var url = g.ctx + 'pc/supplier/detail?id=' + id
        var invitationEditDialog = openDialog('invitationEditDialog', url, {
            title: '查看供应商信息',
            width: "90%",
            height: "100%",
            cache: false,
            modal: true,
            border: false,
            draggable: true,
            shadow: false,
            onClose: function () {
                invitationEditDialog.dialog('destroy');
            },
            buttons: [{
                text: itablei8n.close, handler: function () {
                    invitationEditDialog.dialog('close');
                }
            }]
        });
    },
    information1: function () {
        let row = $("#tb").datagrid('getSelected');
        if (!row) {
            topWin.layer.msg("请选中一条记录");
            return;
        }
        let id = row.supplierId;
        var url = g.ctx + 'pc/supplier/detailPz?id=' + id
        var invitationEditDialog = openDialog('invitationEditDialog', url, {
            title: '查看供应商信息',
            width: "90%",
            height: "100%",
            cache: false,
            modal: true,
            border: false,
            draggable: true,
            shadow: false,
            onClose: function () {
                invitationEditDialog.dialog('destroy');
            },
            buttons: [{
                text: itablei8n.close, handler: function () {
                    invitationEditDialog.dialog('close');
                }
            }]
        });
    },
    exportExcel: function () {
        openExportDialog('pc/supplier/purchaseCategory/rest/smallbatchExport', 'tb', searchParams())
    },
    onSiteAuthenticationFilesLookupDialog: function (id, purchaseCategoryCode, readOnly) {
        let url = g.ctx + 'pc/supplier/purchaseCategory/page/siteAuthenticationFile'
        url = url + '?id=' + id + '&purchaseCategoryCode=' + purchaseCategoryCode
        var buttons = [{
            text: itablei8n.save, handler: function () {
                topWin.frames["siteAuthenticationScoreDialog"].page.submit(function () {
                    siteAuthenticationScoreDialog.dialog('close');
                    page.initDataGrid.datagrid("reload")
                });

            }
        }, {
            text: itablei8n.close, handler: function () {
                siteAuthenticationScoreDialog.dialog('close');
            }
        }]
        if (readOnly) {
            buttons.shift()
        }
        siteAuthenticationScoreDialog = openDialog('siteAuthenticationScoreDialog', url, {
            title: '认证报告',
            width: "750px",
            height: "100%",
            cache: false,
            modal: true,
            border: false,
            draggable: true,
            shadow: false,
            onClose: function () {
                siteAuthenticationScoreDialog.dialog('destroy');
            },
            buttons: buttons
        });

    },
    convertQualified: function () {
        let rows = $("#tb").datagrid('getChecked');
        if (rows.length <= 0) {
            topWin.layer.msg("请选中一条记录");
            return;
        }
        // 假设第一个supplierId是基准
        const firstSupplierId = rows[0].supplierId;
        const allSame = rows.every(row => row.supplierId === firstSupplierId);
        if (!allSame) {
            topWin.layer.msg("请选中相同供应商的数据!");
            return;
        }
        for (let i = 0; i < rows.length; i++) {
            if (rows[i].sqeState !== '') {
                topWin.layer.msg("请选中待转合格的数据!");
                return;
            }
        }
        const purchaseCategoryCodes = new Set();

        rows.forEach(row => {
            purchaseCategoryCodes.add(row.purchaseCategoryCode);
        });
        let url = g.ctx + 'pc/supplier/supplierQualification/page/apply/detail?lookupFlag=0&supplierCode=' + rows[0].supplierCode
            + '&supplierName=' + rows[0].supplierName
            + '&supplierId=' + rows[0].supplierId
            + '&applyType=1&purchaseCategoryCodes='
            + Array.from(purchaseCategoryCodes).join(',');
        var convertQualifiedEle = openDialog('convertQualifiedEle', url, {
            title: '小批量转合格',
            width: '750px',
            height: '100%',
            cache: false,
            modal: true,
            border: false,
            draggable: true,
            shadow: false,
            onClose: function () {
                convertQualifiedEle.dialog('destroy');
            },
            buttons: [{
                text: itablei8n.save, handler: function () {
                    topWin.frames['convertQualifiedEle'].submitForm(function () {
                        convertQualifiedEle.dialog('close');
                        $('#tb').datagrid('reload');
                    });
                }
            }, {
                text: itablei8n.close, handler: function () {
                    convertQualifiedEle.dialog('close');
                }
            }]
        });
    }
}
$(function () {
    initNecessaryData("pc/sys/purchaseGroup/rest/listAll")
    initDict("supplier_classify")
    page.initDataGrid()
})