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

    //定义加载层id
    var loadId;
    //图片服务器地址
    var fastDfsServer = $('#fastDfsServer').val();

    //  页面数据
    var pageData = {

    };

    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个字符!'
            }
        },
        cardNum : function (value) {
            if (value != ''){
                if (!/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(value)){
                    return  '请输入正确的身份证号码!'
                }
            }
        },
        phoneNum : function (value) {
            if(value != ''){
                // if(!/^1[3|4|5|7|8]\d{9}$/.test(value)){
                if(value.length != 11){
                    return '请输入正确的手机号码!';
                }
            }
        },
    });

    //监听提交
    form.on('submit(submit_button)', function(data){
        common.timer($('button[lay-filter="submit_button"]'));
        common.sendAsyncRequest("/estate/parkingSpace/add",'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);
    });

    //多图片上传
    upload.render({
        elem: '#passport_upload'
        ,url: '/common/file/upload'
        ,exts: 'jpg|png|jpeg'
        ,accept: 'images' //图片
        ,before: function(obj){
            $('#passport_upload').prev('.avatar').remove();
            //开启加载动画
           loadId = layer.msg('图片上传中', {
                icon: 16,
                shade: 0.4,
                time:false //取消自动关闭
            });
        }
        ,done: function(res){  //上传完毕
            //关闭加载动画
            layer.close(loadId);
            if(res.code=="200"){//上传成功
                 $('#passport_upload').before('<div class="avatar" style="display: inline-block;">' +
                     '<input type="hidden" name="passportImgUri" value="'+res.data.fullPath+'" />' +
                     '<img src="'+fastDfsServer+ res.data.fullPath +'" class="layui-upload-img" />' +
                     '</div>');
            }else{
                layer.msg("图片上传失败");
            }
        }
        ,error: function(){
            //关闭加载动画
            layer.close(loadId);
            layer.msg("图片上传失败");
        }
    });

    //多图片上传
    upload.render({
        elem: '#entry_upload'
        ,url: '/common/file/upload'
        ,exts: 'jpg|png|jpeg'
        ,accept: 'images' //图片
        ,before: function(obj){
            $('#entry_upload').prev('.avatar').remove();
            //开启加载动画
            loadId = layer.msg('图片上传中', {
                icon: 16,
                shade: 0.4,
                time:false //取消自动关闭
            });
        }
        ,done: function(res){  //上传完毕
            //关闭加载动画
            layer.close(loadId);
            if(res.code=="200"){//上传成功
                $('#entry_upload').before('<div class="avatar" style="display: inline-block;">' +
                    '<input type="hidden" name="entryImgUri" value="'+res.data.fullPath+'" />' +
                    '<img src="'+fastDfsServer+ res.data.fullPath +'" class="layui-upload-img" />' +
                    '</div>');
            }else{
                layer.msg("图片上传失败");
            }
        }
        ,error: function(){
            //关闭加载动画
            layer.close(loadId);
            layer.msg("图片上传失败");
        }
    });

    //车位类型
    $.ajax({
        type: 'GET',
        url: '/permissions/dict/getByCode/parkinglot_type',
        success: function (result) {
            typeDic.update({data: result.list})
        },
    });
    var typeDic = xmSelect.render({
        el: '#type',
        name: 'type',
        layVerify: 'required',
        radio: true,
        clickClose: true,
        prop: {
            name: 'dictValue',
            value: 'dictKey',
        },
        on: function (data) {},
        model: {
            label: {
                type: 'text',
                text: {
                    left: '',
                    right: '',
                    separator: ', ',
                },
            }
        },
    });

    //社区查询下拉列表
    var agencySelect = xmSelect.render({
        el: '#agency_id',
        name: 'agencyId',
        layVerify: 'required',
        radio: true,
        prop: {
            name: 'agencyName',
            value: 'id',
        },
        clickClose: true,
        paging: true,
        pageRemote: true,
        filterable: true,
        remoteMethod: function (val, cb, show,pageIndex) {
            $.ajax({
                type: 'GET',
                url: '/estate/agency/showAgencyList',
                data: {
                    name : val,
                    current : pageIndex,
                },
                success: function (result) {
                    cb(result.data.records,result.data.pages);
                },
                error: function () {
                    cb([],0);
                }
            });
        },
        //监听选择
        on: function (data) {
            var change = data.change;
            if (change.length > 0) {
                residentialSelect.update({data:initResidentialFn(change[0].id),disabled: false}).setValue([]);
            }
            if (!data.isAdd) {
                residentialSelect.update({disabled: true }).setValue([]);
            }
        },
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });

    //小区查询下拉列表
    var residentialSelect = xmSelect.render({
        el: '#residential_id',
        name: 'residentialId',
        layVerify: 'required',
        disabled: true,
        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: {
                    agencyId: agencySelect.getValue('valueStr'),
                    name : val,
                    current : pageIndex,
                },
                success: function (result) {
                    cb(result.data.records,result.data.pages);
                },
                error: function () {
                    cb([],0);
                }
            });
        },
        //监听选择
        on: function (data) {
            var change = data.change;
            if (change.length > 0) {

            }
            if (!data.isAdd) {

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

    function initResidentialFn(agencyId) {
        var list;
        common.sendSyncRequest("/estate/residential/showResidentialList", 'get', {agencyId: agencyId}, function (result) {
            list = result.data.records;
        });
        return list;
    }
});