<?php
$title = 'Dashboard Admin';
?>
<section class="admin-section">
    <div class="container admin-dashboard">
        <div class="hero-panel">
            <span class="eyebrow">Ringkasan Sistem</span>
            <h1>Dashboard Admin</h1>
            <p class="lead">
                Pantau pengguna, paket, dan pembelian dari satu tempat, lalu tindak lanjuti transaksi yang masih menunggu verifikasi.
            </p>
            <div class="cta-row admin-cta-row">
                <a class="button-primary" href="<?= e(url('/admin/products')) ?>">Kelola Produk</a>
                <a class="button-secondary" href="<?= e(url('/admin/purchases')) ?>">Review Pembelian</a>
                <a class="button-secondary" href="<?= e(url('/admin/users')) ?>">Lihat User</a>
            </div>
            <div class="meta-grid">
                <div class="meta-item">
                    <strong><?= e((string) $stats['total_users']) ?></strong>
                    <span>Total user</span>
                </div>
                <div class="meta-item">
                    <strong><?= e((string) $stats['total_clients']) ?></strong>
                    <span>Client aktif</span>
                </div>
                <div class="meta-item">
                    <strong><?= e((string) $stats['total_products']) ?></strong>
                    <span>Produk</span>
                </div>
                <div class="meta-item">
                    <strong><?= e((string) $stats['total_purchases']) ?></strong>
                    <span>Total pembelian</span>
                </div>
                <div class="meta-item">
                    <strong><?= e((string) $stats['pending_purchases']) ?></strong>
                    <span>Pending</span>
                </div>
                <div class="meta-item">
                    <strong><?= e((string) $stats['verified_purchases']) ?></strong>
                    <span>Verified</span>
                </div>
                <div class="meta-item">
                    <strong><?= e((string) $stats['completed_purchases']) ?></strong>
                    <span>Selesai</span>
                </div>
                <div class="meta-item">
                    <strong><?= e((string) $stats['rejected_purchases']) ?></strong>
                    <span>Ditolak</span>
                </div>
            </div>
        </div>

        <div class="dashboard-columns">
            <div class="card panel-stack admin-stack">
                <div class="section-head">
                    <div>
                        <h2 class="panel-title">Pembelian terbaru</h2>
                        <p class="panel-text">Transaksi yang paling baru masuk ke sistem.</p>
                    </div>
                    <a class="inline-link" href="<?= e(url('/admin/purchases')) ?>">Buka semua</a>
                </div>
                <div class="admin-table-wrap">
                    <table class="admin-table">
                        <thead>
                        <tr class="admin-table-head">
                            <th>User</th>
                            <th>Paket</th>
                            <th>Harga</th>
                            <th>Status</th>
                            <th>Waktu</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php if ($recentPurchases === []): ?>
                            <tr>
                                <td colspan="5">Belum ada pembelian.</td>
                            </tr>
                        <?php else: ?>
                            <?php foreach ($recentPurchases as $purchase): ?>
                                <tr>
                                    <td><?= e($purchase['user_name']) ?></td>
                                    <td><?= e($purchase['product_name']) ?></td>
                                    <td>Rp<?= e(number_format((float) $purchase['harga'], 0, ',', '.')) ?></td>
                                    <td><span class="status-badge status-<?= e($purchase['status']) ?>"><?= e($purchase['status']) ?></span></td>
                                    <td><?= e($purchase['created_at']) ?></td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="card panel-stack admin-stack">
                <div class="section-head">
                    <div>
                        <h2 class="panel-title">User terbaru</h2>
                        <p class="panel-text">Daftar singkat user yang baru dibuat.</p>
                    </div>
                    <a class="inline-link" href="<?= e(url('/admin/users')) ?>">Buka semua</a>
                </div>
                <div class="admin-table-wrap">
                    <table class="admin-table">
                        <thead>
                        <tr class="admin-table-head">
                            <th>Nama</th>
                            <th>Email</th>
                            <th>Role</th>
                            <th>Terdaftar</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php if ($recentUsers === []): ?>
                            <tr>
                                <td colspan="4">Belum ada user.</td>
                            </tr>
                        <?php else: ?>
                            <?php foreach ($recentUsers as $user): ?>
                                <tr>
                                    <td><?= e($user['nama']) ?></td>
                                    <td><?= e($user['email']) ?></td>
                                    <td><span class="status-badge status-<?= e($user['role']) ?>"><?= e($user['role']) ?></span></td>
                                    <td><?= e($user['created_at']) ?></td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</section>
