layui.extend({
    common: 'js/common',
    orgTree: 'layui/lay/mymodules/org-tree',
    region: 'layui/lay/mymodules/region',
    xmSelect: 'layui/dist/xm-select',
    dropdown: 'layui/lay/modules/dropdown'
}).use(['tree', 'table', 'jquery', 'layer', 'common', 'orgTree', 'region', 'form', 'dropdown', 'xmSelect', 'laydate'], function () {
    //  弹窗插件
    var layer = layui.layer;
    //  Jquery插件
    var $ = layui.jquery;
    //  引入公共模块
    var common = layui.common;
    //  引入表格
    var table = layui.table;
    //  引入表单
    var form = layui.form;
    //  引入下拉列表组件
    var xmSelect = layui.xmSelect;
    var laydate = layui.laydate;
    var dropdown = layui.dropdown;

    var layuiTable = null;

    // 查询框表单
    var $node = {
        //  小区搜索
        residentialId: $('#residential_id'),
        //  单元搜索
        unitId: $('#unit_id'),
        //  房间搜索
        roomId: $('#room_id'),
        //卡号
        cardNoInput: $('input[name="cardNo"]'),
        //姓名
        nameInput: $('input[name="name"]'),
        //  有效期开始日期
        beginDateInput: $('input[name="beginDate"]'),
        //  有效期截至日期
        endDateInput: $('input[name="endDate"]'),
        cardDate: $('#card_date')
    };

    //初始化
    var layuiTable = common.layuiTable.render(layui.table, {
        elem: '#table_id'
        , url: '/estate/card/showCardList'
        , cellMinWidth: 80 //全局定义常规单元格的最小宽度，
        , cols: [[
            {field: 'cardNo', title: '卡号', align: 'center'}
            , {field: 'beginDate', title: '卡有效开始时间', align: 'center'}
            , {field: 'endDate', title: '卡有效结束时间', align: 'center'}
            , {
                field: 'type', title: '卡类型', align: 'center', templet: function (d) {
                    if (d.type === 0) {
                        return '住户';
                    }
                    if (d.type === 1) {
                        return '房间';
                    }
                    if (d.type === 2) {
                        return '单元';
                    }
                    if (d.type === 3) {
                        return '小区';
                    }
                    if (d.type === 4) {
                        return '服务人员';
                    }
                }
            }
            , {field: 'userName', title: '住户名称', align: 'center'}
            , {field: 'residentialName', title: '小区名称', align: 'center'}
            , {field: 'unitName', title: '单元名称', align: 'center'}
            , {field: 'roomName', title: '房间名称', align: 'center'}
            , {title: '操作', width: 180, align: 'center', toolbar: '#table-useradmin-webuser'}
        ]]
        //  开启分页功能
        , page: true
    });

    //日期时间范围
    laydate.render({
        elem: '#card_date'
        , type: 'datetime'
        , range: true
        , min: '1990-1-1 00:00:00'
        , done: function (value, date, endDate) {
            var dateArray = value.split(" - ");
            $("#begin_date").val(dateArray[0]);
            $("#end_date").val(dateArray[1]);
        }
    });

    //  重新加载表格
    var reloadTable = function (obj) {
        //重新加载表格
        layuiTable.reload({
            where: {
                residentialId: $node.residentialId.val(),
                unitId: $node.unitId.val(),
                roomId: $node.roomId.val(),
                cardNo: $node.cardNoInput.val(),
                name: $node.nameInput.val(),
                beginDate: $node.beginDateInput.val(),
                endDate: $node.endDateInput.val(),
                // //排序
                // sortField: obj ? obj.field : '',
                // sortOrder: obj ? obj.type : '',
            }
            //重新从第1页开始
            , page: {
                curr: 1
            },
            initSort: obj
        });
    };


    //排序
    layui.table.on('sort(table_event)', function (obj) {
        reloadTable(obj);
    });


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

    //监听重置查询
    form.on('submit(reset)', function () {
        residentialSelect.setValue([]);
        unitSelect.update({data: [], disabled: true});
        roomSelect.update({data: [], disabled: true});
        $node.residentialId.val(''),
            $node.unitId.val(''),
            $node.roomId.val(''),
            $('#card_date').val(''),
            $node.cardNoInput.val(''),
            $node.nameInput.val(''),
            $node.beginDateInput.val(''),
            $node.endDateInput.val(''),
            reloadTable();
    });

    table.on('tool(table_event)', function (obj) {
        var data = obj.data; //获得当前行数据
        switch (obj.event) {
            case 'edit'://加入黑名单
                layer.confirm('确定把这张门禁卡设为黑名单么？', function (index) {
                    common.sendAsyncRequest("/estate/card/updateBlackCard/"+data.id, 'GET', "", function (result) {
                        if (result.data) {
                            layuiTable.reload();
                            layer.msg("操作成功");
                            layer.close(index);//关闭弹窗提示
                        } else {
                            layer.msg("操作失败");
                        }
                    });
                });
                break;
            case 'cancel'://取消黑名单
                layer.confirm('确定把这张门禁卡取消黑名单么？', function (index) {
                    common.sendAsyncRequest("/estate/card/cancelBlackCard/"+data.id, 'GET', "", function (result) {
                        if (result.data) {
                            layuiTable.reload();
                            layer.msg("操作成功");
                            layer.close(index);//关闭弹窗提示
                        } else {
                            layer.msg("操作失败");
                        }
                    });
                });
                break;
            case 'xiafa'://手动下发
                layer.confirm('是否确定下发这张门禁卡？', function (index) {
                    common.sendAsyncRequest("/estate/card/issueCard/"+data.id, 'GET', "", function (result) {
                        if (result.data) {
                            layuiTable.reload();
                            layer.msg("操作成功");
                            layer.close(index);//关闭弹窗提示
                        } else {
                            layer.msg("操作失败");
                        }
                    });
                });
                break;
            default:
                break;
        }
    });

    //小区查询下拉列表
    var residentialSelect = xmSelect.render({
        el: '#residential_name',
        radio: true,
        prop: {
            name: 'name',
            value: 'id',
        },
        toolbar: {
            show: true,
            list: [{
                name: '清空',
                method() {
                    residentialSelect.setValue([]);
                    $node.residentialId.val('');
                    unitSelect.update({disabled: true}).setValue([]);
                    $node.unitId.val('');
                    roomSelect.update({disabled: true}).setValue([]);
                    $node.roomId.val('');
                }
            }]
        },
        clickClose: true,
        filterable: true,
        remoteSearch: true,
        remoteMethod: function (val, cb, show) {
            $.ajax({
                type: 'GET',
                url: '/estate/residential/getListByLikeName',
                data: {
                    name: val,
                    isSelect: true
                },
                success: function (result) {
                    cb(result.list);
                },
                error: function () {
                    cb([]);
                }
            });
        },
        //监听选择
        on: function (data) {
            var change = data.change;
            if (change.length > 0) {
                $node.residentialId.val(change[0].id)
                unitSelect.update({data: initUnitFn(change[0].id), disabled: false});
                roomSelect.update({data: []});
            }
            if (!data.isAdd) {
                $node.residentialId.val('');
                unitSelect.update({disabled: true});
                roomSelect.update({disabled: true});
            }
            $node.unitId.val('');
            $node.roomId.val('');
        },
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });

    //单元查询下拉列表
    var unitSelect = xmSelect.render({
        el: '#unit_name',
        disabled: true,
        tips: '请先选择小区',
        radio: true,
        prop: {
            name: 'name',
            value: 'id',
        },
        toolbar: {
            show: true,
            list: [{
                name: '清空',
                method() {
                    unitSelect.setValue([]);
                    $node.unitId.val('');
                    roomSelect.update({disabled: true}).setValue([]);
                    $node.roomId.val('');
                }
            }]
        },
        clickClose: true,
        filterable: true,
        remoteSearch: true,
        remoteMethod: function (val, cb, show) {
            $.ajax({
                type: 'GET',
                url: '/estate/unit/getListByLikeName',
                data: {
                    name: val,
                    residentialId: $node.residentialId.val(),
                    isSelect: true
                },
                success: function (result) {
                    cb(result.list);
                },
                error: function () {
                    cb([]);
                }
            });
        },
        //监听选择
        on: function (data) {
            var change = data.change;
            if (change.length > 0) {
                $node.unitId.val(change[0].id);
                roomSelect.update({data: initRoomFn(change[0].residentialId, change[0].id), disabled: false});
            }
            if (!data.isAdd) {
                $node.unitId.val('');
                roomSelect.update({data: [], disabled: true});
            }
            $node.roomId.val('');
        },
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });

    //房间查询下拉列表
    var roomSelect = xmSelect.render({
        el: '#room_name',
        disabled: true,
        tips: '请先选择小区和单元',
        radio: true,
        prop: {
            name: 'name',
            value: 'id',
        },
        toolbar: {
            show: true,
            list: [{
                name: '清空',
                method() {
                    roomSelect.setValue([]);
                    $node.roomId.val('');
                }
            }]
        },
        clickClose: true,
        filterable: true,
        remoteSearch: true,
        remoteMethod: function (val, cb, show) {
            $.ajax({
                type: 'GET',
                url: '/estate/room/getListByLikeName',
                data: {
                    name: val,
                    residentialId: $node.residentialId.val(),
                    unitId: $node.unitId.val(),
                    isSelect: true
                },
                success: function (result) {
                    cb(result.list);
                },
                error: function () {
                    cb([]);
                }
            });
        },
        //监听选择
        on: function (data) {
            var change = data.change;
            if (change.length > 0) {
                $node.roomId.val(change[0].id)
            }
            if (!data.isAdd) {
                $node.roomId.val('');
            }
        },
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });

    function initResidentialFn() {
        var residentialList;
        common.sendSyncRequest("/estate/residential/getListByLikeName", 'get', {
            selectTreeId: selectTreeId,
            isSelect: true
        }, function (result) {
            residentialList = result.list;
        });
        return residentialList;
    }

    function initUnitFn(residentialId) {
        var unitList;
        common.sendSyncRequest("/estate/unit/getListByLikeName", 'get', {
            residentialId: residentialId,
            isSelect: true
        }, function (result) {
            unitList = result.list;
        });
        return unitList;
    }

    function initRoomFn(residentialId, unitId) {
        var roomList;
        common.sendSyncRequest("/estate/room/getListByLikeName", 'get', {
            residentialId: residentialId,
            unitId: unitId,
            isSelect: true
        }, function (result) {
            roomList = result.list;
        });
        return roomList;
    }
});
