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

    //  页面数据
    var pageData = {
        selectTreeId    :   $('#selectTreeId').val(),
        residentialId   :   $('#residentialId').val(),
    };
    layui.form.verify({
        specialCharacters : function (value) {
            if(!/^[a-zA-Z0-9_\u4e00-\u9fa5\s·]+$/.test(value)){
                return '名称不能有特殊字符';
            }
            if(/(^\_)|(\__)|(\_+$)/.test(value)){
                return '名称首尾不能出现下划线\'_\'';
            }
            if(/^\d+\d+\d$/.test(value)){
                return '名称不能全为数字';
            }
        },
        length : function (value) {
            if (value.length <2 || value.length > 20){
                return  '名称只能输入2-20个字符!'
            }
        },
        phoneNum : function (value) {
            if(value != ""){
                if(!/^1[3|4|5|7|8]\d{9}$/.test(value)){
                    return '请输入正确的手机号码!';
                }
            }
        },
        serial : function (value) {
            if (value[0] == 0){
                return '请输入不以0开头的数字！'
            }
            if(!/(^$)|^\d+$/.test(value)){
                return '只能填写数字!';
            }
        },
        personName : function (value) {
            if (value != ''){
                if (value.length < 2 || value.length >6){
                    return  '负责人名称只能输入2-6个字符!'
                }
            }
        },
    });

    //监听提交
    form.on('submit(submit_button)', function(data){
        common.timer($('button[lay-filter="submit_button"]'));
        common.sendAsyncRequest("/estate/unit/addUnit",'POST',data.field,callbackFn);
    });

    //回调函数
    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,
        paging: true,
        pageRemote: true,
        filterable: true,
        remoteMethod: function (val, cb, show,pageIndex) {
            $.ajax({
                type: 'GET',
                url: '/estate/residential/showResidentialList',
                data: {
                    name : val,
                    current : pageIndex,
                },
                success: function (result) {
                    cb(result.data.records,10);
                },
                error: function () {
                    cb([],0);
                }
            });
        },
        //监听选择
        on: function (data) {
            var change = data.change;
            if (change.length > 0) {
                isInitBuilidngFn(change[0].id)
            }
            if (!data.isAdd) {
            }
        },
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });

    var isInitBuilidngFn = function(residentialId){
        common.sendAsyncRequest("/estate/building/getListByResidentialId/"+residentialId,'GET',null,function(result){
            if( null == result.list || result.list.length == 0){
                // $("#org_pid_input").hide();
            }else{
                var node = $('#building_id');
                //  清空元素
                node.html('');
                node.append('<option value="">请选择</option>');
                result.list.forEach(function(item){
                    node.append('<option value="' + item.id + '">' + item.name + '</option>');
                });
                //重新渲染
                form.render('select','building_filter');
                node.next().find('dl dd').eq(0).click();
            }
        });
    };

    // (function() {
    //     if (pageData.residentialId != null){
    //         $.ajax({
    //             type: 'GET',
    //             url: '/estate/residential/findResidentialById/'+pageData.residentialId,
    //             success: function (result) {
    //                 residentialSelect.update({disabled:true,data:[result.data]}).setValue([pageData.residentialId])
    //             }
    //         });
    //     }
    // })();

});