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

    var laypage = layui.laypage //分页
        ,$ = layui.jquery
        ,layer = layui.layer //弹层
        ,table = layui.table //表格
        ,form = layui.form
        ,laydate = layui.laydate;

    var cid = $('input[name="cid"]').val();
    //第二个实例--区域历史价格记录
    table.render({
        elem: '#recordcity'
        ,url: '/pricetrend/recordpriceform' //数据接口
        ,where:{token:csrfToken,city_id:cid}
        ,method:'post'
        ,page: true //开启分页
        ,text:'数据加载中'
        ,id:'tablerReloadss'
        ,limit:20
        ,cols: [[ //表头
            {type:'numbers'}
            ,{field: 'price', title: '价格',align:'center'}
            ,{field: 'price_at', title: '时间',align:'center'}
            ,{field: 'contrast', title: '价格对比',align:'center'}
            ,{field: 'trend', title: '趋势',align:'center'}
            ,{fixed: 'right',title:'操作', width: 165, align:'center', toolbar: '#operate'}
        ]]
    });

    //列表操作
  var indexs =  table.on('tool(recordcity)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){
            layer.open({
                type: 2,
                title: '修改',
                closeBtn: 1, //不显示关闭按钮
                area: ['550px', '400px'],
                anim: 2,
                content: '/pricetrend/priceedit?id='+data.id,
                end:function () {
                        table.reload('tablerReloadss');
                }
            });
        } else if(layEvent === 'del') {
            layer.confirm('您确定要删除吗?',{   btn: ['确定', '取消']},function () {
                $.post('/pricetrend/pricedel',{'id':data.id},function (data) {

                    if(data.data != null){
                        layer.msg(ReturnInfo(data.data),{icon:5});
                        return ;
                    }
                    layer.msg(data.msg);
                    table.reload('tablerReloadss');
                });
            });
        }
    });


    //
    //
    // var $ = layui.$, active = {
    //     addData: function(){ //获取选中数据
    //         layer.open({
    //             type: 2,
    //             title: '添加价格',
    //             closeBtn: 1, //不显示关闭按钮
    //             shade: [0],
    //             area: ['780px', '550px'],
    //             anim: 2,
    //             content: '/pricetrend/priceadd',
    //         });
    //     }
    // };
    //日期
    laydate.render({
        elem: '#date1'
    });
    $('.demoTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    // $('.The_house').focus(function(){
    //     indexOpen = parent.layui.layer.open({
    //         type: 2,
    //         title: '选择需要添加的区域',
    //         area: ['','100%'],
    //         content: '/publicuse/city?type=1',
    //         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(editcity)', function(data){

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

        var formData = new FormData($('form')[0]);
        $.ajax({
            cache : true,
            type : "post",
            url : '/pricetrend/priceeditform',
            data : formData,  // 你的formid
            contentType: false,   //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作,从而失去分界符,而使服务器不能正常解析文件
            processData: false,   //当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
            error : function(request) {
                layer.alert("网络超时");
            },
            success : function(data) {
                if(data.data != null){
                    layer.msg(ReturnInfo(data.data),{icon:5});
                    return ;
                }
                layer.msg(data.msg);

            }
        });
        return false;
    });


});