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

    var laypage = layui.laypage //分页
        ,$ = layui.jquery
        ,layer = layui.layer //弹层
        ,table = layui.table //表格
        ,element = layui.element //元素操作
        ,form = layui.form
        ,upload = layui.upload;


    var tableIds = 'videolist';  //表格ID
    //第一个实例
    table.render({
        elem: '#'+tableIds
        ,url: '/push/rotationforms' //数据接口
        ,where:{token:csrfToken}
        ,method:'post'
        ,page: true //开启分页
        ,text:'数据加载中'
        ,id:'tablerReload'
        ,cols: [[ //表头
            {type: "checkbox", width:50},
            {type:'numbers',title:'序号',width:50},
            {field: 'title', title: '轮播标题', align:'center'},
            {field: 'name', title: '归属楼盘名称', align:'center'},
            {field: 'city_name', title: '所属区域', align:'center',width:100},
            {field: 'img', title: '图片',width:200, align:'center',templet:function(d){
                return '<img class="layui_magnify" src="'+d.img+'"/>'
            }}
            ,    {field: 'url', title: '地址',width:200, align:'center',templet:function(d){
                    return '<a href="'+d.url+'" target="_blank">'+d.url+'</a>';
                }}
            ,{field: 'expiration_date', title: '有效期',event:'expirationData',edit: 'text',width:150, align:'center'}
            ,{field: 'create_at', title: '添加时间',width:150, align:'center'}
            ,{field: 'sort', title: '排序',width:100, event:'sortData',edit: 'text', align:'center',sort: true,templet:"#sortBox"}
            ,{field: 'is_show', title: '审核',align:'center',width:100, templet:function (d) {
                    if(d.is_show == 2){
                        return  '<input type="checkbox"  value="'+d.id+'" name="open"  lay-skin="switch" lay-filter="switchTest" lay-text="|">'
                    } else {
                        return  '<input type="checkbox" checked  value="'+d.id+'" name="open"  lay-skin="switch" lay-filter="switchTest" lay-text="|">'
                    }
                }}
            ,{field: 'universal', title: '公共',align:'center',width:100, templet:function (d) {
                    if(d.universal == 0){
                        return  '<input type="checkbox"  value="'+d.id+'" data-universal="1" lay-skin="switch" lay-filter="switchTest1" lay-text="|">'
                    } else {
                        return  '<input type="checkbox" checked  value="'+d.id+'" data-universal="0"  lay-skin="switch" lay-filter="switchTest1" lay-text="|">'
                    }
                }}
            ,{fixed: 'right',title:'操作', width: 165, align:'center', toolbar: '#operate'}

        ]]
    });

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

    // 列表点击放大图片
    $('.layui-table-main').on('click','.layui_magnify', function(){
        var _src = $(this).attr('src');
        layui.layer.open({
            type: 1,
            title: false,           //是否显示标题
            area: ['95%', '55%'],
            shade: 0.6,           //透明度
            closeBtn: 1,         //按钮 1有,0无
            shadeClose: true,
            content: '<img style="width:100%;height:100%" src="'+_src+'">',
        });
    });


    //监听指定开关
    form.on('switch(switchTest)', function(data){
        layer.confirm('确定要修改此操作吗?',{btn: ['确定', '取消']},function () {
            $.post('/push/imgdelshow',{id:data.value,type:'show'},function (data) {
                layer.msg(data.msg);
                table.reload('tablerReload');

            })
        },function (aa) {
            table.reload('tablerReload');        //重新加载
        })
    });
    form.on('switch(switchTest1)', function(data){
        var universal = $(this).data('universal');
        layer.confirm('确定要修改此操作吗?',{btn: ['确定', '取消']},function () {
            $.post('/push/rotationuniversal',{id:data.value,universal:universal},function (data) {
                layer.msg(data.msg);
                table.reload('tablerReload');

            })
        },function (aa) {
            table.reload('tablerReload');        //重新加载
        })
    });

     //列表操作
    table.on('tool('+tableIds+')', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){
           var index = layer.open({
                type: 2,
                title: '修改轮播图',
                area:['800px','500px'],
                content: '/push/rotationedit?id='+data.id+'&house_name='+data.name,
                end:function () {
                    table.reload('tablerReload');        //重新加载
                }
            });
        } else if(layEvent === 'del') {
            layer.confirm('您确定要删除吗?',{   btn: ['确定', '取消']},function () {
                $.post('/push/imgdelshow',{id:data.id,type:'del'},function (data) {
                    layer.msg(data.msg);
                    table.reload('tablerReload');
                })
            })
        } else if(layEvent === 'sortData'){       //修改排序
            table.on('edit('+tableIds+')', function(obj){
                var data = obj.data;
                // console.log(data)
                $.post('/push/imgdelshow',{id:data.id,type:'sort',sort:data.sort},function (data) {
                    if(data.code == 200)
                    {
                        layer.msg(data.msg);
                    } else if(data.code == 300 ) {
                        layer.msg(data.msg);
                    }
                })
            })
        }else if(layEvent === 'expirationData'){
            table.on('edit('+tableIds+')', function(obj){
                var data = obj.data;
                $.post('/push/imgdelshow',{id:data.id,type:'expiration',expiration:data.expiration_date},function (data) {
                    if(data.code == 200)
                    {
                        layer.msg(data.msg);
                    } else if(data.code == 300 ) {
                        layer.msg(data.msg);
                    }
                })
            })
        }
    });

    //添加图片上传
    upload.render({
         elem: '#test1'
        ,url: '/house/createimg'
        ,data:{token:csrfToken}
        ,field:'img'
        ,size:500              //限制上传图片的大小,单位为KB
        ,exts: 'png|jpg|jpeg'     //只允许上传压缩文件
        ,auto:false
        ,bindAction:'#addcity'
        ,choose: function(obj){
            obj.preview(function(index, file, result){
                $('#breviary_img').attr('src', result); //图片链接(base64)
            });
        }
    });
    // 图片放大浏览
    $('.layui_magnify').click(function(){
        var _src = $(this).attr('src');
        layui.layer.open({
            type: 1,
            title: false,           //是否显示标题
            area: ['60%', '80%'],
            shade: 0.6,           //透明度
            closeBtn: 1,         //按钮 1有,0无
            shadeClose: true,
            content: '<img style="width:100%;height:100%" src="'+_src+'">',
        });
    })

     // 新增
     var $ = layui.$, active = {
        addData: function(){ //获取选中数据
            var index = layui.layer.open({
                type: 2,
                title: '添加轮播图',
                closeBtn: 1, //不显示关闭按钮
                area: ['800px','500px'],
                content: '/push/rotationadd' //iframe的url,no代表不显示滚动条
                ,success:function (layero, index) {
                },
                end:function () {
                    table.reload('tablerReload');
                }
            });
        },
        reload:function(){//搜索
            var form = pfgs_serialize($('form'));
            // var name = '';
            // var house_name   = '';
            // if(form.input_house == 'name'){
            //     name = form.input_house_text;
            // } else if(form.input_house == 'house_name'){
            //     house_name = form.input_house_text;
            // }
            table.reload('tablerReload',{
                where:{
                    title:form.input_house_text,
                    // house_name:house_name,
                    city:form.input_city,
                    type_name:form.type_name
                },
                page:{
                    curr:1
                }
            })
        },
    };
    $('.demoTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });


    $('.The_house').focus(function(){
        // $('form').append('<input type="hidden" name="aa" value="aaa">');
        indexOpen = parent.layui.layer.open({
            type: 2,
            title: '选择需要添加的楼盘',
            area: ['','100%'],
            content: '/publicuse/housename',
            success:function (layero, index) {
            },
            end:function(){
            }
        });
        parent.layui.layer.full(indexOpen);
        //改变窗口大小时,重置弹窗的宽高,防止超出可视区域(如F12调出debug的操作)
        $(window).on("resize",function(){
            parent.layui.layer.full(indexOpen);
        })
    })



    //添加页面提交数据
    form.on('submit(adds)', function(data){
        var formData = new FormData($('form')[0]);
       if(formData.getAll('img')[0].size == 0){
           layer.msg('请选择图片');
           return false;
       }
        var index = layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8});      //数据提交提示
        $.ajax({
            cache : false,
            type : "post",
            url : "/push/rotationform",
            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) {
                if(data.data != null){
                    layer.msg(ReturnInfo(data.data),{icon:5});
                    return ;
                }
                layer.msg(data.msg);
            }
        });
        return false;
    });

    form.on('submit(edit)', function(data){
        var index = layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8});      //数据提交提示
        // var oindex = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        var formData = new FormData($('form')[0]);
        $.ajax({
            type : "post",
            url : "/push/rotationeditform",
            data : formData,
            contentType: false,   //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作,从而失去分界符,而使服务器不能正常解析文件
            processData: false,   //当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
            error : function(request) {
                layer.close(index);
                layer.msg("请联系管理员");
            },
            success : function(data) {
                if(data.data != null){
                    layer.msg(ReturnInfo(data.data),{icon:5});
                    return ;
                }
                layer.msg(data.msg);
                parent.layer.close(oindex); //再执行关闭   
            }
        });
        return false;
    });

    //批量删除
    form.on('submit(del_alls_push)',function (data) {
        var checkStatus = table.checkStatus('tablerReload');
        if(typeof  checkStatus.data[0] == 'object'){
            layer.confirm('您确定要删除吗?',{btn:['确认','取消']},function(){
                var index = layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8});      //数据提交提示
                var formdat = new FormData();
                $.each(checkStatus.data,function (key,val) {
                    formdat.append('id[]',val.id);
                })
                $.ajax({
                    cache : true,
                    type : "post",
                    url : "/push/imgdelall",
                    data : formdat,  // 你的formid
                    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('tablerReload')
                        } else {
                            layer.msg(data.msg);
                        }
                    }
                });

            })
            return false;
        }
        layer.msg('请选择需要删除的轮播图',{icon:5});
        return false;
    });
})