<%@ 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 datagrid;
	$(function() {

		datagrid = $('#datagrid').datagrid({
			url : 'personinfo!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 : 'id',
			sortOrder : 'asc',
			checkOnSelect : true,
			selectOnCheck : true,
			frozenColumns : [ [ {
				title : '编号',
				field : 'id',
				width : 10,
				sortable : true,
				checkbox : true
			} ] ],
			columns : [ [ {
				title : '人员编号',
				field : 'personcode',
				width : 30,

			}, {
				title : '人员姓名',
				field : 'personname',
				width : 50
			}, {
				title : '卡号',
				field : 'cardcode',
				width : 100,

			}, {
				title : '性别',
				field : 'personsex',
				width : 80,
                formatter : function(value, row, index) {
					if (value == "0") {
						return "男";
					} else {
						return "女";
					}
				}
			}, {
				title : '电话',
				field : 'personphone',
				width : 80
			}, {
				title : '部门',
				field : 'departname',
				width : 120
			}, {
				title : '岗位',
				field : 'postname',
				width : 120
			}, {
				title : '班组',
				field : 'groupname',
				width : 120
			}, {
				title : '备注',
				field : 'remark',
				width : 80,
				hidden : true,
			} ] ],
			toolbar : [ {
				text : '增加',
				iconCls : 'icon-add',
				handler : function() {
					append();
				}
			}, '-', {
				text : '删除',
				iconCls : 'icon-remove',
				handler : function() {
					remove();
				}
			}, '-', {
				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');
				}
			}, '-' ],
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});

	});

	function edit() {
		var rows = datagrid.datagrid('getSelections');
		if (rows.length == 1) {
			var p = parent.dj
					.dialog({
						title : '编辑人员',
						href : '${pageContext.request.contextPath}/personinfo!personinfoEdit.do',
						width : 540,
						height : 300,
						buttons : [ {
							text : '编辑',
							handler : function() {
								var f = p.find('form');
								f
										.form(
												'submit',
												{
													url : '${pageContext.request.contextPath}/personinfo!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');

							var deptId = f.find('input[name=departcode]');
							var deptIdComboboxTree = deptId
									.combotree({
										url : '${pageContext.request.contextPath}/dept!doNotNeedSession_treeRecursive.do',
										multiple : false,
										editable : false,
										panelHeight : 'auto'
									});
							var areacode = f.find('input[name=areacode]');
							var areaIdComboboxTree = areacode
									.combotree({
										url : '${pageContext.request.contextPath}/area!doNotNeedSession_treeRecursive.do',
										multiple : false,
										editable : false,
										panelHeight : 'auto'
									});
							var mineareacode = f
									.find('input[name=mineareacode]');
							var mineareaIdComboboxTree = mineareacode
									.combotree({
										url : '${pageContext.request.contextPath}/minearea!doNotNeedSession_treeRecursive.do',
										multiple : false,
										editable : false,
										panelHeight : 'auto'
									});
							var postcode = f.find('input[name=postcode]');
							var areaIdComboboxTree = postcode
									.combotree({
										url : '${pageContext.request.contextPath}/post!doNotNeedSession_treeRecursive.do',
										multiple : false,
										editable : false,
										panelHeight : 'auto'
									});
							var groupcode = f.find('input[name=groupcode]');
							var areaIdComboboxTree = groupcode
									.combotree({
										url : '${pageContext.request.contextPath}/group!doNotNeedSession_treeRecursive.do',
										multiple : false,
										editable : false,
										panelHeight : 'auto'
									});
							//alert(rows[0].personcode);

							f.form('load', {
								id : rows[0].id,
								personcode : rows[0].personcode,
								personname : rows[0].personname,
								cardcode : rows[0].cardcode,
								zigbee : rows[0].zigbee,
								personsex : rows[0].personsex,
								personphone : rows[0].personphone,
								departcode : rows[0].departcode,
								areacode : rows[0].areacode,
								mineareacode : rows[0].mineareacode,
								postcode : rows[0].postcode,
								groupcode : rows[0].groupcode,
								remark : rows[0].remark,

							});
						}
					});
		} else if (rows.length > 1) {
			parent.dj.messagerAlert('提示', '同一时间只能编辑一条记录！', 'error');
		} else {
			parent.dj.messagerAlert('提示', '请选择要编辑的记录！', 'error');
		}
	}
	function append() {
		var p = parent.dj
				.dialog({
					title : '添加人员',
					href : '${pageContext.request.contextPath}/personinfo!personinfoAdd.do',
					width : 540,
					height : 300,
					buttons : [ {
						text : '添加',
						handler : function() {
							var f = p.find('form');
							f
									.form(
											'submit',
											{
												url : '${pageContext.request.contextPath}/personinfo!add.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');
						var deptId = f.find('input[name=departcode]');
						var deptIdComboboxTree = deptId
								.combotree({
									url : '${pageContext.request.contextPath}/dept!doNotNeedSession_treeRecursive.do',
									multiple : false,
									editable : false,
									panelHeight : 'auto'
								});
						var postcode = f.find('input[name=postcode]');
						var postIdComboboxTree = postcode
								.combotree({
									url : '${pageContext.request.contextPath}/post!doNotNeedSession_treeRecursive.do',
									multiple : false,
									editable : false
								});
						var groupcode = f.find('input[name=groupcode]');
						var groupIdComboboxTree = groupcode
								.combotree({
									url : '${pageContext.request.contextPath}/group!doNotNeedSession_treeRecursive.do',
									multiple : false,
									editable : false,
									panelHeight : 'auto'
								});
					}
				});
	}
	function remove() {
		var rows = datagrid.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			parent.dj
					.messagerConfirm(
							'请确认',
							'您要删除当前所选项目？',
							function(r) {
								if (r) {
									for ( var i = 0; i < rows.length; i++) {
										ids.push(rows[i].id);
									}
									$
											.ajax({
												url : '${pageContext.request.contextPath}/personinfo!delete.do',
												data : {
													ids : ids.join(',')
												},
												dataType : 'json',
												success : function(d) {
													datagrid.datagrid('load');
													datagrid
															.datagrid('unselectAll');
													parent.dj.messagerShow({
														title : '提示',
														msg : d.msg
													});
												}
											});
								}
							});
		} else {
			parent.dj.messagerAlert('提示', '请勾选要删除的记录！', 'error');
		}
	}
	function _search() {
		datagrid.datagrid('load', dj.serializeObject($('#searchForm')));
	}
	function cleanSearch() {
		datagrid.datagrid('load', {});
		$('#searchForm input').val('');
	}
	function exportd() {
		Export('人员信息', $('#datagrid'))
	}
</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="personname" style="width:160px;" /></td>
					<th>卡号</th>
					<td ><input name="cardcode" style="width:160px;" /></td>
					<th>岗位</th>
					<td>
					<select name="postcode" class="easyui-combotree" style="width:160px;"
        data-options="url:'${pageContext.request.contextPath}/post!doNotNeedSession_treeRecursive.do'"></select>
        </td>
        <th>班组</th>
					<td>
					<select name="groupcode" class="easyui-combotree" style="width:160px;"
        data-options="url:'${pageContext.request.contextPath}/group!doNotNeedSession_treeRecursive.do'"></select>
        </td>
				</tr>
					<th>部门</th>
					<td>
					<select  name="departcode" class="easyui-combotree" style="width:160px;"
        data-options="url:'${pageContext.request.contextPath}/dept!doNotNeedSession_treeRecursive.do'"></select>
					</td>
					<th></th>
					<td>
					 <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="datagrid"></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>
</body>
</html>
