$(function () {
	var contextPath = $("#contextPath").val();
	var layer;
	var flag = false;
	layui.use(['layer'], function () {
		 layer = layui.layer;
	});
	function padLeftZero (str) {
		return ('00' + str).substr(str.length);
		};
	//<span class="read-card">读取</span>
	//<span class="search-btn"></span>
	var tpl = {
		"printTpl": '<div class="dialog-wp">\
			<div class="mask"></div>\
			<div class="dialog dialog-print-con">\
				<div class="title-wp">\
					<span>打印</span>\
					<span class="close-btn"></button>\
				</div>\
				<div class=" dialog-print-wp "  >\
					<div class="print-item">\
						<div class="p-i-tip">访客姓名：</div>\
						<div class="p-i-con visitor-name"><%=visitorName%></div>\
					</div>\
					<div class="print-item">\
						<div class="p-i-tip">手机号码：</div>\
						<div class="p-i-con visitor-phone"><%=visitorTelephone%></div>\
					</div>\
					<div class="print-item">\
						<div class="p-i-tip">到访公司：</div>\
						<div class="p-i-con visitor-company"><%=visitorCompany%></div>\
					</div>\
					<div class="print-item">\
						<div class="p-i-tip">预计离开时间：</div>\
						<div class="p-i-con">\
							<input class="p-i-input visitor-time" id="testTime1" value="<%=leaveTime%>" readonly placeholder="请选择预计离开时间"/>\
						</div>\
					</div>\
					<div class="print-item">\
						<div class="p-i-tip">身份证号：</div>\
						<div class="p-i-con id-number">\
							<input class="p-i-input visitor-number" value="<%=idenNumber%>" type="text"  placeholder="请输入身份证号码"/>\
							\
						</div>\
					</div>\
					<div class="print-item">\
						<div class="p-i-tip">民族：</div>\
						<div class="p-i-con">\
							<input class="p-i-input visitor-nation"  value="<%=race%>" type="text"  placeholder="请输入民族"/>\
						</div>\
					</div>\
					<div class="print-item">\
						<div class="p-i-tip">户籍地址：</div>\
						<div class="p-i-con">\
							<input class="p-i-input visitor-address" value="<%=censusAddr%>" type="text"  placeholder="请输入户籍地址"/>\
						</div>\
					</div>\
					<div class="print-item">\
						<div class="p-i-tip">签发机关：</div>\
						<div class="p-i-con">\
							<input class="p-i-input visitor-office" type="text" value="<%=police%>" placeholder="请输入签发机关"/>\
						</div>\
					</div>\
			   </div>		\
			  <div class="dialog-btns">\
					<button class="btn-white cancel-btn">取消</button>\
					<button class="btn-red  dialog-print-btn">打印</button>\
				</div>\
			</div>\
		</div>	',
		"scanPrintTpl": '<div class="dialog-wp">\
			<div class="mask"></div>\
			<div class="dialog dialog-print-con" >\
				<div class="title-wp">\
					<span>扫码打印</span>\
					<span class="close-btn"></button>\
				</div>\
				<%if(type==1){%>\
					<div class=" dialog-print-wp ">\
						<img class="scan-img" src="../../images/scan_img.png">\
						<div class="scan-tip">请扫描访客码...</div>\
					</div>\
				<%}else if(type==2){%>\
					<div class=" dialog-print-wp ">\
						<img class="scan-img" src="../../images/scan_fail.png">\
						<div class="scan-tip">无效二维码或已过期</div>\
					</div>\
				<%}else{%>\
					<div class=" dialog-print-wp " >\
						<div class="print-item">\
							<div class="p-i-tip">访客姓名：</div>\
							<div class="p-i-con visitor-name"><%=visitorName%></div>\
						</div>\
						<div class="print-item">\
							<div class="p-i-tip">手机号码：</div>\
							<div class="p-i-con visitor-phone"><%=visitorTelephone%></div>\
						</div>\
						<div class="print-item">\
							<div class="p-i-tip">到访公司：</div>\
							<div class="p-i-con visitor-company"><%=visitorCompany%></div>\
						</div>\
						<div class="print-item">\
							<div class="p-i-tip">预计离开时间：</div>\
							<div class="p-i-con">\
								<input class="p-i-input visitor-time" id="testTime" readonly placeholder="请选择预计离开时间"/>\
							</div>\
						</div>\
						<div class="print-item">\
							<div class="p-i-tip">身份证号：</div>\
							<div class="p-i-con id-number">\
								<input class="p-i-input visitor-number" type="text"  placeholder="请输入身份证号码"/>\
								<span class="read-card">读取</span>\
							</div>\
						</div>\
						<div class="print-item">\
							<div class="p-i-tip">民族：</div>\
							<div class="p-i-con">\
								<input class="p-i-input visitor-nation" type="text"  placeholder="请输入民族"/>\
							</div>\
						</div>\
						<div class="print-item">\
							<div class="p-i-tip">户籍地址：</div>\
							<div class="p-i-con">\
								<input class="p-i-input visitor-address" type="text"  placeholder="请输入户籍地址"/>\
							</div>\
						</div>\
						<div class="print-item">\
							<div class="p-i-tip">签发机关：</div>\
							<div class="p-i-con">\
								<input class="p-i-input visitor-office" type="text"  placeholder="请输入签发机关"/>\
							</div>\
						</div>\
				   </div>\
				<%}%>\
				<%if(type==3){%>\
				  <div class="dialog-btns">\
						<button class="btn-white cancel-btn">取消</button>\
						<button class="btn-red dialog-print-btn">打印</button>\
					</div>\
				<%}%>\
			</div>\
		</div>	',
		"printModelTpl": '<div class=" dialog-print-wp " id="printCon" style="display: none">\
							<div class="print-item">\
								<div class="p-i-tip">来访时间：</div>\
								<div class="p-i-con"><%=visitTime%></div>\
							</div>\
							<div class="print-item">\
								<div class="p-i-tip">预计离开时间：</div>\
								<div class="p-i-con"><%=time%></div>\
							</div>\
							<div class="print-item">\
								<div class="p-i-tip">访客姓名：</div>\
								<div class="p-i-con"><%=name%></div>\
							</div>\
							<div class="print-item">\
								<div class="p-i-tip">手机号码：</div>\
								<div class="p-i-con"><%==phone%></div>\
							</div>\
							<div class="print-item">\
								<div class="p-i-tip">身份证号码：</div>\
								<div class="p-i-con"><%=number%></div>\
							</div>\
							<div class="print-item">\
								<div class="p-i-tip">到访公司：</div>\
								<div class="p-i-con"><%=company%></div>\
							</div>\
						</div>',
			"visitorTpl": 	'<div class="dialog-wp">\
				<div class="mask"></div>\
				<div class="dialog dialog-print-con">\
					<div class="title-wp">\
						<span>访客登记</span>\
						<span class="close-btn"></button>\
					</div>\
					<div class=" dialog-print-wp "  >\
						<div class="base-title">基础信息</div>\
						<div class="print-item">\
							<div class="p-i-tip"><span style="color: red;padding-right: 3px">*</span>访客姓名：</div>\
							<div class="p-i-con visitor-name">\
								<input class="p-i-input" type="text"  placeholder="请输入访客姓名"/>\
							</div>\
						</div>\
						<div class="print-item">\
							<div class="p-i-tip"><span style="color: red;padding-right: 3px">*</span>手机号码：</div>\
							<div class="p-i-con visitor-phone">\
								<input class="p-i-input" type="text"  placeholder="请输入手机号码"/>\
							</div>\
						</div>\
						<div class="print-item">\
							<div class="p-i-tip">到访公司：</div>\
							<div class="p-i-con visitor-company">\
								\
								<input class="default-select companyInput-id " name="companyInputId" id="companyInputId" autocomplete="off" placeholder="请选择公司" />\
								<span class="select_wrap _hide"></span>\
							</div>\
						</div>\
						<div class="print-item">\
							<div class="p-i-tip">预计离开时间：</div>\
							<div class="p-i-con">\
								<input class="p-i-input visitor-time" id="testTime" readonly placeholder="请选择预计离开时间"/>\
							</div>\
						</div>\
						<div class="base-title">身份证信息</div>\
						<div class="print-item">\
							<div class="p-i-tip">身份证号：</div>\
							<div class="p-i-con id-number">\
								<input class="p-i-input visitor-number" type="text"  placeholder="请输入身份证号码"/>\
								\
							</div>\
						</div>\
						<div class="print-item">\
							<div class="p-i-tip">民族：</div>\
							<div class="p-i-con">\
								<input class="p-i-input visitor-nation" type="text"  placeholder="请输入民族"/>\
							</div>\
						</div>\
						<div class="print-item">\
							<div class="p-i-tip">户籍地址：</div>\
							<div class="p-i-con">\
								<input class="p-i-input visitor-address" type="text"  placeholder="请输入户籍地址"/>\
							</div>\
						</div>\
						<div class="print-item">\
							<div class="p-i-tip">签发机关：</div>\
							<div class="p-i-con">\
								<input class="p-i-input visitor-office" type="text"  placeholder="请输入签发机关"/>\
							</div>\
						</div>\
				   </div>		\
				  <div class="dialog-btns">\
						<button class="btn-white cancel-btn">取消</button>\
						<button class="btn-red  dialog-next-btn">下一步</button>\
					</div>\
				</div>\
			</div>	',	
			"visitorModelTpl": '<div class="dialog-wp">\
						<div class="mask"></div>\
						<div class="dialog dialog-print-con">\
							<div class="title-wp">\
								<span>打印访客单</span>\
								<span class="close-btn"></button>\
							</div>\
						<div class=" dialog-print-wp "  >\
						<div class="print-item">\
							<div class="p-i-tip">来访时间：</div>\
							<div class="p-i-con"><%=visitTime%></div>\
						</div>\
						<div class="print-item">\
							<div class="p-i-tip">预计离开时间：</div>\
							<div class="p-i-con"><%=time%></div>\
						</div>\
						<div class="print-item">\
							<div class="p-i-tip">访客姓名：</div>\
							<div class="p-i-con"><%=name%></div>\
						</div>\
						<div class="print-item">\
							<div class="p-i-tip">手机号码：</div>\
							<div class="p-i-con"><%==phone%></div>\
						</div>\
						<div class="print-item">\
							<div class="p-i-tip">身份证号：</div>\
							<div class="p-i-con"><%=number%></div>\
						</div>\
						<div class="print-item">\
							<div class="p-i-tip">到访公司：</div>\
							<div class="p-i-con"><%=company%></div>\
						</div>\
					</div>\
				  <div class="dialog-btns">\
						<button class="btn-white cancel-btn">取消</button>\
						<button class="btn-red  dialog-print-btn">打印</button>\
					</div>\
				</div>\
			</div>	'
	}
	var visitorOrder = {
			init: function(){
				this.render();
				this.bindEvent();
			},
			render: function(){
				var _this = this;
				layui.use('laydate', function(){
					  var laydate = layui.laydate;
					  
					  //执行一个laydate实例
					  laydate.render({
					    elem: '#startTime' //指定元素
				    	
					  });
					  laydate.render({
						    elem: '#endTime' //指定元素
					   
						  });
					});
				
				_this.renderTable();
			},
			bindEvent: function(){
				var _this = this;
//				$("#btn-search").click(function () {
//					$("#conditionform").submit();
//				});
				if(sessionStorage.getItem("passType") == 3){
					$(".visitor-list").show();
				}else{
					$(".visitor-list").hide();
				}
				// 关闭公共弹层
				$('body').on('click', '.pop_close', function(event) {
					event.preventDefault();
					/* Act on the event */
					$('.mask').remove()
				});
				//查询
				$("#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: '#c_startTime' //提交开始时间
				    	
					  });
					  laydate.render({
						    elem: '#c_endTime' //提交结束时间
					   
						  })
					});
				//重置
				$(".set-btn").on("click",function(){
					$("#conditionform select,#conditionform input").val("");
					$("#conditionform select").val("");
					$("#conditionform .choose-person-btn").data("id","");
					_this.againRenderTable({type:1});
				});
				$(".visitorId").on("change",function(){
					///////////
					_this.againRenderTable();
					flag= true
					console.log("刚刚开始的flag",flag)
					
				})
				//scanPrintTpl
				//扫码打印
//				$(".scan-print").on("click",function(){
//					var html = template.render(tpl.scanPrintTpl)({type: 2})
//					$("body").append(html)
//					$(html).find(".print-btn").on("click",function(){
//						var myWindow = window.open('', '标题');
//						//获取id为app内的html
//				        var bodyHtml = window.document.getElementById('printCon').innerHTML;
//				        // 获取head标签内的html
//				        var headHtml = document.head.innerHTML;
//				        // 头中的screen换成打印样式print
//				        headHtml = headHtml.replace('screen', 'screen, print');
//				        //重新写入文档流
//				        myWindow.document.write('<html>');
//				        myWindow.document.write(headHtml);
//				        myWindow.document.write('<body >');
//				        myWindow.document.write(bodyHtml);
//				        myWindow.document.write('<script>setTimeout(function() {window.print(); window.close();}, 500)</');
//				        myWindow.document.write('script>');
//				        myWindow.document.write('</body></html>');
//					})
//					$(".close-btn").on("click",function(){
//		        		$(".dialog-wp").remove();
//		        	})
//				})
				//访客登记
				$(".visitor-register").on("click",function(){
					$("body").append(template.render(tpl.visitorTpl))
//					console.log(_this.formatDate(new Date(),'yyyy-MM-dd hh:mm:ss'))
					layui.use('laydate', function () {
	    				var laydate = layui.laydate;
	    				laydate.render({
		        		    elem: '#testTime'
		        		    ,type: 'datetime'
		        		    ,min:  _this.formatDate(new Date(),'yyyy-MM-dd hh:mm:ss')
		        		  });
	        		})	
	        		//visitorModelTpl
	        		$(".dialog-next-btn").on("click",function(){
	        			var name = $(".visitor-name input").val();
	        			if(!name){
	        				layer.msg('请输入访客姓名');
	        				return false
	        			}
	        			var phone = $(".visitor-phone input").val();
	        			if(!/^[1][0-9]{10}$/.test(phone)){
	        				layer.msg('请输入正确的手机号码');
	        				return false
	        			}
	        			var data = {
	        					name: name,
		        				phone: phone,
		        				visitTime:  _this.formatDate(new Date(),'yyyy-MM-dd'),
		        				company:$(".visitor-company input").val(),
		        				time:$(".visitor-time").val(),
		        				number:$(".visitor-number").val(),
		        				nation:$(".visitor-nation").val(),
		        				address:$(".visitor-address").val(),
		        				office:$(".visitor-office").val(),	
		        				companyId:$(".visitor-company input").attr("com__id")
	        			}
	        			// PC访客登记
	        			_this.visitorRegister(data,function(){
	        				_this.pirntEvent(data,1)
	        			})
	        			
	        		})
					$(".close-btn,.dialog-wp .cancel-btn").on("click",function(){
		        		$(".dialog-wp").remove();
		        	})
				})
				// 模糊搜索
				this.search_fuzzy();
				//模糊查找弹框的企业信息
				this.search_fuzzyInutDialog();
				
			},
			search_fuzzyInutDialog: function (){
				var default_option = {
					org_attrName:'org__id',
					com_attrName:'com__id',
					dep_attrName:'dep__id',
					org_spanName:'org__span',
					com_spanName:'com__span',
					dep_spanName:'dep__span',
					org_path:'/admin/templ/getOrgListByName',
					com_path:'/admin/templ/getVirList',
					dep_path:'/admin/templ/getDept',
					contextPath:$("#contextPath").val()
				}
				var option_1 = $.extend(true,{},default_option,{
					org_target:'.default-select#orgId',
					com_target:'.default-select#companyInputId',
					dep_target:'.default-select#departmentId',
				});
				var option_2 = $.extend(true,{},default_option,{
					org_target:'.item-select.organ-id',
				});

				function eve(option_,otherOption){
					var obj = $.extend(true, {}, option_, otherOption);
					var type = obj.type
					var _path, _target, _attrName, _spanName
					if(type == 1){
						_path = obj.org_path
						_target = obj.org_target
						_attrName = obj.org_attrName
						_spanName = obj.org_spanName
					}else if(type == 2){
						_path = obj.com_path
						_target = obj.com_target
						_attrName = obj.com_attrName
						_spanName = obj.com_spanName
					}else if(type == 3){
						_path = obj.dep_path
						_target = obj.dep_target
						_attrName = obj.dep_attrName
						_spanName = obj.dep_spanName
					}
					$('body').on("input",_target,function(event){
						var _this = $(this)
						var obj_input = $.extend(true,{}, obj,{"_this":_this});
						cjqt.search_fuzzy.event_input(_this,_attrName,_spanName,contextPath,_path,obj_input)
					});
					$('body').on("focus",_target,function(event){
						var _this = $(this)
						var obj_focus = $.extend(true,{},obj,{"_this":_this});
						cjqt.search_fuzzy.event_focus(_this,_attrName,_spanName,contextPath,_path,obj_focus)
					});
					$('body').on('blur',_target,function(event) {
						var _this = $(this)
						var obj_blur = $.extend(true,{},obj,{"_this":_this});
						cjqt.search_fuzzy.event_blur(_this,_attrName,obj_blur)
					});
					$('body').on('click',_target+'~.select_wrap>.'+_spanName, function(event) {
						event.preventDefault();
						event.stopPropagation();
						/* Act on the event */
						var _this = $(this)
						var obj_click = $.extend(true,{},obj,{"_this":_this});
						cjqt.search_fuzzy.event_click(_this,_attrName,_target,obj_click)
					});

				}
				// 搜索联动
				eve(option_1,{type:1})
				eve(option_1,{type:2})
				eve(option_1,{type:3})
				// 自动充值
				eve(option_2,{type:1})
			},
			//模糊搜索事件
			search_fuzzy: function (){
				var default_option = {
					org_attrName:'org__id',
					com_attrName:'com__id',
					dep_attrName:'dep__id',
					org_spanName:'org__span',
					com_spanName:'com__span',
					dep_spanName:'dep__span',
					org_path:'/admin/templ/getOrgListByName',
					com_path:'/admin/templ/getVirList',
					dep_path:'/admin/templ/getDept',
					contextPath:$("#contextPath").val()
				}
				var option_1 = $.extend(true,{},default_option,{
					org_target:'.default-select#orgId',
					com_target:'.default-select#companyId',
					dep_target:'.default-select#departmentId',
				});
				var option_2 = $.extend(true,{},default_option,{
					org_target:'.item-select.organ-id',
				});

				function eve(option_,otherOption){
					var obj = $.extend(true, {}, option_, otherOption);
					var type = obj.type
					var _path, _target, _attrName, _spanName
					if(type == 1){
						_path = obj.org_path
						_target = obj.org_target
						_attrName = obj.org_attrName
						_spanName = obj.org_spanName
					}else if(type == 2){
						_path = obj.com_path
						_target = obj.com_target
						_attrName = obj.com_attrName
						_spanName = obj.com_spanName
					}else if(type == 3){
						_path = obj.dep_path
						_target = obj.dep_target
						_attrName = obj.dep_attrName
						_spanName = obj.dep_spanName
					}
					$('body').on("input",_target,function(event){
						var _this = $(this)
						var obj_input = $.extend(true,{}, obj,{"_this":_this});
						cjqt.search_fuzzy.event_input(_this,_attrName,_spanName,contextPath,_path,obj_input)
					});
					$('body').on("focus",_target,function(event){
						var _this = $(this)
						var obj_focus = $.extend(true,{},obj,{"_this":_this});
						cjqt.search_fuzzy.event_focus(_this,_attrName,_spanName,contextPath,_path,obj_focus)
					});
					$('body').on('blur',_target,function(event) {
						var _this = $(this)
						var obj_blur = $.extend(true,{},obj,{"_this":_this});
						cjqt.search_fuzzy.event_blur(_this,_attrName,obj_blur)
					});
					$('body').on('click',_target+'~.select_wrap>.'+_spanName, function(event) {
						event.preventDefault();
						event.stopPropagation();
						/* Act on the event */
						var _this = $(this)
						var obj_click = $.extend(true,{},obj,{"_this":_this});
						cjqt.search_fuzzy.event_click(_this,_attrName,_target,obj_click)
					});

				}
				// 搜索联动
				eve(option_1,{type:1})
				eve(option_1,{type:2})
				eve(option_1,{type:3})
				// 自动充值
				eve(option_2,{type:1})
			},
			renderTable: function(){
				var _this = this;
				layui.use('table', function(){
					  table = layui.table;
					  
					  //方法级渲染
					  table.render({
					    elem: '#LAY_table_user',
					    method: 'post'
					    	 ,skin: 'nob' //行边框风格
					    		  ,even: true //开启隔行背景
					    		  ,size: 'lg' //小尺寸的表格
					    ,url: $("#contextPath").val()+ "/visitor/order/getVisitorOrderList"
					    ,cols: [[
						  {

							  title: '序号',
							  templet: '#indexTpl',
							  width: 80
						  } ,{
				    		   field:'visitorName', 
				    		   title: '访客姓名', 
				    		   minWidth:200
				    	  },{
				    		  field:'visitorCompanyName',
				    		  title: '来访公司',
				    		  minWidth: 120	  
			    		  },{
			    			  field:'visitorPhone', 
			    			  title: '访客电话', 
			    			  minWidth:200
		    			  },{
			    			  field:'visitorPassword', 
			    			  title: '访客码', 
			    			  minWidth:200
		    			  },{
		    				  field:'visitorStartTime', 
		    				  title: '访问时间', 
		    				  minWidth:200
		  				  },{
		  					  field:'imageFaceUrl', 
		  					  title: '照片',
		  					  minWidth: 100	,
		  					 templet: function(data){
		  						  if(data.imageFaceUrl){
		  							  return '<img style="width: 100px;height: 100px;" src="'+data.imageFaceUrl+'" />'
		  						  }else{
		  							  return '暂无图片'
		  						  }
		  					  }
		  				  },{
		  					  field:'userName', 
		  					  title: '受访人',
		  					  minWidth:200
		  					 
						  },{
							  field:'visitorReason', 
							  title: '到访目的',
								  minWidth:200
							
						  },{
							  field:'visitorToFloor', 
							  title: '到访楼层',
							  minWidth:200,
							 
							
						  },{
		  					  field:'virName', 
		  					  title: '被访人公司', 
		  					  width: 200,
		  					  
						  },{
							  field:'visitorOrderStatus', 
							  title: '状态',
							  width: 130,
							  templet: function(data){
		  						  if(data.visitorOrderStatus==1){
		  							  return '已授权'
		  						  }else if(data.visitorOrderStatus==2){
		  							  return '未通过'
		  						  }else if(data.visitorOrderStatus ==3){
		  							  return '已作废'
		  						  }else if(data.visitorOrderStatus==0){
		  							  return '未授权'
		  						  }
		  					  }
							
						  },{
//		  					  field:'id', 
							  fixed: 'right',
		  					  title: '操作', 
		  					  width: 240,
		  					  templet: function(data){
		  						  var str = ''
		  						str +=  "<span data-id="+data.visitorId+" data-status= "+data.visitorOrderStatus+" class='check-blue-btn check-detail'>查看详情</span>";
		  						if((sessionStorage.getItem("passType") == 3) && (data.visitorOrderStatus==1)){
		  							str +=  "<span data-id="+data.visitorId+" data-status= "+data.visitorOrderStatus+" class='check-blue-btn print-btn1'>打印</span>"
		  						}
		  						return str
		  					  }
						  }
						  ]]
						    ,id: 'testReload'
						    ,page: true,
						    where: {
								orgId: $("#conditionform  #orgId").attr('org__id'),
							},
						    response: {
						    	  statusName: 'code' //数据状态的字段名称，默认：code
						    	  ,statusCode: 200 //成功的状态码，默认：0
						    	  ,countName: "count"//数据总数的字段名称，默认：count
						    	  ,dataName: "data" //数据列表的字段名称，默认：data
						    	}   ,
						    done: function(res, curr, count){
						        //如果是异步请求数据方式，res即为你接口返回的信息。
						    	//查看详情
						        $(".check-detail").on("click",function(){
						        	var id = $(this).data("id")
						        	var status = $(this).data("status")
						        	//查看订单详情
						        	_this.ajaxDetail(id);		
						        });
						        //打印
						        $(".print-btn1").on("click",function(){
						        	var id = $(this).data("id")
						        	
						        	_this.ajaxDetail(id,function(data){
						        		$(".dialog-wp").remove();
						        		var html = template.render(tpl.printTpl)(data)
						        		$("body").append(html);
						        		layui.use('laydate', function () {
						    				var laydate = layui.laydate;
						    				laydate.render({
							        		    elem: '#testTime1'
							        		    ,type: 'datetime'
							        		    	,min:   _this.formatDate(new Date(),'yyyy-MM-dd hh:mm:ss')
							        		  });
						        		})		
						        		
						        		$(".dialog-print-btn").on("click",function(){
						        			_this.pirntEvent({
						        				visitorId: data.visitorId,
						        				name: $(".visitor-name").text(),
						        				phone: $(".visitor-phone").text(),
						        				company:$(".visitor-company").text(),
						        				time:$(".visitor-time").val(),
						        				number:$(".visitor-number").val(),
						        				nation:$(".visitor-nation").val(),
						        				address:$(".visitor-address").val(),
						        				office:$(".visitor-office").val(),
						        				visitTime: data.visitTime
						        			})
						        		})
						        		$(".close-btn,.dialog-wp .cancel-btn").on("click",function(){
							        		$(".dialog-wp").remove();
							        	})
						        	});	
						        	
						        });
						        //弹出框
						        console.log("结束的flag",flag)
						        if(flag && res.data.length==1){
						        	$(".print-btn1").click();
						        	flag = false;
						        	
						        	
						        }
						      }
					  });
					  //查询
					  $('#conditionform  .search-btn').on('click', function(){
						  _this.againRenderTable();
					  });
					});
				
			},
			pirntEvent: function(data,type){
				var _this = this
				if(type==1){
					//visitorModelTpl
					$(".dialog-wp").remove();
					$("body").append(template.render(tpl.visitorModelTpl)(data))
        			$(".close-btn,.dialog-wp .cancel-btn").on("click",function(){
		        		$(".dialog-wp").remove();
		        		 _this.againRenderTable();
		        	})
		        	$(".dialog-print-btn").on("click",function(){
		        		$("#printCon").remove();
						$("body").append(template.render(tpl.printModelTpl)(data))
						//_this.scanUpdateVisitorInfo(data,function(){
							$(".dialog-wp").remove();
							_this.openPrint();
						//})
		        		
		        	})
				}else{
					$("#printCon").remove();
					$("body").append(template.render(tpl.printModelTpl)(data))
						_this.scanUpdateVisitorInfo(data,function(){
							$(".dialog-wp").remove();
							_this.openPrint();
					})
				}	
			},
			scanUpdateVisitorInfo: function(data,callback){
				if(data.number && !checkIDCard(data.number)){
					layer.msg('身份证号有误！');
					return false;
				}
				$.ajax({
					type: "post",
					url: $("#contextPath").val() + "/lingling/scanUpdateVisitorInfo",
					data: {
						"visitorId": data.visitorId,
						"leaveTime": data.time,//预计离开时间
						"idenNumber":data.number,//身份证编码
						"censusAddr":data.address,//户籍地址
						"race":data.nation,//民族
						"police":data.office//签发机关
					},
					success: function(data) {
						if(data && data.code==200){
							callback && callback();
						}
					},
					error: function(error) {
						layer.msg('error')
					}
				});
			},
			
		    formatDate: function(date, fmt) {
		    	  if (/(y+)/.test(fmt)) {
		    	      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
		    	  }
		    	  let o = {
		    	      'M+': date.getMonth() + 1,
		    	      'd+': date.getDate(),
		    	       'h+': date.getHours(),
		    	       'm+': date.getMinutes(),
		    	       's+': date.getSeconds()
		    	  }
		    	   for (let k in o) {
		    	       if (new RegExp(`(${k})`).test(fmt)) {
		    	           let str = o[k] + ''
		    	           fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
		    	         }
		    	     }
		    	  return fmt
	    	},
			visitorRegister: function(data,callback){
				var _this = this;
				$.ajax({
					type: "post",
					url: $("#contextPath").val() + "/lingling/saveVisitorUserInfo",
					data: {
						"arrarId":1,
						"startDateTime":_this.formatDate(new Date(),'yyyy-MM-dd')+" 00:00",
						"endDateTime":_this.formatDate(new Date(),'yyyy-MM-dd')+" 23:59",
						"leaveTime": data.time,//预计离开时间
						"idenNumber":data.number,//身份证编码
						"censusAddr":data.address,//户籍地址
						"race":data.nation,//民族
						"police":data.office,//签发机关
						"visitorType1": 5,
						"type": 5,
						"visitorPhone1": data.phone,//手机号码
						"virtualArchId": data.companyId,//企业id
						"companyName": data.company,//企业名称
						"visitorName1":data.name,//访客姓名
					},
					success: function(data) {
						if(data && data.code==200){
							callback && callback();
						}
					},
					error: function(error) {
						layer.msg('error')
					}
				});
			},
			
			openPrint: function(){
				var myWindow = window.open('', '标题');
				//获取id为app内的html
		        var bodyHtml = window.document.getElementById('printCon').innerHTML;
		        // 获取head标签内的html
		        var headHtml = document.head.innerHTML;
		        // 头中的screen换成打印样式print
		        headHtml = headHtml.replace('screen', 'screen, print');
		        //重新写入文档流
		        myWindow.document.write('<html>');
		        myWindow.document.write(headHtml);
		        myWindow.document.write('<body >');
		        myWindow.document.write(bodyHtml);
		        myWindow.document.write('<script>setTimeout(function() {window.print(); window.close();}, 500)</');
		        myWindow.document.write('script>');
		        myWindow.document.write('</body></html>');
			},
			/*重新渲染table*/
			againRenderTable: function(option){
				var obj = $.extend(true, {}, option);
				var type =  obj.type || ''
				var $orgId = $("#conditionform  .single-orgid");
				var orgId = $("#conditionform  #orgId").attr('org__id')
				if(type == 1){
					table.reload('testReload', {
						  where: { //设定异步数据接口的额外参数，任意设
							  orgId:  $orgId.length?orgId:'',
							   name: $(".name").val(),
							   beginTime: $("#startTime").val(),
					  			endTime: $("#endTime").val(),
					  			mobile: $(".mobile").val(),
					  			respondents: $(".respondents").val(),
					  			//respondentsVirt: $(".respondentsVirt").val(),
					  			objective: $(".objective").val(),
					  			respondentsVirt:$("#conditionform #companyId").attr("com__id"),
					  			visitorId: $(".visitorId").val()
						  }
						  ,page: {
						    curr: 1 //重新从第 1 页开始
						  }
					});
				}else{
					table.reload('testReload', {
						  where: { //设定异步数据接口的额外参数，任意设
							  orgId: orgId,
							  name: $(".name").val(),
							  beginTime: $("#startTime").val(),
					  			endTime: $("#endTime").val(),
					  			mobile: $(".mobile").val(),
					  			respondents: $(".respondents").val(),
					  			//respondentsVirt: $(".respondentsVirt").val(),
					  			respondentsVirt:$("#conditionform #companyId").attr("com__id"),
					  			objective: $(".objective").val(),
					  			visitorId: $(".visitorId").val()
						  }
						  ,page: {
						    curr: 1 //重新从第 1 页开始
						  }
					});
				}
				 
			},
			ajaxDetail: function(visitorId,callback){
				$.ajax({
					type: "post",
					url: $("#contextPath").val() + "/lingling/visitorInfo",
					data: {
						"visitorId": visitorId
					},
					success: function(data) {
						if(data && data.code==200){
							var html = detailTmp(data.data)
							if(!callback) $("body").append(html)
							callback && callback(data.data);
						}
					},
					error: function(error) {
						layer.msg('error')
					}
				});
			}
	}
	visitorOrder.init();		
});
function checkIDCard(idcode){
    // 加权因子
    var weight_factor = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
    // 校验码
    var check_code = ['1', '0', 'X' , '9', '8', '7', '6', '5', '4', '3', '2'];

    var code = idcode + "";
    var last = idcode[17];//最后一位

    var seventeen = code.substring(0,17);

    // ISO 7064:1983.MOD 11-2
    // 判断最后一位校验码是否正确
    var arr = seventeen.split("");
    var len = arr.length;
    var num = 0;
    for(var i = 0; i < len; i++){
        num = num + arr[i] * weight_factor[i];
    }
    
    // 获取余数
    var resisue = num%11;
    var last_no = check_code[resisue];

    // 格式的正则
    // 正则思路
    /*
    第一位不可能是0
    第二位到第六位可以是0-9
    第七位到第十位是年份，所以七八位为19或者20
    十一位和十二位是月份，这两位是01-12之间的数值
    十三位和十四位是日期，是从01-31之间的数值
    十五，十六，十七都是数字0-9
    十八位可能是数字0-9，也可能是X
    */
    var idcard_patter = /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/;

    // 判断格式是否正确
    var format = idcard_patter.test(idcode);

    // 返回验证结果，校验码和格式同时正确才算是合法的身份证号码
    return last === last_no && format ? true : false;
}
	function detailTmp(res) {
		var html = ''
		html += '<div class="mask">'
		html +=	'<div class="pop_wrap">'
		html +=	'<div class="pop_title">访客详情<span class="pop_close"></span></div>'
		html +=	'<div class="pop_content">'
		
		if(res.nodeStr){
			var nodeArr = res.nodeStr.split(";");
			html +=	'<ul class="layui-timeline" style="margin-left: 30px; ">'
			for(var i=0;i<nodeArr.length;i++){
				html +=	' <li class="layui-timeline-item">'
				html +=	'<i class="layui-icon layui-timeline-axis"></i>'
				html +=	'<div class="layui-timeline-content layui-text">'
				html +=	'<h3 class="layui-timeline-title">'+nodeArr[i]+'</h3>'
				html +=	'</div>'
				html +=	'</li>'
			}
			html +=	'</ul>'
		}	
		// 基础信息
		html +=	'<div class="part_ clearfloat">'
		html +=	'<div class="part_title float-left part_title_depart">基础信息</div>'
		html +=	'<div class="part_content float-left"></div>'
		html +=	'</div>'
		if(res.visitorName){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">访客姓名:</div>'
			html +=	'<div class="part_content float-left">'+res.visitorName+'</div>'
			html +=	'</div>'
		}
		if(res.visitorCompany){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">来访人公司:</div>'
			html +=	'<div class="part_content float-left">'+res.visitorCompany+'</div>'
			html +=	'</div>'
		}
		if(res.visitorTelephone){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">手机号码:</div>'
			html +=	'<div class="part_content float-left">'+res.visitorTelephone+'</div>'
			html +=	'</div>'
		}
		if(res.visitCode){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">访客码:</div>'
			html +=	'<div class="part_content float-left">'+res.visitCode+'</div>'
			html +=	'</div>'
		}
		if(res.visitTime){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">来访时间:</div>'
			html +=	'<div class="part_content float-left">'+res.visitTime+'</div>'
			html +=	'</div>'
		}

		if(res.interviewee){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">受访人:</div>'
			html +=	'<div class="part_content float-left">'+res.interviewee+'</div>'
			html +=	'</div>'
		}
		if(res.visitReason){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">到访目的:</div>'
			html +=	'<div class="part_content float-left">'+res.visitReason+'</div>'
			html +=	'</div>'
		}
		if(res.visitFloor){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">到访楼层:</div>'
			html +=	'<div class="part_content float-left">'+res.visitFloor+'</div>'
			html +=	'</div>'
		}
		if(res.visitToCompany){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">到访公司:</div>'
			html +=	'<div class="part_content float-left">'+res.visitToCompany+'</div>'
			html +=	'</div>'
		}
		if(res.visitorImage){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">访客照片:</div>'
			html +=	'<div class="part_content float-left"><img class="touxiang" src="'+res.visitorImage+'" alt="" /></div>'
			html +=	'</div>'
		}
		if(res.leaveTime){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">预计离开时间:</div>'
			html +=	'<div class="part_content float-left">'+res.leaveTime+'</div>'
			html +=	'</div>'
		}
		// 身份信息
		if(res.idenNumber){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left part_title_depart">身份证信息</div>'
			html +=	'<div class="part_content float-left"></div>'
			html +=	'</div>'
		}
		if(res.idenNumber){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">身份证号码:</div>'
			html +=	'<div class="part_content float-left">'+res.idenNumber+'</div>'
			html +=	'</div>'
		}
		if(res.cardId){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">身份证卡ID:</div>'
			html +=	'<div class="part_content float-left">'+res.cardId+'</div>'
			html +=	'</div>'
		}
		if(res.birthday){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">出生日期:</div>'
			html +=	'<div class="part_content float-left">'+res.birthday+'</div>'
			html +=	'</div>'
		}
		if(res.censusAddr){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">户籍地址:</div>'
			html +=	'<div class="part_content float-left">'+res.censusAddr+'</div>'
			html +=	'</div>'
		}
		if(res.race){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">名族:</div>'
			html +=	'<div class="part_content float-left">'+res.race+'</div>'
			html +=	'</div>'
		}
		if(res.police){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">签发机关:</div>'
			html +=	'<div class="part_content float-left">'+res.police+'</div>'
			html +=	'</div>'
		}
		if(res.idenActiveFrom){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">身份证有效开始时间:</div>'
			html +=	'<div class="part_content float-left">'+res.idenActiveFrom+'</div>'
			html +=	'</div>'
		}
		if(res.idenActiveTo){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">身份证有效结束时间:</div>'
			html +=	'<div class="part_content float-left">'+res.idenActiveTo+'</div>'
			html +=	'</div>'
		}
		if(res.createTime){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">创建时间:</div>'
			html +=	'<div class="part_content float-left">'+res.createTime+'</div>'
			html +=	'</div>'
		}
		if(res.headImgName){
			html +=	'<div class="part_ clearfloat">'
			html +=	'<div class="part_title float-left">身份证头像:</div>'
			html +=	'<div class="part_content float-left"><img class="touxiang" src="'+res.headImgName+'" alt="" /></div>'
			html +=	'</div>'
		}

		html +=	'</div></div></div>'
		return html
	}