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


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

    loadGrid();
});


/**
 * 初始化页面下拉框
 */
function initPageComCoBox() {
    // 初始化指标分类下拉框
    $(el.classify).combobox({
        url: g.ctx + 'pc/sys/dealt/type/rest/listAll',
        panelHeight: 260,
        method: 'GET',
        valueField: 'id',
        textField: 'name'
    });

    // 初始化指标类型下拉框
    $(el.category).combobox({
        url: g.ctx + 'pc/sys/dict/getDicByType?type=deal_with_target_class',
        panelHeight: 260,
        method: 'GET',
        valueField: 'code',
        textField: 'name',
        editable: false
    });
}


/**
 * 加载页面表格
 */
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/info/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: 'categoryName',
                    title: '指标类型',
                    width: 80
                },
                {
                    field: 'targetTypeCode',
                    title: '分类编码',
                    width: 80
                },
                {
                    field: 'targetTypeName',
                    title: '分类名称',
                    width: 160
                },
                {
                    field: 'code',
                    title: '指标编码',
                    width: 100
                },
                {
                    field: 'name',
                    title: '指标名称',
                    width: 200
                },
                {
                    field: 'description',
                    title: '指标描述',
                    width: 160
                },
                {
                    field: 'sort',
                    title: '排序',
                    width: 70
                },
                {
                    field: 'sqlStr',
                    title: 'SQL语句',
                    width: 80,
                    formatter: function (value, row) {
                        return '<a href="javascript:void(0)" onclick="sqlDetail(' + row.id + ')">查看</a>';
                    }
                },
                {
                    field: 'url',
                    title: 'URL',
                    width: 100
                },
                {
                    field: 'param',
                    title: '参数配置',
                    width: 100
                },
                {
                    field: 'createUserName',
                    title: '创建人',
                    width: 80
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    width: 130,
                    formatter: function (value) {
                        if (!value) {
                            return '';
                        }
                        return moment(value).format('YYYY-MM-DD HH:mm');
                    }
                },
                {
                    field: 'modifyUserName',
                    title: '修改人',
                    width: 80
                },
                {
                    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',
                hidden: true,
                handler: function () {
                    detail(0);
                }
            },
            '-',
            {
                text: buttonI18n['edit'],
                iconCls: 'icon-edit',
                handler: function () {
                    detail(1);
                }
            },
            '-',
            {
                text: buttonI18n['delete'],
                hide: true,
                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/info/detail?id=' + id;
    el.addOrEditDialog = openDialog('editDealt', url, {
        title: flag===0?'新增指标':'编辑指标',
        width: '650px',
        height: '85%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            el.addOrEditDialog.dialog('destroy');
        },
        buttons: [
            {
                text: '保存',
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['editDealt'].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/info/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});
            }
        });
    });
}


/**
 * 查看指标的SQL
 *
 * @param id 指标ID
 */
function sqlDetail(id) {
    var url = g.ctx + 'pc/sys/dealt/info/sql/detail?id=' + id;
    el.lookSqlDetail = openDialog('lookSql', url, {
        title: 'SQL详情',
        width: '650px',
        height: '400px',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            el.lookSqlDetail.dialog('destroy');
        },
        buttons: [
            {
                text: '关闭',
                iconCls: 'icon-clear',
                handler: function () {
                    el.lookSqlDetail.dialog('close');
                }
            }
        ]
    });
}


/**
 * 列表数据查询
 */
function searchImData() {
    var queryParams = $(el.tb).datagrid('options').queryParams;
    queryParams.code = $.trim($(el.code).val());
    queryParams.name = $.trim($(el.name).val());
    queryParams.classify = $(el.classify).combobox('getValue');
    queryParams.category = $(el.category).combobox('getValue');
    $(el.tb).datagrid('load');
}


/**
 * 列表数据查询重置
 */
function resetImSearch() {
    $(el.code).val('');
    $(el.name).val('');
    $(el.classify).combobox('setValue', '');
    $(el.category).combobox('setValue', '');

    searchImData();
}