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

$(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'
    });
});


function loadGrid() {
    $('#tb').datagrid({
        url: g.ctx + "pc/spe/supAppraiseDimension/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: 'dimensionCode',
                title: '维度编码',
                width: 110
            },
            {
                field: 'dimensionName',
                title: '维度名称',
                width: 200
            },

            {
                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);
            }
        }, "-", {
            text: '编辑',
            iconCls: 'icon-edit',
            handler: function () {
                addOrEdit(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);
    }
    topWin.layer.confirm('确定执行该操作吗？', {icon: 3}, function (index) {
        $.ajax({
            url: g.ctx + 'pc/spe/supAppraiseDimension/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 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 supAppraiseDimensionDialog = openDialog('supAppraiseDimension', g.ctx + 'pc/spe/supAppraiseDimension/detail?id=' + id, {
        title: flag===0?'新增':'编辑',
        width: '700px',
        height: '420px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            supAppraiseDimensionDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.save,
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['supAppraiseDimension'].saveOrUpdate(function () {
                        topWin.layer.msg('数据保存成功', {icon: 1});
                        supAppraiseDimensionDialog.dialog('close');
                        $(el.tb).datagrid('reload');
                    });
                }
            },
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    supAppraiseDimensionDialog.dialog('close');
                }
            }
        ]
    });
}
