
var permDelete = $("#perm-delete").val();//删除权限
var permEdit = $("#perm-edit").val();//编辑权限

/*
 * 楼层房间号配置
 */
$(function() {
	var $addLine = $(".add-line");
	var tpl = {
			"dialog": '<div class="dialog-wp">\
				<div class="mask"></div>\
				<div class="dialog dialog-room-con">\
					<div class="title-wp">\
						<span>编辑房间号</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class="room-list">\
					  <% for(var i=0;i<roomList.length;i++){%>	\
						<div class="room-item">\
							<span class="tip">房间号：</span>\
							<input class="item-text" type="text" value="<%= roomList[i].roomInfo.name %>" data-roomid="<%= roomList[i].roomInfo.id %>" data-floorid="<%= roomList[i].roomInfo.floorId %>" readonly/>\
							<span class="tip">面积（m²）：</span>\
							<input class="item-name-text" type="text" value="<%= roomList[i].roomInfo.area %>" data-roomid="<%= roomList[i].roomInfo.id %>" data-floorid="<%= roomList[i].roomInfo.floorId %>" readonly/>\
							<span class="item-edit-btn" data-roomid="<%= roomList[i].roomInfo.id %>" data-floorid="<%= roomList[i].roomInfo.floorId %>">编辑</span>\
							<span class="item-sure-btn" data-roomid="<%= roomList[i].roomInfo.id %>" data-floorid="<%= roomList[i].roomInfo.floorId %>">确定</span>\
							<span class="item-del-btn" data-roomid="<%= roomList[i].roomInfo.id %>">删除</span>\
						</div>\
					 <% } %> 	\
					 <div class="room-item add-room-wp"><span class="add-room">添加</span></div>\
					</div>\
				</div>\
			</div>',
			 "itemTpl": '<option value="">选择校区</option>\
					<%for (var i=0;i<list.length;i++){ %>\
						<option value="<%=list[i].id %>"><%= list[i].name %></option>\
					 <% } %>',
			 "buildTpl": '<option value="">选择楼宇</option>\
			 		<%for (var i=0;i<list.length;i++){ %>\
						<option value="<%=list[i].id %>"><%= list[i].name %></option>\
					 <% } %>',
				 "roomList": ' <% for(var i=0;i<roomList.length;i++){%>	\
						<div class="room-item">\
							<span class="tip">房间号：</span>\
							<input class="item-text" type="text" value="<%= roomList[i].roomInfo.name %>" data-roomid="<%= roomList[i].roomInfo.id %>" data-floorid="<%= roomList[i].roomInfo.floorId %>" readonly/>\
							<span class="tip">面积（m²）：</span>\
							<input class="item-name-text" type="text" value="<%= roomList[i].roomInfo.area %>" data-roomid="<%= roomList[i].roomInfo.id %>" data-floorid="<%= roomList[i].roomInfo.floorId %>" readonly/>\
							<span class="item-edit-btn" data-roomid="<%= roomList[i].roomInfo.id %>" data-floorid="<%= roomList[i].roomInfo.floorId %>">编辑</span>\
							<span class="item-sure-btn" data-roomid="<%= roomList[i].roomInfo.id %>" data-floorid="<%= roomList[i].roomInfo.floorId %>">确定</span>\
							<span class="item-del-btn" data-roomid="<%= roomList[i].roomInfo.id %>">删除</span>\
						</div>\
					<% } %> 	\
							 <div class="room-item add-room-wp"><span class="add-room">添加</span></div>',
				"roomItem": '<div class="room-item">\
					<span class="tip">房间号：</span>\
					<input class="item-text" type="text" value="<%=value%>" />\
					<span class="tip">面积（m²）：</span>\
					<input class="item-name-text" type="text"  />\
					<span class="item-edit-btn" >编辑</span>\
					<span class="item-sure-btn">确定</span>\
					<span class="item-del-btn" >删除</span>\
				</div>'	,
		"addFloorDialog": '<div class="dialog-wp">\
				<div class="mask"></div>\
				<div class="dialog add-floor-con">\
					<div class="title-wp">\
						<span>添加楼层</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class="add-floor">\
						<div class="add-name-wp">\
						<span>楼宇</span>\
						<select class="buildId default-select">\
			<% if (list.length>1){%>\
				 <option value="">选择楼宇</option>\
				 <% }%>\
							<%for (var i=0;i<list.length;i++){ %>\
								<option value="<%=list[i].id %>"><%= list[i].name %></option>\
						 	<% } %>\
						</select>\
						</div>\
						<div class="add-name-wp">\
							<span>楼层</span>\
							<input type="text" class="floor-name"/>\
							<span>楼</span>\
							<span class="error">请输入楼层</span>\
						</div>\
						<div class="add-name-wp">\
							<span class="tips">有会议室：</span>\
							<div class="radio-wp" >\
								<span class="radio-input">\
									<input type="radio" id="indate" data-type="1" class="radio-item" name="date" />\
									<span class="radio-inner"></span>\
								</span>		\
								<label class="radio-con" for="indate">是</label>\
							</div>\
							<div class="radio-wp" >\
								<span class="radio-input">\
									<input type="radio" id="outdate" data-type="0" class="radio-item" name="date" />\
									<span class="radio-inner"></span>\
								</span>		\
								<label class="radio-con" for="outdate">否</label>\
							</div>\
						</div>\
						<button class="sure-add-floor-btn btn-red-width90">保存</button>\
					</div>\
				</div>\
			</div>',
			"updateFloorDialog": '<div class="dialog-wp">\
				<div class="mask"></div>\
				<div class="dialog add-floor-con">\
					<div class="title-wp">\
						<span>添加楼层</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class="add-floor">\
						<div class="add-name-wp">\
						<span>楼宇</span>\
						<select class="buildId default-select">\
			<% if (list.length>1){%>\
				 <option value="">选择楼宇</option>\
				 <% }%>\
							<%for (var i=0;i<list.length;i++){ %>\
								<option value="<%=list[i].id %>" <%if(buildId==list[i].id){%> selected<%}%> ><%= list[i].name %></option>\
						 	<% } %>\
						</select>\
						</div>\
						<div class="add-name-wp">\
							<span>楼层</span>\
							<input type="text" class="floor-name" value="<%=floorName%>"/>\
							<span>楼</span>\
							<span class="error">请输入楼层</span>\
						</div>\
						<div class="add-name-wp">\
							<span class="tips">有会议室：</span>\
							<div class="radio-wp" >\
								<span class="radio-input">\
									<input type="radio" id="indate" data-type="1" class="radio-item" name="date" <%if(isHaveMeeting==1){%>  checked <%}%>/>\
									<span class="radio-inner"></span>\
								</span>		\
								<label class="radio-con" for="indate">是</label>\
							</div>\
							<div class="radio-wp" >\
								<span class="radio-input">\
									<input type="radio" id="outdate" data-type="0" class="radio-item" name="date"  <%if(isHaveMeeting==0){%>  checked <%}%>/>\
									<span class="radio-inner"></span>\
								</span>		\
								<label class="radio-con" for="outdate">否</label>\
							</div>\
						</div>\
						<button class="sure-add-floor-btn btn-red-width90">保存</button>\
					</div>\
				</div>\
			</div>'
	};
	var layer;
	var table;
	var buildList=[];
	layui.use('layer', function(){
		   layer = layui.layer;  
	}); 
	var roomSetting = {
	   init: function(){
	   	    var _this = this;
		   this.render();
		   this.bindEvent();
		   // this.buildList();
		   this.getOrgList('1',function(){
               _this.renderTable();
		   });
	   },
	   render: function(){

	   },
	   bindEvent: function(){
		   var _this = this;
		   //添加楼层
		 $(".add-floors-btn").on("click",function(){
			 _this.addFloor('add');
		 })
		 // 模糊搜索
			_this.search_fuzzy()

	   },
	   // 模糊搜索事件
		search_fuzzy: function(){
			$('body').on("input",'#orgId',function(event){
				var _this = $(this)
				cjqt.search_fuzzy.event_input(_this,'org__id','org__span',$("#contextPath").val(),'/admin/templ/getOrgListByName')
			});
			$('body').on("focus",'#orgId',function(event){
				var _this = $(this)
				cjqt.search_fuzzy.event_focus(_this,'org__id','org__span',$("#contextPath").val(),'/admin/templ/getOrgListByName')
			});
			$('body').on('blur','#orgId',function(event) {
				var _this = $(this)
				cjqt.search_fuzzy.event_blur(_this,'org__id')
			});
			$('body').on('click','.select_wrap>.org__span', function(event) {
				event.preventDefault();
				event.stopPropagation();
				/* Act on the event */
				var _this = $(this)
				cjqt.search_fuzzy.event_click(_this,'org__id','#orgId')
			});
		},
        /**
		 * 获取校区列表
		 * @param  type为1渲染table
         */

	   getOrgList: function(type,callback){
			var _this = this;
			if(!!$(".orgIds").val()){
                var orgId=$(".orgIds").val();
                _this.buildList(orgId);
			}else{
                $.ajax({
                    type: "post",
                    url: $("#contextPath").val()+ "/admin/templ/getOrgList",
                    success: function(data){
                        if(data.code == '200'){
                            // $("#orgId").html(template.render(tpl.itemTpl)({list: data.data}));
                            // if(orgList.length==1){
                            //     var orgId=orgList[0].id;
                            //     _this.buildList(orgId);
                            // }
                            // $("#orgId").val(data.data[0].id);
                            _this.buildList(data.data[0].id);
                            if(type==1){
								callback();
							}
                            $("#orgId").on("change",function(){
                               	setTimeout(function(){
   									var id = $("#orgId").attr('org__id');
                                	console.log(id)
                               		_this.buildList(id);
                               	},400)
                            })

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

		},
		
		/**
		 * 请求楼宇数据
		 * @param orgid
		 */
	   buildList:function(orgId){
		   var _this = this;
		   $.ajax({
			   type: "post",
			   url: $("#contextPath").val()+ "/admin/floor/getBuildByOrg", 
			   data: {
				   "orgId": orgId
			   },
			   success: function(data){
				   if(data.code == '200'){
                       buildList = data.data;
					   $("#buildId").html(template.render(tpl.buildTpl)({list: data.data}));
					   if(data.data.length>0){
                           $("#buildId").val(data.data[0].id)
					   }
					   _this.renderTable();
				   }else{
					   layer.alert(data.msg);
				   }   
			   },
			   error: function(){   
			   }
	   });
	   },
	   /**
	    * 渲染table
	   */
	   renderTable: function(){
		   var _this = this;
			layui.use('table', function(){
				  table = layui.table;
				  
				  //方法级渲染
				  table.render({
				    elem: '#LAY_table_user'
				    ,url: $("#contextPath").val()+ "/admin/floor/getFloorAndRoomList"
				   , method: "post"
					   ,skin: 'nob' //行边框风格
				    		  ,even: true //开启隔行背景
				    		  ,size: 'lg' //小尺寸的表格
				    ,cols: [[
				            {
				    		   field:'Index', 
				    		   title: '序号',
				    		   templet: '#indexTpl',
				    		   width: '10%'
				    	  },{
				    		   field:'buildName', 
				    		   title: '楼宇',
				    		   width: '15%'
				    	  },{
			    		   field:'floorName', 
			    		   title: '楼层', 
			    		   width:'15%',
//			    		   fixed: 'left', 
			    		   templet: function(data){
			    			   var str = '';
			    			   var length = data.roomList.length;
			    			   str += "<div class='floor-name td-change-floor'>";
							   str += "<span class='floor-name-span' data-floorid = "+data.floorId+" data-floornum = "+length+" >"+data.floorName+"</span>"	
							   str += "<input class='floor-name-text' type='text'  data-floorid = "+data.floorId+" data-floornum = "+length+" value="+data.floorName+"  />楼"
							   str += '</div>'
			    			   return str;
			    		   }
			    	  },{
			    		  field:'roomList',
			    		  title: '房间号',
			    		  width: "50%",
//			    	      minWidth: 400,
			    		  
			    	      templet: function(data){
			    	    	  var str = "<div class='floors'>";
			    	    	  for(var i=0;i<data.roomList.length;i++){
			    	    		  str+="<span class='num'>房间号:"+data.roomList[i].roomName+",面积："+data.roomList[i].area+"m²；</span>"
			    	    	  };
			    	    	  str+='</div>'
			    	    	  return str;
			    	      }
		    		  },{
						  field:'id', 
						  title: '操作', 
						  width: '20%',
						  fixed: 'right', 
						  templet: function(data){
							  var str = '';
							  if (permEdit == 1){
                                  str += '<button class="t-btn t-red-btn t-edit-floor-btn" data-id='+data.floorId+'  data-buildid='+data.buildId+' data-name='+data.floorName+' data-num='+data.roomList.length+' data-has='+data.isHaveMeeting+'>编辑楼层</button>';
                                  str += '<button class="t-btn t-red-btn t-publish-btn" data-id='+data.floorId+' data-type="1">编辑房间</button>';
                              }
                              if (permDelete == 1){
                                  str += '<button class="t-btn t-white-btn t-reject-btn" data-id='+data.floorId+' data-type="2">删除</button>';
							  }
 								return str;
						  }
					  }
					  ]]
					    ,id: 'testReload'
					    ,page: false,
					    where: {
					    		buildId:$("#buildId").val()
					    	},
					    response: {
					    	  statusName: 'code' //数据状态的字段名称，默认：code
					    	  ,statusCode: 200 //成功的状态码，默认：0
					    	  ,countName: "count"//数据总数的字段名称，默认：count
					    	  ,dataName: "data" //数据列表的字段名称，默认：data
					    	}   ,
					    done: function(res, curr, count){
					        //如果是异步请求数据方式，res即为你接口返回的信息。
					        //删除
					        $(".t-reject-btn").on("click",function(){
					        	var id = $(this).data("id");
								_this.delFloorAjax(id);
					        });
					        //编辑
					        $(".t-publish-btn").on("click",function(){
					        	var id = $(this).data("id");
					        	_this.editFloor(id);
					        });
					        //点击修改楼层
							  $(".td-change-floor").on("click",function(){
								  $(this).find(".floor-name-span").hide();
								  $(this).find(".floor-name-text").show().focus();
							  });
							  //失去焦点修改楼层名
							  $(".floor-name-text").on("blur",function(){
								  var floorName = $(this).val();
								  var floorId = $(this).data("floorid");
								  var buildId = $("#buildId").val();
								  var roomCount = $(this).data("floornum");
								  $(this).attr("readonly",true);
								  $(this).find(".floor-name-span").show();
								  $(this).find(".floor-name-text").hide().focus();
								  _this.sureAddFloor(buildId,floorName,roomCount,floorId)
							  })
							//编辑修改楼层t-floor-btn
						        $(".t-edit-floor-btn").on("click",function(){
						        	var floorId = $(this).data("id");
						        	var buildId = $(this).data("buildid");
						        	var floorName = $(this).data("name");
						        	var roomCount = $(this).data("num");
						        	var isHaveMeeting = $(this).data("has")
						        	_this.showEditFloor(floorId,buildId,floorName,roomCount,isHaveMeeting)
						        });
					      }
				  });
				  //查询
				  $('.search-btn').on('click', function(){
					  table.reload('testReload', {
						  where: { //设定异步数据接口的额外参数，任意设
							  buildId: $("#buildId").val()
						  }
						  // ,page: {
						  //   curr: 1 //重新从第 1 页开始
						  // }
					}); 
				  });
				});
	   },
	   /**
	    * 添加楼层弹窗
	    * @param type 'add' 新增 'update'修改
	    *  @param buildId 楼宇
	    *  @param floorName 楼层名称
	    * @param floorId 楼层id
	    * @param roomCount 楼层房间数量
	    * @param isHaveMeeting 是否有会议室
	   */
	   
	   addFloor: function(type,floorId,buildId,floorName,roomCount,isHaveMeeting){

		   var _this = this;
		    if(type=='add'){
	    		$("body").append(template.render(tpl.addFloorDialog)({list:buildList}));
		    }else{
		    	$("body").append(template.render(tpl.updateFloorDialog)({list:buildList,buildId: buildId,floorName:floorName,isHaveMeeting:isHaveMeeting}));
		    }
		   		$(".sure-add-floor-btn").on("click",function(){
		   			var floorName = $(".dialog .floor-name").val();
		   			var buildId = $(".buildId").val();
		   			var selectMeeting = $(".dialog .radio-wp .radio-item:checked").length;
		   			if(!buildId){
		   				layer.alert("请选择楼宇！"); 
		   				return false;
		   			};
		   			if(!floorName){
		   				$(".add-name-wp .error").show();
		   				return false;
		   			};
		   			if(selectMeeting==0){
		   				layer.alert("请选择是否有会议室！");
		   				return false;
		   			}else{
		   				var isHaveMeeting = $(".dialog .radio-wp .radio-item:checked").data("type");
		   			}
		   			if(!isNaN(floorName)){
		   				$(".add-name-wp .error").hide();
		   				if(type=='add'){
		   					_this.sureAddFloor(isHaveMeeting,buildId,floorName,18);
		   				}else{
		   					_this.sureAddFloor(isHaveMeeting,buildId,floorName,roomCount,floorId);
		   				}
		   				
		   			}else{
		   				$(".add-name-wp .error").hide();
		   				if(type=='add'){
		   					_this.sureAddFloor(isHaveMeeting,buildId,floorName,0);
		   				}else{
		   					_this.sureAddFloor(isHaveMeeting,buildId,floorName,roomCount,floorId);
		   				}
		   				
		   			}
		   			
		   			
		   		});
		   		$(".close-btn").on("click",function(){
		   			$(".dialog-wp").remove();
		   		})
		   		
//		   })
	   },
	   /**
	    * 显示修改楼层弹窗
	    * @param orgId 楼层 校区
	    *  @param floorName 楼层名称
	    * @param floorId 楼层id
	    * @param roomCount 楼层房间数量
	    * @param isHaveMeeting 是否有会议室
	    */
	   showEditFloor: function(floorId,buildId,floorName,roomCount,isHaveMeeting){
		   this.addFloor('update',floorId,buildId,floorName,roomCount,isHaveMeeting)
	   },
	   /*
	    * 新增或者修改楼层
	    * @param isHaveMeeting 是否有会议室
	    * @param floorName
	    * @param floorId
	    * @param roomCount
	   */
	   sureAddFloor: function(isHaveMeeting,buildId,floorName,roomCount,floorId){
		   var _this = this;
		   $.ajax({
			   type: "post",
			   url: $("#contextPath").val()+ "/admin/floor/saveUpdateFloor", 
			   data: {
				   floorName: floorName,
				   buildId: buildId,
				   roomCount: roomCount,
				   floorId: floorId,
				   isHaveMeeting: isHaveMeeting
			   },
			   success: function(data){
				   var result = JSON.parse(data);
				   if(result.code == '200'){
					   _this.againRenderTable();
//					   $("#conditionform").submit();
				   }else {
					   layer.alert(result.msg);
//					   $("#conditionform").submit();
				   }
			   },
			   error: function(){
				   
			   }
		   });
	   		$(".dialog-wp").remove();
	   },
	   /*
	    * 编辑楼层
	    * @param floorId
	    */
	   editFloor: function(floorId){
		   var _this = this;
		   //弹出编辑弹窗
		   _this.ajaxFloor(floorId,function(data){
//			   console.log(data.roomList);
			   $("body").append(template.render(tpl.dialog)({roomList: data.data}));
				  //关闭编辑弹窗
				  $(".close-btn").on("click",function(){
					  $(".dialog-wp").remove();
					  _this.againRenderTable();
//					  $("#conditionform").submit();
				  })
				  //编辑房间
				  _this.editRoom($(".room-item"),floorId);
				  //删除房间
				  _this.delRoom($(".room-item"),floorId);
				  //添加房间
				  _this.addRoom(floorId);
				  //编辑失去焦点
//				  _this.blurEvent($(".item-text"),floorId); 
		   })
	   },
	   /*
	    * 请求楼层房间信息
	    * @param floorId
	    * @param callback
	    */
	   ajaxFloor: function(floorId,callback){
		   $.ajax({
			   type: "post",
			   url: $("#contextPath").val()+ "/admin/room/getRoomList", 
			   data: {
				   floorId: floorId
			   },
			   success: function(data){
				  var result = JSON.parse(data);
			     if(result.code == '200'){
			    	 callback(result);
			     }else{
			    	 console.log('error')
			     }
			   },
			   error: function(){
				   
			   }
		   });
	   },
	   /**
	    * 编辑房间 
	    * @param dom
	    * @param floorId
	    */
	   editRoom:  function($dom,floorId){
		   var _this = this;
		   $dom.find(".item-edit-btn").on("click",function(){
			   $(this).hide()
			   $(this).closest(".room-item").find(".item-sure-btn").show();
			   
			   $(this).closest(".room-item").find(".item-text").attr("readonly",false);
			   $(this).closest(".room-item").find(".item-name-text").attr("readonly",false);
//			   $(this).closest(".room-item").siblings().find(".item-text").attr("readonly",true);  
		   });
		   $dom.find(".item-sure-btn").on("click",function(){
			  
			   _this.blurEvent($(this).closest(".room-item"),floorId)
//			   $(this).closest(".room-item").siblings().find(".item-text").attr("readonly",true);  
		   });
	   },
	   /**
	    * 编辑之后失去焦点
	    * @param $dom
	    * @param floorId
	    */
	   blurEvent: function($dom,floorId){
		   var _this = this;
//		  $dom.on("blur",function(){
			   var roomName = $dom.find(".item-text").val();
			   var roomId = $dom.find(".item-text").data("roomid");
			   var area = $dom.find(".item-name-text").val();
			   var $sureBtn = $dom.find(".item-sure-btn");
			   if(!roomName){
				   layer.msg("请输入房间名称");
				   return false;
			   }
			   if(!area){
				   layer.msg("请输入房间面积");
				   return false;
			   };
			   $sureBtn.hide()
			   $sureBtn.closest(".room-item").find(".item-edit-btn").show();
			   
			   $sureBtn.closest(".room-item").find(".item-text").attr("readonly",true);
			   $sureBtn.closest(".room-item").find(".item-name-text").attr("readonly",true);
			   if(!roomId){
				   _this.editRoomAjax(floorId,roomName,$dom,"",area);
			   }else{
				   //$(this).attr("readonly",true);
		   		   _this.editRoomAjax(floorId,roomName,$dom,roomId,area);
			   }
	   		  
//	   	   })  
	   },
	   /*
	    * 新增或修改房间
	    * @param floorId
	    * @param roomName
	    * @param roomId
	    * @param $dom
	    * @param area 面积
	    */
	   editRoomAjax: function(floorId,roomName,$dom,roomId,area){
		   var _this = this;
		   $.ajax({
			   type: "post",
			   url: $("#contextPath").val()+ "/admin/room/saveUpdateRoom", 
			   data: {
				   roomId: roomId,
				   floorId: floorId,
				   roomName: roomName,
				   area: area
			   },
			   success: function(data){
				   var data = JSON.parse(data);
				   if(data.code == '200'){
					   $dom.find(".item-text").attr("readonly",true);	
					   $dom.find(".item-name-text").attr("readonly",true);	
					   $dom.find(".item-edit-btn").show();
					   $dom.find(".item-sure-btn").hide();
					   if(!roomId){
						   _this.renderRoomDialog(floorId);
					   }
				   }else {
					   layer.alert(data.msg);
//					   if(!roomId){
						   _this.renderRoomDialog(floorId);
//					   }
				   }   
			   },
			   error: function(){   
			   }
		   });
	   },
	   /*
	    * 删除房间
	    * @param $dom
	    * @param floorId
	    */
	   //删除房间
	   delRoom: function($dom,floorId){
		   var _this = this;
		   $dom.find(".item-del-btn").on("click",function(){
			   var roomId = $(this).data("roomid");
			   if(confirm("确定删除该房间吗？")){
				   if(roomId){
					   _this.delRoomAjax(roomId,floorId,(this).closest(".room-item"));
				   }else{
					   $(this).closest(".room-item").remove();
				   } 
			   }
			   
			  
		   })
	   },
	   /*
	    * 添加房间
	    * @param floorId
	    */
	   addRoom: function(floorId){
		   var _this = this;
		   $(".add-room-wp .add-room").on("click",function(){
			   var $dom = $(this).closest(".room-item");
		      
			   var preValue = $(this).closest(".room-item").prev(".room-item").find(".item-text").val();
			   if(isNaN(preValue)){
				   var html = template.render(tpl.roomItem)({value: ''});
				   
				   $dom.before(html);	
				   var $preDom = $(this).closest(".room-item").prev(".room-item");
				   $preDom.find(".item-edit-btn").hide();
				   $preDom.find(".item-sure-btn").show();
//				   if($(".room-item").find(".item-text").attr("readonly")){
//					   $preDom.find(".item-text").focus();
//					   _this.blurEvent($preDom.find(".item-text"),floorId);
					   _this.editRoom($preDom,floorId);
					   _this.delRoom($preDom,floorId);
//				   }
			   }else{
				   var html = template.render(tpl.roomItem)({value: +preValue+1});
				  
				   $dom.before(html);
				   var $preDom = $(this).closest(".room-item").prev(".room-item");
				   $preDom.find(".item-edit-btn").hide();
				   $preDom.find(".item-sure-btn").show();
//				   if($(".room-item").find(".item-text").attr("readonly")){
//					   $preDom.find(".item-text").focus();
//					   _this.blurEvent($preDom.find(".item-text"),floorId);
					   _this.editRoom($preDom,floorId);
					   _this.delRoom($preDom,floorId);
//				   }
			   };
			   
		   })
	   },
	   /**
	    * 删除楼层
	    * @param floorId
	    * @param $dom
	    */
	   delFloorAjax: function(floorId,$dom){
		   var _this = this;
		   layer.confirm('确定删除?', {
				  btn: ['确定', '取消'] 
				}, function(index, layero){
					 layer.close(index); //如果设定了yes回调，需进行手工关闭
					 $.ajax({
						   type: "post",
						   url: $("#contextPath").val()+ "/admin/floor/deleteFloor", 
						   data: {
							   floorId: floorId
						   },
						   success: function(data){
							   var data = JSON.parse(data);
							   if(data.code == '200'){
								   _this.againRenderTable();
								   //$("#conditionform").submit();
							   }
							   
						   },
						   error: function(){
							   
						   }
					   });
				}, function(index){
					
				});
		   
	   },
	   /*
	    * 删除房间
	    * @param roomid
	    * @param floorId
	    * @param $dom
	    */
	   delRoomAjax: function(roomId,floorId,$dom){
		   var _this = this;
		   $.ajax({
			   type: "post",
			   url: $("#contextPath").val()+ "/admin/floor/deleteRoom", 
			   data: {
				   roomId: roomId
			   },
			   success: function(data){
				   var data = JSON.parse(data);
				   if(data.code == '200'){
//					   _this.againRenderTable();
					   $dom.remove();
//					   _this.renderRoomDialog(floorId);
					   //$("#conditionform").submit();
				   }   
			   },
			   error: function(){   
			   }
		   });
	   },
	   //重新渲染table
	   againRenderTable: function(){
		   table.reload('testReload', {
				  // page: {
				  //   curr: 1 //重新从第 1 页开始
				  // }
			}); 
	   },
	   /*
	    * 重新渲染房间列表
	    * @param floorId
	    */
	   renderRoomDialog: function(floorId){
		   var _this = this;
		   $.ajax({
			   type: "post",
			   url: $("#contextPath").val()+ "/admin/room/getRoomList", 
			   data: {
				   floorId: floorId
			   },
			   success: function(data){
				  var result = JSON.parse(data);
			     if(result.code == '200'){
			    	 $(".room-list").html(template.render(tpl.roomList)({roomList: result.data}));
//					  //关闭编辑弹窗
//					  $(".close-btn").on("click",function(){
//						  $(".dialog-wp").remove();
//						  $("#conditionform").submit();
//					  })
					  //编辑房间
					  _this.editRoom($(".room-item"),floorId);
					  //删除房间
					  _this.delRoom($(".room-item"),floorId);
					  //添加房间
					  _this.addRoom(floorId);
					  //编辑失去焦点
					  _this.blurEvent($(".item-text"),floorId); 
			     }else{
			    	 console.log('error')
			     }
			   },
			   error: function(){
				   
			   }
		   });
	   }
	};
	roomSetting.init();
});
