<?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));
if (!is_super_admin()) {
    redirect('/admin/dashboard/index.php');
}

$keyword = isset($_GET['keyword']) ? trim((string)$_GET['keyword']) : '';
$statusOptions = admin_status_options();
$toggleStatus = isset($_GET['toggle_status']) ? trim((string)$_GET['toggle_status']) : '';
$toggledAdmin = isset($_GET['toggled_admin']) ? trim((string)$_GET['toggled_admin']) : '';
$deleteStatus = isset($_GET['delete_status']) ? trim((string)$_GET['delete_status']) : '';
$deletedAdmin = isset($_GET['deleted_admin']) ? trim((string)$_GET['deleted_admin']) : '';
$dbError = '';
$admins = array();

try {
    $admins = list_admins($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>集中查看管理员账号、状态与创建时间，后续可扩展新增、编辑与权限控制。</p></div>
                <div class="topbar-actions"><a class="btn-ghost" href="/admin/dashboard/index.php">返回仪表盘</a><a class="btn-primary" href="/admin/user/add.php">新增管理员</a></div>
            </header>
            <section class="page-card">
                <?php if ($toggleStatus === 'success_disabled'): ?>
                    <div class="mini-item" style="margin-bottom:16px; border-color: rgba(245,158,11,0.18); background: rgba(245,158,11,0.06);">
                        <strong style="color:#d97706;">操作成功</strong>
                        <p style="margin:0; color:#b45309;">管理员 <?php echo e($toggledAdmin); ?> 已停用。</p>
                    </div>
                <?php elseif ($toggleStatus === 'success_enabled'): ?>
                    <div class="mini-item" style="margin-bottom:16px; border-color: rgba(34,197,94,0.18); background: rgba(34,197,94,0.06);">
                        <strong style="color:#15803d;">操作成功</strong>
                        <p style="margin:0; color:#166534;">管理员 <?php echo e($toggledAdmin); ?> 已启用。</p>
                    </div>
                <?php elseif ($toggleStatus === 'missing'): ?>
                    <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;">未找到对应的管理员记录。</p>
                    </div>
                <?php elseif ($toggleStatus === 'invalid'): ?>
                    <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;">缺少有效的管理员ID或状态参数。</p>
                    </div>
                <?php elseif ($toggleStatus === 'error'): ?>
                    <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;">管理员状态切换失败，请稍后重试。</p>
                    </div>
                <?php elseif ($toggleStatus === 'protected'): ?>
                    <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;">系统至少需要保留 1 个启用状态的管理员账号，最后一个启用管理员不可禁用。</p>
                    </div>
                <?php elseif ($toggleStatus === 'self_protected'): ?>
                    <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;">当前登录管理员不允许禁用自己的账号。</p>
                    </div>
                <?php endif; ?>
                <?php if ($deleteStatus === 'success'): ?>
                    <div class="mini-item" style="margin-bottom:16px; border-color: rgba(34,197,94,0.18); background: rgba(34,197,94,0.06);">
                        <strong style="color:#15803d;">删除成功</strong>
                        <p style="margin:0; color:#166534;">管理员 <?php echo e($deletedAdmin); ?> 已删除。</p>
                    </div>
                <?php elseif ($deleteStatus === 'missing'): ?>
                    <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;">未找到对应的管理员记录。</p>
                    </div>
                <?php elseif ($deleteStatus === 'invalid'): ?>
                    <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;">缺少有效的管理员ID。</p>
                    </div>
                <?php elseif ($deleteStatus === 'protected'): ?>
                    <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;">系统至少需要保留 1 个启用状态的管理员账号，最后一个启用管理员不可删除。</p>
                    </div>
                <?php elseif ($deleteStatus === 'self_protected'): ?>
                    <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;">当前登录管理员不允许删除自己的账号。</p>
                    </div>
                <?php elseif ($deleteStatus === 'error'): ?>
                    <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;">管理员删除失败，请稍后重试。</p>
                    </div>
                <?php endif; ?>
                <?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;">请确认 `admins` 表已创建，且 `app/config.php` 数据库配置正确。</p>
                    </div>
                <?php endif; ?>
                <form class="filter-row" method="get" action="/admin/user/list.php">
                    <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>
                        <th>更新时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php if (!empty($admins)): ?>
                        <?php foreach ($admins as $item): ?>
                            <tr>
                                <td><?php echo (int)$item['id']; ?></td>
                                <td><?php echo e($item['username']); ?></td>
                                <td><?php echo e($item['display_name']); ?></td>
                                <td><?php echo e(admin_role_label(isset($item['role']) ? $item['role'] : 'user')); ?></td>
                                <td><span class="badge <?php echo e(domain_badge_class($item['status'])); ?>"><?php echo e(isset($statusOptions[$item['status']]) ? $statusOptions[$item['status']] : $item['status']); ?></span></td>
                                <td><?php echo e($item['created_at']); ?></td>
                                <td><?php echo e($item['updated_at']); ?></td>
                                <td>
                                    <a href="/admin/user/edit.php?id=<?php echo (int)$item['id']; ?>">编辑</a>
                                    <?php if (!isset($item['role']) || $item['role'] !== 'super_admin'): ?>
                                    /
                                    <?php if ($item['status'] === 'enabled'): ?>
                                        <form method="post" action="/admin/user/toggle.php" style="display:inline;" onsubmit="return confirm('确认停用管理员 <?php echo e($item['username']); ?> 吗？');"><?php echo csrf_field(); ?><input type="hidden" name="id" value="<?php echo (int)$item['id']; ?>"><input type="hidden" name="status" value="disabled"><button type="submit" class="link-btn">禁用</button></form>
                                    <?php else: ?>
                                        <form method="post" action="/admin/user/toggle.php" style="display:inline;" onsubmit="return confirm('确认启用管理员 <?php echo e($item['username']); ?> 吗？');"><?php echo csrf_field(); ?><input type="hidden" name="id" value="<?php echo (int)$item['id']; ?>"><input type="hidden" name="status" value="enabled"><button type="submit" class="link-btn">启用</button></form>
                                    <?php endif; ?>
                                    /
                                    <form method="post" action="/admin/user/delete.php" style="display:inline;" onsubmit="return confirm('确认删除管理员 <?php echo e($item['username']); ?> 吗？');"><?php echo csrf_field(); ?><input type="hidden" name="id" value="<?php echo (int)$item['id']; ?>"><button type="submit" class="link-btn" style="color:#dc2626;">删除</button></form>
                                    <?php endif; ?>
                                    / <a href="/admin/user/logs.php?id=<?php echo (int)$item['id']; ?>">日志</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="8" class="empty-note"><?php echo $dbError === '' ? '暂无管理员数据，请先检查初始化 SQL 是否已执行。' : '当前无法加载管理员数据。'; ?></td>
                        </tr>
                    <?php endif; ?>
                    </tbody>
                </table>
                <div class="table-note">当前已支持管理员新增、编辑、启用/停用、删除与日志查看。系统至少保留 1 个启用管理员，后续可继续增加角色权限功能。</div>
            </section>
        </div>
    </main>
</div>
<script src="/assets/js/admin-ui.js"></script>
</body>
</html>
