/**
 * 待办配置-新增指标分类页面JS
 *
 * Modify by li.kui on 2021/4/7.
 */
'use strict';
var el = {
    id: '#id',
    category: '#category',
    targetType: '#targetType',
    targetTypeId: '#targetTypeId',
    name: '#name',
    code: '#code',
    description: '#description',
    sort: '#sort',
    sqlStr: '#sqlStr',
    url: '#url',
    param: '#param'
};


/**
 * 文档就绪
 */
$(function () {
    // 初始化指标分类下拉框
    $(el.targetType).combobox({
        url: g.ctx + 'pc/sys/dealt/type/rest/listAll',
        panelHeight: 260,
        valueField: 'id',
        method: 'GET',
        textField: 'name',
        onSelect: function (data) {
            $(el.targetTypeId).val(data.id);
        }
    });

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


/**
 * 数据保存
 */
function dataSave(callback) {
    var code = $.trim($(el.code).val()), name = $.trim($(el.name).val()), description = $.trim($(el.description).val()),
        sort = $(el.sort).numberbox('getValue'), sqlStr = $(el.sqlStr).val(), url = $(el.url).val(), param = $(el.param).val(),
        targetTypeId = $(el.targetTypeId).val(),  targetType = $(el.targetType).combobox('getValue'),
        category = $(el.category).combobox('getValue'), id = $(el.id).val();
    if(!category) {
        layer.msg('请选择指标类型');
        return false;
    }
    if(!targetType) {
        layer.msg('请选择指标分类');
        return false;
    }
    if(!targetTypeId) {
        layer.msg('您填写的指标分类在系统不存在, 请选择指标分类');
        return false;
    }
    if(!code) {
        layer.msg('指标编码不能为空');
        return false;
    }
    if(!name) {
        layer.msg('指标名称不能为空');
        return false;
    }
    if(!sort) {
        layer.msg('排序不能为空');
        return false;
    }
    if (sort.length > 6) {
        layer.msg('排序值必须小于1000000');
        return false;
    }
    if(!sqlStr) {
        layer.msg('请填写SQL语句');
        return false;
    }

    var params = {};
    params.id = id;
    params.code = code;
    params.name = name;
    params.sort = sort;
    params.sqlStr = sqlStr;
    params.url = url;
    params.param = param;
    params.category = category;
    params.description = description;
    params.targetTypeId = targetTypeId;

    topWin.layer.confirm('您确定要保存指标信息吗?', {icon: 3}, function (index) {
        topWin.layer.close(index);

        var loading = topWin.layer.msg('数据保存中, 请耐心等待......', {icon: 16, time: 0, shade: 0.1});
        $.ajax({
            url: g.ctx + 'pc/sys/dealt/info/rest/saveOrUpdate',
            type: 'POST',
            data: params,
            success: function () {
                topWin.layer.close(loading);
                callback();
            },
            error: function (data) {
                topWin.layer.close(loading);
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    });
}