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

var page = {
    cycleChange: function (val, val2) {
        var url = g.ctx + 'pc/spe/supAppraiseTemplate/rest/listAll' + '?types=' + val + ",-1";
        $('#templateId').combobox("clear");
        $('#templateId').combobox("reload", url);
    },
    loadDetail: function () {
        $.ajax({
            url: g.ctx + "pc/spe/supAppraiseTemplate/rest/getFieldByPlan",
            method: "GET",
            data: {planId: id},
            success: function (data) {
                let frozenColumns = [{
                    field: 'ck', checkbox: true
                }, {
                    field: 'date', title: '考核期间', width: 80, align: 'center'
                }, {
                    field: 'status',
                    title: '状态',
                    width: 80,
                    hidden:true,
                    formatter: function (value, row) {
                        if (value === 1) {
                            return '<span style="color: darkgreen">已提交整改</span>'
                        }
                        return '未提交整改'
                    }
                }, {
                    field: 'supplierName', title: '供应商名称', width: 120, align: 'center'
                }, {
                    field: 'supplierCode', title: '供应商</br>编码', width: 55, align: 'center'
                }, {
                    field: 'purchaseUser', title: '采购员', width: 50, align: 'center'
                }, {
                    field: 'sqe', title: 'SQE', width: 50, align: 'center'
                }, {
                    field: 'ce', title: 'CE', width: 50, align: 'center'
                }, {
                    field: 'purchaseCategoryNames', title: '物料类别', width: 100, align: 'center'
                }]
                let firstColumns = []
                let thirdColumns = []
                $.each(data, function (idx, itm) {
                    firstColumns.push({
                        field: itm.code,
                        align: 'center',
                        title: itm.name,
                        colspan: itm.item.length

                    })
                    $.each(itm.item, function (ii, iit) {
                        var width = iit.name.length * 13 + 10;
                        if (itm.item.length === 1) {
                            width = 160
                        }
                        thirdColumns.push({
                            field: iit.code,
                            title: iit.name,
                            width: width,
                            sortable: true,
                        })
                    })
                })
                page.initDetailTable([frozenColumns], [firstColumns, thirdColumns])
            },
            error: function (data) {
                topWin.layer.alert(JSON.parse(data.responseText).message);
            }
        });
    },
    loadData: function () {
        if (id !== 0) {
            $.ajax({
                url: g.ctx + 'pc/spe/supAppraisePlan/rest/detail?id=' + id,
                type: "GET",
                success: function (data) {
                    page.filePath = data.importFile;
                    $("#fileName").html(data.fileName);
                    $("#fileName").attr('href', g.ctx + 'pc/sys/file/rest/download/' + data.importFile);
                    for (var prop in data) {
                        var val = data[prop];
                        switch (prop) {
                            case 'planCode':
                            case 'planTitle':
                            case 'evaluationDate':
                            case 'remark':
                                $("#" + prop).textbox("setValue", val);
                                break;
                            case 'templateId':
                            case 'evaluationCycle':
                            case 'companyCode':
                                $("#" + prop).combobox("setValue", val);
                                break;
                            default:
                                $("#" + prop).val(val);
                                break;
                        }
                    }

                    //查看和审核所有控件只读
                    // 设置所有 datetimebox 为只读模式
                    $('.easyui-datebox').datebox('readonly', true);
                    // 设置所有 TextBox 为只读模式
                    $('.easyui-textbox').textbox('readonly', true);
                    // 设置所有 ComboBox 为只读模式
                    $('.easyui-combobox').combobox('readonly', true);
                }
            });
        }
    },
    initDetailTable: function (frozenColumns, columns) {
        let h = document.documentElement.clientHeight - $("#dataForm").outerHeight() - 30
        $("#detail_tb").datagrid({
            url: g.ctx + 'pc/spe/supAppraisePlan/rest/detailList?planId=' + id,
            method: "GET",
            loadMsg: '数据远程载入中,请等待...',
            singleSelect: true,
            pagination: false,
            checkbox: true,
            rownumbers: true,
            remoteSort:false,
            height: h <= 0 ? 400 : h,
            frozenColumns: frozenColumns,
            columns: columns,
            onLoadSuccess: function () {
                $("#detail_tb").datagrid("resize")
            }
        })
    },
    submitRectification: function () {
        var rows = $("#detail_tb").datagrid('getSelections');
        if (rows.length !== 1) {
            layer.msg('请选中一条记录进行查看操作');
            return;
        }
        var row = rows[0];
        var url = g.ctx + 'pc/spe/rectifiedNotice/page/add?pageType=1&supplierPlanId=' + row.id;
        page.rectifiedEditDialog = openDialog('rectifiedAdd', url, {
            title: '编辑整改通知单',
            width: 1000,
            height: 680,
            cache: false,
            modal: true,
            border: 'thin',
            onClose: function () {
                page.rectifiedEditDialog.dialog('destroy');
            },
            buttons: [{
                text: '保存',
                handler: function () {
                    topWin.frames['rectifiedAdd'].saveData(function () {
                        $("#detail_tb").datagrid('reload');
                        page.rectifiedEditDialog.dialog('close');
                    });
                }
            }, {
                text: '关闭',
                handler: function () {
                    page.rectifiedEditDialog.dialog('close');
                }
            }]
        });
    },
    submitBatchRectification: function () {
        var url = g.ctx + 'pc/spe/rectifiedNotice/page/rectified/supplierList?id=' + id;
        page.batchRectifiedEditDialog = openDialog('batchRectifiedEditDialog', url, {
            title: '批量整改',
            width: '100%',
            height: '100%',
            cache: false,
            modal: true,
            border: 'thin',
            onClose: function () {
                page.batchRectifiedEditDialog.dialog('destroy');
            },
            buttons: [{
                text: '保存',
                handler: function () {
                    topWin.frames['batchRectifiedEditDialog'].page.saveData(function () {
                        $("#detail_tb").datagrid('reload');
                        page.batchRectifiedEditDialog.dialog('close');
                    });
                }
            }, {
                text: '关闭',
                handler: function () {
                    page.batchRectifiedEditDialog.dialog('close');
                }
            }]
        });
    }
}
$(function () {
    page.loadData()
    page.loadDetail();
})