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;
    //定义加载层id
    var loadId;
    //图片服务器地址
    var fastDfsServer = $('#fastDfsServer').val();
    var xmSelect = layui.xmSelect;

    $.ajax({
        type: 'GET',
        url: '/permissions/dict/getByCode/userType',
        success: function (result) {
            userTypeDic.update({data:result.list}).setValue([$('#typeValue').val()])
        },
    });
    var userTypeDic = 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: ', ',
                },
            }
        },
    });
    $.ajax({
        type: 'GET',
        url: '/permissions/dict/getByCode/sex',
        success: function (result) {
            sexDic.update({data:result.list}).setValue([$('#sexValue').val()])
        },
    });
    var sexDic = xmSelect.render({
        el: '#sex_dic',
        name: 'sex',
        layVerify: 'required',
        radio: true,
        clickClose: true,
        prop: {
            name: 'dictValue',
            value: 'dictKey',
        },
        on: function (data) {},
        model: {
            label: {
                type: 'text',
                text: {
                    left: '',
                    right: '',
                    separator: ', ',
                },
            }
        },
    });

    //监听提交
    form.on('submit(submit_button)', function(data){
        var type = data.field.type;
        if('0' == type){
            var tel = data.field.tel;
            if(tel == null || tel == '' ){
                layer.msg("添加业主，手机号必填！");
                return;
            }
            var nationality = data.field.nationality;
            var passportImgUri = data.field.passportImgUri;
            var entryImgUri = data.field.entryImgUri;
            if('3' == nationality && (passportImgUri == null || passportImgUri == ''|| entryImgUri == null || entryImgUri== '' ) ){
                layer.msg("添加海外业主，护照图片及入境图片必填！");
                return;
            }
            //校验长度，类型
            if(data.field.idCard.length > 18)
            {
                layer.msg("请核实身份证号是否正确，再重新输入");
                return ;
            }
        }
        common.sendAsyncRequest("/estate/user/updateUser",'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 initSelectFn = function () {
        var sex = 'dd[lay-value=' +$("#sex").attr("value") + ']';
        $('#sex').siblings("div.layui-form-select").find('dl').find(sex).click();

        var type = 'dd[lay-value=' +$("#type").attr("value") + ']';
        $('#type').siblings("div.layui-form-select").find('dl').find(type).click();

        var nationality = $("#nationality").attr("value");
        if(nationality == null || nationality == ''){
            $('#nationality').siblings("div.layui-form-select").find('dl dd').eq(0).click();
        }else{
            nationality = 'dd[lay-value=' +nationality + ']';
            $('#nationality').siblings("div.layui-form-select").find('dl').find(nationality).click();
        }
    };

    //  选定证件类型
    form.on('select(nationality_event)', function(data){
        if(data.value == '0'){
            $('#card_name').text("身份证");
            $('#birth_place').show();
            $('#passport_pic').hide();
            $('#entry_pic').hide();
        }else if(data.value == '1'){
            $('#card_name').text("港澳居民往来内地通行证");
            $('#birth_place').hide();
            $('#passport_pic').hide();
            $('#entry_pic').hide();
        }else if(data.value == '2'){
            $('#card_name').text("台湾居民往来大陆通行证");
            $('#birth_place').hide();
            $('#passport_pic').hide();
            $('#entry_pic').hide();
        }else{
            $('#card_name').text("护照");
            $('#birth_place').hide();
            $('#passport_pic').show();
            $('#entry_pic').show();
        }
    });

    //多图片上传
    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("图片上传失败");
        }
    });

    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.length != 11){
                return '请输入正确的手机号码!';
            }
        },
    });

    //  初始化
    (function () {
        initSelectFn();
    })();

});