/**
 * 推送历史
 * 2018/4/18 by liufang
 */
$(function(){
	var layer;
	var table;
	var ERROR_OK = 200;
	layui.use('layer', function(){
		   layer = layui.layer;  
	});
	var tpl = {
		"floorList": '<% for(var i = 0;i<list.length;i++){%>\
				<li class="select-item">\
					<div class="checkbox-wp">\
					   <span class="checkbox-input">\
						 <input type="checkbox" id="i-<%=list[i].id%>" data-id="<%=list[i].id%>" data-name="<%=list[i].name%>" class="check-item" />\
						  <span class="checkbox-inner"></span>\
					   </span>		\
					   <label class="checkbox-con" for="i-<%=list[i].id%>"><%=list[i].name%>楼</label>\
					</div>\
				</li>\
			<%}%>'	,
			 "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>\
							 <% } %>',
		"selectList": '<%for(var i = 0;i<selectList.length;i++){%>\
				<span data-id="<%=selectList[i].id%>" data-name="<%=selectList[i].name%>" ><%=selectList[i].name%>楼</span>\
			<%} %>'	,
		"detail": '<div class="dialog-wp">\
					<div class="mask"></div>\
					<div class="dialog dialog-detail-con">\
						<div class="title-wp">\
							<span>消息详情</span>\
							<span class="close-btn"></button>\
						</div>\
						<div class="dialog-detail-wp">\
							<%=con%>\
						</div>\
					</div>\
				</div>	',
		"wxTpl": 	'<div class="dialog-wp">\
			<div class="mask"></div>\
			<div class="dialog dialog-wx-con">\
				<div class="title-wp">\
					<span>消息详情</span>\
					<span class="close-btn"></button>\
				</div>\
				<div class="dialog-wx-wp">\
					<div class="wx-template-wp">\
			   			<h2 class="t-title">企业账单</h2>\
			   			<span class="t-date"><%=createTime%></span>\
			   			<div class="t-title-wp"><%= title%></div>\
			   			<ul class="t-detail-list">\
			   				<li class="t-detail-item">企业总金额：<%=money%>元</li>\
			   				<li class="t-detail-item">账单月份：<%=month%>月</li>\
							\
			   					<li class="t-detail-item">计费区间：<%if(!!beginTime && !!endTime){%><%=beginTime%> ~ <%=endTime %><%}%></li>\
							\
			   			</ul>\
			   			<div class="t-type"><%=remark%></div>\
			   		</div>\
				</div>\
			</div>\
		</div>	'
	};
	var billHistory = {
		init: function(){
			this.render();
			this.bindEvent();
			this.getOrgList();
		},
		render: function(){
			this.renderTable();
		},
		bindEvent: function(){
			var _this = this;
			
			//查询
			$(".search-btn").on("click",function(){
				var floorArr = []
				$(".floor-selected-list span").each(function(index,ele){
					var id = $(ele).data("id");
					floorArr.push(id);
				})
				$(".floors-input").val(floorArr.join(";"));
				
//				$("#conditionform").submit();
			});
			
			//重置
			$(".set-btn").on("click",function(){
				$("#conditionform input").val("");
				$("#conditionform select").val("");
				 $(".floor-selected-list").html("");
				 $(".floor-select .check-item").each(function(index,ele){
					 $(ele).prop("checked",false);
				 })
			})
			layui.use('laydate', function(){
				  var laydate = layui.laydate;
				  
				  //执行一个laydate实例
				  laydate.render({
				    elem: '#startTime' //指定元素
			    	
				  });
				  laydate.render({
					    elem: '#endTime' //指定元素
				   
					  });
				});
//			//输入开始时间
//			$("#startTime").on("click",function(){
//				laydate({
//		            elem: document.getElementById('startTime'),
////					max: laydate.now(), //-1代表昨天，-2代表前天，以此类推
//		        });
//			});
//			//输入结束时间
//			$("#endTime").on("click",function(){
//				laydate({
//		            elem: document.getElementById('endTime'),
////					min: laydate.now(), //-1代表昨天，-2代表前天，以此类推
//		        });
//			});
			//选择楼宇触发事件
			$(".buildings-select").on("change",function(){
				 var $this = $(this);
				 if($this.val()){
					 _this.ajaxFloors($this.val());
				 }else{
					 $(".floor-selected-list").html("");
					 $(".floor-select .check-item").each(function(index,ele){
						 $(ele).prop("checked",false);
					 })
				 }
			})
			//点击body 楼层下拉框收起来
			$("body").on("click",function(event){
				if($(".floors-wp").hasClass("active")){
					$(".floors-wp").removeClass("active");
				}
			});
			$(".floor-select").on("click",function(event){
				 event.stopPropagation();
				 var selectList =  [];
				$(".floor-select .check-item").each(function(index,ele){
					if($(ele).is(':checked')){
						selectList.push({id: $(ele).data("id"),name: $(ele).data("name")})
					}
				});
				$(".floor-selected-list").html(template.render(tpl.selectList)({selectList: selectList}))
			})
			//展示 楼层
			$(".default-name").on("click",function(event){
				var $this = $(this);
				event.stopPropagation();
				if($(".buildings-select").val()){
					$this.closest(".floors-wp").toggleClass("active");
				}else{
					alert('请先选择楼宇')
				}
			});	
			//点击展开详情
//			$(".check-btn").on("click",function(){
//				if($(this).data("type") !=1){
//					var con = $(this).data("con");
//					$("body").append(template.render(tpl.detail)({con: con}));
//					$(".close-btn").on("click",function(){
//						$(".dialog-wp").remove();
//					})
//				}else{
//					var title = $(this).data("title");
//					var remark = $(this).data("remark");
//					var beginTime = $(this).data("begin");
//					var endTime = $(this).data("end")
//					var money = $(this).data("money");
//					var month = $(this).data("month");
//					var createTime = $(this).data("createtime")
//					$("body").append(template.render(tpl.wxTpl)({title:title,remark:remark,beginTime:beginTime,endTime:endTime,money: money,month:month,createTime:createTime}));
//					$(".close-btn").on("click",function(){
//						$(".dialog-wp").remove();
//					})
//				}
//				
//			})
		},
		getOrgList: function(){
			var _this = this;
			$.ajax({
				   type: "post",
				   url: $("#contextPath").val()+ "/admin/templ/getOrgList", 
				   success: function(data){
					   if(data.code == '200'){
						  orgList = data.data;
						  $("#orgId").html(template.render(tpl.itemTpl)({list: data.data}));
						  if(orgList.length==1){
							  var orgId=orgList[0].id;
							  _this.ajaxBuild(orgId);
						  }
						  $("#orgId").on("change",function(){
						  	 var id = $(this).val();
                              _this.ajaxBuild(id);
						  })

					   }   
				   },
				   error: function(){   
				   }
		   });
		},
		renderTable: function(){
			var _this = this;
			layui.use('table', function(){
				  table = layui.table;
				  
				  //方法级渲染
				  table.render({
				    elem: '#LAY_table_user',
				    method: 'post'
				    ,url: $("#contextPath").val()+ "/admin/push/pushBillList"
				    ,skin: 'nob' //行边框风格
			    		  ,even: true //开启隔行背景
			    		  ,size: 'lg' //小尺寸的表格
				    ,cols: [[
				      {
				    		   field:'Index', 
				    		   title: '序号',
				    		   templet: '#indexTpl',
				    		   width: '5%'
				    	  },{
			    		   field:'createTime', 
			    		   title: '推送时间', 
			    		   width: '15%'
			    	  },
//			    	  {
//			    		  field:'orgName',
//			    		  title: '项目',
//			    		  width: '8%'
//		    		  },
		    		  {
			    		  field:'buildName',
			    		  title: '楼宇',
			    		  width: '12%'
		    		  },
//		    		  {
//		    			  field:'floorNames', 
//		    			  title: '楼层', 
//		    			  width: '8%'
//	    			  },
	    			  {
	    				  field:'roomName', 
	    				  title: '房间号',
	    				  width: '10%'
	    				  
	  				  },{
	  					  field:'virName', 
	  					  title: '企业名称',
	  					width: '10%'
	  					 				 
	  				  },{
	  					  field:'billType', 
	  					  title: '账单类型',
	  					  width: '8%',
						  templet: function(data){
							 if(data.billType==0){
								 return '出账日账单'
							 }else{
								 return '逾期账单'
							 }
						  }
	  					 
					  },{
						  field:'pushUser1', 
						  title: '账单接收人',
						  width: '10%',
						  templet: function(data){
							 if(!!data.pushUser1 && !!data.pushUser2 ){
								 return data.pushUser1+';' + data.pushUser2
							 }else if(!!data.pushUser1 && !data.pushUser2){
								 return data.pushUser1
							 }else if(!data.pushUser1 && !!data.pushUser2){
								 return data.pushUser2
							 }
						  }
						
					  },{
						  field:'pushAmount', 
						  title: '账单金额',
						  width: '10%'
					  },
					  {
						  field:'messageData', 
						  title: '短信推送详情',
						  width:'10%',
						  templet: function(data){
							  return '<span class="deep-blue-text check-msg"  data-id='+data.id+'>详情</span>'
						  }
						
					  },{
						  field:'emailData', 
						  title: '邮件推送详情',
						  width: '10%',
						  templet: function(data){
							  return "<span class='deep-blue-text check-email'  data-id="+data.id+" >详情</span>"
						  }
					  }
					  ]]
					    ,id: 'testReload'
					    ,page: true,
					    response: {
					    	  statusName: 'code' //数据状态的字段名称，默认：code
					    	  ,statusCode: 200 //成功的状态码，默认：0
					    	  ,countName: "count"//数据总数的字段名称，默认：count
					    	  ,dataName: "data" //数据列表的字段名称，默认：data
					    	}   ,
					    	where: {
								virName: $("#conditionform .virName").val()
							} ,
					    done: function(res, curr, count){
					    	//查看短信详情
					    	$(".check-msg").on("click",function(){
					    		var id = $(this).data("id");
					    		_this.ajaxDetail(id,'message',function(con){
					    			$("body").append(template.render(tpl.detail)({con: con}));
									$(".close-btn").on("click",function(){
										$(".dialog-wp").remove();
									})
					    		})
								
					    	});
					    	
					    	//查看微信详情
					    	$(".check-wx").on("click",function(){
					    		var billtype = $(this).data("billtype");
//					    		var title = $(this).data("title");
//								var remark = $(this).data("remark");
								var beginTime = $(this).data("begin");
								var endTime = $(this).data("end")
								var money = $(this).data("money");
								var month = $(this).data("month");
								var createTime = $(this).data("createtime");
								_this.renderWxDetail(billtype,beginTime,endTime,money,month,createTime);
								
					    	});
					    	//查看邮件详情
					    	$(".check-email").on("click",function(){
					    		var id = $(this).data("id");
					    		_this.ajaxDetail(id,'email',function(con){
					    			$("body").append(template.render(tpl.detail)({con: con}));
									$(".close-btn").on("click",function(){
										$(".dialog-wp").remove();
									})
					    		})
					    	});
					      }
				  });
				  //查询
				  $('#conditionform  .search-btn').on('click', function(){
					  _this.againRenderTable();
				  });
				});
		},
		/*重新渲染table*/
		againRenderTable: function(){
			var floorArr = []
			$(".floor-selected-list span").each(function(index,ele){
				var id = $(ele).data("id");
				floorArr.push(id);
			});
//			$(".floors-input").val(floorArr.join(";"));
			table.reload('testReload', {
				where: {
					beginTime: $("#conditionform #startTime").val(),
					endTime: $("#conditionform #endTime").val(),
					billType: $("#conditionform .bill-type").val(),
					buildId: $("#conditionform .buildings-select").val(),//楼宇id
					floorIds: floorArr.join(";"),
					virName: $("#conditionform  .vir-name").val(),
					proId:$("#orgId").val(),//项目id
				} ,
				  page: {
				    curr: 1 //重新从第 1 页开始
				  }
			}); 
		},
		renderWxDetail: function(billtype,beginTime,endTime,money,month,createTime){
			var _this = this;
			 $.ajax({
				   type: "post",
				   url: $("#contextPath").val()+ "/admin/push/getTitleAndRemark", 
				   success: function(data){
					   
					   if(data.code == '200'){
						  if(billtype==0){
							  $("body").append(template.render(tpl.wxTpl)({title:data.data.title1,remark:data.data.remark1,beginTime:beginTime,endTime:endTime,money: money,month:month,createTime:createTime}));
								$(".close-btn").on("click",function(){
									$(".dialog-wp").remove();
								})
						  }else{
							  $("body").append(template.render(tpl.wxTpl)({title:data.data.title2,remark: data.data.remark2,beginTime:beginTime,endTime:endTime,money: money,month:month,createTime:createTime}));
								$(".close-btn").on("click",function(){
									$(".dialog-wp").remove();
								}) 
						  }
					   }else{
						   layer.alert(data.msg);
					   }   
				   },
				   error: function(){   
				   }
			   });
		},
		/**
		 * 请求楼层数据
		 * @param orgid
		 */
		ajaxFloors: function(buildId){
			var _this = this;
			 $.ajax({
				   type: "post",
				   url: $("#contextPath").val()+ "/admin/floor/getFloorList", 
				   data: {
					   "buildId": buildId
				   },
				   success: function(data){
					   var result = JSON.parse(data);
					   if(result.code == '200'){
						   $(".floor-select").html(template.render(tpl.floorList)({list: result.data}))
					   }else{
						   layer.alert(result.msg);
					   }   
				   },
				   error: function(){   
				   }
			   });
		},
		
		/**
		 * 请求楼宇数据
		 * @param orgid
		 */
		ajaxBuild: function(orgId){
			var _this = this;
			 $.ajax({
				   type: "post",
				   url: $("#contextPath").val()+ "/admin/floor/getBuildByOrg", 
				   data: {
					   "orgId": orgId
				   },
				   success: function(data){
					   if(data.code == '200'){
//						   $(".buildings-select").html(template.render(tpl.buildTpl)({list: data.data}))
					   }else{
						   layer.alert(data.msg);
					   }   
				   },
				   error: function(){   
				   }
			   });
		},
		/**
		 * 请求短信和邮件详情
		 * @param id
		 * @param type 注："message"为短信详情,"email"为邮箱详情
		 */
		ajaxDetail: function(id,type,callback){
			var _this = this;
			 $.ajax({
				   type: "post",
				   url: $("#contextPath").val()+ "/admin/push/getDataByPush", 
				   data: {
					   "id": id,
					   "type": type
				   },
				   success: function(data){
					  
					   if(data.code == ERROR_OK){
						   callback(data.data)
					   }else{
						   layer.alert(data.msg);
					   }   
				   },
				   error: function(){   
					   alert('error')
				   }
			   });
		}
	};
	billHistory.init();
})