<?php
$title = 'Dashboard Client';
$description = 'Dashboard client untuk melihat ringkasan akun, paket aktif, dan status pembelian.';
$latestStatus = $recentPurchase['status'] ?? null;
$latestCompleted = is_array($recentPurchase ?? null) && ($recentPurchase['status'] ?? '') === 'completed';
?>
<section class="client-overview">
    <?php if ($latestCompleted): ?>
        <div class="alert alert-success client-status-alert">
            <strong>Pembelian Anda telah selesai.</strong>
            <span>
                Pembelian Anda dengan paket <?= e($recentPurchase['product_name'] ?? '-') ?> telah selesai. Terima kasih, layanan Anda sudah diproses.
            </span>
        </div>
    <?php endif; ?>

    <div class="client-overview-grid">
        <article class="card panel-stack client-hero-card">
            <span class="eyebrow">Ringkasan Akun</span>
            <h2>Halo, <?= e($user['nama'] ?? 'Client') ?>.</h2>
            <p class="lead">
                Dari sini kamu bisa memilih paket, upload bukti transfer, dan memantau status pembelian dengan cepat.
            </p>
            <div class="cta-row">
                <a class="button-primary" href="<?= e(url('/dashboard/purchases')) ?>">Beli Paket</a>
                <a class="button-secondary" href="<?= e(url('/dashboard/upload')) ?>">Upload Bukti</a>
                <a class="button-secondary" href="<?= e(url('/dashboard/status')) ?>">Lihat Status</a>
            </div>
            <div class="client-stat-grid">
                <div class="client-stat-card">
                    <strong><?= e((string) ($summary['total_purchases'] ?? 0)) ?></strong>
                    <span>Total pembelian</span>
                </div>
                <div class="client-stat-card">
                    <strong><?= e((string) ($summary['pending_purchases'] ?? 0)) ?></strong>
                    <span>Pending</span>
                </div>
                <div class="client-stat-card">
                    <strong><?= e((string) ($summary['verified_purchases'] ?? 0)) ?></strong>
                    <span>Diverifikasi</span>
                </div>
                <div class="client-stat-card">
                    <strong><?= e((string) ($summary['completed_purchases'] ?? 0)) ?></strong>
                    <span>Selesai</span>
                </div>
            </div>
        </article>

        <aside class="client-side-stack">
            <section class="card panel-stack">
                <h2 class="panel-title">Status pembelian terbaru</h2>
                <?php if ($recentPurchase): ?>
                    <p class="panel-text">
                        Paket <strong><?= e($recentPurchase['product_name']) ?></strong>
                        sekarang berstatus
                        <span class="status-badge status-<?= e($recentPurchase['status']) ?>"><?= e(purchase_status_label((string) $recentPurchase['status'])) ?></span>
                    </p>
                    <ul class="list">
                        <li><span class="dot"></span><span>Harga: Rp<?= e(number_format((float) $recentPurchase['harga'], 0, ',', '.')) ?></span></li>
                        <li><span class="dot"></span><span>Dibuat: <?= e($recentPurchase['created_at']) ?></span></li>
                    </ul>
                <?php else: ?>
                    <p class="panel-text">Belum ada pembelian. Silakan pilih paket di halaman paket.</p>
                <?php endif; ?>
            </section>

            <section class="card panel-stack">
                <h2 class="panel-title">Informasi akun</h2>
                <ul class="list">
                    <li><span class="dot"></span><span>Nama: <?= e($user['nama'] ?? '-') ?></span></li>
                    <li><span class="dot"></span><span>Email: <?= e($user['email'] ?? '-') ?></span></li>
                    <li><span class="dot"></span><span>WhatsApp: <?= e($user['whatsapp'] ?? '-') ?></span></li>
                </ul>
            </section>
        </aside>
    </div>

    <div class="card panel-stack client-preview-card">
        <div class="section-head">
            <div>
                <h2 class="panel-title">Paket aktif</h2>
                <p class="panel-text">Daftar paket yang bisa kamu pilih saat ini.</p>
            </div>
            <a class="inline-link" href="<?= e(url('/dashboard/purchases')) ?>">Lihat semua paket</a>
        </div>

        <div class="product-grid">
            <?php foreach (array_slice($products, 0, 4) as $product): ?>
                <article class="product-card">
                    <div class="product-card-top">
                        <h3><?= e($product['nama_produk']) ?></h3>
                        <?php if ($product['nama_produk'] === 'Jaringan Super Top'): ?>
                            <span class="product-recommendation">Rekomendasi</span>
                        <?php endif; ?>
                    </div>
                    <p class="panel-text"><?= e($product['deskripsi'] ?? '-') ?></p>
                    <div class="product-card-footer">
                        <strong>Rp<?= e(number_format((float) $product['harga'], 0, ',', '.')) ?></strong>
                        <a class="button-secondary" href="<?= e(url('/dashboard/purchases')) ?>">Pilih</a>
                    </div>
                </article>
            <?php endforeach; ?>
        </div>
    </div>
</section>
