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 fastDfsServer = $('#fastDfsServer').val()

    common.layuiTable.render(table, {
        elem: '#table-id'
        , id: 'table-id'
        , url: '/estate/car/page' //模拟接口
        , method: 'get'
        , cellMinWidth: 80 //全局定义常规单元格的最小宽度，
        , cols: [[
            {field: 'number', title: '车牌号', align: 'center'}
            , {field: 'personName', title: '车主姓名', align: 'center'}
            , {field: 'personPhone', title: '车主电话', align: 'center'}
            , {field: 'type', title: '车辆类型', align: 'center', templet: '#car_type'}
            , {field: 'carImg', title: '车辆照片', align: 'center', templet: '#car_img'}
            , {field: 'model', title: '车辆型号', align: 'center'}
            , {field: 'color', title: '车身颜色', align: 'center'}
            , {field: 'auditStatus', title: '审核状态', align: 'center', templet: function (d) {
                    if (d.auditStatus == 1){
                        return '审核通过'
                    }else if (d.auditStatus == 0){
                        return '待审核'
                    }else {
                        return '未通过审核'
                    }
                }}
            , {field: 'vehicleDrivingLicense1', title: '行驶证正面图片', align: 'center', width: 110, templet: '#vehicleDrivingLicense1'}
            , {field: 'vehicleDrivingLicense2', title: '行驶证背面图片', align: 'center', width: 110, templet: '#vehicleDrivingLicense2'}

            , {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 "application" :
                layer.confirm('审核是否通过？', {
                    btn: ['通过','不通过','取消'], //按钮
                    icon: 7,
                    title: '提示'
                }, function(){
                    common.sendAsyncRequest("/estate/car/edit", 'POST', {
                        id: data.id,
                        auditStatus: 1
                    }, function (result) {
                        if (result.data) {
                            //  刷新表格
                            queryTableFn();
                            layer.msg("审核成功");
                        } else {
                            layer.msg("审核失败");
                        }
                    });
                }, function(){
                    common.sendAsyncRequest("/estate/car/edit", 'POST', {
                        id: data.id,
                        auditStatus: -1
                    }, function (result) {
                        if (result.data) {
                            //  刷新表格
                            queryTableFn();
                            layer.msg("审核成功");
                        } else {
                            layer.msg("审核失败");
                        }
                    });
                });
                break;
            case "edit" :
                common.openDefaultIframe({
                    title: '修改车辆信息',
                    content: '/estate/car/editView/' + data.id
                });
                break;
            case "delete" :
                layer.confirm('是否确认删除？', {icon: 7, title: '提示'}, function (index) {
                    common.sendAsyncRequest("/estate/car/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/car/addView';
        common.openDefaultIframe({
            title: '添加车辆',
            content: content
        });
    });
});

