"use strict";
layui.extend({
    common    :   'js/common'
}).use(['jquery','layer','common','table','form'],function () {
    var $ = layui.jquery;
    var type = $('input[name="type"]').val();


    //  初始化表单
    var layuiTable = layui.common.layuiTable.render(layui.table,{
        elem: '#data_table'
        ,url: '/ad/threshold_config/pagingQuery?&type=' + type
        ,cols: [[
            { field: 'region', title: '区域',align:'center' }
            ,{ field: 'regionCode', title: '编号',align:'center' }
            ,{ field: 'value', title: '阈值',align:'center' }
            , { title: '操作', align: 'center',toolbar: '#table-operate',width:140}
        ]]
    });


    layui.form.on('submit(add)', function(){
        layer.open({
            type    :   2,
            area    :   ['600px','320px'],
            maxmin  :   true,
            move    :   false,
            title   :   '新增',
            content :   '/ad/threshold_config/add?type=' + type
        });
    });

    layui.table.on('tool(data_table_event)',function (obj) {
        switch (obj.event) {
            case 'edit':
                layer.open({
                    type    :   2,
                    area    :   ['600px','270px'],
                    maxmin  :   true,
                    move    :   false,
                    title   :   '修改',
                    content :   '/ad/threshold_config/edit?' + 'id=' + obj.data.id
                });
                break;
            case 'delete':
                layer.confirm('是否确认删除？',{icon: 7, title:'提示'}, function(index) {
                    $.ajax({
                        url : "/ad/threshold_config/delete",
                        type : 'POST',
                        data : { id : obj.data.id }
                    }).then(function (result) {
                        if (result.code === '200'){
                            layuiTable.reload({ page: { curr: 1 } });
                        }else {
                            layer.alert(result.msg);
                        }
                    });
                    layer.close(index);
                });
                break;
            default:break;
        }
    });


});
