<?php
$title = 'Pilih Paket';
$description = 'Halaman client untuk membeli paket aktif.';
$errors = flash('errors', []);
$selectedProduct = $selectedProduct ?? null;
$paymentSettings = $paymentSettings ?? [];
?>
<section class="client-section">
    <?php if ($status = flash('status')): ?>
        <div class="alert alert-success"><?= e($status) ?></div>
    <?php endif; ?>

    <?php if ($errors !== []): ?>
        <div class="alert alert-error">
            <strong>Periksa input:</strong>
            <ul class="field-errors">
                <?php foreach ($errors as $messages): ?>
                    <?php foreach ((array) $messages as $message): ?>
                        <li><?= e($message) ?></li>
                    <?php endforeach; ?>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

    <div class="card panel-stack">
        <div class="section-head">
            <div>
                <h2 class="panel-title">Paket aktif</h2>
                <p class="panel-text">Pilih paket untuk menampilkan instruksi pembayaran dari admin sebelum konfirmasi pembelian.</p>
            </div>
        </div>

        <div class="purchase-layout">
            <div class="product-grid">
                <?php foreach ($products as $product): ?>
                    <article class="product-card <?= ((int) ($selectedProduct['id'] ?? 0) === (int) $product['id']) ? 'product-card-active' : '' ?>">
                        <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-primary" href="<?= e(url('/dashboard/purchases?product_id=' . (string) $product['id'])) ?>">Pilih Paket</a>
                        </div>
                    </article>
                <?php endforeach; ?>
            </div>

            <aside class="payment-panel card panel-stack">
                <div class="section-head">
                    <div>
                        <h2 class="panel-title">Detail Pembayaran</h2>
                        <p class="panel-text">Informasi ini mengikuti pengaturan yang diupload admin.</p>
                    </div>
                </div>

                <?php if ($selectedProduct): ?>
                    <div class="payment-summary">
                        <strong><?= e($selectedProduct['nama_produk']) ?></strong>
                        <span>Rp<?= e(number_format((float) $selectedProduct['harga'], 0, ',', '.')) ?></span>
                    </div>

                    <div class="payment-method">
                        <span class="settings-meta-label">Nomor Rekening</span>
                        <strong><?= e($paymentSettings['bank_account_number'] ?? '-') ?></strong>
                    </div>

                    <div class="payment-method">
                        <span class="settings-meta-label">Barcode Pembayaran</span>
                        <?php if (! empty($paymentSettings['payment_barcode'])): ?>
                            <div class="settings-preview-image settings-barcode">
                                <img src="<?= e(url('/media?path=' . rawurlencode((string) $paymentSettings['payment_barcode']))) ?>" alt="Barcode pembayaran">
                            </div>
                        <?php else: ?>
                            <div class="settings-placeholder">Barcode belum diatur admin</div>
                        <?php endif; ?>
                    </div>

                    <form action="<?= e(url('/dashboard/purchases')) ?>" method="post">
                        <?= csrf() ?>
                        <input type="hidden" name="product_id" value="<?= e((string) $selectedProduct['id']) ?>">
                        <button class="button-primary" type="submit">Konfirmasi Pembelian</button>
                    </form>
                <?php else: ?>
                    <div class="client-empty">
                        <p class="panel-text">Pilih salah satu paket di sebelah kiri untuk melihat rekening, barcode, dan langkah pembayaran berikutnya.</p>
                    </div>
                <?php endif; ?>
            </aside>
        </div>
    </div>

    <div class="card panel-stack client-table-card">
        <div class="section-head">
            <div>
                <h2 class="panel-title">Riwayat pembelian</h2>
                <p class="panel-text">Semua transaksi milik akun kamu akan muncul di sini.</p>
            </div>
            <a class="inline-link" href="<?= e(url('/dashboard/status')) ?>">Buka status</a>
        </div>

        <div class="admin-table-wrap">
            <table class="admin-table">
                <thead>
                <tr class="admin-table-head">
                    <th>Paket</th>
                    <th>Harga</th>
                    <th>Status</th>
                    <th>Bukti</th>
                    <th>Tanggal</th>
                </tr>
                </thead>
                <tbody>
                <?php if ($purchases === []): ?>
                    <tr>
                        <td colspan="5">Belum ada pembelian.</td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($purchases as $purchase): ?>
                        <tr>
                            <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_label((string) $purchase['status'])) ?></span></td>
                            <td><?= e($purchase['bukti_transfer'] ? basename((string) $purchase['bukti_transfer']) : '-') ?></td>
                            <td><?= e($purchase['created_at']) ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>
