<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<jsp:include page="../../inc.jsp"></jsp:include>
<script type="text/javascript" charset="utf-8">
	var a = '${sessionInfo.mineareacode}';
	var li_tempb = 0;
	//通过矿区id打开不同的页面
	if (a == '0')//整体
	{
		b = '1';
	} else if (a == 'e9da32a6-13a4-4083-98f1-e67f12667192')//一采区
	{
		b = '1';
	} else if (a == '825c66e1-f1ab-4527-a386-296393d25418')//三采区
	{
		b = '2';
	} else if (a == 'b9b6e0ca-4ee6-4aed-b88e-baf67d9c23ad')//四采区
	{
		b = '3';
	}

	var datagrid;
	$(function() {

		datagrid = $('#attendancedatagrid')
				.datagrid(
						{
							url : 'attendance!datagrid.do',
							pagination : true,
							pagePosition : 'bottom',
							pageSize : 20,
							pageList : [ 20, 40, 60, 80, 100, 120, 140, 160,
									200 ],
							fit : true,
							fitColumns : true,
							nowrap : true,
							border : false,
							idField : 'id',
							sortName : 'intime',
							sortOrder : 'desc',
							checkOnSelect : true,
							selectOnCheck : true,
							frozenColumns : [ [ {
								title : '编号',
								field : 'id',
								width : 10,
								sortable : true,
								checkbox : true
							} ] ],
							columns : [ [
									{
										title : '矿区',
										field : 'mineareaname',
										width : 120,

									},
									{
										title : '部门',
										field : 'departname',
										width : 120,

									},
									{
										title : '人员编号',
										field : 'personcode',
										width : 120,

									},
									{
										title : '人员姓名',
										field : 'personname',
										width : 120,

									},
									{
										title : '入井时间',
										field : 'intime',
										width : 120,

									},
									{
										title : '出井时间',
										field : 'outtime',
										width : 120,
										formatter : function(value, row, index) {
											if (value == "1970-01-01 00:00:00") {
												return "";

											} else {
												return value;
											}
										}
									},
									{
										title : '时长',
										field : 'staytime',
										width : 120
									},
									{
										title : '状态',
										field : 'flag',
										width : 120,
										formatter : function(value, row, index) {
											if (value == "0") {
												return "正常";
											} else if (value == "1") {
												//return "<font color='red'>未读到入井信息</font>";
												return "正常";
											} else if (value == "2") {
												//return "<font color='red'>未读到升井信息</font>";
												return "正常";
											} else if (value == "3") {
												//return "<font color='red'>未读到升井信息</font>";
												return "正常";
											}

										}
									},
									{
										field : 'opt',
										title : '操作',
										width : 50,
										align : 'center',
										formatter : function(value, rec) {
											if (rec.outtime == null
													|| rec.outtime == "") {
												var btn = '<a class="editcls" onclick="finishKq(\''
														+ rec.personcode
														+ '\',\''
														+ rec.intime
														+ '\')" href="javascript:void(0)">手动结束考勤</a>';
												return btn;
											}
										}
									} ] ],

							toolbar : [ {
								text : '修改',
								iconCls : 'icon-edit',
								handler : function() {
									edit();
								}
							}, '-', {
								text : '导出',
								iconCls : 'icon-edit',
								handler : function() {
									exportd();
								}
							}, '-', {
								text : '取消选中',
								iconCls : 'icon-undo',
								handler : function() {
									datagrid.datagrid('clearSelections');
									datagrid.datagrid('unselectAll');
								}
							}, '-' ],

							//增加双击弹出轨迹页面，同时弹出三维页面
							onDblClickRow : function(rowIndex, rowData) { //双击行弹框编辑
								//被编辑列高亮，其他列去除高亮
								// alert(rowIndex);
								// alert(rowData.personcode);
								// alert(rowData.intime);
								// alert(rowData.outtime);
								// 打开三维界面 传值 ：人员编号   开始时间   结束时间  以供三维反查回来

								var starttime = rowData.intime;
								var endtime = rowData.outtime;
								starttime = starttime.replace("-", "/")
										.replace("-", "/");
								endtime = endtime.replace("-", "/").replace(
										"-", "/");
								//alert("12341");
								//通过 personcode 和 intime读取 tab_readinfo 表数据
								var timeSe = new Date().getTime() / 1000;

								var layer = '50';
								var kqid = 1;

								$.getJSON('http://192.168.1.3:8080/MineJSON/vzigbee.action?type=8&param='
									+ rowData.personcode+ '-'
									+ (Date.parse(new Date(starttime)) / 1000)+ '-'
									+ (Date.parse(new Date(endtime)) / 1000)
									+ '&time=' + timeSe,null,
									function(obj) {

													layer = obj[0].layer;

													//alert(layer);
													if (layer == '50'
															|| layer == '75') {
														kqid = 1;
													}
													if (layer == '105') {
														kqid = 2;
													}
													if (layer == '150'
															|| layer == '175'
															|| layer == '225') {
														kqid = 3;
													}

													window
															.open(
																	'test.html?type=1,2&param='
																			+ b
																			+ ','
																			+ rowData.personcode
																			+ '-'
																			+ (Date
																					.parse(new Date(
																							starttime)) / 1000)
																			+ '-'
																			+ (Date
																					.parse(new Date(
																							endtime)) / 1000)
																			+ '-'
																			+ 1,
																	'newwindow',
																	'height=800,width=1280,top=0,left=0,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');

												});

							},

							onRowContextMenu : function(e, rowIndex, rowData) {
								e.preventDefault();
								$(this).datagrid('unselectAll');
								$(this).datagrid('selectRow', rowIndex);
								$('#menu').menu('show', {
									left : e.pageX,
									top : e.pageY
								});
							}
						});

	});

	function _search() {
		datagrid.datagrid('load', dj.serializeObject($('#searchForm')));
	}
	function cleanSearch() {
		datagrid.datagrid('load', {});
		$('#searchForm input').val('');
	}
	function exportd() {
		Export('考勤信息', $('#attendancedatagrid'))

	}

	//只修改出井时间，标志为4
	function edit() {
		var rows = datagrid.datagrid('getSelections');
		if (rows.length == 1) {
			var p = parent.dj
					.dialog({
						title : '编辑考勤信息',
						href : '${pageContext.request.contextPath}/attendance!attendanceEdit.do',
						width : 540,
						height : 300,
						buttons : [ {
							text : '编辑',
							handler : function() {
								var f = p.find('form');
								f
										.form(
												'submit',
												{
													url : '${pageContext.request.contextPath}/attendance!edit.do',
													success : function(d) {
														var json = $
																.parseJSON(d);
														if (json.success) {
															datagrid
																	.datagrid('reload');
															p.dialog('close');
														}
														parent.dj
																.messagerShow({
																	msg : json.msg,
																	title : '提示'
																});
													}
												});
							}
						} ],
						onLoad : function() {
							var f = p.find('form');

							f.form('load', {
								id : rows[0].id,
								personcode : rows[0].personcode,
								personname : rows[0].personname,
								intime : rows[0].intime,
								outtime : rows[0].outtime,

							});
						}
					});
		} else if (rows.length > 1) {
			parent.dj.messagerAlert('提示', '同一时间只能编辑一条记录！', 'error');
		} else {
			parent.dj.messagerAlert('提示', '请选择要编辑的记录！', 'error');
		}
	}
	function finishKq(personcode, intime) {
		//alert(personcode);
		//alert(intime);
		parent.dj
				.messagerConfirm(
						'请确认',
						'您要手动考勤当前所选项目？',
						function(r) {
							if (r) {

								$
										.ajax({
											url : '${pageContext.request.contextPath}/attendance!finishkq.do',
											data : {
												personcode : personcode,
												intime : intime,
											},
											dataType : 'json',
											success : function(d) {
												datagrid.datagrid('load');
												datagrid
														.datagrid('unselectAll');
												parent.dj.messagerShow({
													title : '提示',
													msg : d.msg
												});
											}
										});
							}
						});

	}

	// 获取当前时间戳(以s为单位)
	//var timestamp = Date.parse(new Date());
	//timestamp = timestamp / 1000;
	//alert(timestamp);
	//当前时间戳为：1403149534
	//console.log("当前时间戳为：" + timestamp);

	// 获取某个时间格式的时间戳
	//var stringTime = "2014-07-10 10:21:12";
	//var timestamp2 = Date.parse(new Date(stringTime));
	//timestamp2 = timestamp2 / 1000;
	//2014-07-10 10:21:12的时间戳为：1404958872
	//console.log(stringTime + "的时间戳为：" + timestamp2);

	// 将当前时间换成时间格式字符串
	//var timestamp3 = 1403058804;
	//var newDate = new Date();
	//newDate.setTime(timestamp3 * 1000);
	// Wed Jun 18 2014
	//console.log(newDate.toDateString());
	// Wed, 18 Jun 2014 02:33:24 GMT
	//console.log(newDate.toGMTString());
	// 2014-06-18T02:33:24.000Z
	//console.log(newDate.toISOString());
	// 2014-06-18T02:33:24.000Z
	//console.log(newDate.toJSON());
	// 2014年6月18日
	//console.log(newDate.toLocaleDateString());
	// 2014年6月18日 上午10:33:24
	//console.log(newDate.toLocaleString());
	// 上午10:33:24
	//console.log(newDate.toLocaleTimeString());
	// Wed Jun 18 2014 10:33:24 GMT+0800 (中国标准时间)
	//console.log(newDate.toString());
	// 10:33:24 GMT+0800 (中国标准时间)
	//console.log(newDate.toTimeString());
	// Wed, 18 Jun 2014 02:33:24 GMT
	//console.log(newDate.toUTCString());

	Date.prototype.format = function(format) {
		var date = {
			"M+" : this.getMonth() + 1,
			"d+" : this.getDate(),
			"h+" : this.getHours(),
			"m+" : this.getMinutes(),
			"s+" : this.getSeconds(),
			"q+" : Math.floor((this.getMonth() + 3) / 3),
			"S+" : this.getMilliseconds()
		};
		if (/(y+)/i.test(format)) {
			format = format.replace(RegExp.$1, (this.getFullYear() + '')
					.substr(4 - RegExp.$1.length));
		}
		for ( var k in date) {
			if (new RegExp("(" + k + ")").test(format)) {
				format = format.replace(RegExp.$1,
						RegExp.$1.length == 1 ? date[k] : ("00" + date[k])
								.substr(("" + date[k]).length));
			}
		}
		return format;
	}
	//console.log(newDate.format('yyyy-MM-dd h:m:s'));
</script>
</head>
<body class="easyui-layout" data-options="fit:true">
	<div data-options="region:'north',border:false,title:'过滤条件'"
		style="height: 110px;overflow: hidden;" align="left">
		<form id="searchForm">
			<table class="tableForm datagrid-toolbar"
				style="width: 100%;height: 100%;">
				<tr>
					<th>编号：</th>
					<td><input name="personcode" style="width:160px;" />
					<th>姓名：</th>
					<td><input name="personname" style="width:160px;" /></td>
					<th>矿区：</th>
					<td>

					<select name="mineareacode" class="easyui-combotree"
						style="width:160px;"
						data-options="url:'${pageContext.request.contextPath}/minearea!doNotNeedSession_treeRecursive.do'">
					</select>

					</td>
					<th>部门：</th>
					<td>
					<select name="departcode" class="easyui-combotree"
						style="width:160px;"
						data-options="url:'${pageContext.request.contextPath}/dept!doNotNeedSession_treeRecursive.do'">
					</select>
					</td>

				</tr>
				<tr>

					<th>入井时间起：</th>
					<td><input class="easyui-datetimebox" name="intimeStart"
						style="width:160px;" /></td>
					<th>止：</th>
					<td><input class="easyui-datetimebox" name="intimeEnd"
						style="width:160px;" />
					<th>出井时间起：</th>
					<td><input class="easyui-datetimebox" name="outtimeStart"
						style="width:160px;" /></td>
					<th>止：</th>
					<td><input class="easyui-datetimebox" name="outtimeEnd"
						style="width:160px;" /></td>
					<td colspan="6"><a href="javascript:void(0);"
						class="easyui-linkbutton" onclick="_search();">过滤</a><a
						href="javascript:void(0);" class="easyui-linkbutton"
						onclick="cleanSearch();">取消</a></td>
				</tr>
			</table>
		</form>

	</div>


	<div data-options="region:'center',border:false"
		style="overflow: hidden;">
		<table id="attendancedatagrid"></table>
	</div>

	<div id="menu" class="easyui-menu" style="width:120px;display: none;">
		<!-- <div onclick="append();" data-options="iconCls:'icon-add'">增加</div>
		<div onclick="remove();" data-options="iconCls:'icon-remove'">删除</div>
		 -->
		<div onclick="edit();" data-options="iconCls:'icon-edit'">编辑</div>
		<div onclick="exportd();" data-options="iconCls:'icon-edit'">导出</div>
	</div>
	<div id="tttt" title="yyyy"></div>

</body>
</html>
