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

    var layer = layui.layer //弹层
        ,table = layui.table //表格
        ,element = layui.element //元素操作
        ,form = layui.form
        ,upload = layui.upload;
    //第一个实例
    var indexTtable =table.render({
        elem: '#housefacilitieslist'
        ,url: '/dictionary/facilitiesform' //数据接口
        ,where:{token:csrfToken}
        ,method:'post'
        ,page: true //开启分页
        ,text:'数据加载中'
        ,id:'tablerReload'
        ,cols: [[ //表头
            // {field: 'id', title: 'ID', width:80, sort: true, fixed: 'left',align:'center'},
            {type:'numbers'},
            {field: 'name', title: '户型',align:'center'}
            ,{field: 'sort', title: '排序',width:200, event:'sortData',edit: 'text',align:'center',sort: true,templet:"#sortBox"}
            ,{field: 'is_show', title: '审核',align:'center',width:200, 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: 'create_at', title: '添加时间', width:120,align:'center'}
            ,{fixed: 'right',title:'操作', width: 165, align:'center', toolbar: '#operate'}

        ]]
    });

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

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

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

    //列表操作
    table.on('tool(housefacilitieslist)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){
            layer.open({
                type: 2,
                title: '修改配套信息',
                closeBtn: 1, //不显示关闭按钮
                shade: [0],
                area: ['600px', '500px'],
                anim: 2,
                content: ['/dictionary/facilitiessave?id='+data.id, 'yes'], //iframe的url,no代表不显示滚动条
            });
        } else if(layEvent === 'del') {
            layer.confirm('您确定要删除吗?',{   btn: ['确定', '取消']},function () {
                $.post('/dictionary/facilitiesdel',{'id':data.id},function (data) {
                    if(data.code == 200)
                    {
                        layer.msg(data.msg);
                        table.reload('tablerReload');
                    } else if(data.code == 300 ) {
                        layer.msg(data.msg);
                    }
                })
            })
        }else if(layEvent === 'sortData'){       //修改排序
            table.on('edit(housefacilitieslist)', function(obj){
                var data = obj.data;
                $.post('/dictionary/facilitiessort',{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);
                    }
                })
            })
        }
    });

    var $ = layui.$, active = {
        addData: function(){ //获取选中数据

            layer.open({
                type: 2,
                title: '添加配套信息',
                closeBtn: 1, //不显示关闭按钮
                shade: [0],
                area: ['600px', '500px'],
                anim: 2,
                content: ['/dictionary/facilitiesadd', 'yes'], //iframe的url,no代表不显示滚动条
            });

            // var checkStatus = table.checkStatus('idTest')
            //     ,data = checkStatus.data;
            // layer.alert(JSON.stringify(data));
        }
    };

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


    //添加图片上传
    upload.render({
        elem: '#test1'
        ,url: '/house/createimg'
        ,data:{token:csrfToken}
        ,field:'img'
        ,size:1000              //限制上传图片的大小,单位为KB
        ,exts: 'png|jpg|jpeg'     //只允许上传压缩文件
        ,auto:false
        ,bindAction:'#addcity'
        ,choose: function(obj){
            obj.preview(function(index, file, result){
                $('#demo1').attr('src', result); //图片链接(base64)
            });
        }
    });

    //添加图片上传
    upload.render({
        elem: '#test2'
        ,url: '/house/createimg'
        ,data:{token:csrfToken}
        ,field:'mobile_img'
        ,size:1000              //限制上传图片的大小,单位为KB
        ,exts: 'png|jpg|jpeg'     //只允许上传压缩文件
        ,auto:false
        ,bindAction:'#addcity'
        ,choose: function(obj){
            obj.preview(function(index, file, result){
                $('#demo2').attr('src', result); //图片链接(base64)
            });
        }
    });

    //添加图片上传
    upload.render({
        elem: '#test3'
        ,url: '/house/createimg'
        ,data:{token:csrfToken}
        ,field:'map_img'
        ,size:1000              //限制上传图片的大小,单位为KB
        ,exts: 'png|jpg|jpeg'     //只允许上传压缩文件
        ,auto:false
        ,bindAction:'#addcity'
        ,choose: function(obj){
            obj.preview(function(index, file, result){
                $('#demo3').attr('src', result); //图片链接(base64)
            });
        }
    });


    //修改
    form.on('submit(save_form)', function(data){
        var index = layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8});      //数据提交提示

        var formData = new FormData($('#facilities_form')[0]);
        $.ajax({
            cache : true,
            type : "post",
            url : '/dictionary/facilitiessaveform',
            data : formData,  // 你的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);
                    parent.layui.table.reload('housefacilitieslist');
                    TableClose();
                } else if(data.code == 300){
                    layer.msg(data.msg);
                    layer.msg(ReturnInfo(data.data));
                }
            }
        });
        return false;
    });

    // 点击放大图片
    $('.layui_magnify').on('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+'">',
        });
    });


    //添加
    form.on('submit(add_form)', function(data){
        var index = layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8});      //数据提交提示
        var formData = new FormData($('#facilities_form')[0]);
        $.ajax({
            cache : true,
            type : "post",
            url : '/dictionary/facilitiesaddform',
            data : formData,  // 你的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);
                    parent.layui.table.reload('tablerReload');
                    // TableClose();
                } else if(data.code == 300){
                    layer.msg(data.msg);
                    layer.msg(ReturnInfo(data.data),{icon:5});
                }
            }
        });
        return false;
    });

});