<?php
session_start();

if( isset($_REQUEST['resume']) && (int)$_REQUEST['resume'] == 1 && isset($_SESSION['argv_list_admins']) )
{
	header('Location:list_admins.php' . $_SESSION['argv_list_admins']);
	exit();
}

if( isset($_SESSION['init_info']) )
{
	if( ( $str_temp = trim($_SESSION['init_info']) ) != '' )
		$str_init_info = $str_temp;

	unset($_SESSION['init_info']);
}

include('includes/check_level.php');

$str_query_condition = '1';
	
if( isset($_REQUEST['account']) && ( $str_account = remove_quotes(strip_tags($_REQUEST['account'])) ) )
{
	$static_str_account = $str_account;
	$str_query_condition .= ' AND `Account` = \'' . mysqli_real_escape_string($obj_class_admin->sql_connect, $static_str_account)  . '\'';

	$static_str_real_name_keyword = '';
	$static_int_level = 128;
}
else
{
	$static_str_account = '';

	if( isset($_REQUEST['real_name_keyword']) && ( $str_real_name_keyword = process_quotes(strip_tags($_REQUEST['real_name_keyword'])) ) )
	{
		$static_str_real_name_keyword = $str_real_name_keyword;
		$str_query_condition .= ' AND `RealName` LIKE \'%' . mysqli_real_escape_string($obj_class_admin->sql_connect, $static_str_real_name_keyword)  . '%\'';

		$static_int_level = 128;
	}
	else
	{
		$static_str_real_name_keyword = '';

		if( isset($_REQUEST['level']) && ( $int_level = (int)$_REQUEST['level'] ) >= -127 && $int_level <= 127 )
		{
			$static_int_level = $int_level;
			$str_query_condition .= ' AND `Level` = \'' . $int_level . '\' ';
		}
		else
			$static_int_level = 128;
	}
}

if( !isset($_REQUEST['row_count']) || ( $uint_row_count = (int)$_REQUEST['row_count'] ) <= 0 )
	$static_uint_row_count = 15;
else
	$static_uint_row_count = $uint_row_count;

if( !isset($_REQUEST['page_no']) || ( $uint_page_no = (int)$_REQUEST['page_no'] ) <= 0 )
	$static_uint_page_no = 1;
else
	$static_uint_page_no = $uint_page_no;

$arr_page_info = $obj_class_admin->page_info('`AdminId`', 'admins', $str_query_condition, $static_uint_row_count);

if( $arr_page_info['PageCount'] && $static_uint_page_no > $arr_page_info['PageCount'] )
{
	header('Location:list_admins.php?account=' . $static_str_account . '&real_name_keyword=' . urlencode($static_str_real_name_keyword) . '&level=' . $static_int_level . '&row_count=' . $static_uint_row_count . '&page_no=' . $arr_page_info['PageCount']);
	exit();
}

$arr_admins = $obj_class_admin->search_admins($static_str_account, $static_str_real_name_keyword, $static_int_level, ( $static_uint_page_no - 1 ) * $static_uint_row_count, $static_uint_row_count);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="utf-8" lang="utf-8">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta http-equiv="content-language" content="zh-tw" />
	<meta name="robots" content="none" />
	<link rel="stylesheet" type="text/css" href="admin.css" />
	<title>管理帳號列表</title>
	<script type="text/javascript" src="scripts/jquery.js"></script>
	<script type="text/javascript">
<!--
var global_bool_processing = false;

function do_search_admins()
{
	if( global_bool_processing )
		return false;

	location.replace('list_admins.php?account=' + encodeURI(document.search_admins.account.value) + '&real_name_keyword=' + encodeURI(document.search_admins.real_name_keyword.value) + '&level=' + document.search_admins.level.value + '&row_count=<?=$static_uint_row_count?>');

	return false;
}

function edit_admin(static_uint_admin_id)
{
	if( global_bool_processing )
		return false;

	var ele_form = $('<form></form>').attr('action', 'edit_admin.php').attr('method', 'post'); 

	$('<input />').attr('type', 'hidden').attr('name', 'admin_id').attr('value', static_uint_admin_id).appendTo(ele_form);

	ele_form.appendTo('body').submit();
}

function delete_admin(static_uint_admin_id, static_str_account)
{
	if( global_bool_processing )
		return false;

	if( !confirm('確定要刪除帳號［' + static_str_account + '］？') )
		return false;

	global_bool_processing = true;

	$.ajax(
	{
		url: 'ajax_do_delete_admin.php',
		type: 'POST',
		data: 'admin_id=' + static_uint_admin_id,
		dataType: 'json',
		success: function(jdata)
		{
			if( jdata['RedirectURL'] )
				location.replace(jdata['RedirectURL']);
			else if( jdata['ErrMessage'] != '' )
				alert(jdata['ErrMessage']);
			else if( jdata['ErrCode'] != '1' )
				alert('Error:' + jdata['ErrCode']);

			if( jdata['ErrCode'] == '1' )
				location.reload();

			global_bool_processing = false;
		}
	});

	return true;
}
//-->
	</script>
</head>

<body<?=( isset($str_init_info) ? ' onLoad="javascript:alert(\'' . $str_init_info . '\');"' : '' )?>>
<? include('_title.php'); ?>
<? include('_menu.php'); ?>
<div id="content">
<h2>管理帳號列表</h2>
<form name="search_admins" onsubmit="return do_search_admins();">
<fieldset class="search">
<legend>搜尋條件</legend>
帳號：<input class="search_content" type="text" name="account" value="<?=$static_str_account?>" maxlength="32" />
姓名關鍵字：<input class="search_content" type="text" name="real_name_keyword" value="<?=$static_str_real_name_keyword?>" maxlength="15" />
權限等級：<select name="level"><option value="128">顯示全部</option><?
foreach( $arr_admin_level as $int_level=>$str_level )
{
	echo('<option value="' . $int_level . '"');

	if( $int_level == $static_int_level )
		echo(' selected');
	
	echo('>' . $str_level . '</option>');
}
?></select>
<input class="button" type="submit" value="進行搜尋" />
<input class="button" type="button" value="清除搜尋條件" onclick="location.replace('list_admins.php');" />
</fieldset>
</form>
<?
if( !$arr_admins )
	echo("<p>目前並無任何資料</p>\n");
else
{
?>
<table class="data_list"><tbody>
<tr><th>真實姓名</th><th>登入帳號</th><th>權限等級</th><th>建立日期</th><th>上次登入時間</th><th>上次登入IP</th><th>上次經由Proxy</th><th>管理</th></tr>
<?
	foreach( $arr_admins as $n=>$arr_row )
	{
		echo('<tr id="tr_' . $arr_row['AdminId'] . '"><td class="name">' . $arr_row['RealName'] . '</td><td class="account">' . $arr_row['Account'] . '</td><td class="level">');

		if( isset( $arr_admin_level[$arr_row['Level']] ) )
			echo($arr_admin_level[$arr_row['Level']]);
		else
			echo('其他：' . $arr_row['Level']);
			
		echo('</td><td class="date">' . $arr_row['BuildDate'] . '</td><td class="date_time">' . $arr_row['LastLogin'] . '</td><td class="ip">' . $arr_row['LLIP'] . '</td><td class="ip">');
		
		if( $arr_row['LLProxy'] == '' )
			echo('--');
		else
			echo($arr_row['LLProxy']);
		
		echo('</td><td class="function"><input type="button" value="編輯" onclick="javascript:edit_admin(' . $arr_row['AdminId'] . ');" />&nbsp;<input id="button_delete_' . $arr_row['AdminId'] . '" type="button" value="刪除" ');

		if( $arr_row['LastLogin'] != '0000-00-00 00:00:00' )
			echo('onclick="javascript:alert(\'已登入啟用之帳號無法被刪除，如人員已離職，請將權限設定為禁止登入。\');"');
		else
			echo('onclick="javascript:delete_admin(' . $arr_row['AdminId'] . ', \'' . $arr_row['Account'] . '\');"');
		
		echo("/></td></tr>\n");
	}
?>
</tbody></table>
<form name="list_admins" action="list_admins.php" method="get">
<fieldset class="page_ctrl">
<input type="hidden" name="account"  value="<?=$static_str_account?>" /><input type="hidden" name="real_name_keyword"  value="<?=$static_str_real_name_keyword?>" /><input type="hidden" name="level"  value="<?=$static_int_level?>" /><input class="button" type="button" value="第一頁" onclick="window.location = 'list_admins.php?account=<?=$static_str_account?>&real_name_keyword=<?=urlencode($static_str_real_name_keyword)?>&level=<?=$static_int_level?>&row_count=<?=$static_uint_row_count?>&page_no=1';" /> <?
	if( $static_uint_page_no <= 1 )
		echo('<input class="button" type="button" value="已至第一頁" disabled /> ');
	else
		echo('<input class="button" type="button" value="←上一頁" onclick="window.location = \'list_admins.php?account=' . $static_str_account . '&real_name_keyword=' . urlencode($static_str_real_name_keyword) . '&level=' . $static_int_level . '&row_count=' . $static_uint_row_count . '&page_no=' . ( $static_uint_page_no - 1 ) . '\';" /> ');

	if( $static_uint_page_no >= $arr_page_info['PageCount'] )
		echo('<input class="button" type="button" value="已至最後一頁" disabled /> ');
	else
		echo('<input class="button" type="button" value="下一頁→" onclick="window.location = \'list_admins.php?account=' . $static_str_account . '&real_name_keyword=' . urlencode($static_str_real_name_keyword) . '&level=' . $static_int_level . '&row_count=' . $static_uint_row_count . '&page_no=' . ( $static_uint_page_no + 1 ) . '\';" /> ');
?><input class="button" type="button" value="最後一頁" onclick="window.location = 'list_admins.php?account=<?=$static_str_account?>&real_name_keyword=<?=urlencode($static_str_real_name_keyword)?>&level=<?=$static_int_level?>&row_count=<?=$static_uint_row_count?>&page_no=<?=$arr_page_info['PageCount']?>';" />　每頁顯示<input  class="page_content" type="text" name="row_count" value="<?=$static_uint_row_count?>" onchange="document.list_admins.page_no.value = '1';" />筆資料，共<?=$arr_page_info['PageCount']?>頁，到第<input  class="page_content" type="text" name="page_no" value="<?=$static_uint_page_no?>" />頁<input class="page_submit" type="submit" value="→" /></fieldset></form>
<?
}
?>
</div>
<? include('_footer.php'); ?>
</body>

</html>