layui.use(['form','layer','laydate','table','laytpl','layedit'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;

    var tableIds = 'newsList';
    var tableIns = table.render({
        elem: '#'+tableIds,
        url : '/housech/newslistform',
        where:{hid:$('#houseId').val()},
        method:'post',
        page : true,
        height:600,
        limit : 20,
        id:'tablerReload',
        cols : [[
            {type:'numbers'},
            {type: "checkbox", width:50},
            {field: 'id', title: 'ID', width:80, align:"center",sort: true},
            {field: 'subject', title: '标题',align:'left'},
            // {field: 'city_name', title: '区域',width:80, align:'center'},
            {field: 'name', title: '添加人',width:75,  align:'center'},
            {field: 'news_name', title: '栏目',width:150, align:'center'},
            // {field: 'is_view', title: '审核',width:80, event:'housesNews',  align:'center',templet:'#sexTpl'},
            // {field: 'comment', title: '评论',width:80, align:'center',templet:"#comment",templet:function(d){
            //         return '<a href="#">['+d.comment+']</a>'
            //     }},
            {field: 'clicks', title: '点击数',width:100, align:'center',sort: true},
            {field: 'create_at', title: '添加时间',width:180, event:'sortData', align:'center',sort: true},
            {title: '操作', width:200, templet:'#housesListBar',fixed:"right",align:"center"}

        ]]
    });

    
    tableIns.reload('tablerReload');        //重新加载


    $('.demoTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    var $ = layui.$, active = {
        loadnews: function(){ //获取选中数据
            var index = parent.layui.layer.open({
                title : "加载资讯",
                type : 2,
                area:['','100%'],
                maxmin: true,
                content : "/housech/loadnews",
            })

            parent.layui.layer.full(index);
            // // //改变窗口大小时,重置弹窗的宽高,防止超出可视区域(如F12调出debug的操作)
            $(window).on("resize",function(){
                parent.layui.layer.full(index);
            })
        },//筛选
        reload:function () {
            var form = pfgs_serialize($('form'));
            var name = '';
            var id = '';
            // console.log($('form').serializeArray());
            if(form.input_house == 'name'){
                name = form.input_house_text;
            } else if(form.input_house == 'id'){
                id = form.input_house_text;
            }
            table.reload('tablerReload',{
                where:{
                    sort:form.input_sort,
                    id:id,
                    name:name,
                    category:form.category
                }
            })
        },  //加载资讯的搜索
        load_news_reload:function () {
            var form = pfgs_serialize($('form'));
            var name = '';
            var id = '';
            // console.log($('form').serializeArray());
            if(form.input_house == 'name'){
                name = form.input_house_text;
            } else if(form.input_house == 'id'){
                id = form.input_house_text;
            }
            table.reload('loadnewsId',{
                where:{
                    id:id,
                    name:name,
                    news:form.category
                }
            })
        }

    };




    //列表操作
    table.on('tool('+tableIds+')', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){		//删除楼盘文章
            layer.confirm('您确定要删除吗?',{btn:['确认','取消']},function () {
                $.post('/housech/housenewsdel',{dataid:data.hnid},function (data) {
                    if(data.code == 200){
                        layer.msg(data.msg);
                        table.reload('tablerReload');
                    }else if(data.code == 300){
                        layer.msg(data.msg);
                    }

                })

            })

        }else if(obj.event === 'Relieve'){
            var _this = $(this);

            layer.confirm('您确定要解审吗?',{btn:['确认','取消']},function () {
                $.post('/housech/housenewsdel',{dataid:data.hnid,type:2},function (data) {
                    if(data.code == 200){
                       // _this.text('aa');
                        layer.msg('操作成功');
                        table.reload('tablerReload');
                    }else if(data.code == 300){
                        layer.msg('操作失败');
                    }

                })

            })

        }
    });


    //加载资讯
    $('.loadnews').click(function () {
        var index = parent.layui.layer.open({
            title :'<span style="color:red" >'+$('.house_name').html()+'</span>加载资讯',
            type : 2,
            area:['','100%'],
            maxmin: true,
            content : "/housech/loadnews?hid="+$('#houseId').val(),
            end:function () {
                layui.table.reload('tablerReload');
            }
        })

        parent.layui.layer.full(index);
        // // //改变窗口大小时,重置弹窗的宽高,防止超出可视区域(如F12调出debug的操作)
        $(window).on("resize",function(){
            parent.layui.layer.full(index);
        })
    })

    //资讯列表
    table.render({
        elem: '#loadnewsList',
        url : '/housech/loadnewslist',
        method:'post',
        where:{hid:$('#houseId').val()},    //接口的其它参数。如:where: {token: 'sasasas', id: 123}
        // height:'380px',         //容器高度
        id:'loadnewsId',
        page : true,
        limit : 20,         //注意:请务必确保 limit 参数(默认:10)是与你服务端限定的数据条数一致
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'id', title: 'ID', width:80, align:"center",sort: true},
            {field: 'subject', title: '标题',align:'left'},
            // {field: 'city_name', title: '区域',width:80, align:'center'},
            // {field: 'news_name', title: '栏目',width:150, align:'center'},
            {field: 'state', title: '审核',width:80, event:'housesNews',  align:'center'},
            // {field: 'comment', title: '评论',width:80, align:'center',templet:"#comment",templet:function(d){
            //         return '<a href="#">['+d.comment+']</a>'
            //     }},
            {field: 'clicks', title: '点击数',width:100, align:'center',sort: true},
            {field: 'create_at', title: '添加时间',width:150, event:'sortData', align:'center',sort: true},
            // {title: '操作', width:200, templet:'#housesListBar',fixed:"right",align:"center"}

        ]]
    });

    form.on('submit(add_load_news)',function (data) {
        var checkStatus = table.checkStatus('loadnewsId');
        var index = layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8});      //数据提交提示
        // console.log(checkStatus.length);
        // console.log(typeof checkStatus.data[0]);
        // return false;
        if(typeof  checkStatus.data[0] == 'object'){
             var Hid = $('#houseId').val();
            var formdat = new FormData();
            $.each(checkStatus.data,function (key,val) {
                formdat.append('nid[]',val.id);
            })
            formdat.append('hid',Hid);
            $.ajax({
                cache : true,
                type : "post",
                url : "/housech/relationnews",
                data : formdat,  // 你的formid
                // async : false,          //async 默认为true异步,修改为false为同步
                contentType: false,   //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作,从而失去分界符,而使服务器不能正常解析文件
                processData: false,   //当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
                error : function(request) {
                    layer.close(index);         //关闭数据提交信息
                    layer.alert("网络超时");
                },
                success : function(data) {
                    if (data.code == 200) {
                        layer.msg(data.msg);

                        // table.reload('loadnewsId');
                        TableClose();
                    } else {
                        layer.msg(data.msg);
                        TableClose();
                    }
                }
            });
            return false;
        }
            layer.msg('请选择资讯');
            return false;
    })

})