/**
 * 企业信息管理
 *
 */
$(function(){
    var tpl = {
        "buildTpl": '<%for (var i=0;i<data.length;i++){ %>\
                        <option value="<%=data[i].id %>"><%= data[i].name %></option>\
                     <% } %>',
        "addRelation": '<div class="dialog-wp"> \
							<div class="mask"></div>\
							<div class="dialog dialog-floor-con">\
								<div class="title-wp">\
									<span>添加关联企业</span>\
									<span class="close-btn"></button>\
								</div>\
								<div class="dialog-floor-wp">\
									<div class="floors-wp clearfloat">\
										<div class="choose-room clearfloat">\
											\
											<div class="relations-select">\
												<div class="relations-select-btn">关联企业</div>\
												<ul class="relation-list">\
                                                <% for(var i = 0;i < list.length; i++){ %>\
                                                <li class = "relation-item <%if(selectArr.indexOf(list[i].id)!=-1){%> select <%}%>" data-relationid = "<%=list[i].id %>" data-relationname = "<%=list[i].name %>" > <%= list[i].name %> </li>\
                                                <% } %>\
												</ul>\
											</div> \
											<div class="select-relation-wp clearfloat">\
											    <% if(!!selectArr){%>\
                                                    <% for(var i=0;i<selectArr.length;i++){ %>\
                                                        <span class="select-relation" data-roomid="<%=selectArr[i].id%>" data-roomname="<%=selectArr[i].name%>" ><span class="con"><%=selectArr[i].name%></span><span class="del-select-btn" title="删除">×</span></span>\
                                                    <% } %>\
                                                <%}%>    \
											</div>\
										</div>\
									</div>\
									<div class="enterprise-btns">\
										<button class="btn-red relation-sure-btn ">确定添加</button>\
										<button class="btn-white relation-cancel-btn ">取消</button>\
									</div>\
								</div>\
							</div>\
						</div>',
        "addFloor": '<div class="dialog-wp"> \
							<div class="mask"></div>\
							<div class="dialog dialog-floor-con">\
								<div class="title-wp">\
									<span>添加楼层房间号</span>\
									<span class="close-btn"></button>\
								</div>\
								<div class="dialog-floor-wp">\
									<div class="floors-wp clearfloat">\
										<div class="choose-room clearfloat">\
											<span class="tips">楼层房间号</span>\
											<select class="select-width100 floors-select">\
												<option value="">楼层</option>\
												<% for(var i=0;i<floorList.length;i++){%>\
													<option value="<%=floorList[i].id %>"><%=floorList[i].name%></option>\
												<%} %>\
											</select>\
											<div class="rooms-select">\
												<div class="rooms-select-btn">房间号</div>\
												<ul class="room-list">\
												</ul>\
											</div> \
											<div class="select-room-wp clearfloat">\
											    <% if(!!selectArr){%>\
                                                    <% for(var i=0;i<selectArr.length;i++){ %>\
                                                        <span class="select-room" data-roomid="<%=selectArr[i].roomId%>" data-roomname="<%=selectArr[i].roomName%>" data-floorid="<%=selectArr[i].floorId%>" data-floorname="<%=selectArr[i].floorName%>"><span class="con"><%=selectArr[i].floorName%>楼<%=selectArr[i].roomName%></span><span class="del-select-btn" title="删除">×</span></span>\
                                                    <% } %>\
                                                <%}%>    \
											</div>\
										</div>\
									</div>\
									<div class="enterprise-btns">\
										<button class="btn-red floor-sure-btn ">确定添加</button>\
										<button class="btn-white floor-cancel-btn ">取消</button>\
									</div>\
								</div>\
							</div>\
						</div>',
        "addAgentRange": '<div class="dialog-wp"> \
							<div class="mask"></div>\
							<div class="dialog dialog-range-con" style="width:560px;">\
								<div class="title-wp">\
									<span>添加代理区域</span>\
									<span class="close-btn"></button>\
								</div>\
								<div class="dialog-range-wp">\
									<div class="ranges-wp clearfloat">\
										<div class="choose-room clearfloat" style="width: 100%;display: flex;">\
										  <div>\
										    <span class="tips">楼栋</span>\
										    <select class="select-width100 builds-select" name="range_build_id" id="range_build_id" >\
                                                <option value="">楼栋</option>\
										    <%for (var i=0;i<data.length;i++){ %>\
                                                <option value="<%=data[i].id %>"><%= data[i].name %></option>\
                                            <% } %>\
                                            </select>\
                                           </div>\
                                           <div>\
											<span class="tips">楼层房间号</span>\
											<select id="range-floors-select" class="select-width100 floors-select">\
												<option value="">楼层</option>\
											</select>\
											</div>\
											<div class="rooms-select">\
												<div class="rooms-select-btn">房间号</div>\
												<ul class="room-list">\
												</ul>\
											</div> \
										</div>\
										<div class="select-room-wp clearfloat" style="clear: both;">\
											    <% if(!!selectArr){%>\
                                                    <% for(var i=0;i<selectArr.length;i++){ %>\
                                                        <span class="select-room" data-roomid="<%=selectArr[i].roomId%>" ><span class="con"><%=selectArr[i].roomName%></span><span class="del-select-btn" title="删除">×</span></span>\
                                                    <% } %>\
                                                <%}%>\
											</div>\
									</div>\
									<div class="enterprise-btns">\
										<button class="btn-red range-sure-btn ">确定添加</button>\
										<button class="btn-white range-cancel-btn ">取消</button>\
									</div>\
								</div>\
							</div>\
						</div>',
        //
        "selectRoom": '<% for(var i=0;i<selectArr.length;i++){ %>\
	    	  	<span class="select-room" data-roomid="<%=selectArr[i].roomId %>" data-roomname="<%=selectArr[i].roomName %>" data-floorid="<%=selectArr[i].floorId %>" data-floorname="<%=selectArr[i].floorName%>"><span class="con"><%=selectArr[i].buildName %><%= selectArr[i].floorName %>楼<%= selectArr[i].roomName %></span><span class="del-select-btn" title="删除">×</span></span>\
	    	 <% } %> \
	    	  ',
        "roomItem": '<% for(var i=0;i<list.length;i++){ %>\
			  			<li class="room-item <%if(selectArr.indexOf(list[i].roomInfo.id)!=-1){%> select <%}%> <%if(list[i].isLess == 1 && selectArr.indexOf(list[i].roomInfo.id)==-1){%> disabled <%}%>"  data-roomid="<%=list[i].roomInfo.id %>" data-roomname="<%=list[i].roomInfo.name %>" data-floorid="<%=floorId %>" data-floorname="<%=floorName%>" ><%=list[i].roomInfo.name %></li>\
			  	<% } %>',
        "floorItem": '<option value="">楼层</option>\
                    <% for(var i=0;i<floorList.length;i++){%>\
						<option value="<%=floorList[i].id %>"><%=floorList[i].name%></option>\
					<%} %>',
        "selectRelation": '<% for(var i=0;i<selectArr.length;i++){ %>\
	    	  	<span class="select-relation" data-relationid="<%=selectArr[i].id %>" data-relationname="<%=selectArr[i].name %>" ><span class="con"><%= selectArr[i].name %></span><span class="del-select-btn" title="删除">×</span></span>\
	    	 <% } %> \
	    	  '
    };
    // console.log($("#floorAndRoomList").val());debugger
    var virtualId=$("#virtualId").val();
    var dealFlag=$("#dealFlag").val();
    var cacheBuilds;
    var archForm = {
        init: function(){
            this.render();
            this.bindEvent();
        },
        render: function(){
            this.initBuild();
            this.initLevel();
        },
        bindEvent: function(){
            var  _this = this;
            layui.use('laydate', function () {
                var laydate = layui.laydate;
                //日期
                if(! $('#signingTime').attr('readonly')) {
                    laydate.render({
                        elem: '#signingTime'
                    });
                }

                if(! $('#contExpireTime').attr('readonly')) {
                    laydate.render({
                        elem: '#contExpireTime'
                    });
                }

            });
            if(dealFlag!=""&&dealFlag!=null){
                if("success"==dealFlag){
                    alert("处理成功！");
                    window.close();
                }
                else{
                    alert($("#resultDesc").val());
                }
            };
            //选择校区联动楼宇
            $("#organizationId").bind("change",function(){
                var organizationId=$("#organizationId").val();
                //新增的时候
                if(virtualId==""||virtualId==null){
                    $("#address").val($("#organization_"+organizationId).val());
                }
                //根据校区联动楼宇列表
                _this.initBuild();
            });
            //添加楼层房间号
            $(".add-floor-room").on("click",function(){
                $("#addFloorRoomMode").val("floor");
                _this.addFloorRoom();
            })
            //添加关联企业
            $(".add-relation").on("click",function(){
                _this.addRelation();
            })

            //代理区域
            $(".add-range").on("click",function(){
                $("#addFloorRoomMode").val("area");
                //_this.addFloorRoom();
                _this.addAgentRange();
            })
            //保存
            $("#btn-save").bind("click",function(){
                _this.saveForm();
            })

            $("#agency1").on("click",function(){
                var level=$("#agency1").val();
                _this.getVirList(level);
                _this.switchLevel(1);
            })

            $("#agency2").on("click",function(){
                var level=$("#agency2").val();
                _this.switchLevel(2);
            })


        },

        switchLevel: function(level) {
            if(level == 1) {
                $("#agencySelectDiv").css("display","block");
                $("#agencyNewDiv").css("display","none");
            } else if(level == 2) {
                $("#agencySelectDiv").css("display","none");
                $("#agencyNewDiv").css("display","block");
            }
        },

        /**
         * 查询楼宇信息
         */
        initBuild: function(){
            var organizationId=$("#organizationId").val();
            if(organizationId==null||organizationId==""){
                organizationId=$("#orgId").val();
            }
            var buildId_=$("#buildId_").val();
            $("#buildId").empty();
            if(organizationId!=""){
                $.ajax({
                    url :$("#contextPath").val()+"/admin/floor/getBuildByOrg",
                    type : "POST",
                    data : {orgId:organizationId},
                    dataType:"json",
                    success : function(data) {
                        $("#buildId").html(template.render(tpl.buildTpl)(data));
                        if(!!buildId_){
                            $("#buildId").val(buildId_);
                        }
                        cacheBuilds = data;
                    },
                    error : function(data) {
                    }
                });
            }
            else{
                $("#buildId").append("<option value=''>请选择楼宇</option>");
            }
        },

        initLevel: function() {
            var level = $("#level_").val();
            if(level == "1" || level == "2") {
                this.switchLevel(level);
                if(level==="1"){
                    this.getVirList();
                }
            }
            var radioNode = $('.level-select[value="' + level +  '"]');
            if(radioNode.length > 0) {
                radioNode.attr("checked", true);
            }

        },

        /**
         * 添加关联企业
         */
        addRelation: function() {
            var _this = this;
            var organizationId = $("#organizationId").val();
            if(!$("#organizationId").val()){
                alert("请选择所属校区！");
                return false;
            }else{
                _this.ajaxRelationList(organizationId, []);

            }
        },

        /**
         * 获取关联企业
         * @param organizationId
         * @param selectArr
         */
        ajaxRelationList: function(organizationId, selectArr) {
            var _this = this;
            $.ajax({
                url :$("#contextPath").val()+"/admin/templ/getVirListByNoAgant",
                type : "POST",
                data : {
                    orgId:organizationId,
                    id:$("#vir_id").val()
                },
                dataType:"json",
                success : function(data) {

                    if(data.code == '200'){
                        //$dom.html(template.render(tpl.roomItem)({ list:  data.data, floorName: floorName, floorId: floorId, selectArr: _this.toNumArr(selectArr)}));
                        $("body").append(template.render(tpl.addRelation)({list: data.data, selectArr: selectArr}));
                        $(".relation-item").on("click",function(){
                            var $this = $(this);
                            if($this.hasClass("select")){
                                $this.removeClass("select");
                                _this.delSelectRelation($this.data("relationid"),selectArr);
                            }else{
                                $this.addClass("select");
                                selectArr.push({
                                    "id": $this.data("relationid"),
                                    "name":  $this.data("relationname")
                                });
                                $(".select-relation-wp").html(template.render(tpl.selectRelation)({selectArr: selectArr}));
                                $(".del-select-btn").on("click",function(){
                                    var $this = $(this)
                                    _this.delSelectRelation($this.closest(".select-relation").data("relationid"),selectArr);
                                    $(this).closest(".select-relation").remove();


                                    //$(".select-room-wp").html(template.render(tpl.selectRoom)({selectArr: selectArr}));
                                })
                            }
                        })

                        $(".relations-select-btn").on("click",function(event){
                            event.stopPropagation();
                            var $parent = $(this).parent(".relations-select");
                            if($parent.hasClass("active")){
                                $parent.removeClass("active")
                            }else{
                                $parent.addClass("active")
                            }
                        });
                        $(".relation-list").on("click",function(event){
                            event.stopPropagation();
                        })



                        //关闭弹窗
                        $(".close-btn,.relation-cancel-btn").on("click",function(){
                            $(this).closest(".dialog-wp").remove();
                        });
                        $(".relation-sure-btn").on("click",function(){
                            var $this = $(this);

                            _this.saveRelation(function () {
                                $this.closest(".dialog-wp").remove();
                            })
                        });
                        $(".del-select-btn").on("click",function(){
                            var $this = $(this)
                            _this.delSelectRelation($this.closest(".select-relation").data("relationid"),selectArr);
                            $(this).closest(".select-relation").remove();
                        })

                        //已选值
                        var hadSelect=$("#relation_id").val();
                        let rang_id_array=hadSelect.split(',');
                        rang_id_array.forEach(function (item) {
                            $(".relation-list li[data-relationid='"+item+"']").click();
                        })
                    }

                },
                error : function(data) {
                }
            });
        },
        /**
         * 删除选中关联企业
         * @param relationId
         * @param selectaRR
         */
        delSelectRelation: function(relationId, selectArr){
            var _this = this;
            var index ;
            for(var i=0;i<selectArr.length;i++){
                if(relationId == selectArr[i].id){
                    index = i;
                }
            };
            if(!isNaN(index)){
                selectArr.splice(index,1);
                $(".select-relation-wp").html(template.render(tpl.selectRelation)({selectArr: selectArr}));
                $(".relation-item").each(function(index,ele){
                    if($(ele).data("relationid") == relationId){
                        $(ele).removeClass("select");
                    }
                })

                $(".del-select-btn").on("click",function(){
                    var $this = $(this);
                    _this.delSelectRelation($this.closest(".select-relation").data("relationid"),selectArr);
                    $(this).closest(".select-relation").remove();
                })
            }
        },


        /**
         * 保存楼层房间号信息
         * @param callback
         */
        saveRelation: function(callback){
            var $selectRelation = $(".select-relation"),
                relationIds = [],
                relationNames = [];
                relationName = '';
            if(!$selectRelation.length){
                alert("请选择关联企业信息");
                return false;
            }else{
                $selectRelation.each(function(index,ele){
                    var id = $(this).data("relationid");
                    var name = $(this).data("relationname");
                    if(relationIds.indexOf(id) == -1){
                        relationIds.push(id);
                    };
                    if(relationNames.indexOf(name) == -1){
                        relationNames.push(name);
                    }
                    relationName += name+'、'
                });

                $("#relation_id").val(relationIds.join(",")+",");
                $("#relation-name").html(relationName);

                callback();
            }
        },

        parseRangeSelected: function() {
            var rangeId = $("#range_id").val(),
                rangeIds = rangeId.split(","),
                rangeName = $("#area-names").text(),
                rangeNames = rangeName.split(","),
                selectArr = [];
            if(rangeId!==""&&rangeName!==""){
                for(var i = 0; i < rangeIds.length; i++) {
                    selectArr.push({
                        "roomId":  rangeIds[i],
                        "roomName":  rangeNames[i]
                    });
                }
            }
            return selectArr;
        },

        /**
         * 添加楼层房间号/选择代理区域
         */
        addAgentRange: function() {
            if(cacheBuilds) {
                var _this = this,
                    selectArr  = _this.parseRangeSelected();


                $("body").append(template.render(tpl.addAgentRange)({data: cacheBuilds.data, selectArr: selectArr}));
                //selectArr

                // //关闭弹窗
                $(".close-btn,.range-cancel-btn").on("click",function(){
                    $(this).closest(".dialog-wp").remove();
                });

                $(".range-sure-btn").on("click",function(){
                    var $this = $(this);
                    //房间楼层好信息
                    //传房间信息到页面上
                    _this.saveFloorAndRoom(function () {
                        $this.closest(".dialog-wp").remove();
                    })
                });



                //选择楼栋层之后请求楼层
                $(".builds-select").on("change",function(){
                    var buildId = $(this).val();

                    $("#range-floors-select").html("<option value=''>楼层</option>");
                    $(".rooms-select").removeClass("active");
                    $(".room-list").html("");

                    if(buildId){
                        _this.ajaxFloorList(buildId, selectArr, true);

                    }
                });

                _this.bindFloor(selectArr);
            }
        },



        /**
         * 添加楼层房间号/选择代理区域
         */
        addFloorRoom: function() {
            var _this = this;
            var buildId = $("#buildId").val();
            if(!$("#buildId").val()){
                alert("请选择楼宇！");
                return false;
            }else{
                var mode = $("#addFloorRoomMode").val();
                if(!$("#buildId_").val() || mode === "area"){
                    _this.ajaxFloorList(buildId,[])
                } else {
                    _this.ajaxChooseFloorAndRoom($("#virtualId").val(),function(data){
                        _this.ajaxFloorList(buildId, data.data)
                    })
                }
            }
        },

        /**
         * 获取楼宇的楼层
         * @param build 楼宇id
         */
        ajaxFloorList: function(buildId,selectArr, inc){
            var _this = this;
            $.ajax({
                url :$("#contextPath").val()+"/admin/floor/getFloorList",
                type : "POST",
                data : {buildId:buildId},
                dataType:"json",
                success : function(data) {
                    if(data.code==200){

                            if(inc) {
                                $("#range-floors-select").html(template.render(tpl.floorItem)({floorList: data.data,selectArr: selectArr}));
                            } else {
                                $("body").append(template.render(tpl.addFloor)({floorList: data.data,selectArr: selectArr}));

                                _this.bindFloor(selectArr);
                            }


                            if(inc) {
                                return;
                            }

                            //关闭弹窗
                            $(".close-btn,.floor-cancel-btn").on("click",function(){
                                $(this).closest(".dialog-wp").remove();
                            });
                            $(".floor-sure-btn").on("click",function(){
                               var $this = $(this);
                                //房间楼层好信息
                                //传房间信息到页面上
                                _this.saveFloorAndRoom(function () {
                                    $this.closest(".dialog-wp").remove();
                                })
                            });


                    }else{
                        // alert('系统繁忙...')
                    }

                },
                error : function(data) {
                }
            });
        },

        bindFloor: function(selectArr) {
            var _this = this;
            //选择楼层之后请求房间
            $(".floors-select").on("change",function(){
                var floorId = $(this).val();
                var floorName = $(".floors-select option:selected").text();
                if(!floorId){
                    // $(".room-list").hide();
                    $(".rooms-select").removeClass("active")
                }else{
                    $(".select-room-wp").html(template.render(tpl.selectRoom)({selectArr: selectArr}))

                    $(".del-select-btn").on("click",function(){
                        var $this = $(this)
                        _this.delSelectRoom($this.closest(".select-room").data("roomid"),selectArr);
                        $(this).closest(".select-room").remove();
                    })
                    var rangeIds = _this.parseRangeIds();
                    _this.ajaxRoomList(floorId,$(".room-list"),2,floorName,selectArr, rangeIds);
                }
            });
            $(".del-select-btn").on("click",function(){
                var $this = $(this)
                _this.delSelectRoom($this.closest(".select-room").data("roomid"),selectArr);
                $(this).closest(".select-room").remove();
            })

            $(".rooms-select-btn").on("click",function(event){
                event.stopPropagation();
                var $parent = $(this).parent(".rooms-select");
                if($(".floors-select").val()){
                    if($parent.hasClass("active")){
                        $parent.removeClass("active")
                    }else{
                        $parent.addClass("active")
                    }
                }
            });
            $(".room-list").on("click",function(event){
                event.stopPropagation();
            })
        },

        /**
         * 保存楼层房间号信息
         * @param callback
         */
        saveFloorAndRoom: function(callback){
            var mode = $("#addFloorRoomMode").val();
            var $roomRoom = $(".select-room");
            var floorIds = [];
            var roomIds =[];
            var floorNames = [];
            var roomNames = [];
            var floorAndNames = ''
            if(!$roomRoom.length){
                alert("请选择楼层房间信息");
                return false;
            }else{
                $roomRoom.each(function(index,ele){
                    var floorId = $(this).data("floorid");
                    var floorName = $(this).data("floorname");
                    var roomId = $(this).data("roomid");
                    var roomName = $(this).data("roomname");
                    if(floorIds.indexOf(floorId) == -1){
                        floorIds.push(floorId);
                    };
                    if(roomIds.indexOf(roomId) == -1){
                        roomIds.push(roomId);
                    }
                    if(floorNames.indexOf(floorName) == -1){
                        floorNames.push(floorName);
                    }
                    if(roomNames.indexOf(roomName) == -1){
                        roomNames.push(roomName);
                    };
                    floorAndNames += $(this).find(".con").html()+'、'
                });

                if(mode === "floor") {
                    $("#floorNames").val(floorNames.join(";")+";");
                    $("#floorIds").val(floorIds.join(";")+";");
                    $("#roomNames").val(roomNames.join(";")+";");
                    $("#roomIds").val(roomIds.join(";")+";");
                    $("#floorAndNames").val(floorAndNames)
                }
                else {
                    $("#range_id").val(roomIds.join(",")+",");
                    $("#area-names").html(floorAndNames);
                }

                callback();
            };
        },
        /**
         * 删除选中房间
         * @param roomId
         * @param selectaRR
         */
        delSelectRoom: function(roomId,selectArr){
            var _this = this;
            var index ;
            for(var i=0;i<selectArr.length;i++){
                if(roomId == selectArr[i].roomId){
                    index = i;
                }
            };
            if(!isNaN(index)){
                selectArr.splice(index,1);
                $(".select-room-wp").html(template.render(tpl.selectRoom)({selectArr: selectArr}));
                $(".room-item").each(function(index,ele){
                    if($(ele).data("roomid") == roomId){
                        $(ele).removeClass("select");
                    }
                })

                $(".del-select-btn").on("click",function(){
                    var $this = $(this);
                    _this.delSelectRoom($this.closest(".select-room").data("roomid"),selectArr);
                    $(this).closest(".select-room").remove();
            })
            }
        },

        /**
         * 解析rangeIds，用于限制房间可选范围
         * 1. 管理员页面，不需要rangeIds，返回null
         * 2. 非管理员页面，必须有rangeIds数组，如果页面获取不到默认[]，相当于限制不能选择
         */
        parseRangeIds: function() {
            var rangeIdNode = $("#room_rangeId");
            if(rangeIdNode.length > 0) { //非管理员
                return rangeIdNode.val() ? rangeIdNode.val().split(",").map(Number) : [];
            } else {
                return null;
            }
        },

        /**
         * 限制房间可选范围
         * @param data
         * @param rangeId
         * @returns {*}
         */
        rangeRoomData: function(data, rangeIds) {
            if(rangeIds) {
                for(var i = 0; i < data.length; i++) {
                    if(rangeIds.indexOf(data[i].roomInfo.id) == -1) {
                        data[i].isLess = 1;
                    }
                }
            }
            return data;
        },



        /**
		 * 请求房间信息
		 * @param floorId
		 * @param $dom
		 * @param type
		 * @param floorName
		 * @patam selectArr
         * @param rangeId 可选范围
		 */
        ajaxRoomList: function(floorId,$dom,type,floorName,selectArr, rangeIds){
            var _this = this;
            $.ajax({
                type: "post",
                url: $("#contextPath").val()+ "/admin/room/getRoomList",
                data: {
                    floorId: floorId
                },
                success: function(data){
                    var data = JSON.parse(data);
                    if(data.code == '200'){
                        if(type !=2){
                            $dom.html(template.render(tpl.roomList)({roomList: data.data}));
                        }else{

                            $dom.html(template.render(tpl.roomItem)({ list:  _this.rangeRoomData(data.data, rangeIds), floorName: floorName, floorId: floorId,selectArr: _this.toNumArr(selectArr)}));
                            $(".room-item:not(.disabled)").on("click",function(){
                                var $this = $(this);
                                if($this.hasClass("select")){
                                    $this.removeClass("select");
                                    _this.delSelectRoom($this.data("roomid"),selectArr);
                                }else{
                                    $this.addClass("select");


                                    var buildName = $("#addFloorRoomMode").val() === "floor" ? "" : $("#range_build_id option:selected").text();

                                    selectArr.push({
                                        "buildName": buildName,
                                        "floorId": $this.data("floorid"),
                                        "floorName":  $this.data("floorname"),
                                        "roomId":  $this.data("roomid"),
                                        "roomName":  $this.data("roomname")
                                    });
                                    $(".select-room-wp").html(template.render(tpl.selectRoom)({selectArr: selectArr}));
                                    $(".del-select-btn").on("click",function(){
                                        var $this = $(this)
                                        _this.delSelectRoom($this.closest(".select-room").data("roomid"),selectArr);
                                        $(this).closest(".select-room").remove();


                                        //$(".select-room-wp").html(template.render(tpl.selectRoom)({selectArr: selectArr}));
                                    })
                                }
                            })
                        }
                    }
                },
                error: function(){
                }
            });
        },
        /**
         * 把选中的数组对象转换成id的数组
         * @param selectArr
         */
        toNumArr: function(selectArr){
            var arr = [];
            for(var i = 0;i<selectArr.length;i++){
                arr.push(+selectArr[i].roomId);
            };
            return arr;
        },
        /**
         * 获取选择的楼层房间号
         * @param id 企业id
         * @param callback
         */
        ajaxChooseFloorAndRoom: function (id,callback){
            $.ajax({
                url :$("#contextPath").val()+"/admin/virtual/getFloorAndRoomList",
                type : "POST",
                data : {virId:id},
                success : function(data) {
                    if(data.code==200){
                        callback(data)
                    }else{
                        // alert('系统繁忙...')
                    }

                },
                error : function(data) {
                }
            });
        },
        //保存
        saveForm: function(){
            var organizationId=$("#organizationId").val();
            if(organizationId==""||organizationId==null){
                alert("请选择校区");
                return;
            }
            var buildId=$("#buildId").val();
            if(buildId==""||buildId==null){
                alert("请选择楼宇");
                return;
            }
            var virtualName=$("#virtualName").val();
            if(virtualName==""||virtualName==null){
                alert("请输入企业名称");
                $("#virtualName").focus();
                return;
            }
            var address=$("#address").val();
            if(address==""||address==null){
                alert("请输入企业地址");
                $("#address").focus();
                return;
            }
            // var signingTime=$("#signingTime").val();
            // if(signingTime==""||signingTime==null){
            //     alert("请输入签约时间");
            //     return;
            // }

            var contExpireTime=$("#contExpireTime").val();
            if(contExpireTime==""||contExpireTime==null){
                alert("请输入合同到期时间");
                return;
            }
            if(virtualId!=""){
                var customerNumber=$("#customerNumber").val();
                // if(customerNumber==""||customerNumber==null){
                //     alert("请输入客户编号");
                //     $("#customerNumber").focus();
                //     return;
                // }
            }

            var phone=$("#phone").val();
            if(phone==""||phone==null){
                alert("请输入联系电话");
                $("#phone").focus();
                return;
            }

            var email=$("#email").val();
            if(email==""||email==null){
                alert("请输入邮箱");
                $("#email").focus();
                return;
            }

            var employees=$("#employees").val();
            if($.trim(employees)==""){
                $("#employees").val(1);
            }
            else{
                var r = /^\+?[1-9][0-9]*$/;
                if(!r.test(employees)){
                    alert("企业规模请输入数字");
                    $("#employees").focus();
                    return;
                }
            }
            $('#buildId').attr("disabled",false);
            $("#conditionform").submit();
        },
        //模糊搜索企业
        getVirList: function(level){
            var organizationId=$("#organizationId").val();
            if(organizationId==null||organizationId==""){
                organizationId=$("#orgId").val();
            }
            if(organizationId!=""){
                $.ajax({
                    url :$("#contextPath").val()+"/admin/templ/getVirList",
                    type : "POST",
                    data : {"orgId":organizationId,"level":"2","virtualId":$("#virtualId").val()},
                    dataType:"json",
                    success : function(data) {
                        if(data.code==200){
                            $("#agencySelect").html("<option value=\"\">请选择企业</option>" + template.render(tpl.buildTpl)(data));
                            if($("#page_agent_id").val()!==null||$("#page_agent_id").val()!==''){
                                $("#agencySelect ").val($("#page_agent_id").val());
                            }
                        }else{
                            alert("请选择楼宇");
                        }
                    },
                    error : function(data) {
                    }
                });
            }
        }
    }
    archForm.init();
    $(document).click(function(){
        if($('.rooms-select').is(':visible')){
            $(".rooms-select").removeClass("active");
        }
    });

});
