
/**
 * 放行审批
 * 2018/5/25 by liufang
 *
 */
$(function(){
	var layer;
	var table;
	var ERROR_OK = 200;
	layui.use('layer', function(){
		   layer = layui.layer;  
	});
	var tpl = {
			"picTpl": '<div class="dialog-wp">\
				<div class="mask"></div>\
				<div class="dialog dialog-pic-con">\
					<div class="title-wp">\
						<span><%if(type==1){%>申请<%}else if(type==2){%> 放行<%}%>图片</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class=" dialog-pic-wp ">\
						<div class="pic-list">\
							<% if(listImg.length>0){%>\
								<% for(i=0;i<listImg.length;i++){%>\
									<% var img = listImg[i].split("@")[0]%>\
									<% var descript = listImg[i].split("@")[1]%>\
									<div class="pic-item clearfloat">\
										<div class="pic-left"><img class="img " src="<%=img%>"></div>\
										<div class="pic-right">\
											<div class="title">图片<%=i+1%>的描述</div>\
											<div class="descript"><%=descript%></div>\
										</div>\
									</div>\
								<%} %>\
							<%}else{%>\
								<div class="no-data">暂无数据</div>\
							<%} %>\
						</div>\
				   </div>		\
				</div>\
			</div>	',
			"releaseTpl": '<div class="dialog-wp">\
				<div class="mask"></div>\
				<div class="dialog dialog-release-con">\
					<div class="title-wp">\
						<span>订单动态</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class=" dialog-release-wp ">\
						<%if(!!operateDto && operateDto.length>0){%>\
							<div class="release-list">\
								<% for(var i=0;i<operateDto.length;i++){%>\
									<div class="release-item"><%=operateDto[i].auditTime%> &nbsp;&nbsp;<%=operateDto[i].auditUser%>&nbsp;&nbsp;<%=operateDto[i].auditResult%>&nbsp;&nbsp;<span class="red-text"><%=operateDto[i].remark%></span></div>\
								<%}%>\
							</div>\
						<%}else{%>\
							<div class="no-data">暂无数据</div>\
						<%}%>\
				   </div>		\
				</div>\
			</div>	',
			"goodTpl": '<div class="dialog-wp">\
				<div class="mask"></div>\
				<div class="dialog dialog-good-con">\
					<div class="title-wp">\
						<span>放行物品</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class=" dialog-good-wp ">\
						<%if(!!list && list.length>0){%>\
							<div class="good-list">\
								<% for(var i=0;i<list.length;i++){%>\
									<div class="good-item">\
										<div class="good-name">物品<%=i+1%>名称：<%=list[i].goodsName%></div>\
										<div class="good-num">物品<%=i+1%>数量：<%=list[i].amount%></div>\
									</div>\
								<%}%>\
							</div>\
						<%}else{%>\
							<div class="no-data">暂无数据</div>\
						<%}%>\
				   </div>		\
				</div>\
			</div>	',
			"choosePersonTpl":  '<div class="dialog-wp">\
				<div class="mask"></div>\
				<div class="dialog dialog-choose-person-con">\
					<div class="title-wp">\
						<span>选择放行人</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class="dialog-choose-person-wp ">\
						<div class="dialog-choose-person-header">\
							<h3 class="choose-person-title">放行人</h3>\
							<div class="person-radio-list clearfloat">\
							<% for(var i=0;i<data.length;i++){%>\
								<div class="person-radio-item">\
									<div class="radio-wp" >\
										<span class="radio-input">\
											<input type="radio" id="person-<%=i%>"  name="serviser" class="radio-item"  data-id="<%=data[i].id%>"/>\
											<span class="radio-inner"></span>\
										</span>		\
										<label class="radio-con" for="person-<%=i%>"><%=data[i].name%></label>\
									</div>\
								</div>\
							<%}%>	\
							</div>\
						</div>\
						<div class="choose-person-footer">\
							<button class="btn-red sure-choose-person">确认</button>\
						</div>\
				   </div>		\
				</div>\
			</div>	',
	}
	var releaseApprove = {
			init: function(){
				this.render();
				this.bindEvent();
				
			},
			render: function(){
				this.renderTable();
			},
			bindEvent: function(){
				var _this = this;
				//查询
				$("#conditionform input[type='text']").on("keydown",function(event){
					if(event.keyCode == 13){
						$("#conditionform .search-btn").click();
					}
				});
				layui.use('laydate', function(){
					  var laydate = layui.laydate;
					  
					  //执行一个laydate实例
					  laydate.render({
					    elem: '#billStartDate' //发起开始时间
				    	
					  });
					  laydate.render({
						    elem: '#billEndDate' //发起结束时间
					   
						  });
					  laydate.render({
						    elem: '#releaseStartDate' //放行开始时间
					    	
						  });
						  laydate.render({
							    elem: '#releaseEndDate' //放行结束时间
						   
							  })
					});
				//重置
				$(".set-btn").on("click",function(){
					$("#conditionform input,#conditionform select").val("");
					$("#conditionform .release-user-wp").data("id","")
				});
				//选择放行人
				$(".release-user-wp").on("click",function(){
					var $this = $(this)
					_this.showUserDialog(function(id){
						$this.data("id",id);
					});
				})
			},
			renderTable: function(){
				var _this = this;
				layui.use('table', function(){
					  table = layui.table;
					  
					  //方法级渲染
					  table.render({
					    elem: '#LAY_table_user',
					    method: 'post'
					    ,url: $("#contextPath").val()+ "/release/list"
					    ,skin: 'nob' //行边框风格
				    		  ,even: true //开启隔行背景
				    		  ,size: 'lg' //小尺寸的表格
					    ,cols: [[
						  {

							  title: '序号',
							  templet: '#indexTpl',
							  width: 80
						  } ,{
				    		   field:'releaseCode', 
				    		   title: '放行编号', 
				    		   minWidth: 200
				    	  },{
				    		  field:'createTime',
				    		  title: '发起时间',
				    		  minWidth: 200
			    		  },{
				    		  field:'applyUserInfo',
				    		  title: '发起人',
				    		  minWidth: 200
			    		  },{
			    			  field:'companyName', 
			    			  title: '校区', 
			    			  minWidth: 200
		  				  // },{
		  					//   field:'companyHeadUser',
		  					//   title: '校区负责人',
		  					// 	 minWidth: 200
		  					//
						  },{
							  field:'releaseDesc', 
							  title: '放行描述',
							  minWidth: 200
							
						  // },{
							//   field:'releaseTypeName',
							//   title: '申请类型',
							//   minWidth: 200
							  
							
						  },{
				    		  title: '放行物品',
				    		  minWidth: 200,
				    		  templet: function(data){
								  return "<span class='deep-blue-text check-apply-pic' data-id="+data.id+"  data-type='4'>查看物品</span>"
							  }  
			    		  },{
							  field:'repairTypeName', 
							  title: '申请图片',
							  templet: function(data){
								  return "<span class='deep-blue-text check-apply-pic' data-id="+data.id+" data-type='1'>查看</span>"
							  }
							  
							
						  },{
							  field:'repairTypeName', 
							  title: '放行图片',
							  minWidth: 200,
							  templet: function(data){
								  return "<span class='deep-blue-text check-release-pic'  data-id="+data.id+" data-type='2'>查看</span>"
							  }
							
						  },{
							  field:'isSendSettle', 
							  title: '放行动态',
							  minWidth: 200,
							  templet: function(data){
								  return "<span class='deep-blue-text check-release'  data-id="+data.id+" data-type='3'>查看</span>"
								  
							  }
							
						  },{  field:'releaseStatus', 
							  title: '审核状态',
							  minWidth: 200,
							  templet: function(data){
								  if(data.releaseStatus==1){
									  return '待放行'
								  }else if(data.releaseStatus==5){
									  return '已放行'
								  }else if(data.releaseStatus==10){
									  return '审核中'
								  }else if(data.releaseStatus==15){
									  return '已拒绝'
								  }
							  }
							
						  }
						  ]]
						    ,id: 'testReload'
						    ,page: true,
						    where: {
					    		releaseStatus: "1,5,15"
					    	},
						    request: {
						    	  pageName: 'pageNo' //页码的参数名称，默认：page
						    	  ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
					    	}, 
						    response: {
						    	  statusName: 'code' //数据状态的字段名称，默认：code
						    	  ,statusCode: 200 //成功的状态码，默认：0
						    	  ,countName: "count"//数据总数的字段名称，默认：count
						    	  ,dataName: "data" //数据列表的字段名称，默认：data
						    	}   ,
						  
						    done: function(res, curr, count){
						    	//查看申请图片
						    	$(".check-apply-pic").on("click",function(){
						    		var id = $(this).data("id");
						    		var type = $(this).data("type");
						    		_this.showPicDialog(type,id);
						    	});
						    	
						    	//查看放行图片
						    	$(".check-release-pic").on("click",function(){
						    		var id = $(this).data("id");
						    		var type = $(this).data("type");
						    		_this.showPicDialog(type,id);
						    	});
						    	//放行动态
						    	$(".check-release").on("click",function(){
						    		var id = $(this).data("id");
						    		var type = $(this).data("type");
						    		_this.showPicDialog(type,id);
						    	});
						      }
					  });
					  //查询
					  $('#conditionform  .search-btn').on('click', function(){
						  _this.againRenderTable();
					  });
					});
			},
			/*重新渲染table*/
			againRenderTable: function(){
				table.reload('testReload', {
					where: {
						billStartDate: $("#conditionform #billStartDate").val(),
						billEndDate: $("#conditionform #billEndDate").val(),
						releaseStartDate: $("#conditionform #releaseStartDate").val(),
						releaseEndDate: $("#conditionform #releaseEndDate").val(),
						releaseType: $("#conditionform .releaseType").val(),
						releaseCode:  $("#conditionform .releaseCode").val(),
						releaseUser: $("#conditionform .release-user-wp").data("id")
					} ,
					  page: {
					    curr: 1 //重新从第 1 页开始
					  }
				}); 
			},
			/**
			 * 显示图片弹窗
			 * @param type 1申请图片 2放行图片 3放行动态 4物品放行列表
			 * @param id 放行id
			 */
			showPicDialog: function(type,id){
				var _this = this;
				$.ajax({
					   type: "post",
					   url: $("#contextPath").val()+ "/release/getDetail", 
					   data: {
						   id: id
					   },
					   success: function(data){
						   if(data.code== ERROR_OK){
							   
							   if(type ==1){
								   var listImg = !!data.data.applypicUrl ?  data.data.applypicUrl.split(";") : [];
								   $("body").append(template.render(tpl.picTpl)({type: type,listImg: listImg}));
								   $(".pic-left .img").zoomify()
							   }else if(type ==2){
								   var listImg = !!data.data.releasepicUrl ?  data.data.releasepicUrl.split(";") : [];
								   $("body").append(template.render(tpl.picTpl)({type: type,listImg: listImg}));
								   $(".pic-left .img").zoomify()
							   }else if(type ==4){
								   $("body").append(template.render(tpl.goodTpl)({list: data.data.goodsDetails}));
							   }else{
								   $("body").append(template.render(tpl.releaseTpl)(data.data))
							   }
								$(".close-btn").on("click",function(){
									   $(this).closest(".dialog-wp").remove();
							    });
//								_this.settledBindEvent(callback);
						   }else{
							   layer.msg(data.msg);
						   }   
					   },
					   error: function(){  
					   }
			   });
				
			},
			/**
			 * 选择方形人
			 * @param callback
			 */
			showUserDialog: function(callback){
				var _this = this;
				$.ajax({
					   type: "get",
					   url: $("#contextPath").val()+ "/release/getReleaseUser", 
					   success: function(data){
						   if(data.code== ERROR_OK){
							   $("body").append(template.render(tpl.choosePersonTpl)(data));
							   $(".sure-choose-person").on("click",function(){
								   var length = $(".dialog-wp ").find(".radio-item:checked").length;
								   var pesonId = $(".dialog-wp ").find(".radio-item:checked").data("id");
								   var $dom =  $(this).closest(".dialog-wp");
								   if(length==0){
									   layer.msg("请选择放行人")
								   }else{
									   callback(pesonId);
									   $dom.remove();
								   } 
							   });
								$(".close-btn").on("click",function(){
									   $(this).closest(".dialog-wp").remove();
							    });
						   }else{
							   layer.msg(data.msg);
						   }   
					   },
					   error: function(){  
					   }
			   });
			}
	};
	releaseApprove.init();
})