/**
 * app公告发布 发送历史
 * 2018/4/20 by liufang
 */
$(function(){
	
	var tpl = {
			"ruleTpl": '<div class="dialog-wp">\
				<div class="mask"></div>\
				<div class="dialog dialog-rule-con">\
					<div class="title-wp">\
						<span>新增规则</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class="dialog-rule-wp">\
						<div class="rule-con">\
							<div class="rule-name-wp rule-item">\
								<div class="rule-tip">规则名称</div>\
								<input class="rule-name item-con" type="text" placeholder="请输入规则名称">\
							</div>\
							<div class="price-wp rule-item">\
								<div class="rule-tip">单价</div>\
								<div class="price-con item-con">\
									<div class="price-house">\
										每户<input type="text" class="house-text"/>元/每小时\
									</div>\
									<div class="price-hour">\
										每平米<input type="text" class="hour-text"/>元/每小时\
									</div>\
								</div>\
							</div>\
							<div class="date-type-wp rule-item">\
								<div class="rule-tip">日期类型</div>\
								<div class="date-type-con item-con">\
									<div class="radio-wp">\
									   <span class="radio-input">\
										 <input type="radio" id="work" class="radio-item audit-pass" data-status="0" name="dataType" />\
										  <span class="radio-inner"></span>\
									   </span>		\
									   <label class="radio-con" for="work">工作日</label>\
									</div>\
									<div class="radio-wp">\
									   <span class="radio-input">\
										 <input type="radio" id="no_work" class="radio-item audit-no-pass" data-status="1" name="dataType"/>\
										  <span class="radio-inner"></span>\
									   </span>		\
									   <label class="radio-con" for="no_work">休息日</label>\
									</div>\
									<div class="radio-wp">\
									   <span class="radio-input">\
										 <input type="radio" id="no_pass" class="radio-item audit-no-pass" data-status="2" name="dataType"/>\
										  <span class="radio-inner"></span>\
									   </span>		\
									   <label class="radio-con" for="no_pass">节假日</label>\
									</div>\
								</div>\
							</div>\
							<div class="charge-wp rule-item">\
								<div class="rule-tip">收费时段</div>\
								<div class="charge-con item-con" >\
									<input class="charge-text" type="text" id="charge" placeholder="请设置时段" readonly/>\
								</div>\
							</div>\
							<div class="date-type-wp rule-item">\
								<div class="rule-tip">应用模块</div>\
								<div class="date-type-con item-con">\
									<div class="checkbox-wp">\
									   <span class="checkbox-input">\
										  <input type="checkbox" id="part" class="check-item enterprise-wx" name="applicationMeeting"/>\
										  <span class="checkbox-inner"></span>\
									   </span>		\
									   <label class="checkbox-con" for="part">预定会议室</label>\
									</div>\
									<div class="checkbox-wp">\
									   <span class="checkbox-input">\
										 <input type="checkbox" id="wx" class="check-item enterprise-wx" name="applicationAirAddTime"/>\
										  <span class="checkbox-inner"></span>\
									   </span>		\
									   <label class="checkbox-con" for="wx">空调加时</label>\
									</div>\
								</div>\
							</div>\
							<div class="remark-wp rule-item">\
								<div class="rule-tip">备注</div>\
								<textarea class="remark-con item-con" placeholder="请输入备注"></textarea>\
							</div>\
						</div>\
						<div class="dialog-btm">\
							<button class="cancel-rule-btn btn-white">取消</button>\
							<button class="sure-rule-btn btn-red">保存</button>\
						</div>\
					</div>\
				</div>\
			</div>	',
			"ruleEditTpl": '<div class="dialog-wp">\
				<div class="mask"></div>\
				<div class="dialog dialog-rule-con">\
					<div class="title-wp">\
						<span>编辑规则</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class="dialog-rule-wp">\
						<div class="rule-con">\
							<div class="rule-name-wp rule-item">\
								<div class="rule-tip">规则名称</div>\
								<input class="rule-name item-con" type="text" placeholder="请输入规则名称" value="<%=ruleName%>">\
							</div>\
							<div class="price-wp rule-item">\
								<div class="rule-tip">单价</div>\
								<div class="price-con item-con">\
									<div class="price-house">\
										每户<input type="text" class="house-text" value="<%=priceHouse%>"/>元/每小时\
									</div>\
									<div class="price-hour">\
										每平米<input type="text" class="hour-text" value="<%=priceHour%>"/>元/每小时\
									</div>\
								</div>\
							</div>\
							<div class="date-type-wp rule-item">\
								<div class="rule-tip">日期类型</div>\
								<div class="date-type-con item-con">\
									<div class="radio-wp">\
									   <span class="radio-input">\
										 <input type="radio" id="work" class="radio-item audit-pass" data-status="0" name="dataType" <%if(dataType==0){%> checked <%}%>/>\
										  <span class="radio-inner"></span>\
									   </span>		\
									   <label class="radio-con" for="work">工作日</label>\
									</div>\
									<div class="radio-wp">\
									   <span class="radio-input">\
										 <input type="radio" id="no_work" class="radio-item audit-no-pass" data-status="1" name="dataType" <%if(dataType==1){%> checked <%}%>/>\
										  <span class="radio-inner"></span>\
									   </span>		\
									   <label class="radio-con" for="no_work">休息日</label>\
									</div>\
									<div class="radio-wp">\
									   <span class="radio-input">\
										 <input type="radio" id="no_pass" class="radio-item audit-no-pass" data-status="2" name="dataType" <%if(dataType==2){%> checked <%}%>/>\
										  <span class="radio-inner"></span>\
									   </span>		\
									   <label class="radio-con" for="no_pass">节假日</label>\
									</div>\
								</div>\
							</div>\
							<div class="charge-wp rule-item">\
								<div class="rule-tip">收费时段</div>\
								<div class="charge-con item-con" >\
									<input class="charge-text" type="text" id="charge" placeholder="请设置时段" value="<%=startTime%> - <%=endTime%>" readonly/>\
								</div>\
							</div>\
							<div class="date-type-wp rule-item">\
								<div class="rule-tip">应用模块</div>\
								<div class="date-type-con item-con">\
									<div class="checkbox-wp">\
									   <span class="checkbox-input">\
										  <input type="checkbox" id="part" class="check-item enterprise-wx" name="applicationMeeting" <%if(applicationMeeting==1){%> checked <%}%>/>\
										  <span class="checkbox-inner"></span>\
									   </span>		\
									   <label class="checkbox-con" for="part">预定会议室</label>\
									</div>\
									<div class="checkbox-wp">\
									   <span class="checkbox-input">\
										 <input type="checkbox" id="wx" class="check-item enterprise-wx" name="applicationAirAddTime" <%if(applicationAirAddTime==1){%> checked <%}%>/>\
										  <span class="checkbox-inner"></span>\
									   </span>		\
									   <label class="checkbox-con" for="wx">空调加时</label>\
									</div>\
								</div>\
							</div>\
							<div class="remark-wp rule-item">\
								<div class="rule-tip">备注</div>\
								<textarea class="remark-con item-con" placeholder="请输入备注" value=""><%=remark%></textarea>\
							</div>\
						</div>\
						<div class="dialog-btm">\
							<button class="cancel-rule-btn btn-white">取消</button>\
							<button class="sure-rule-btn btn-red">保存</button>\
						</div>\
					</div>\
				</div>\
			</div>	',
			"applyTpl": '<div class="dialog-wp">\
				<div class="mask"></div>\
				<div class="dialog dialog-apply-con">\
					<div class="title-wp">\
						<span>申请/取消规则</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class="dialog-apply-wp">\
						<div class="apply-con">\
							<div class="apply-item">\
								<div class="apply-tip">提前申请时间</div>\
								<div class="item-con">\
									<input class="apply-time" type="text" placeholder="请输入" value="<%=beforeApplyTime%>">小时\
								</div>\
							</div>\
							<div class="apply-item">\
								<div class="apply-tip">提前取消时间</div>\
								<div class="item-con">\
									<input class="cancel-time" type="text" placeholder="请输入" value="<%=beforeCancelTime%>">小时\
								</div>\
							</div>\
							<div class="apply-item">\
								<div class="apply-tip">周六日期类型</div>\
								<div class="item-con">\
									<div class="checkbox-wp">\
									   <span class="checkbox-input">\
										 <input type="checkbox" id="wx" name="satDataType" class="check-item enterprise-wx" <%if(satDataType==1){%>checked<%}%>/>\
										  <span class="checkbox-inner"></span>\
									   </span>		\
									   <label class="checkbox-con" for="wx">勾选后非节假日的周六为工作日</label>\
									</div>\
								</div>\
							</div>\
						</div>\
						<div class="dialog-btm">\
							<button class="cancel-rule-btn btn-white">取消</button>\
							<button class="sure-rule-btn btn-red">保存</button>\
						</div>\
					</div>\
				</div>\
			</div>	',
			"detailTpl": '<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">\
						<div class="detail-con">\
							<div class="detail-item">\
								<div class="detail-tip">规则名称:</div>\
								<div class="item-con"><%=ruleName%></div>\
							</div>\
							<div class="detail-item">\
								<div class="detail-tip">单价:</div>\
								<div class="item-con">每户<%=priceHouse%>元/每小时,每平米<%=priceHour%>元/每小时</div>\
							</div>\
							<div class="detail-item">\
								<div class="detail-tip">日期类型:</div>\
								<div class="item-con">\
									<%if(dataType==0){%>\
										工作日\
									<%}else if(dataType==1){%>\
										休息日\
									<%}else if(dataType==2){%>	\
										节假日\
									<%}%>\
								</div>\
							</div>\
							<div class="detail-item">\
								<div class="detail-tip">收费时段:</div>\
								<div class="item-con"><%=startTime%> - <%=endTime%></div>\
							</div>\
							<div class="detail-item">\
								<div class="detail-tip">应用模块:</div>\
								<div class="item-con">\
									<%if(applicationAirAddTime==1){%>\
										预定会议室&nbsp;\
									<%}%>\
									<%if(applicationMeeting==1){%>\
										空调加时\
									<%}%>\
								</div>\
							</div>\
							<div class="detail-item">\
								<div class="detail-tip">备注:</div>\
								<div class="item-con"><%=remark%></div>\
							</div>\
						</div>\
					</div>\
				</div>\
			</div>	',
	}
	template.config("escape", false);
	var contextPath =  $("#contextPath").val();
	var table;
	var appSend = {
			init: function(){
				this.render();
				this.bindEvent();
			},
			render: function(){
				this.rendertable();
//				$("body").append(template.render(tpl.applyTpl))
			},
			bindEvent: function(){
				var _this = this;
				layui.use('laydate', function(){
					  var laydate = layui.laydate;
					  
					  //执行一个laydate实例
					  laydate.render({
					    elem: '#startTime' //提交开始时间
				    	
					  });
					  laydate.render({
						    elem: '#endTime' //提交结束时间
					  });
					
					});
				//重置
				$(".set-btn").on("click",function(){
					$("#conditionform input,#conditionform select").val("");
					 _this.againRenderTable();
				});
				//新增规则
				$(".add-rule").on("click",function(){
					var laydate = layui.laydate;
					$("body").append(template.render(tpl.ruleTpl));
					//关闭弹窗
					$(".close-btn,.dialog-wp .cancel-rule-btn ").on("click",function(){
						$(".dialog-wp").remove();
					});
					laydate.render({
					    elem: '#charge'
					    ,type: 'time'
					    ,range: true
					    ,format: 'HH:mm'
				    });
					//点击保存绑定事件
					$(".sure-rule-btn").on("click",function(){
						_this.addRuleEvent();
					})
				});
				//申请/取消规则
				$(".apply-or-cancel-rule").on("click",function(){
					_this.getRule(function(data){
						$("body").append(template.render(tpl.applyTpl)(data));
						//关闭弹窗
						$(".close-btn,.dialog-wp .cancel-rule-btn ").on("click",function(){
							$(".dialog-wp").remove();
						});
						$(".sure-rule-btn").on("click",function(){
							_this.saveRuleConfig();
						})
					})
				})
			},
			rendertable: function(){
				var _this = this;
				layui.use('table', function(){
					   table = layui.table;
					  
					  //方法级渲染
					  table.render({
					    elem: '#LAY_table_user'
				    	 ,skin: 'nob' //行边框风格
			    		  ,even: true //开启隔行背景
			    		  ,size: 'lg' //小尺寸的表格
					    ,url:contextPath + "/rule/list"
					    ,cols: [[
					      {
//				    		   field:'id', 
//				    		   title: 'ID', 
//				    		   minWidth:60
//				    	  },{
				    		  field:'ruleName',
				    		  title: '规则名称',
				    		  minWidth:200
			    		  },{
			    			  field:'priceDisplay', 
			    			  title: '单价', 
			    			  minWidth:300
	    				  },{
		    				  field:'chargeTimeDisplay', 
		    				  title: '收费时段', 
		    				  minWidth:200
	    				  },{
		    				  field:'applicationAirAddTime', 
		    				  title: '应用模块', 
		    				  minWidth:150,
		    				  templet: function(data){
		    					var str = ''; 
		    					if(data.applicationMeeting==1){
		    						str += '预定会议室 &nbsp'
		    					};
		    					if(data.applicationAirAddTime==1){
		    						str += '空调加时'
		    					};
		    					return str;
	    					  }
	    				  },{
		    				  field:'isEnabled', 
		    				  title: '状态', 
		    				  minWidth:80,
		    				  templet: function(data){
			    					if(data.isEnabled==1){
			    						return '启用'
			    					}else if(data.isEnabled==0){
			    						return '停用'
			    					}
		    					  }
	    				  },{
		    				  field:'createTime', 
		    				  title: '创建时间', 
		    				  minWidth:180
	    				  },{
	    					  field:'approval_user', 
	    					  title: '查看详情', 
	    					  minWidth:120,
	    					  templet: function(data){
		    						 return "<span data-id="+data.id+" class='check-blue-btn check-detail'>详情<span>"
	    					  }
	  					  },{
							  field:'id', 
							  title: '操作', 
							  fixed: "right",
							  minWidth: 180,
							  templet: function(data){
								  var str = '';
    							  str+= "<span data-id="+data.id+" class='check-blue-btn edit-btn'>编辑</span>";
    							  if(data.isEnabled==1){
    								  str+= "<span data-id="+data.id+" data-status="+data.isEnabled+" class='red disable-btn'>停用</span>"
    							  }else{
    								  str+= "<span data-id="+data.id+" data-status="+data.isEnabled+" class='red enable-btn'>启用</span>"
    							  }
    							  
    							  str+= "<span data-id="+data.id+" data-status="+data.isEnabled+" class='red del-btn'>删除</span>"
	    						 return str;
	    					  }
						  }
						  ]]
						    ,id: 'testReload'
						    ,page: true,
						    method: 'post',
						    response: {
						    	  statusName: 'code' //数据状态的字段名称，默认：code
						    	  ,statusCode: 200 //成功的状态码，默认：0
						    	  ,countName: "count"//数据总数的字段名称，默认：count
						    	  ,dataName: "data" //数据列表的字段名称，默认：data
						    	}   ,
					    	request: {
					    	    pageName: 'pageNo' //页码的参数名称，默认：page
					    	    ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
					    	  }	,
						    done: function(res, curr, count){
						        //如果是异步请求数据方式，res即为你接口返回的信息。
						    	
						        //详情
						        $(".check-detail").on("click",function(){
						        	var id = $(this).data("id");
						        	
						        	//显示详情弹窗
						        	_this.showDetailDialog(id)
						        });
						        //编辑
						        $(".edit-btn").on("click",function(){
						        	var id = $(this).data("id");
						        	_this.showEditDialog(id);
						        })
						        //启用
						        $(".enable-btn").on("click",function(){
						        	var id = $(this).data("id");
						        	
						        	_this.showOperate(id,'enable')
						        })
						        //停用
						        $(".disable-btn").on("click",function(){
						        	var id = $(this).data("id");
						        	
						        	_this.showOperate(id,'disable')
						        })
						        //删除
						        
						        $(".del-btn").on("click",function(){
						        	var id = $(this).data("id");
						        	
						        	_this.delAjax(id)
						        })
						      }
					  });
					  //查询
					  $('#conditionform  .search-btn').on('click', function(){
						  _this.againRenderTable();
					  });
					});
			},
			/*重新渲染table*/
			againRenderTable: function(){
				table.reload('testReload', {
					where: {
						isEnabled: $("#conditionform .isEnabled").val(),
						dataType: $("#conditionform .dataType").val()
					} ,
					  page: {
					    curr: 1 //重新从第 1 页开始
					  }
				}); 
			},
			
			/**
			 * 启用或者停用
			 * @param id
			 * @param type 操作内型 enable启用 disable禁用
			 */
			showOperate: function(id,type){
				var _this = this;
				$.ajax({
					   type: "post",
					   url: contextPath+ "/rule/enableOrDisable", 
					   data:{
						   id: id,
						   operationType: type
					   },
					   success: function(data){
						   if(data.code == 200){
							   _this.againRenderTable();
							   if(type=='enable'){
								   layer.msg("启用成功")
							   }else{
								   layer.msg("停用成功")
							   }
						   }else{
							   layer.msg(data.msg)
						   }   
					   },
					   error: function(error){   
						   layer.msg('error')
					   }
			   });
			},
			/**
			 * 删除
			 * @param id
			 */
			delAjax: function(id){
				var _this = this;
				layer.confirm('确定删除?', {
					  btn: ['确定', '取消'] 
					}, function(index, layero){
						 layer.close(index); //如果设定了yes回调，需进行手工关闭
						 $.ajax({
							   type: "post",
							   url: contextPath+ "/rule/del", 
							   data:{
								   id: id
							   },
							   success: function(data){
								   if(data.code == 200){
									   _this.againRenderTable();
									   layer.msg("删除成功！")
								   }else{
									   layer.msg(data.msg)
								   }   
							   },
							   error: function(error){   
								   layer.msg('error')
							   }
					   });
					}, function(index){
						
					});	
			},
			/**
			 * 编辑或者新增规则弹窗绑定事件
			 * @param id
			 */
			addRuleEvent: function(id){
				var _this = this;
				var result = {
						id: !!id ? id: '',
						ruleName: '',
						priceHouse: '',//每户/元
						priceHour: '',//每平米/元
						dataType: '',//日期类型(0工作日\1休息日2节假日
						startTime:'',//收费开始时间
						endTime: '',//收费结束时间
						applicationMeeting: '',//应用模块(预定会议室 0 1
						applicationAirAddTime: '',//应用模块(空调加时 0 1
						remark:''//备注
				}
				//规则名称
			    var ruleName = $(".rule-name").val();
			    if(!ruleName){
					layer.msg('请输入规则名称！');
					return false;
				};
				result['ruleName'] = ruleName;
			
				//每户/元
				var priceHouse = $(".house-text").val();
				if(!priceHouse){
					layer.msg('请输入每户多少元/每小时！');
					return false;
				};
				result['priceHouse'] = priceHouse;
				
				//每平米/元
				var priceHour = $(".hour-text").val();
				if(!priceHour){
					layer.msg('请输入每户多少元/每小时！');
					return false;
				};
				result['priceHour'] = priceHour;
				
				//日期类型
				var dataType = $(".radio-wp .radio-item[name='dataType']:checked").data("status");
				if(dataType != 0 && dataType != 1 && dataType != 2 ){
					layer.msg('请选择日期类型！');
					return false;
				}
				result['dataType'] = dataType;
				
				//收费时间
				var time = $(".charge-text").val();
				if(!time ){
					layer.msg('请选择收费时段！');
					return false;
				}
				result['startTime'] = time.substring(0,5);
				result['endTime'] = time.substring(time.length-5);
				
				//应用模块
				var applicationMeeting = $(".checkbox-wp .check-item[name='applicationMeeting']").prop('checked') ? 1 : 0;
				var applicationAirAddTime= $(".checkbox-wp .check-item[name='applicationAirAddTime']").prop('checked') ? 1 : 0;
				if(!applicationMeeting && !applicationAirAddTime){
					layer.msg('请选择应用模块！');
					return false;
				}
				result['applicationMeeting'] = applicationMeeting;
				result['applicationAirAddTime'] = applicationAirAddTime;
				
			   //备注
				var remark = $(".remark-con").val();
				result['remark'] = remark;
				
				_this.ruleAjax(result);
				
			},
			/**
			 * 新增或者编辑规则请求
			 * @param data
			 */
			ruleAjax: function(data){
				var _this = this;
				 var tip = !!data.id ? '编辑成功!' : '新增成功!';
				 $.ajax({
					   type: "post",
					   url: contextPath+ "/rule/addOrSaveRole", 
					   data:data,
					   success: function(data){
						   if(data.code == 200){
							   _this.againRenderTable();
							   $(".dialog-wp").remove();
							   layer.msg(tip)
						   }else{
							   layer.msg(data.msg)
						   }   
					   },
					   error: function(error){   
						   layer.msg('error')
					   }
			   });
			},
			/**
			 * 显示编辑弹窗
			 * @param id
			 */
			showEditDialog: function(id){
				var _this = this;
				_this.ajaxDetail(id,function(data){
					var laydate = layui.laydate;
					$("body").append(template.render(tpl.ruleEditTpl)(data));
					//关闭弹窗
					$(".close-btn,.dialog-wp .cancel-rule-btn ").on("click",function(){
						$(".dialog-wp").remove();
					});
					laydate.render({
					    elem: '#charge'
					    ,type: 'time'
					    ,range: true
					    ,format: 'HH:mm',
					   
				    });
					//点击保存绑定事件
					$(".sure-rule-btn").on("click",function(){
						_this.addRuleEvent(id);
					})
				})	
			},
			/**
			 * 请求详情
			 * @param id
			 * @param callback
			 */
			ajaxDetail: function(id,callback){
				var _this = this;
				
				 $.ajax({
					   type: "post",
					   url: contextPath+ "/rule/getDetail", 
					   data:{
						   id: id
					   },
					   success: function(data){
						   if(data.code == 200){
							  callback(data.data);
						   }else{
							   layer.msg(data.msg)
						   }   
					   },
					   error: function(error){   
						   layer.msg('error')
					   }
			   });
			},
			/**
			 * 显示详情弹窗
			 * @param id
			 */
			showDetailDialog: function(id){
				var _this = this;
				_this.ajaxDetail(id,function(data){
					$("body").append(template.render(tpl.detailTpl)(data));
					//关闭弹窗
					$(".close-btn").on("click",function(){
						$(".dialog-wp").remove();
					});
				})
			},
			//获取规则
			getRule: function(callback){
				var _this = this;
				
				 $.ajax({
					   type: "post",
					   url: contextPath+ "/projectConfig/getProjectConfig", 
					   success: function(data){
						   if(data.code == 200){
							   
							  callback(data.data);
						   }else{
							   layer.msg(data.msg)
						   }   
					   },
					   error: function(error){   
						   layer.msg('error')
					   }
			   });
			},
			//点击保存 申请/取消规则
			saveRuleConfig: function(){
				var _this = this;
				var result = {
						beforeApplyTime: '',//提前申请时间
						beforeCancelTime: '',//提前取消时间
						satDataType: ''//周六日期类型(勾选后非节假日的周六为工作日 0否 1是
				};
				//提前申请时间
				var beforeApplyTime = $(".apply-time").val();
				if(!beforeApplyTime ){
					layer.msg('请输入提前申请时间！');
					return false;
				}
				result['beforeApplyTime'] = beforeApplyTime;
				
				var beforeCancelTime = $(".cancel-time").val();
				if(!beforeCancelTime ){
					layer.msg('请输入提前取消时间！');
					return false;
				}
				result['beforeCancelTime'] = beforeCancelTime;
				
				result['satDataType'] = $(".checkbox-wp .check-item[name='satDataType']").prop('checked') ? 1 : 0;
				
				 $.ajax({
					   type: "post",
					   url: contextPath+ "/projectConfig/saveProjectConfig", 
					   data: result,
					   success: function(data){
						   if(data.code == 200){
							   $(".dialog-wp").remove();
							   layer.msg("申请/取消规则成功！");
						   }else{
							   layer.msg(data.msg)
						   }   
					   },
					   error: function(error){   
						   layer.msg('error')
					   }
			   });
			}
	};
	appSend.init();
})