<?php
defined('PHP168_PATH') or die();

/**
* ÈÕÖ¾¹ÜÀí
**/

$this_controller->check_admin_action($ACTION) or message('no_privilege');

if(REQUEST_METHOD == 'GET'){
	
	$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
	if($id){
		$data = $DB_master->fetch_one("SELECT data FROM {$core->TABLE_}admin_log WHERE id = '$id'");
		
		exit(html_entities($data['data']));
	}
	
	$uid = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
	$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
	$desc = empty($_GET['desc']) ? ' DESC' : ' ASC';
	$page = max($page, 1);
	
	$select = select();
	$select->from($core->TABLE_ .'admin_log', 'id, uid, username, title, url, ip, timestamp');
	$select->order('id'. $desc);
	if($uid){
		$select->in('uid', $uid);
		$select->order('timestamp'. $desc);
	}
	
	$count = 0;
	$page_size = 20;
	$list = $core->list_item(
		$select,
		array(
			'page' => &$page,
			'count' => &$count,
			'page_size' => $page_size
		)
	);
	
	$page_url = $this_url .'?page=?page?';
	
	$pages = list_page(array(
		'count' => $count,
		'page' => $page,
		'page_size' => $page_size,
		'url' => $page_url
	));
	
	include template($core, 'log', 'admin');
	
}else if(REQUEST_METHOD == 'POST'){
	
	$act = isset($_POST['act']) ? $_POST['act'] : '';
	switch($act){
	
	case 'truncate':
		$DB_master->query("TRUNCATE TABLE {$core->TABLE_}admin_log");
		
		$ADMIN_LOG = array('title' => $P8LANG['_core_truncate_admin_log']);
		
		message('done');
	break;
	
	case 'delete':
		$id = isset($_POST['id']) ? filter_int($_POST['id']) : array();
		$id or message('access_denied');
		
		$DB_master->delete(
			$core->TABLE_ .'admin_log',
			'id IN ('. implode(',', $id) .')'
		);
		
		$ADMIN_LOG = array('title' => $P8LANG['_core_delete_admin_log']);
		
		message('done');
	break;
	
	}
	
}