/**
 * 供应商绩效考核计划 首页JS
 *
 * Created by likui on 2019/8/24.
 */

'use strict';
var el = {
    tb: '#tb',
    searchForm: '#searchForm',
    status: '#status',
    planCode: '#planCode',
    planTitle: '#planTitle',
    evaluationCycle: '#evaluationCycle',
    addEditDialog: null,
    planTemType: '#planTemType',
    bodyHeight: document.documentElement.clientHeight
};

$(function () {
    initComBoBoxData();
    initDataGrid();
});

/**
 * 初始化数据表格
 */
function initDataGrid() {
    var searchHeight = $(el.searchForm).outerHeight() - 10;
    var pager = initGridPager(el.bodyHeight - searchHeight * 2 - 120, 40);
    var toolbar = [
        {
            text: '新增',
            hide: true,
            id: 'plan_add',
            iconCls: 'icon-add',
            handler: function () {
                addEditPlan(0);
            }
        }, "-",
        {
            text: '编辑',
            hide: true,
            id: 'plan_edit',
            iconCls: 'icon-edit',
            handler: function () {
                addEditPlan(1);
                setOperationLog("编辑按钮", "考核计划");
            }
        }, "-",
        {
            text: '删除',
            hide: true,
            id: 'plan_delete',
            iconCls: 'icon-remove',
            handler: function () {
                removePlan(1);
            }
        }, "-",
        {
            text: '发布',
            id: 'plan_release',
            iconCls: 'icon-ok',
            handler: function () {
                executePlan();
            }
        },
        "-",
        {
            text: '评分审核',
            id: 'plan_examine',
            iconCls: 'icon-plan',
            handler: function () {
                var row = $(el.tb).datagrid('getSelected');
                if (!row) {
                    topWin.layer.msg('请选中一条记录进行评分审核操作');
                    return;
                }
                if (row.status !== 3) {
                    topWin.layer.msg('只有已发布的计划才可以进行评分操作', {icon: 0});
                    return;
                }
                var url = g.ctx + 'supplier/appraise/examine/examine?planId=' + row.id;
                var examineDialog = openDialog('examineDialog', url, {
                    title: '',
                    width: 400,
                    height: 200,
                    cache: false,
                    modal: true,
                    border: 'thin',
                    onClose: function () {
                        examineDialog.dialog('destroy');
                    },
                    buttons: [{
                        text: '关闭',
                        handler: function () {
                            examineDialog.dialog('close');
                        }
                    }]
                });

            }
        },
        "-",
        {
            text: '明细查看',
            id: 'plan_detail_look',
            iconCls: 'icon-file-search',
            handler: function () {
                page.openDetailDialog();
                setOperationLog("明细查看按钮", "考核计划");
            }
        }, "-",
        {
            text: '报告查看',
            id: 'plan_report_look',
            iconCls: 'icon-file-search',
            handler: function () {
                page.openReportDialog();
            }
        },
    ];
    $(el.tb).datagrid({
        url: g.ctx + 'pc/spe/supAppraisePlan/rest/list',
        method: "GET",
        loadMsg: '数据远程载入中,请等待...',
        singleSelect: true,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        checkbox: true,
        rownumbers: true,
        height: el.bodyHeight - 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
            },
            {
                field: 'updateUserName',
                title: '最后一次更新人',
                width: 120
            },
            {
                field: 'updateTime',
                title: '最后一次更新时间'
            }
        ]],
        toolbar: toolbar,
        onLoadSuccess: function () {
            displayButtonByUserRole('Appraisal Manage');
        },
        onCheck: function (rowIndex) {
            var rows = $(el.tb).datagrid('getRows');
            var flag = rows[rowIndex].status;
            if (flag == 0) { //0新建 1评分中 2已生成报告
                $('#plan_add').linkbutton('enable');
                $('#plan_edit').linkbutton('enable');
                $('#plan_delete').linkbutton('enable');
                $('#plan_release').linkbutton('enable');
                $('#plan_detail_look').linkbutton('enable');
                $('#plan_generate_report').linkbutton('disable');
                $('#planScore').linkbutton('disable');
                // $('#plan_report_look').linkbutton('disable');
            } else if (flag == 1) { //0新建 1评分中 2已生成报告

                $('#plan_add').linkbutton('enable');
                $('#plan_edit').linkbutton('disable');
                // $('#plan_delete').linkbutton('disable');
                $('#plan_release').linkbutton('disable');
                $('#plan_detail_look').linkbutton('enable');
                $('#plan_generate_report').linkbutton('enable');
                $('#planScore').linkbutton('enable');
                // $('#plan_report_look').linkbutton('disable');
            } else if (flag == 2) { //0新建 1评分中 2已生成报告
                $('#plan_add').linkbutton('enable');
                $('#plan_edit').linkbutton('disable');
                $('#plan_delete').linkbutton('disable');
                $('#plan_release').linkbutton('disable');
                $('#plan_detail_look').linkbutton('enable');
                $('#plan_generate_report').linkbutton('disable');
                $('#planScore').linkbutton('disable');
                // $('#plan_report_look').linkbutton('enable');
            }
        }
    });
}

/**
 * 初始化下拉框
 */
function initComBoBoxData() {
    var type = [
        {label: '全部', value: ''},
        {label: '年度', value: '1'},
        {label: '季度', value: '2'},
        {label: '月度', value: '3'}
    ];
    // 初始化考核周期下拉框
    $(el.evaluationCycle).combobox({
        valueField: 'value',
        textField: 'label',
        value: '',
        data: type,
        editable: false,
        panelHeight: 'auto'
    });

    // 初始化状态下拉框
    $(el.status).combobox({
        valueField: 'value',
        textField: 'label',
        value: '',
        data: [
            {label: '全部', value: ''},
            {label: '新建', value: '0'},
            {label: '评分中', value: '1'},
            {label: '已评分', value: '3'}
        ],
        editable: false,
        panelHeight: 'auto'
    });
}

/**
 * 新增或编辑考核计划
 *
 * @param type 0:新增 1:编辑
 */
function addEditPlan(type) {
    var planTemType = $(el.planTemType).val();
    var url = g.ctx + 'pc/spe/supAppraisePlan/page/detail';
    var title = '新增';
    if (type !== 0) {
        var row = $(el.tb).datagrid('getSelected');
        if (!row) {
            topWin.layer.msg('请选中一条记录进行编辑操作');
            return;
        }
        url = g.ctx + 'pc/spe/supAppraisePlan/page/detail?id=' + row.id;
        title = '编辑';
    }

    el.addEditDialog = openDialog('addEditPlan', url, {
        title: title,
        width: 800,
        height: 500,
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            el.addEditDialog.dialog('destroy');
        },
        buttons: [{
            text: '保存',
            handler: function () {
                topWin.frames['addEditPlan'].page.saveData(function () {
                    $(el.tb).datagrid('load');
                    el.addEditDialog.dialog('close');
                });
            }
        }, {
            text: '关闭',
            handler: function () {
                el.addEditDialog.dialog('close');
            }
        }]
    });
}

/**
 * 删除新建状态的考核计划
 */
function removePlan() {
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        topWin.layer.msg('请选中一条记录进行删除操作');
        return;
    }
    topWin.layer.confirm('确定执行该操作吗？', {icon: 3}, function (index) {
        const load = topWin.layer.load(1);
        $.ajax({
            url: g.ctx + 'pc/spe/supAppraisePlan/rest/remove',
            type: 'POST',
            data: {id: row.id},
            complete: function () {
                topWin.layer.close(load)
            },
            success: function () {
                topWin.layer.alert('操作成功', {icon: 1}, function (idx) {
                    topWin.layer.close(idx);
                    $(el.tb).datagrid('reload');
                    topWin.layer.close(index);
                });

            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON, {icon: 2});
                topWin.layer.close(index);
            }
        })
    });
}

/**
 * 执行考核计划
 */
function executePlan() {
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        topWin.layer.msg('请选中一条记录进行发布操作');
        return;
    }
    if (row.status !== 0) {
        topWin.layer.msg('只有新建状态的计划才可以发布', {icon: 0});
        return;
    }
    topWin.layer.confirm('确定执行该操作吗？', {icon: 3}, function (index) {
        $.ajax({
            url: g.ctx + 'pc/spe/supAppraisePlan/rest/updateStatus',
            type: 'POST',
            data: {planId: row.id},
            success: function () {
                topWin.layer.alert('操作成功', {icon: 1, time: 500});
                $(el.tb).datagrid('reload');
                topWin.layer.close(index);
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        })
    });
}

/**
 * 对计划评分
 */
function planScore() {
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        topWin.layer.msg('请选中一条记录进行评分操作');
        return;
    }
    if (row.status !== 1) {
        topWin.layer.msg('只有已发布的计划才可以进行评分操作', {icon: 0});
        return;
    }
    var url = g.ctx + 'supplier/appraise/result/index?planId=' + row.id, title = '评分';

    el.addEditDialog = openDialog('planScore', url, {
        title: title,
        width: 1300,
        height: 700,
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            el.addEditDialog.dialog('destroy');
        },
        buttons: [
            {
                text: '保存',
                handler: function () {
                    topWin.layer.load();
                    // TODO 2019/9/26 保存时假的，啊哈哈哈
                    setTimeout(function () {
                        el.addEditDialog.dialog('close');
                        topWin.layer.closeAll('loading');
                    }, 1500);
                }
            },
            {
                text: '关闭',
                handler: function () {
                    el.addEditDialog.dialog('close');
                }
            }
        ]
    });
}

/**
 * 对计划评分
 */
function planScore() {
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        topWin.layer.msg('请选中一条记录进行评分操作');
        return;
    }
    if (row.status !== 1) {
        topWin.layer.msg('只有已发布的计划才可以进行评分操作', {icon: 0});
        return
    }
    if (row.status === 1) {
        if (row.evlStatus === 1) {
            topWin.layer.msg('已经评分，无法重新评分', {icon: 0});
            return
        }
    }
    var url = g.ctx + 'supplier/appraise/result/index?planId=' + row.id,
        title = '评分';

    el.addEditDialog = openDialog('planScore', url, {
        title: title,
        width: 1300,
        height: 700,
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            el.addEditDialog.dialog('destroy');
        },
        buttons: [
            {
                text: '保存',
                handler: function () {
                    topWin.frames['planScore'].page.save(closeDialog);
                }
            },
            {
                text: '关闭',
                handler: function () {
                    el.addEditDialog.dialog('close');
                }
            }
        ]
    });
}

function closeDialog() {
    el.addEditDialog.dialog('close');
    $(el.tb).datagrid('reload');
}

var page = {
    detailDialog: {},
    openDetailDialog: function () {
        var row = $(el.tb).datagrid('getSelected');
        if (!row) {
            topWin.layer.msg('请选中一条记录进行编辑操作');
            return;
        }
        var planId = row.id;
        var url = g.ctx + 'pc/spe/supAppraisePlan/page/scoreDetail?id=' + planId;
        page.detailDialog = openDialog('supplierDetialDialog', url, {
            title: '供应商绩效查看',
            width: '100%',
            height: '100%',
            cache: false,
            modal: true,
            border: 'thin',
            onClose: function () {
                page.detailDialog.dialog('destroy');
            },
            buttons: [{
                text: '关闭',
                handler: function () {
                    page.detailDialog.dialog('close');
                }
            }]
        });
    }
};