<?php

$pageTitle = 'Admin Dashboard - PET SPA';
$activeAdminPage = 'dashboard';

require BASE_PATH . '/app/views/layouts/header.php';
require BASE_PATH . '/app/views/layouts/navbar.php';

$statLabels = [
    'users' => 'Tổng người dùng',
    'customers' => 'Khách hàng',
    'owners' => 'Owner',
    'admins' => 'Admin',
    'active_users' => 'Đang hoạt động',
    'locked_users' => 'Đã khóa',
    'posts' => 'Bài viết'
];
?>

<main class="admin-area">
    <div class="container-fluid">
        <div class="row g-4">
            <div class="col-lg-3 col-xl-2">
                <?php require BASE_PATH . '/app/views/layouts/admin-sidebar.php'; ?>
            </div>

            <div class="col-lg-9 col-xl-10">
                <div class="mb-4">
                    <h1 class="h3 mb-1">Admin Dashboard</h1>
                    <p class="text-muted mb-0">Tổng quan hệ thống PET SPA.</p>
                </div>

                <div class="row g-3 mb-4">
                    <?php foreach ($statLabels as $key => $label): ?>
                        <div class="col-6 col-md-4 col-xl-3">
                            <div class="card h-100">
                                <div class="card-body">
                                    <div class="text-muted small"><?= htmlspecialchars($label) ?></div>
                                    <div class="fs-4 fw-semibold">
                                        <?= number_format((int) $stats[$key]) ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>

                <div class="row g-4">
                    <section class="col-xl-6">
                        <div class="card h-100">
                            <div class="card-header d-flex justify-content-between align-items-center">
                                <span>Người dùng mới</span>
                                <a href="<?= BASE_URL ?>/?url=admin/users" class="btn btn-sm btn-outline-primary">Xem tất cả</a>
                            </div>
                            <div class="table-responsive">
                                <table class="table table-sm mb-0 align-middle">
                                    <thead><tr><th>Tên</th><th>Vai trò</th><th>Trạng thái</th></tr></thead>
                                    <tbody>
                                        <?php foreach ($recentUsers as $user): ?>
                                            <tr>
                                                <td>
                                                    <a href="<?= BASE_URL ?>/?url=admin/user&id=<?= (int) $user['id'] ?>">
                                                        <?= htmlspecialchars($user['full_name']) ?>
                                                    </a>
                                                </td>
                                                <td><?= htmlspecialchars($user['role']) ?></td>
                                                <td><?= htmlspecialchars($user['status']) ?></td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </section>

                    <section class="col-xl-6">
                        <div class="card h-100">
                            <div class="card-header d-flex justify-content-between align-items-center">
                                <span>Activity gần đây</span>
                                <a href="<?= BASE_URL ?>/?url=admin/activity-logs" class="btn btn-sm btn-outline-primary">Xem tất cả</a>
                            </div>
                            <ul class="list-group list-group-flush">
                                <?php if ($recentLogs === []): ?>
                                    <li class="list-group-item text-muted">Chưa có activity log.</li>
                                <?php endif; ?>
                                <?php foreach ($recentLogs as $log): ?>
                                    <li class="list-group-item">
                                        <div class="fw-semibold"><?= htmlspecialchars($log['action']) ?></div>
                                        <small class="text-muted">
                                            <?= htmlspecialchars($log['actor_name'] ?? 'System') ?> ·
                                            <?= htmlspecialchars($log['created_at']) ?>
                                        </small>
                                    </li>
                                <?php endforeach; ?>
                            </ul>
                        </div>
                    </section>
                </div>
            </div>
        </div>
    </div>
</main>

<?php require BASE_PATH . '/app/views/layouts/footer.php'; ?>
