/**
 *
 * @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();
                }
            }
        ]
        let column = [[
            {
                field: 'ck',
                checkbox: 'true'
            },
            {
                field: 'state',
                title: '供应商状态',
                width: '100',
                align: 'left',
                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: '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'
            },
            {
                field: 'purchaseCategoryCode',
                title: '采购品类编码',
                width: '120',
                align: 'left'
            },
            {
                field: 'purchaseCategoryName',
                title: '采购品类名称',
                width: '120',
                align: 'left'
            },
            {
                field: 'remark',
                title: '备注',
                width: '60',
                align: 'left'

            },
            {
                field: 'assessType',
                title: '评审方式',
                width: 100,
                exportEnum: getDictList("assess_type"),
                formatter: function (value) {
                    return getDicName('assess_type', value)
                }
            },
            {
                field: 'recommender',
                title: '申请人',
                width: 100
            },
            {
                field: 'supplierNameEn',
                title: '供应商英文名',
                width: 100
            },
            {
                field: 'shortName',
                title: '简称',
                width: 100
            },
            {
                field: 'productBrand',
                title: '产品品牌/制造商简称',
                width: 120
            },
            {
                field: 'tSupplierMainCustomerProduct',
                title: '主要客户',
                width: 100
            },
            {
                field: 'supplierGenre',
                title: '企业性质',
                width: 100,
                formatter: function (value) {
                    return getDicName('supplier_classify', value)
                }
            },
            {
                field: 'attr',
                title: '属性',
                width: 100,
                formatter: function (value) {
                    return getDicName('supplier_attr', value)
                }
            },
            {
                field: 'supplierGroup',
                title: '供应商分组',
                width: 100,
                formatter: function (value) {
                    return getDicName('supplier_group', value)
                }
            },
            {
                field: 'inviteDesc',
                title: '申请原因',
                width: 100
            },
            {
                field: 'specialRemark',
                title: '特批申请原因',
                width: 100
            },
            {
                field: 'procurementStrategy',
                title: '采购策略',
                width: 100
            },
            {
                field: 'paymentTerms',
                title: '付款条件',
                width: 165,
                formatter: function (value) {
                    return getDicName('payment_method', value)
                }
            },
            {
                field: 'paymentMethod',
                title: '付款方式',
                width: 100,
                formatter: function (value) {
                    return getDicName('payment_way', value)
                }
            },
            {
                field: 'effectiveTime',
                title: '小批量有效日期',
                width: 135
            },
            {
                field: 'isFreeze',
                title: '冻结状态',
                width: '60',
                align: 'left',
                formatter: function (value) {
                    if (value == 1) {
                        return '<span style="color: red">冻结</span>'
                    } else {
                        return '<span style="color: green">正常</span>'
                    }
                }
            },
            {
                field: 'apprState',
                title: '审批状态',
                width: '60',
                align: 'left',
                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',
                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/qualifiedList',
            singleSelect: false,
            method: "GET",
            pagination: true,
            autoHeight: true,
            loadMsg: "数据加载中......",
            rownumbers: true,
            fitColumns: false,
            striped: true,
            page: 1,
            pageSize: 10,
            columns: column,
            toolbar: toolbar,
            onLoadSuccess: function () {
                displayButtonByUserRole('StandardSupplierManage');
            }
        });
    },
    /**
     * 查看供应商注册信息
     */
    information: function () {
        let row = $("#tb").datagrid('getSelected');
        if (!row) {
            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) {
            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/qualifiedExport', '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
        });

    }
}
$(function () {
    initNecessaryData("pc/sys/purchaseGroup/rest/listAll")
    initDict("supplier_classify,assess_type,supplier_attr,supplier_group,procurement_strategy,payment_method,payment_way")
    page.initDataGrid()
})