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

    var houid = $('input[name=hid]').val();
    var houname = $('input[name=name]').val();
    //调用编辑器
    var tableIds = 'housespriceList';
    //楼盘列表
    var tableIns = table.render({
        elem: '#'+tableIds,
        url : '/housech/priceviewform',
        cellMinWidth : 95,
        where:{hid:houid},
        method:'post',
        page : true,
        height : 600,
        limit : 20,
        id:'pricetablerReload',
        cols : [[
            {type:'numbers',fixed:"left"},
            {type: "checkbox",fixed:"left", width:50},
            {field: 'create_time', title: '记录时间', align:"center",sort: true},
            {field: 'price', title: '均价', width:120, align:"center",sort: true},
            {field: 'price_unit', title: '单位', width:120, align:"center",sort: true},
            {field: 'min_price', title: '最低价', width:120, align:"center",sort: true},
            {field: 'explain', title: '价格说明', width:200, align:"center",sort: true},
            {title: '操作', width:150, templet:'#housesListBar',fixed:"right",align:"center"}
        ]]
    });

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

    var $ = layui.$, active = {
        addData: function(){ //获取选中数据
            var index = parent.layui.layer.open({
                title : "添加新价格",
                type : 2,
                area:['','100%'],
                maxmin: true,
                content : "/housech/priceadd?hid="+houid+'&name='+houname,
                end:function(){
                    layui.table.reload('pricetablerReload');
                }
            })

            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,
                    city:form.input_city
                }
            })
        }

    };

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

    //日期
    laydate.render({
        elem: '#date1'
    });



    //列表操作
    table.on('tool('+tableIds+')', function(obj){
        var data = obj.data;
   if(obj.event  === 'edit'){
            var index = parent.layui.layer.open({
                title : "修改价格信息",
                type : 2,
                area:['','100%'],
                maxmin: true,
                content : "/housech/priceedit?id="+data.id+'&name='+houname,
                end:function(){
                    layui.table.reload('pricetablerReload');
                }
            })

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

            layer.confirm('您确认要删除吗?',{btn:['确认','取消']},function () {
                $.post('/housech/housedel',{id:data.id},function (data) {
                    if (data.code == 200) {
                        layer.msg(data.msg);
                        // parent.layui.
                        table.reload('tablerReload');
                        // TableClose();
                    } else {
                        layer.msg(data.msg);
                    }
                })
            })
        }
    });


    form.on('submit(pricetijiao)',function (data) {

        var index = layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8});      //数据提交提示

        var formData = new FormData($('#price_form')[0]);
        console.log(formData)

        $.ajax({
            cache : true,
            type : "post",
            url : "/housech/priceaddform",
            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);
                    TableClose();

                } else if(data.code == 300){
                    layer.msg(data.msg);
                    layer.msg(ReturnInfo(data.data),{icon:5});
                }
            }
        });
        return false;
    })


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

        var formData = new FormData($('#price_form')[0]);
        $.ajax({
            cache : true,
            type : "post",
            url : "/housech/priceeditform",
            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) {
                layer.close(index);         //关闭数据提交信息
                if (data.code == 200) {
                    layer.msg(data.msg);
                    // parent.layui.table.reload('pricetablerReload');
                    TableClose();
                } else if(data.code == 300){
                    layer.msg(data.msg);
                    layer.msg(ReturnInfo(data.data),{icon:5});

                }
            }
        });
        return false;
    })



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

        var checkStatus = table.checkStatus('pricetablerReload');
        if(typeof  checkStatus.data[0] == 'object'){
            // var Hid = $('#houseId').val();
            var formdat = new FormData();
            $.each(checkStatus.data,function (key,val) {
                formdat.append('id[]',val.id);
            })
            $.ajax({
                cache : true,
                type : "post",
                url : "/housech/pricedel",
                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);
                        layui.table.reload('pricetablerReload');
                    } else if(data.code == 300){
                        layer.msg(data.msg);
                        layer.msg(ReturnInfo(data.data),{icon:5});
                    }
                }
            });
            return false;
        }
        layer.msg('请选择需要删除的行');
        return false;
        // $.ajax({
        //     cache : true,
        //     type : "post",
        //     url : "/house/priceeditform",
        //     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('pricetablerReload');
        //             TableClose();
        //         } else if(data.code == 300){
        //             layer.msg(data.msg);
        //             layer.msg(ReturnInfo(data.data),{icon:5});
        //
        //         }
        //     }
        // });
        // return false;
    })


})

$(function(){
    // 动态添加批量删除按钮
    var _btn = '<div class="layui-inline " style="float:left;position:relative;left:10px;bottom:35px">'
              + '<a class="layui-btn layui-btn-danger search_btn layui_w_btn" style="height:30px;line-height:30px;font-size:14px;" lay-submit="" lay-filter="del_price">删除</a>'
              + '</div>'
    $('.layui-form').append(_btn)
})