/**
 * 待办配置-指标分类首页JS
 *
 * Modify by li.kui on 2021/4/7.
 */
'use strict';
var el = {
    searchForm: '#searchForm',
    name: '#name',
    code: '#code',
    desc: '#desc',
    tb: '#tb',
    addOrEditDialog: null,
    bodyHeight: document.documentElement.clientHeight
};


/**
 * 文档就绪
 */
$(function () {
    loadGrid();
});


/**
 * 加载页面表格
 */
function loadGrid() {
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight, 37, 40);

    $(el.tb).datagrid({
        loadMsg: '数据加载中......',
        emptyMsg: '暂无数据',
        url : g.ctx + 'pc/sys/dealt/type/rest/list',
        height: el.bodyHeight - searchHeight,
        singleSelect: true,
        rownumbers: true,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        queryParams: {},
        columns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'code',
                    title: '分类编码',
                    width: 90
                },
                {
                    field: 'name',
                    title: '分类名称',
                    width: 140
                },
                {
                    field: 'description',
                    title: '分类描述',
                    width: 240

                },
                {
                    field: 'sort',
                    title: '排序',
                    width: 70

                },
                {
                    field: 'createUserName',
                    title: '创建人',
                    width: 90
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    width: 130,
                    formatter: function (value) {
                        if (!value) {
                            return '';
                        }
                        return moment(value).format('YYYY-MM-DD HH:mm');
                    }
                },
                {
                    field: 'modifyUserName',
                    title: '修改人',
                    width: 90
                },
                {
                    field: 'modifyTime',
                    title: '修改时间',
                    width: 130,
                    formatter: function (value) {
                        if (!value) {
                            return '';
                        }
                        return moment(value).format('YYYY-MM-DD HH:mm');
                    }
                }
            ]
        ],
        toolbar: [
            {
                text: buttonI18n['add'],
                iconCls: 'icon-add',
                handler: function () {
                    detail(0);
                }
            },
            '-',
            {
                text: buttonI18n['edit'],
                iconCls: 'icon-edit',
                handler: function () {
                    detail(1);
                }
            },
            '-',
            {
                text: buttonI18n['delete'],
                iconCls: 'icon-remove',
                handler: function () {
                    delIndexClassify();
                }
            }
        ]
    });
}


/**
 * 编辑指标分类
 */
function detail(flag) {
    var id ='';
    if (flag===1) {
        var row = $(el.tb).datagrid('getSelected');
        if (!row) {
         topWin.layer.alert('请选择一条记录进行操作', {icon: 0});
         return;
       }
        id = row.id;
    }
    var url = g.ctx + 'pc/sys/dealt/type/detail?id=' + id;
    el.addOrEditDialog = openDialog('editClassify', url, {
        title: flag===0?'新增指标分类':'编辑指标分类',
        width: '650px',
        height: '50%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            el.addOrEditDialog.dialog('destroy');
        },
        buttons: [
            {
                text: '保存',
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['editClassify'].dataSave(function () {
                        el.addOrEditDialog.dialog('close');
                        $(el.tb).datagrid('reload');
                    });
                }
            },
            {
                text: '关闭',
                iconCls: 'icon-clear',
                handler: function () {
                    el.addOrEditDialog.dialog('close');
                }
            }
        ]
    });
}


/**
 * 删除指标分类
 */
function delIndexClassify() {
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        topWin.layer.alert('请选择一条记录进行操作', {icon: 0});
        return;
    }

    topWin.layer.confirm('您确定要删除选中的指标分类吗?', {icon: 3}, function (index) {
        $.ajax({
            url: g.ctx + 'pc/sys/dealt/type/rest/remove',
            type: 'POST',
            data: {id: row.id},
            success: function () {
                topWin.layer.close(index);
                $(el.tb).datagrid('reload');
                topWin.layer.msg('删除成功', {icon: 1});
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    });
}

function searchIcData() {
    var queryParams = $(el.tb).datagrid('options').queryParams;
    queryParams.code = $.trim($(el.code).val());
    queryParams.name = $.trim($(el.name).val());
    queryParams.desc = $.trim($(el.desc).val());
    $(el.tb).datagrid('load');
}


/**
 * 列表数据查询重置
 */
function resetIcSearch() {
    $(el.code).val('');
    $(el.name).val('');
    $(el.desc).val('');

    searchIcData();
}