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

    //  页面元素
    var $node = {
        //  省市区表单
        provinceInput   :   $('input[name="regionProvince"]'),

        cityInput       :   $('input[name="regionCity"]'),

        areaInput       :   $('input[name="regionArea"]'),

        regionInput     :   $('#region'),

        selectEstate    :   $('#estate_pid'),
    };

    //地图
    var map = new AMap.Map('container1', {
        resizeEnable: true, //是否监控地图容器尺寸变化
        center: [106.24749,38.508705],
        zoom:16,
        viewMode:'3D',
        showBuildingBlock:true,
        pitch:50,
        pitchEnable:true,
        showIndoorMap:true,
    });
    //输入提示
    var autoOptions = {
        input: "tipinput"
    };
    var auto = new AMap.Autocomplete(autoOptions);
    var placeSearch = new AMap.PlaceSearch({
        map: map
    });  //构造地点查询类
    AMap.event.addListener(auto, "select", select);//注册监听，当选中某条记录时会触发
    function select(e) {
        placeSearch.setCity(e.poi.adcode);
        placeSearch.search(e.poi.name);  //关键字查询查询
    }


    var clickHandler = function(e) {
        $('input[name=longitude]').val(e.lnglat.lng);
        $('input[name=latitude]').val(e.lnglat.lat);
        console.log(e.lnglat);
        // marker.setPosition(e.lnglat);
    };

    map.on('click', clickHandler);

    //表单验证
    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个字符!'
            }
        },
        personName : function (value) {
            if (value != ''){
                if (value.length < 2 || value.length >6){
                    return  '负责人名称只能输入2-6个字符!'
                }
            }
        },
        phoneNum : function (value) {
            if(value != ""){
                if(!/^1[3|4|5|7|8]\d{9}$/.test(value)){
                    return '请输入正确的手机号码!';
                }
            }
        }
    });

    //监听提交
    form.on('submit(submit_button)', function(data){
        layer.load(1); //风格1的加载
        if (data.field.estatePid == '' || data.field.estatePid == null){
            data.field.estatePid = $('#estate_id').val();
        }
        common.sendAsyncRequest("/estate/residential/updateResidential",'POST',data.field,callbackFn);
    });

    $('#editAgency').click(function(){
        initAgencySelect();
        //隐藏input，显示select
        $('#input_agency').hide();
        $('#agency_id').show();
        // estateSelect.update({layVerify: 'required'});
        $(this).hide();
    });

    $('#editEstate').click(function(){
        //隐藏input，显示select
        $('#input_estate').hide();
        $('#select_estate').show();
        // estateSelect.update({layVerify: 'required'});
        $(this).hide();
    });

    function initAgencySelect(){
        var orgSelect = xmSelect.render({
            el: '#agency_id',
            name: 'agencyId',
            layVerify: 'required',
            radio: true,
            prop: {
                name: 'agencyName',
                value: 'id',
            },
            clickClose: true,
            filterable: true,
            remoteSearch: true,
            remoteMethod: function (val, cb, show) {
                $.ajax({
                    type: 'GET',
                    // url: '/permissions/org/getListByLikeName',
                    url: '/estate/agency/showAgencyList',
                    data: {
                        agencyName : val,
                        // flag : 1,
                        // orgType : 1,
                        // isSelect : true
                    },
                    success: function (result) {
                        cb(result.data.records);
                    },
                    error: function () {
                        cb([]);
                    }
                });
            },
            //监听选择
            on: function (data) {
                var change = data.change;
                if (change.length > 0) {}
                if (!data.isAdd) {}
            },
            //显示方式
            model: {
                label: {
                    type: 'text',
                    text: {
                        //左边拼接的字符
                        left: '',
                        //右边拼接的字符
                        right: '',
                        //中间的分隔符
                        separator: ', ',
                    },
                }
            },
        });
    }

    //回调函数
    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(){
        if (parent) {
            //  关闭当前页面弹窗
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        }else {
            //  关闭页面
            window.close();
        }
    });

    //物业下拉框
    var estateSelect = xmSelect.render({
        el: '#estate_pid',
        name: 'estatePid',
        // layVerify: 'required',
        radio: true,
        prop: {
            name: 'name',
            value: 'id',
        },
        clickClose: true,
        filterable: true,
        remoteSearch: true,
        remoteMethod: function (val, cb, show) {
            console.log('进入物业请求');
            $.ajax({
                type: 'GET',
                url: '/permissions/org/getListByLikeName',
                data: {
                    name : val,
                    flag : 1,
                    orgType : 3,
                    isSelect : true
                },
                success: function (result) {
                    console.log(result,'物业下拉');
                    cb(result.list);
                },
                error: function () {
                    cb([]);
                }
            });
        },
        //监听选择
        on: function (data) {
            var change = data.change;
            if (change.length > 0) {}
            if (!data.isAdd) {}
        },
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });

});