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;

    common.layuiTable.render(table,{
         elem: '#table-id'
        , id: 'table-id'
        , url: '/estate/parkingSpace/page' //模拟接口
        ,method:'get'
        , cellMinWidth: 80 //全局定义常规单元格的最小宽度，
        , cols: [[
             { field: 'number', title: '车位编号',align:'center', width: 200}
            , { field: 'parkName', title: '停车场名称',align:'center'}
            , { field: 'residentialName', title: '所属小区',align:'center'}
            , { field: 'carNumber', title: '占用车辆',align:'center'}
            , { field: 'type', title: '车位类型',align:'center', templet: function (d) {
                return common.getDictValue('parkinglot_type',d.type)
                }}
            , { field: 'isFree', title: '是否收费',align:'center',templet: function (d) {
                    if (d.isFree == 0){
                        return '是'
                    }else {
                        return '否'
                    }
                }}
            , { field: 'isIndoor', title: '是否室内',align:'center',templet: function (d) {
                    if (d.isIndoor == 0){
                        return '是'
                    }else {
                        return '否'
                    }
                }}
            , { field: 'createTime', title: '创建时间',align:'center', width: 300/*, templet: function (d){
                    var date = new Date(d.createTime).toJSON();
                    return new Date(+new Date(date)).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
                }*/}
            , {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/parkingSpace/editView/'+data.id
                });
                break;
                break;
            case "delete" :
                layer.confirm('是否确认删除？',{icon: 7, title:'提示'}, function(index){
                    common.sendAsyncRequest("/estate/parkingSpace/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/parkingSpace/addView';
        common.openDefaultIframe({
            title: '添加车位',
            content:  content
        });
    });
});

