<%@ 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 : 'leader!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 : 'leadername',
				width : 100
			}, {
				title : '矿区',
				field : 'mineareaname',
				width : 80
			}, {
				title : '状态',
				field : 'state',
				width : 80,
				formatter : function(value, row, index) {
					if (value == "1") {
						return "<font color='red'>启用</font>";
					} else {
						return "停用";
					}
				}
			} ] ],
			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}/leader!leaderEdit.do',
						width : 540,
						height : 300,
						buttons : [ {
							text : '编辑',
							handler : function() {
								var f = p.find('form');
								f
										.form(
												'submit',
												{
													url : '${pageContext.request.contextPath}/leader!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 mineareacode = f
									.find('input[name=mineareacode]');
							var mineareaIdComboboxTree = mineareacode
									.combotree({
										url : '${pageContext.request.contextPath}/minearea!doNotNeedSession_treeRecursive.do',
										multiple : false,
										editable : false,
										panelHeight : 'auto'
									});
							
							//alert(rows[0].personcode);

							f.form('load', {
								id : rows[0].id,
								leadername : rows[0].leadername,
								state : rows[0].state,
								mineareacode : rows[0].mineareacode,

							});
						}
					});
		} 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}/leader!leaderAdd.do',
					width : 540,
					height : 300,
					buttons : [ {
						text : '添加',
						handler : function() {
							var f = p.find('form');
							f
									.form(
											'submit',
											{
												url : '${pageContext.request.contextPath}/leader!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 mineareacode = f.find('input[name=mineareacode]');
						var mineareaIdComboboxTree = mineareacode
								.combotree({
									url : '${pageContext.request.contextPath}/minearea!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}/leader!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: 55px;overflow: hidden;" align="left">
		<form id="searchForm">
			<table class="tableForm datagrid-toolbar"
				style="width: 100%;height: 100%;">
				<tr>
					<th>姓名</th>
					<td><input name="leadername" 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>
				</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>