layui.use(['layer', 'table', 'form','upload'], function () {

    /*********layui引入参数**********/
    var $ = layui.jquery
        , layer = layui.layer //弹层
        , table = layui.table //表格
        , form = layui.form
        ,upload = layui.upload

    /**********列表参数设置**********/

    var list_id = $('#list_id').val();
    var tableIds = 'advertisinglist';  //表格节点ID
    var rowIds = 'advertisingfunc';  //列操作节点ID
    var url = '/listnav/indexpush/pushtitleform';//数据接口
    var where = {token: csrfToken,list_id:list_id};//参数


    /************表格实列************/
    table.render({
        elem: '#' + tableIds
        , url: url //数据接口
        , where: where
        , method: 'post'
        , page: true //开启分页
        , text: '数据加载中'
        , height: 580
        , id: 'tablerReload'
        , cols: [[ //表头
            {type: 'numbers', title: '序号', width: 50}
            , {field: 'title', title: '推送名称', align: 'center'}
            , {field: 'url', title: '跳转链接', align: 'center'}
            , {
                field: 'sort',
                title: '排序',
                width: 100,
                event: 'sortData',
                edit: 'text',
                align: 'center',
                sort: true,
                templet: "#sortBox"
            }
            , {field: 'state', title: '审核', align: 'center', width: 100,templet: function (d) {
                    if (d.state == 2) {
                        return '<input type="checkbox"  value="' + d.id + '" res_state="1"  lay-skin="switch" lay-filter="switchTest3" lay-text="|">'
                    } else {
                        return '<input type="checkbox" checked  value="' + d.id + '" res_state="2"  lay-skin="switch" lay-filter="switchTest3" lay-text="|">'
                    }
                }}
            , {field: 'create_at', title: '添加时间', width: 200, align: 'center'}
            , {fixed: 'right', title: '操作', width: 180, align: 'center', toolbar: '#' + rowIds}

        ]]
    });

    /********************************************列操作begin***********************************************************/
    table.on('tool(' + tableIds + ')', function (obj) {
        var layEvent = obj.event,
            data = obj.data;
        if (layEvent === 'del') {
            var index = layer.confirm('您确定要删除吗?', {btn: ['确定', '取消']}, function () {
                setListHouse('del',data.id);
                layer.close(index);
            })
        } else if (layEvent === 'sortData') {       //修改排序
            table.on('edit(' + tableIds + ')', function (obj) {
                var data = obj.data;
                setListHouse('sort',data.id,data.sort);
            })
        }
    });
    /********************************************列操作end***********************************************************/
    /********************************************栏目操作gegin*******************************************************/
    var $ = layui.$, active = {
        addData: function () { //获取选中数据
            var index = layui.layer.open({
                type: 2,
                title: '添加首页区域主推楼盘推送',
                closeBtn: 1, //不显示关闭按钮
                area: ['60%', '80%'],
                content: '/listnav/indexpush/pushtitleadd?list_id='+list_id//iframe的url,no代表不显示滚动条
                , success: function (layero, index) {
                },
                end: function () {
                    table.reload('tablerReload');
                }
            });
        }
    };
    $('.demoTable .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    /********************************************栏目操作end*******************************************************/
    /********************************************提交操作begin*****************************************************/
    form.on('submit(adds)', function (data) {
        var formData = new FormData($('form')[0]);
        var index = layer.msg('数据提交中,请稍候', {icon: 16, time: false, shade: 0.8});      //数据提交提示
        $.ajax({
            cache: false,
            type: "post",
            url: "/listnav/indexpush/pushtitleaddform",
            data: formData,  // 你的formid
            contentType: false,   //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作,从而失去分界符,而使服务器不能正常解析文件
            processData: false,   //当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
            error: function (request) {
                layer.close(index);
                layer.msg("请联系管理员");
            },
            success: function (data) {
                layer.msg(data.msg);
                if(data.code == 200){
                    TableClose();
                }
            }
        });
        return false;
    });
    /********************************************提交操作end*********************************************************/
    /********************************************其他操作begin*******************************************************/
    //监听指定开关
    form.on('switch(switchTest3)', function (data) {
        let id = data.value;
        let val = $(this).attr('res_state');
        var index = layer.confirm('确定要修改此操作吗?', {btn: ['确定', '取消']}, function () {
            setListHouse('state',id,val)
            layer.close(index)
        }, function (aa) {
            renderTable();        //重新加载
        })
    });

    function setListHouse(code, id, val = null) {
        $.post('/listnav/indexpush/set',{code:code,id:id,val:val},function(res){
            layer.msg(res.msg)
            table.reload('tablerReload');        //重新加载
        },'json')
    }
    /********************************************其他操作end*********************************************************/
})