<?php
$title = 'Upload Bukti Transfer';
$description = 'Upload bukti transfer untuk pembelian pending.';
$errors = flash('errors', []);
$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">Pembelian pending</h2>
                <p class="panel-text">Upload bukti transfer untuk salah satu pembelian yang masih menunggu verifikasi.</p>
            </div>
            <a class="inline-link" href="<?= e(url('/dashboard/purchases')) ?>">Beli paket</a>
        </div>

        <div class="payment-layout upload-payment-info">
            <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>
        </div>

        <div class="upload-list">
            <?php if ($pendingPurchases === []): ?>
                <div class="client-empty">
                    <p class="panel-text">Belum ada pembelian pending. Silakan pilih paket terlebih dahulu.</p>
                </div>
            <?php else: ?>
                <?php foreach ($pendingPurchases as $purchase): ?>
                    <article class="upload-card">
                        <div class="upload-card-head">
                            <div>
                                <h3><?= e($purchase['product_name']) ?></h3>
                                <p class="panel-text">Rp<?= e(number_format((float) $purchase['harga'], 0, ',', '.')) ?> - dibeli <?= e($purchase['created_at']) ?></p>
                            </div>
                            <span class="status-badge status-pending"><?= e(purchase_status_label((string) $purchase['status'])) ?></span>
                        </div>

                        <?php if (! empty($purchase['bukti_transfer'])): ?>
                            <p class="panel-text">Bukti transfer sudah diunggah: <?= e(basename((string) $purchase['bukti_transfer'])) ?></p>
                        <?php else: ?>
                            <form class="upload-form" action="<?= e(url('/dashboard/upload')) ?>" method="post" enctype="multipart/form-data">
                                <?= csrf() ?>
                                <input type="hidden" name="purchase_id" value="<?= e((string) $purchase['id']) ?>">
                                <div class="field">
                                    <label for="bukti_transfer_<?= e((string) $purchase['id']) ?>">Bukti transfer</label>
                                    <input id="bukti_transfer_<?= e((string) $purchase['id']) ?>" name="bukti_transfer" type="file" accept=".jpg,.jpeg,.png,.pdf">
                                </div>
                                <div class="form-footer">
                                    <span class="form-hint">Format JPG, PNG, atau PDF. Maksimal 5 MB.</span>
                                    <button class="button-primary" type="submit">Kirim Bukti</button>
                                </div>
                            </form>
                        <?php endif; ?>
                    </article>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>
</section>
