/**
 * 表单校验规则配置首页
 *
 * Created by passion on 2025/8/6.
 * view/sys/validation/list.ftl:
 */
'use strict';
const el = {
    searchForm: '#searchForm',
    fieldName: '#fieldName',
    currencyType: '#currencyType',
    tb: '#tb',
    addOrEditDialog: null,
    bodyHeight: document.documentElement.clientHeight
};

$(function () {
    initDataGrid();
    $(el.currencyType).combobox({
        valueField: 'value',
        textField: 'label',
        data: [
            {label: '人民币', value: 'CNY'},
            {label: '非人民币', value: '0'}
        ],
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto'
    });
});


/**
 * 初始化表格
 */
function initDataGrid() {
    const searchHeight = $(el.searchForm).outerHeight();
    $(el.tb).datagrid({
        url: g.ctx + 'pc/sys/validationRuleSetting/rest/list',
        loadMsg: '数据远程载入中,请等待...',
        singleSelect: false,
        height: el.bodyHeight - searchHeight,
        rownumbers:true,
        remoteSort:false,
        nowrap:false,
        fitColumns:true,
        pagination: true,
        columns:[[
            {field: 'ck', checkbox: true},
            {
                field: 'status', title: '状态',
                formatter: function (value) {
                    if (Number(value) === 1) {
                        return '<span style="color: green;">启用</span>';
                    } else {
                        return '<span style="color: red;">禁用</span>';
                    }
                }
            },
            {field: 'fieldName', title: '字段名称', width: '15%'},
            {
                field: 'currencyType', title: '币种', width: '7%',
                formatter: function (value) {
                    if (value === 'CNY') {
                        return '<span>人民币</span>';
                    } else {
                        return '<span>非人民币</span>';
                    }
                }
            },
            {field: 'validationMsg', title: '校验消息', width: '35%'},
            {field: 'createUser', title: '创建人'},
            {field: 'createTime', title: '创建时间'},
            {field: 'updateUser', title: '更新人'},
            {field: 'updateTime', title: '更新时间'}
        ]],

        toolbar: [
            {
                text: '新增',
                iconCls: 'icon-add',
                handler: function () {
                    addOrEdit(1);
                }
            },
            '-',
            {
                text: '编辑',
                iconCls: 'icon-edit',
                handler: function () {
                    addOrEdit(2);
                }
            },
            '-',
            {
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    deleteData();
                }
            },
            '-',
            {
                text: '启用',
                iconCls: 'icon-ok',
                handler: function () {
                    updateStatus(1);
                }
            },
            '-',
            {
                text: '禁用',
                iconCls: 'icon-lock',
                handler: function () {
                    updateStatus(0);
                }
            }
        ]
    });
}

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

    el.addOrEditDialog = openDialog('addOrEditData', url, {
        title: title,
        width: 600,
        height: 450,
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            el.addOrEditDialog.dialog('destroy');
        },
        buttons: [{
            text: itablei8n.save,
            handler: function () {
                topWin.frames['addOrEditData'].saveData(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 deleteData() {
    const rows = $(el.tb).datagrid('getSelections');
    if (rows.length === 0) {
        topWin.layer.msg('请选择您需要删除的数据', {icon: 0});
        return false;
    }

    let ids = [];
    for(let i = 0; i < rows.length; i++){
        ids.push(rows[i].id);
    }
    topWin.layer.confirm('是否删除？', {icon: 0}, function () {
        $.ajax({
            type: 'POST',
            url: g.ctx + 'pc/sys/validationRuleSetting/rest/delete',
            data: {
                regexRuleIds: 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 1:启用 0:禁用
 */
function updateStatus(status) {
    const rows = $(el.tb).datagrid('getSelections');
    if (rows.length === 0) {
        topWin.layer.msg('请选择您需要更新状态的数据', {icon: 0});
        return false;
    }

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

    $.ajax({
        type: 'POST',
        url: g.ctx + 'pc/sys/validationRuleSetting/rest/updateRuleStatus',
        data: {
            regexRuleIds: 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() {
    let queryParams = $(el.tb).datagrid('options').queryParams;
    queryParams.fieldName = $(el.fieldName).val()
    queryParams.currencyType = $(el.currencyType).combobox('getValue');
    $(el.tb).datagrid('load');
}


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