layui.extend({
    common    :   'js/common',
    xmSelect    :   'layui/dist/xm-select'
}).use(['jquery','layer','common','form','laydate','xmSelect'],function () {
    //  Jquery插件
    var $ = layui.jquery;
    //  弹窗插件
    var layer = layui.layer;
    //  引入表单插件
    var form = layui.form;
    //  引入公共模块
    var common = layui.common;

    var laydate = layui.laydate;
    //  页面数据
    var pageData = {
        residentialNode : $('#residential_id'),
        unitNode : $('#unit_id'),
        roomNode : $('#room_id'),

        selectTreeId     :   $('#selectTreeId').val(),
        flag : $('#flag').val()
    };

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

    //监听按钮
    form.on('submit(submit_button)', function(data){
        common.sendAsyncRequest("/estate/accessCard/addAccessCard",'POST',data.field,callbackFn);
    });

    var flag = true
    //下发门禁卡到一般授权设备上
    form.on('submit(save_push_button)', function(data){
        common.timer($('button[lay-filter="save_push_button"]'));
        data.field.deviceAccessType = 10
        var accessCardDto = {cardNo: data.field.cardNo,residentialId: data.field.residentialId};
        common.sendSyncRequest("/estate/accessCard/getAccessCardByCardNo",'POST', accessCardDto,selectbackFn);
        if (flag){
            common.sendSyncRequest("/estate/accessCard/addAccessCard",'POST',data.field,callbackFn);
        }
    });

    function selectbackFn(result) {
        if (result.data != null){
            layer.msg("该卡号已存在，不能添加")
            flag = false
            return;
        }else{
            flag = true;
        }
    }

    //回调函数
    function callbackFn(result) {
        if(result.data){
            layer.msg("设置成功",{
                time: 500 ,
                shade : [0.1 , '#000' , true]
            },function () {
                //获取当前弹窗出层的名字
                var index = parent.layer.getFrameIndex(window.name);
                //关闭弹窗层
                parent.layer.close(index);
                // 父页面刷新
                parent.location.reload();
            });
        }else {//添加失败
            layer.msg("设置失败");
        }
    };

    //取消按钮
    $('#cancel_btn').on("click",function(){
        //获取当前弹窗出层的名字
        var index = parent.layer.getFrameIndex(window.name);
        //关闭弹窗层
        parent.layer.close(index);
    });

    //小区查询下拉列表
    var residentialSelect = xmSelect.render({
        el: '#residential_id',
        name: 'residentialId',
        layVerify: 'required',
        radio: true,
        prop: {
            name: 'name',
            value: 'id',
        },
        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) {
                unitSelect.update({data : initUnitFn({residentialId : change[0].id}),disabled: false });
                roomSelect.update({data : []});
                //发卡对象类型-小区-3
                $('#type').val(3);
                $('#residential_name').val(data.arr[0].name);
                $('#unit_name').val('');
                $('#room_name').val('');
            }
            if (!data.isAdd) {
                unitSelect.update({disabled : true });
                roomSelect.update({disabled : true});
            }
            // unitSelect.setValue([]);
            // roomSelect.setValue([]);

        },
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });

    //单元查询下拉列表
    var unitSelect = xmSelect.render({
        el: '#unit_id',
        name: 'unitId',
        // layVerify: 'required',
        disabled: true,
        radio: true,
        prop: {
            name: 'name',
            value: 'id',
        },
        clickClose: true,
        filterable: true,
        remoteSearch: true,
        remoteMethod: function (val, cb, show) {
            $.ajax({
                type: 'GET',
                url: '/estate/unit/getListByLikeName',
                data: {
                    name : val,
                    residentialId : pageData.flag==2?pageData.selectTreeId:residentialSelect.getValue('valueStr'),
                    isSelect : true
                },
                success: function (result) {
                    cb(result.list);
                },
                error: function () {
                    cb([]);
                }
            });
        },
        //监听选择
        on: function (data) {
            var change = data.change;
            if (change.length > 0) {
                roomSelect.update({data : initRoomFn({residentialId : residentialSelect.getValue('valueStr'),unitId : change[0].id}) , disabled : false});
                //发卡对象类型-单元-2
                $('#type').val(2);
                $('#unit_name').val(data.arr[0].name);
                $('#room_name').val('');
            }
            if (!data.isAdd) {
                roomSelect.update({data : [] , disabled : true});
            }
            // roomSelect.setValue([]);

        },
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });

    //房间查询下拉列表
    var roomSelect = xmSelect.render({
        el: '#room_id',
        name: 'roomId',
        // layVerify: 'required',
        disabled: true,
        radio: true,
        prop: {
            name: 'name',
            value: 'id',
        },
        clickClose: true,
        filterable: true,
        remoteSearch: true,
        remoteMethod: function (val, cb, show) {
            $.ajax({
                type: 'GET',
                url: '/estate/room/getListByLikeName',
                data: {
                    name : val,
                    residentialId : pageData.flag==2?pageData.selectTreeId:residentialSelect.getValue('valueStr'),
                    unitId : pageData.flag==4?pageData.selectTreeId:unitSelect.getValue('valueStr'),
                    isSelect : true
                },
                success: function (result) {
                    cb(result.list);
                },
                error: function () {
                    cb([]);
                }
            });
        },
        //监听选择
        on: function (data) {
            var change = data.change;
            if (change.length > 0) {
                $('#type').val(1);
                $('#room_name').val(data.arr[0].name);
            }
            if (!data.isAdd) {
            }
            //发卡对象类型-房间-1

        },
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });
    function initUnitFn(data) {
        var unitList;
        common.sendSyncRequest("/estate/unit/getListByLikeName", 'get', {residentialId : data.residentialId,id : data.id,isSelect : true}, function (result) {
            unitList = result.list;
        });
        return unitList;
    }
    function initRoomFn(data) {
        var roomList;
        common.sendSyncRequest("/estate/room/getListByLikeName", 'get', {residentialId : data.residentialId,unitId : data.unitId,id : data.id,isSelect : true}, function (result) {
            roomList = result.list;
        });
        return roomList;
    }

    //小区和单元赋值
    (function() {
        if (pageData.selectTreeId != null){
            switch (pageData.flag) {
                case '2'://小区
                    $.ajax({
                        type: 'GET',
                        url: '/estate/residential/findResidentialById/'+pageData.selectTreeId,
                        success: function (result) {
                            residentialSelect.update({disabled:false,data:[result.data]}).setValue([pageData.selectTreeId]);
                            unitSelect.update({disabled: false });
                        }
                    });
                    break;
                case '4'://单元
                    $.ajax({
                        type: 'GET',
                        url: '/estate/unit/findUnitById/'+pageData.selectTreeId,
                        success: function (result) {
                            $.ajax({
                                type: 'GET',
                                url: '/estate/residential/findResidentialById/'+result.data.residentialId,
                                success: function (result1) {
                                    residentialSelect.update({disabled:false,data : [result1.data]}).setValue([result.data.residentialId])
                                }
                            });
                            unitSelect.update({disabled:false,data:[result.data]}).setValue([pageData.selectTreeId]);
                            roomSelect.update({disabled : false});
                        }
                    });
                    break;
                case '5'://房间
                    $.ajax({
                        type: 'GET',
                        url: '/estate/room/findRoomById/'+pageData.selectTreeId,
                        success: function (result) {

                            $.ajax({
                                type: 'GET',
                                async: false,
                                url: '/estate/residential/findResidentialById/'+result.data.residentialId,
                                success: function (result2) {
                                    residentialSelect.update({disabled:false,data:[result2.data]}).setValue([result.data.residentialId])
                                }
                            });
                            $.ajax({
                                type: 'GET',
                                async: false,
                                url: '/estate/unit/findUnitById/'+result.data.unitId,
                                success: function (result1) {
                                    unitSelect.update({disabled:false,data:[result1.data]}).setValue([result.data.unitId]);
                                }
                            });
                            roomSelect.update({disabled:false,data:[result.data]}).setValue([pageData.selectTreeId]);
                        }
                    });
                    break;
                default:
                    break;
            }
        }
    })();

});
