
let el = {
    tb: '#tb',
    useState: '#useState',
    status: '#status',
    evaluationCycle: '#evaluationCycle',
    templateType: '#templateType',
    addEditDialog: '#addEditDialog',
}

$(function () {
    loadGrid();
    // 初始化状态下拉框
    $(el.status).combobox({
        valueField: 'value',
        textField: 'label',
        value: -1,
        data: [
            {label: '全部', value: -1},
            {label: '启用', value: 1},
            {label: '停用', value: 2}
        ],
        editable: false,
        panelHeight: 'auto'
    });
    var type = [
        {label: '全部', value: ""},
        {label: '通用', value: -1},
        {label: '年度', value: 1},
        {label: '季度', value: 2},
        {label: '月度', value: 3}
    ];
    // 初始化评价周期下拉框
    $(el.evaluationCycle).combobox({
        valueField: 'value',
        textField: 'label',
        value: '',
        data: type,
        editable: false,
        panelHeight: 'auto'
    });
});


function loadGrid() {
    $('#tb').datagrid({
        url: g.ctx + "pc/spe/supAppraiseTemplate/rest/list",
        iconCls: 'icon-list',
        title: '',
        height: gridheight,
        width: gridwidth,
        singleSelect: false,
        pagination: true,
        idField: 'id',
        autoHeight: true,
        loadMsg: "数据加载中......",
        rownumbers: true,
        fitColumns: false,
        remoteSort: true,
        columns: [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'status',
                title: '状态',
                width: 80,
                formatter: function (value) {
                    if (!value) {
                        return '';
                    }
                    if (value === 1) {
                        return '<span style="color: green;">启用</span>';
                    }
                    if (value === 2) {
                        return '<span style="color: grey;">停用</span>';
                    }
                }
            },
            {
                field: 'templateCode',
                title: '模板编码',
                width: 120
            },
            {
                field: 'templateName',
                title: '模板名称',
                width: 260
            },
            {
                field: 'evaluationCycle',
                title: '评价周期',
                width: 120,
                formatter: function (value) {
                    switch (value) {
                        case -1:
                            return '通用';
                        case 1:
                            return '年度';
                        case 2:
                            return '季度';
                        case 3:
                            return '月度';
                        default:
                            return '';
                    }
                }
            },
            {
                field: 'createUserName',
                title: '创建人',
                width: 120
            },
            {
                field: 'createTime',
                title: '创建日期',
                width: 150,
                formatter: function (value) {
                    if (value) {
                        return moment(value).format('YYYY-MM-DD HH:mm:ss');
                    }
                    return '';
                }
            },
            {
                field: 'updateUserName',
                title: '最后一次更新人',
                width: 120
            },
            {
                field: 'updateTime',
                title: '最后一次更新时间',
                width: 150,
                formatter: function (value) {
                    if (value) {
                        return moment(value).format('YYYY-MM-DD HH:mm:ss');
                    }
                    return '';
                }
            }
        ]],
        toolbar: ["-", {
            text: '新增',
            iconCls: 'icon-add',
            hidden: true,
            handler: function () {
                //addOrEdit(0);
                addEditTemplate(0);
            }
        }, "-", {
            text: '编辑',
            iconCls: 'icon-edit',
            handler: function () {
                //addOrEdit(1);
                addEditTemplate(1);
            }
        }, "-", {
                text: '启用',
                hide: true,
                iconCls: 'icon-ok',
                handler: function () {
                    invalidOrEffective(1);
                }
            }, "-",
            {
                text: '停用',
                iconCls: 'icon-lock',
                handler: function () {
                    invalidOrEffective(2);
                }
            }]
    });
}
/**
 *考核指标 1:启用；2:停用
 */
function invalidOrEffective  (status) {
    var rows = $(el.tb).datagrid('getSelections');
    if (rows.length === 0) {
        layer.msg('请选中记录进行操作');
        return;
    }
    var ids = [];
    for (var i = 0; i < rows.length; i++) {
        ids.push(rows[i].id);
    }
    layer.confirm('确定执行该操作吗？', {icon: 3}, function (index) {
        $.ajax({
            url: g.ctx + 'pc/spe/supAppraiseTemplate/rest/invalidOrEffective',
            type: 'POST',
            data: {
                ids: ids.join(','),
                status: status
            },
            success: function () {
                layer.alert('操作成功', {icon: 1});
                $(el.tb).datagrid('reload');
                layer.close(index);
            },
            error: function (data) {
                layer.alert(data.responseJSON, {icon: 2});
                layer.close(index);
            }
        })
    });
}
/**
 *新增编辑 0:新增；1:编辑
 */
function addEditTemplate (type) {
    var templateType = $(el.templateType).val();
    var url = g.ctx + 'pc/spe/supAppraiseTemplate/detail?templateType=' + templateType,
        title = '新增';
    if (type !== 0) {
        var rows = $(el.tb).datagrid('getSelections');
        if (rows.length !== 1) {
            layer.msg('请选中一条记录');
            return;
        }
        url = g.ctx + 'pc/spe/supAppraiseTemplate/detail?templateType=' + templateType + '&id=' + rows[0].id;
        title = '编辑';
    }

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

function addOrEdit(flag) {
    var id= '';
    if(flag===1){
        var cbox = $('#tb').datagrid('getSelected');
        if (cbox === null || cbox === undefined) {
            layer.msg("请选中一条记录");
            return;
        }
        id = cbox.id;
        console.log(id)
    }
    var supAppraiseTemplateDialog = openDialog('supAppraiseTemplate', g.ctx + 'pc/spe/supAppraiseTemplate/detail?id=' + id, {
        title: flag===0?'新增':'编辑',
        width: '700px',
        height: '420px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            supAppraiseTemplateDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.save,
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['supAppraiseTemplate'].saveData(function () {
                        topWin.layer.msg('数据保存成功', {icon: 1});
                        supAppraiseTemplateDialog.dialog('close');
                        $(el.tb).datagrid('reload');
                    });
                }
            },
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    supAppraiseTemplateDialog.dialog('close');
                }
            }
        ]
    });
}
