/**
 * 供应商绩效考核整改通知单首页
 *
 * Created by likui on 2019/9/13.
 */
'use strict';
var el = {
    supplierCode: '#supplierCode',
    supplierName: '#supplierName',
    status: '#status',
    tb: '#tb',
    searchForm: '#searchForm',
    rectifiedEditDialog: null,
    bodyHeight: document.documentElement.clientHeight
};

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

/**
 * 初始化数据表格
 */
function initDataGrid() {
    var searchHeight = $(el.searchForm).outerHeight() - 10;
    var pager = initGridPager(el.bodyHeight - searchHeight * 2 - 120, 40);

    $(el.tb).datagrid({
        url: g.ctx + 'pc/spe/rectifiedNotice/rest/list?statusStr=0,2,4,6,10',
        loadMsg: suplierModel.loadData,
        singleSelect: true,
        method: 'GET',
        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: '状态',
            title: commonTxt.status, width: 150, formatter: function (value) {
                if (value === -1) {
                    return '<span style="color: red;">待发布</span>';
                }
                if (value === 0) {
                    // return '<span style="color: red;">待整改</span>';
                    return '<span style="color: red;">' + khzg.dzg + '</span>';
                }
                if (value === 2) {
                    // return '<span style="color: grey;">已提交整改</span>';
                    return '<span style="color: grey;">' + khzg.ytjzg + '</span>';
                }
                if (value === 4) {
                    // return '<span style="color: orange;">待确认</span>';
                    return '<span style="color: orange;">' + khzg.dqr + '</span>';
                }
                if (value === 6) {
                    // return '<span style="color: red;">确认不通过,待整改</span>';
                    return '<span style="color: red;">' + khzg.qrbtg_dzg + '</span>';
                }
                if (value === 10) {
                    // return '<span style="color: orange;">待跟踪</span>';
                    return '<span style="color: orange;">' + khzg.dgz + '</span>';
                }
                if (value === 20) {
                    // return '<span style="color: blue;">待审批</span>';
                    return '<span style="color: blue;">' + khzg.dsp + '</span>';
                }
                if (value === 30) {
                    // return '<span style="color: green;">已完结</span>';
                    return '<span style="color: green;">' + khzg.ywj + '</span>';
                }
                return '';
            }
        }, {
            field: 'noticeCode', // title: '通知单编码',
            title: khzg.noticeCode, width: 130
        }, {
            field: 'supplierCode', // title: '供应商编码',
            title: khzg.supplierCode, width: 130
        }, {
            field: 'supplierName', // title: '供应商名称',
            title: khzg.supplierName, width: 260
        }, {
            field: 'evaluationCycle', //title: '考核周期',
            title: khzg.evaluationCycle, width: 90, formatter: function (value) {
                if (!value) {
                    return '';
                }
                if (value === 1) {
                    // return '年度';
                    return khzg.year;
                } else if (value === 2) {
                    //return '季度';
                    return khzg.quarter;
                } else if (value === 3) {
                    //return '月度';
                    return khzg.month;
                }
            }
        }, {
            field: 'achievementStartDate', // title: '绩效考核期间',
            title: khzg.achievementStartDate, width: 200, formatter: function (value, row, index) {
                if (row.evaluationCycle === 1) {
                    // return row.year + '年';
                    return row.year + khzg.years;
                } else if (row.evaluationCycle === 2) {
                    // return row.year + '年第' + row.quarter + '季';
                    return row.year + khzg.years + khzg.the + row.quarter + khzg.quarters;
                } else if (row.evaluationCycle === 3) {
                    // return row.year + '年第' + row.month + '月';
                    return row.year + khzg.years + khzg.the + row.month + khzg.months;
                }
            }
        }, {
            field: 'templateName', // title: '所选考核模板',
            title: khzg.templateName, width: 160
        }, {
            field: 'opinionRemark', // title: '整改意见',
            title: khzg.opinionRemark, width: 160
        }, {
            field: 'createUserName', //title: '创建人',
            title: khzg.createUserName, width: 100
        }, {
            field: 'createTime', //title: '创建时间',
            title: khzg.createTime, width: 130, formatter: function (value) {
                if (value) {
                    return moment(value).format('YYYY-MM-DD HH:mm:ss');
                }
                return '';
            }
        }, {
            field: 'updateUserName', //title: '最后一次更新人',
            title: khzg.updateUserName, width: 100
        }, {
            field: 'updateTime', //title: '最后一次更新时间',
            title: khzg.updateTime, width: 130, formatter: function (value) {
                if (value) {
                    return moment(value).format('YYYY-MM-DD HH:mm:ss');
                }
                return '';
            }
        }]],
        // onDblClickRow: function (index, row) {
        //     lookRectifiedDetail(row);
        // },
        toolbar: [{
            // text: '整改',
            id: 'btn_rectif',
            text: khzg.zg, iconCls: 'icon-edit', handler: function () {
                rectifiedEdit();
            }
        }, "-", {
            // text: '提交',
            id: 'btn_submit',
            text: buttonI18n.submit, iconCls: 'icon-ok', handler: function () {
                submitRectified();
            }
        }, "-", {
            // text: '跟踪',
            id: 'btn_following',
            text: khzg.gz, iconCls: 'icon-ok', handler: function () {
                trackEdit();
            }
        }],
        onBeforeLoad: function () {
            displayButtonByUserRole("Appraisal Edit");
        },
        onLoadSuccess: function () {
            $(el.tb).datagrid('clearSelections');
        },
        onCheck: function (rowIndex, field, value) {
            var rows = $('#tb').datagrid('getRows');
            var flag = rows[rowIndex].status;
            if (flag == 0) { //数据状态 0:待整改 2:已提交整改 4:待确认 6:确认不通过 10:待跟踪 20:待审批 30:已完结
                $('#btn_rectif').linkbutton('enable');
                $('#btn_submit').linkbutton('enable');
                $('#btn_following').linkbutton('disable');
            } else if (flag == 2) { //数据状态 0:待整改 1:待跟踪 2:待审批 3:已完结
                $('#btn_rectif').linkbutton('enable');
                $('#btn_submit').linkbutton('enable');
                $('#btn_following').linkbutton('disable');
            } else if (flag == 4) { //数据状态 0:待整改 1:待跟踪 2:待审批 3:已完结
                $('#btn_rectif').linkbutton('disable');
                $('#btn_submit').linkbutton('disable');
                $('#btn_following').linkbutton('disable');
            } else if (flag == 6) { //数据状态 0:待整改 1:待跟踪 2:待审批 3:已完结
                $('#btn_rectif').linkbutton('enable');
                $('#btn_submit').linkbutton('enable');
                $('#btn_following').linkbutton('disable');
            } else if (flag == 10) { //数据状态 0:待整改 1:待跟踪 2:待审批 3:已完结
                $('#btn_rectif').linkbutton('disable');
                $('#btn_submit').linkbutton('disable');
                $('#btn_following').linkbutton('disable');
            } else if (flag == 20) { //数据状态 0:待整改 1:待跟踪 2:待审批 3:已完结
                $('#btn_rectif').linkbutton('disable');
                $('#btn_submit').linkbutton('disable');
                $('#btn_following').linkbutton('disable');
            } else if (flag == 30) { //数据状态 0:待整改 1:待跟踪 2:待审批 3:已完结
                $('#btn_rectif').linkbutton('disable');
                $('#btn_submit').linkbutton('disable');
                $('#btn_following').linkbutton('disable');
            }
        },
    });
}

/**
 * 初始化下拉框
 */
function initComBoBoxData() {

    // 初始化状态下拉框
    $(el.status).combobox({
        valueField: 'value', textField: 'label', value: -1, data: [{
            // label: '全部',
            label: khzg.all, value: -1
        }, {
            //label: '待整改',
            label: khzg.dzg, value: 0
        }, {
            //label: '待跟踪',
            label: khzg.dgz, value: 1
        }, {
            //label: '待审批',
            label: khzg.dsp, value: 2
        }, {
            //label: '已完结',
            label: khzg.ywj, value: 3
        }], editable: false, panelHeight: 'auto'
    });
}

/**
 * 查看绩效考核整改通知单
 */
function lookRectifiedDetail(row) {
    if (row === '') {
        row = $(el.tb).datagrid('getSelected');
        if (!row) {
            // layer.msg('请选中一条记录进行编辑操作');
            layer.msg(khzg.msg5);
            return;
        }
    }
    var url = g.ctx + 'supplier/appraise/rectified/notice/detail?pageType=0&id=' + row.id;

    el.rectifiedEditDialog = openDialog('rectifiedEdit', url, {
        // title: '查看绩效考核整改通知单',
        title: khzg.msg2, width: 1000, height: 680, cache: false, modal: true, border: 'thin', onClose: function () {
            el.rectifiedEditDialog.dialog('destroy');
        }, buttons: [{
            // text: '关闭',
            text: buttonI18n.close, handler: function () {
                el.rectifiedEditDialog.dialog('close');
            }
        }]
    });
}

/**
 * 编辑绩效考核整改通知单
 */
function rectifiedEdit() {
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        // layer.msg('请选中一条记录进行编辑操作', {icon: 0});
        topWin.layer.msg(khzg.msg5, {icon: 0});
        return;
    }
    if (row.status === 20 || row.status === 30|| row.status === 10) {
        // layer.msg('对不起！该状态的记录不能操作整改', {icon: 0});
        topWin.layer.msg(khzg.msg3, {icon: 0});
        return;
    }

    var url = g.ctx + 'pc/spe/rectifiedNotice/page/detail?pageType=1&id=' + row.id;
    el.rectifiedEditDialog = openDialog('rectifiedEdit', url, {
        // title: '编辑整改通知单',
        title: khzg.msg4, width: "90%", height: '100%', cache: false, modal: true, border: 'thin', onClose: function () {
            el.rectifiedEditDialog.dialog('destroy');
        }, buttons: [
            {
                // text: '提交',
                text: buttonI18n.submit, handler: function () {
                    topWin.frames['rectifiedEdit'].submit(function () {
                        $(el.tb).datagrid('load');
                        el.rectifiedEditDialog.dialog('close');
                    });
                }
            },
            // {
            //     // text: '保存',
            //     text: buttonI18n.save, handler: function () {
            //         topWin.frames['rectifiedEdit'].saveData(function () {
            //             $(el.tb).datagrid('load');
            //             el.rectifiedEditDialog.dialog('close');
            //         });
            //     }
            // },
            {
                // text: '关闭',
                text: buttonI18n.close, handler: function () {
                    el.rectifiedEditDialog.dialog('close');
                }
            }]
    });
}

/**
 * 跟踪绩效考核整改通知单
 */
function trackEdit() {
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        // layer.msg('请选中一条记录进行编辑操作', {icon: 0});
        topWin.layer.msg(khzg.msg5, {icon: 0});
        return;
    }
    if (row.status === 2 || row.status === 3) {
        // layer.msg('对不起！该状态的记录不能操作整改', {icon: 0});
        topWin.layer.msg(khzg.msg3, {icon: 0});
        return;
    }

    var url = g.ctx + 'pc/spe/rectifiedNotice/page/detail?pageType=2&id=' + row.id;
    el.rectifiedEditDialog = openDialog('rectifiedEdit', url, {
        // title: '跟踪整改通知单',
        title: khzg.msg6, width: "90%", height: '100%', cache: false, modal: true, border: 'thin', onClose: function () {
            el.rectifiedEditDialog.dialog('destroy');
        }, buttons: [{
            // text: '保存',
            text: buttonI18n.save, handler: function () {
                topWin.frames['rectifiedEdit'].saveData(function () {
                    $(el.tb).datagrid('load');
                    el.rectifiedEditDialog.dialog('close');
                });
            }
        }, {
            // text: '关闭',
            text: buttonI18n.close, handler: function () {
                el.rectifiedEditDialog.dialog('close');
            }
        }]
    });
}

/**
 * 绩效考核整改通知单提交审批
 */
function submitRectified() {
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        // layer.msg('请选中一条记录', {icon: 0});
        topWin.layer.msg(commonMsg.msg1, {icon: 0});
        return false;
    }
    topWin.layer.confirm(khzg.msg9, {icon: 3}, function (index) {
        $.ajax({
            url: g.ctx + 'pc/spe/rectifiedNotice/rest/submitRectified',
            type: 'POST',
            data: {noticeId: row.id},
            success: function () {
                // layer.alert('操作成功', {icon: 1, time: 500});
                topWin.layer.alert(commonTxt.operateSuccess, {icon: 1, time: 500});
                $(el.tb).datagrid('reload');
                topWin.layer.close(index);
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message)
            }
        })
    });
}