layui.extend({
    common    :   'js/common'
}).use(['jquery','table','common','util','laydate','form'], function () {
    var $ = layui.jquery;
    var table = layui.table;
    var common=layui.common;
    var util=layui.util;
    var laydate=layui.laydate;
    var  form=layui.form;

    var dict = {
        type: [],
        isFree: [],
        isIndoor: []
    };

    common.layuiTable.render(table,{
         elem: '#table-id'
        , id: 'table-id'
        , url: '/estate/parkingLot/page' //模拟接口
        ,method:'get'
        , cellMinWidth: 80 //全局定义常规单元格的最小宽度，
        , cols: [[
             { field: 'parkKey', title: '车场编号',align:'center'}
            , { field: 'parkName', title: '停车场名称',align:'center'}
            , { field: 'agencyName', title: '所属社区',align:'center'}
            , { field: 'residentialName', title: '所属小区',align:'center'}
            , { field: 'spaceTotal', title: '车位总数',align:'center'}
            , { field: 'remainSpace', title: '空余车位',align:'center'}
            , { field: 'parkLinkman', title: '车场联系人',align:'center'}
            , { field: 'parkTel', title: '车场联系方式',align:'center'}
            , { field: 'regTime', title: '车场注册时间',align:'center', width: 180}
            , { field: 'createTime', title: '创建时间',align:'center', width: 180}
            , {title: '操作', width: 180, align: 'center', toolbar: '#table-useradmin-webuser'}
        ]]
        , page: true
    });

    table.on('tool(table-id)', function (obj) {
        var data = obj.data; //获得当前行数据
        switch (obj.event) {
            case "edit" :
                common.openDefaultIframe({
                    title: '修改车场信息',
                    content:  '/estate/parkingLot/editView/'+data.id
                });
                break;
            case "delete" :
                layer.confirm('是否确认删除？',{icon: 7, title:'提示'}, function(index){
                    common.sendAsyncRequest("/estate/parkingLot/delete",'POST',{
                        id  :  data.id
                    },function (result){
                        if(result.data){
                            //  刷新表格
                            queryTableFn();
                            layer.msg("删除成功");
                            layer.close(index);
                        }else{
                            layer.msg("删除失败");
                        }
                    });
                });
                break;
            default :
                break;
        }
    });

    var queryBeginDate,queryEndDate;
    var queryTableFn=function(){
        //执行重载
        table.reload('table-id', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: {
                beginDate:queryBeginDate,
                endDate:queryEndDate
            }
        });
    }

    //日期时间范围选择
    laydate.render({
        elem: '#laydate-range-datetime',
        format:'yyyy-MM-dd'
        ,range:true //或 range: '~' 来自定义分割字符
        ,done: function(value, date, endDate){
            if(value){
                var dates=value.split(" - ");
                queryBeginDate=dates[0] + ' 00:00:00';
                queryEndDate=dates[1] + ' 23:59:59';
            }else{
                queryBeginDate="";
                queryEndDate="";
            }
        }
    });

    //监听搜索
    form.on('submit(query)', function(){
        queryTableFn();
    });

    //监听重置查询
    form.on('submit(reset)', function(){
        //清空条件
        queryBeginDate="";
        queryEndDate="";
        $("#laydate-range-datetime").val("");
        // 刷新select标签
        form.render('select');
        queryTableFn();
    });

    //监听住户添加
    form.on('submit(add)', function(){
        let content = '/estate/parkingLot/addView';
        common.openDefaultIframe({
            title: '添加车场',
            content:  content
        });
    });
});

