/**
 * 系统参数配置首页
 *
 * Created by passion on 2020/2/4.
 * view/system/config/list.ftl:
 */
'use strict';
var el = {
    searchForm: '#searchForm',
    configType: '#configType',
    remark: '#remark',
    configKey: '#configKey',
    tb: '#tb',
    addOrEditDialog: null,
    bodyHeight: document.documentElement.clientHeight
};


$(function () {
    $(el.configType).combobox({
        valueField: 'value',
        textField: 'label',
        data: [
            {label: '文本型', value: '1'},
            {label: '单选型', value: '2'},
            {label: '文件型', value: '3'}
        ],
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto'
    });


    initDataGrid();
});


/**
 * 初始化表格
 */
function initDataGrid() {
    var searchHeight = $(el.searchForm).outerHeight();
    // var pager = initGridPagerNew(el.bodyHeight - searchHeight, 40, 30);
    $(el.tb).datagrid({
        loadMsg: '数据远程载入中,请等待...',
        singleSelect: false,
        height: el.bodyHeight - searchHeight,
        rownumbers:true,
        remoteSort:false,
        nowrap:false,
        fitColumns:true,
        url: g.ctx + 'pc/sys/sysConfig/rest/listAll',
        columns:[[
            {field: 'ck', checkbox: true},
            {
                field: 'status', title: '状态', width: '5%',
                formatter: function (value) {
                    if (Number(value) === 0) {
                        return '<span style="color: green;">启用</span>';
                    } else {
                        return '<span style="color: red;">禁用</span>';
                    }
                }
            },
            {field: 'configKey', title: '唯一编码', width: '28%'},
            {field: 'remark', title: '描述', width: '30%'},
            {
                field: 'configValue', title: '参数值', width: '35%',
                formatter: function (value, row) {
                    if (String(row.configType) === "2") {
                        if (Number(value) === 0) {
                            return '否';
                        } else {
                            return '是';
                        }
                    }
                    return value;
                }}

        ]],

    //     var fileHtml = "<a href='/files/download?fileName="+row.file_path+"&realName="+ row.file_name +"'>"+ row.file_name +"</a>";
    // return fileHtml;
        toolbar: [
            {
                text: '新增',
                iconCls: 'icon-add',
                handler: function () {
                    addOrEdit(1);
                }
            },
            '-',
            {
                text: '编辑',
                iconCls: 'icon-edit',
                handler: function () {
                    addOrEdit(2);
                }
            },
            '-',
            {
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    delConfigData();
                }
            },
            '-',
            {
                text: '启用',
                iconCls: 'icon-ok',
                handler: function () {
                    updateStatus(0);
                }
            },
            '-',
            {
                text: '禁用',
                iconCls: 'icon-lock',
                handler: function () {
                    updateStatus(1);
                }
            }
        ],
        groupField:'configType',
        view: groupview,
        groupFormatter:function(value){
            var text = "";
            switch (Number(value)) {
                case 1:
                    text = '<span style="color: red;">文本型</span>';
                    break;
                case 2:
                    text = '<span style="color: red;">单选型</span>';
                    break;
                case 3:
                    text = '<span style="color: red;">文件型</span>';
                    break;
                case 4:
                    text = '<span style="color: red;">帮助文档</span>';
                    break;

            }
            return '&nbsp;参数类型:&nbsp;' +text;
        }
    });
}


/**
 * 新增或编辑数据
 *
 * @param type 1:新增 2:编辑
 */
function addOrEdit(type) {
    var url = g.ctx + 'pc/sys/sysConfig/detail', title = '新增';
    if(type === 2){
        var rows = $(el.tb).datagrid('getSelections');
        if (rows.length !== 1) {
            topWin.layer.msg('只能选择一条数据进行编辑操作', {icon: 0});
            return false;
        }
        url = g.ctx + 'pc/sys/sysConfig/detail?id=' + rows[0].id;
        title = '编辑';
    }

    el.addOrEditDialog = openDialog('addOrEditData', url, {
        title: title,
        width: 500,
        height: 450,
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            el.addOrEditDialog.dialog('destroy');
        },
        buttons: [{
            text: itablei8n.save,
            handler: function () {
                topWin.frames['addOrEditData'].saveConfigData(function (msg) {
                    if (msg) {
                        topWin.layer.msg(msg.message, {icon: 2});
                        return false;
                    } else {
                        topWin.layer.msg('操作成功', {icon: 1});
                        $(el.tb).datagrid('load');
                        el.addOrEditDialog.dialog('close');
                    }
                });
            }
        }, {
            text: itablei8n.close,
            handler: function () {
                el.addOrEditDialog.dialog('close');
            }
        }]
    });
}


/**
 * 删除配置数据
 */
function delConfigData() {
    var rows = $(el.tb).datagrid('getSelections');
    if (rows.length === 0) {
        topWin.layer.msg('请选择您需要删除的数据', {icon: 0});
        return false;
    }

    var ids = [];
    for(var i = 0; i < rows.length; i++){
        ids.push(rows[i].id);
    }

    $.ajax({
        type: 'POST',
        url: g.ctx + 'pc/sys/sysConfig/rest/batchRemove',
        data: {ids: JSON.stringify(ids)},
        success: function () {
            topWin.layer.msg('删除成功', {icon: 1});
            $(el.tb).datagrid('load');
        },
        error: function (data) {
            topWin.layer.alert(data.responseJOSN.message, {icon: 2});
        }
    });
}


/**
 * 更新数据状态
 *
 * @param status 0:启用 1:禁用
 */
function updateStatus(status) {
    var rows = $(el.tb).datagrid('getSelections');
    if (rows.length === 0) {
        topWin.layer.msg('请选择您需要更新状态的数据', {icon: 0});
        return false;
    }

    var ids = [];
    for(var i = 0; i < rows.length; i++){
        ids.push(rows[i].id);
    }

    $.ajax({
        type: 'POST',
        url: g.ctx + 'pc/sys/sysConfig/rest/status/update',
        data: {
            ids: JSON.stringify(ids),
            status: status
        },
        success: function () {
            topWin.layer.msg('操作成功', {icon: 1});
            $(el.tb).datagrid('load');
        },
        error: function (data) {
            topWin.layer.alert(data.responseJOSN.message, {icon: 2});
        }
    });
}


/**
 * 送货单数据查询
 */
function searchData() {
    var queryParams = $(el.tb).datagrid('options').queryParams;
    queryParams.configType = $(el.configType).combobox('getValue');
    queryParams.remark = $.trim($(el.remark).val());
    queryParams.configKey = $.trim($(el.configKey).val());
    $(el.tb).datagrid('load');
}


/**
 * 重置查询条件
 */
function resetSearch(){
    $(el.configType).combobox('clear');
    $(el.remark).val('');
    $(el.configKey).val('');
    searchData();
}
