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 tableId = 'citylist';
    //第一个实例
    table.render({
        elem: '#'+tableId
        ,url: '/pricetrend/homeform' //数据接口
        ,where:{token:csrfToken}
        ,method:'post'
        ,page: true //开启分页
        ,text:'数据加载中'
        ,id:'tablerReload'
        ,height : 600
        ,limit:15
        ,cols: [[ //表头
            {type:'numbers'}
            ,{field: 'city_name', title: '区域名称',align:'center'}
			,{field: 'count', title: '价格数量',align:'center'}
            ,{fixed: 'right',title:'操作', width: 165, align:'center', toolbar: '#operate'}
        ]]
    });

    //列表操作
    table.on('tool('+tableId+')', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){
            indexOpen = layui.layer.open({
                type: 2,
                title: '[<span style="color:red">'+data.city_name+'</span>]区域历史价格',
                area: ['','100%'],
                content: '/pricetrend/recordprice?cid='+data.city_id,
                success:function (layero, index) {
                }
            });
            layui.layer.full(indexOpen);
            //改变窗口大小时,重置弹窗的宽高,防止超出可视区域(如F12调出debug的操作)
            $(window).on("resize",function(){
                layui.layer.full(indexOpen);
            })
        } else if(layEvent === 'del') {
            layer.confirm('您确定要删除吗?',{   btn: ['确定', '取消']},function () {
                $.post('/categorycity/delform',{'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 === 'add') {
             layer.open({
                type: 2,
                title: '添加价格',
                closeBtn: 1, //不显示关闭按钮
                shade: [0],
                area: ['780px', '550px'],
                anim: 2,
                content: '/pricetrend/lineadd?cid='+data.city_id+'&city_name='+data.city_name,
            });
        }
    });

    var $ = layui.$, active = {
        addData: function(){ //获取选中数据
            layer.open({
                type: 2,
                title: '添加价格',
                closeBtn: 1, //不显示关闭按钮
                shade: [0],
                area: ['780px', '550px'],
                anim: 2,
                content: '/pricetrend/priceadd.html',
            });
        },
        reload:function () {
            var formdara = pfgs_serialize($('form'));
            table.reload('tablerReload',{
                where:{
                    city_id:formdara.input_city
                }
            })
            // console.log(formdara);
        }
    };
    //日期
    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){

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

        var formData = new FormData($('form')[0]);
        $.ajax({
            cache : true,
            type : "post",
            url : '/categorycity/edithomeform',
            data : formData,  // 你的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.data != null){
                    layer.msg(ReturnInfo(data.data),{icon:5});
                    return ;
                }
                layer.msg(data.msg);
                parent.layui.table.reload('tablerReload');

            }
        });
        return false;
    });

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

        var formData = new FormData($('form')[0]);
        $.ajax({
            cache : true,
            type : "post",
            url : '/pricetrend/priceaddform',
            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);
                parent.layui.table.reload('tablerReload');
            }
        });
        return false;
    });

});