<?php
/**
 * 炎酷CMS - 操作日志（分页列表 + 清空）
 */
define('YKCMS', 1);
require dirname(__DIR__) . '/inc/auth.php';

$page_title = '操作日志';
$active = 'log';
$T = DB_PREFIX . 'log';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    csrf_check();
    if ((isset($_POST['do']) ? $_POST['do'] : '') === 'clear') {
        db_exec("DELETE FROM $T");
        admin_log('清空日志', '清空全部操作日志');
        flash_set('日志已清空');
    }
    header('Location: log.php');
    exit;
}

$page = max(1, (int)(isset($_GET['p']) ? $_GET['p'] : 1));
$pageSize = 20;
$total = (int)db_val("SELECT COUNT(*) FROM $T");
$start = ($page - 1) * $pageSize;
$list = db_all("SELECT * FROM $T ORDER BY id DESC LIMIT $start, $pageSize");

require dirname(__DIR__) . '/inc/header.php';
?>
<div class="yk-card">
  <h3>操作日志</h3>
  <form method="post" action="log.php" style="margin-bottom:14px" onsubmit="return confirm('确定清空全部日志？');">
    <?php echo csrf_field(); ?>
    <input type="hidden" name="do" value="clear">
    <button class="yk-btn red sm" type="submit">清空日志</button>
    <span style="color:#999;font-size:12px;margin-left:10px">共 <?php echo $total; ?> 条</span>
  </form>
  <table class="yk-table">
    <tr><th width="60">ID</th><th width="110">账号</th><th width="110">操作</th><th>详情</th><th width="120">IP</th><th width="150">时间</th></tr>
    <?php foreach ($list as $l): ?>
    <tr>
      <td><?php echo (int)$l['id']; ?></td>
      <td><?php echo e($l['admin']); ?></td>
      <td><?php echo e($l['action']); ?></td>
      <td><?php echo e($l['detail']); ?></td>
      <td><?php echo e($l['ip']); ?></td>
      <td><?php echo e(fdate($l['add_time'], 'Y-m-d H:i:s')); ?></td>
    </tr>
    <?php endforeach; ?>
    <?php if (!$list): ?><tr><td colspan="6" style="text-align:center;color:#999">暂无日志</td></tr><?php endif; ?>
  </table>
  <?php echo admin_pager($total, $page, $pageSize, 'log.php'); ?>
</div>
<?php require dirname(__DIR__) . '/inc/footer.php'; ?>
