<?php
require __DIR__ . '/../../app/bootstrap.php';
require __DIR__ . '/../../app/database.php';
require __DIR__ . '/../../app/admin-sidebar.php';
require_login();

$adminName = isset($_SESSION['admin_name']) ? (string)$_SESSION['admin_name'] : 'Admin';
$adminUsername = isset($_SESSION['admin_username']) ? (string)$_SESSION['admin_username'] : 'admin';
$avatarText = strtoupper(substr($adminName !== '' ? $adminName : 'A', 0, 1));
$targetAdminId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$keyword = isset($_GET['keyword']) ? trim((string)$_GET['keyword']) : '';
$dbError = '';
$logs = array();
$targetAdmin = null;

try {
    if ($targetAdminId > 0) {
        $targetAdmin = find_admin_by_id($targetAdminId);
    }
    $logs = list_admin_operation_logs($targetAdminId, $keyword);
} catch (Exception $e) {
    $dbError = $e->getMessage();
}
?><!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo e(APP_NAME); ?> - 管理员操作日志</title>
    <link rel="stylesheet" href="/assets/css/admin-ui.css">
</head>
<body>
<div class="admin-shell">
    <?php render_admin_sidebar('admins', $adminName, $adminUsername, $avatarText); ?>
    <main class="admin-content">
        <div class="content-wrap">
            <header class="topbar">
                <div>
                    <h1>管理员操作日志</h1>
                    <p><?php echo $targetAdmin ? '查看管理员 ' . e($targetAdmin['username']) . ' 的操作日志。' : '查看全部管理员操作日志。'; ?></p>
                </div>
                <div class="topbar-actions"><a class="btn-ghost" href="/admin/user/list.php">返回管理员列表</a></div>
            </header>
            <section class="page-card">
                <?php if ($dbError !== ''): ?>
                    <div class="mini-item" style="margin-bottom:16px; border-color: rgba(220,38,38,0.18); background: rgba(220,38,38,0.06);">
                        <strong style="color:#dc2626;">日志读取失败</strong>
                        <p style="margin:0; color:#b91c1c;"><?php echo e($dbError); ?></p>
                        <p style="margin:8px 0 0; color:#7f1d1d;">请先执行 `database/domains.sql`，确保 `admin_operation_logs` 表已创建。</p>
                    </div>
                <?php endif; ?>
                <form class="filter-row" method="get" action="/admin/user/logs.php">
                    <?php if ($targetAdminId > 0): ?>
                        <input type="hidden" name="id" value="<?php echo (int)$targetAdminId; ?>">
                    <?php endif; ?>
                    <input class="input" type="text" name="keyword" value="<?php echo e($keyword); ?>" placeholder="搜索操作人 / 目标账号 / 动作 / 详情">
                    <button class="btn-primary" type="submit">查询</button>
                </form>
                <table class="data-table">
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>操作人</th>
                        <th>目标账号</th>
                        <th>动作</th>
                        <th>详情</th>
                        <th>时间</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php if (!empty($logs)): ?>
                        <?php foreach ($logs as $item): ?>
                            <tr>
                                <td><?php echo (int)$item['id']; ?></td>
                                <td><?php echo e($item['operator_username']); ?></td>
                                <td><?php echo e($item['target_username']); ?></td>
                                <td><?php echo e($item['action']); ?></td>
                                <td><?php echo e($item['detail']); ?></td>
                                <td><?php echo e($item['created_at']); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="6" class="empty-note"><?php echo $dbError === '' ? '暂无管理员操作日志数据。' : '当前无法加载管理员操作日志。'; ?></td>
                        </tr>
                    <?php endif; ?>
                    </tbody>
                </table>
                <div class="table-note">当前日志记录管理员新增、编辑、启用/停用、删除和登录成功等关键操作。</div>
            </section>
        </div>
    </main>
</div>
<script src="/assets/js/admin-ui.js"></script>
</body>
</html>
