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

var page = {
    initTable: function () {
        var searchHeight = $("#searchForm").outerHeight() - 10;
        var pager = initGridPager(document.documentElement.clientHeight - searchHeight * 2 - 120, 40);
        var toolbar = [{
            text: '评分', hide: true, id: 'plan_scoring', iconCls: 'icon-add', handler: function () {
                var row = $("#tb").datagrid('getSelected');
                if (!row) {
                    topWin.layer.msg('请选中一条记录进行编辑操作');
                    return;
                }
                if (row.status !== 1) {
                    topWin.layer.msg('请选中评分中的数据操作');
                    return;
                }
                page.detailDialog(row.id, 0)
            }
        },
            {
                text: '发布', hide: true, id: 'plan_release', iconCls: 'icon-add', handler: function () {
                    var row = $("#tb").datagrid('getSelected');
                    if (!row) {
                        topWin.layer.msg('请选中一条记录进行操作');
                        return;
                    }
                    if (row.status !== 1) {
                        topWin.layer.msg('请选中评分中的数据操作');
                        return;
                    }
                    page.detailDialog(row.id, 2)
                }
            },
            {
                text: '查看', hide: true, id: 'plan_lookup', iconCls: 'icon-book', handler: function () {
                    var row = $("#tb").datagrid('getSelected');
                    if (!row) {
                        topWin.layer.msg('请选中一条记录进行查看操作');
                        return;
                    }
                    page.detailDialog(row.id, 1)
                    setOperationLog("查看按钮", "考核评分");
                }
            }];
        $("#tb").datagrid({
            url: g.ctx + 'pc/spe/supAppraisePlan/rest/listForScoring',
            method: "GET",
            loadMsg: '数据远程载入中,请等待...',
            singleSelect: true,
            pagination: true,
            pageSize: pager.pageSize,
            pageList: pager.pageList,
            queryParams: {
                status: '1,3'
            },
            checkbox: true,
            rownumbers: true,
            height: document.documentElement.clientHeight - searchHeight - 20,
            columns: [[{
                field: 'ck', checkbox: true
            }, {
                field: 'status', title: '状态', width: 80, formatter: function (value, row) {
                    if (value === 0) {
                        return '<span style="color: grey;">新建</span>';
                    } else if (value === 1) {
                        return '<span style="color: orange;">评分中</span>';
                    } else if (value === 3) {
                        return '<span style="color:  lightblue;"> 已评分</span>';
                    }
                    return '';
                }
            }, {
                field: 'companyName', title: '公司', width: 110
            }, {
                field: 'planCode', title: '计划编码', width: 110
            }, {
                field: 'planTitle', title: '计划标题', width: 200
            }, {
                field: 'evaluationCycle', title: '考核周期', width: 90, formatter: function (value) {
                    if (!value) {
                        return '';
                    }
                    if (value === 1) {
                        return '年度';
                    } else if (value === 2) {
                        return '季度';
                    } else if (value === 3) {
                        return '月度';
                    }
                }
            }, {
                field: 'achievementPeriod', title: '绩效考核期间', width: 200, formatter: function (value, row) {
                    if (row.evaluationCycle === 1) {
                        return row.year + "年";
                    } else if (row.evaluationCycle == 2) {
                        return row.year + "年第" + row.quarter + "季";
                    } else if (row.evaluationCycle == 3) {
                        return row.year + "年" + row.month + "月";
                    }
                }
            }, {
                field: 'remark', title: '备注', width: 300
            }, {
                field: 'createUserName', title: '创建人', width: 120
            }, {
                field: 'createTime', title: '创建时间', width: 150
            }]],
            toolbar: toolbar,
            onBeforeLoad:function (){
                displayButtonByUserRole("Appraisal Score");
            },
        })
    }, /**
     * 初始化下拉框
     */
    initComBoBoxData: function () {
        var type = [{label: '全部', value: ""}, {label: '年度', value: '1'}, {label: '季度', value: '2'}, {
            label: '月度',
            value: '3'
        }];
        // 初始化考核周期下拉框
        $("#evaluationCycle").combobox({
            valueField: 'value', textField: 'label', value: '', data: type, editable: false, panelHeight: 'auto'
        });

        // 初始化状态下拉框
        $("#status").combobox({
            valueField: 'value',
            textField: 'label',
            value: '1,3',
            data: [{label: '全部', value: '1,3'}, {label: '评分中', value: '1'}, {label: '已评分', value: '3'}],
            editable: false,
            panelHeight: 'auto'
        });
    }, detailDialog: function (id, type) {
        //评分
        let title = '评分'
        let btns = [{
            text: '保存', handler: function () {
                topWin.frames['scoringDialog'].page.calculationRow(function () {
                    $("#tb").datagrid('load');
                    scoringDialog.dialog('close');
                });
            }
        }, {
            text: '关闭', handler: function () {
                scoringDialog.dialog('close');
            }
        }]
        if (type === 1) {
            title = '查看'
            btns.shift()
        }
        if (type === 2) {
            title = '发布'
            btns[0] = {
                text: '发布', handler: function () {
                    topWin.frames['scoringDialog'].page.calculation(2, function () {
                        $("#tb").datagrid('load');
                        scoringDialog.dialog('close');
                    });
                }
            }
        }
        var scoringDialog = openDialog('scoringDialog', g.ctx + "pc/spe/supAppraisePlan/page/scoringDetail?id=" + id + "&type=" + type, {
            title: title,
            width: "100%",
            height: "100%",
            cache: false,
            modal: true,
            border: 'thin',
            onClose: function () {
                scoringDialog.dialog('destroy');
            },
            buttons: btns
        });
    }
}
$(function () {
    page.initTable();
    page.initComBoBoxData();
})