<?php
$title = 'Pembelian Admin';
?>
<section class="admin-section">
    <div class="container">
        <div class="hero-panel">
            <span class="eyebrow">Manajemen Pembelian</span>
            <h1>Pembelian</h1>
            <p class="lead">Verifikasi atau tolak pembelian yang masuk dari client.</p>

            <?php if ($status = flash('status')): ?>
                <div class="alert alert-success"><?= e($status) ?></div>
            <?php endif; ?>
        </div>

        <div class="card panel-stack admin-stack">
            <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>Bukti</th>
                        <th>Detail</th>
                        <th>Aksi</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($purchases as $purchase): ?>
                        <tr>
                            <td>
                                <?= e($purchase['user_name']) ?><br>
                                <small><?= e($purchase['user_email']) ?></small>
                            </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>
                                <?php if (! empty($purchase['bukti_transfer'])): ?>
                                    <span class="status-badge status-verified">Tersedia</span>
                                <?php else: ?>
                                    <span class="status-badge status-rejected">Belum ada</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if (! empty($purchase['bukti_transfer'])): ?>
                                    <?php
                                    $proofPath = (string) $purchase['bukti_transfer'];
                                    $proofExt = strtolower(pathinfo($proofPath, PATHINFO_EXTENSION));
                                    $proofUrl = url('/media?path=' . rawurlencode($proofPath));
                                    ?>
                                    <div class="proof-preview">
                                        <?php if (in_array($proofExt, ['jpg', 'jpeg', 'png'], true)): ?>
                                            <a class="proof-preview-link" href="<?= e($proofUrl) ?>" target="_blank" rel="noopener">
                                                <img class="proof-thumb" src="<?= e($proofUrl) ?>" alt="Bukti transfer">
                                            </a>
                                        <?php else: ?>
                                            <a class="inline-link" href="<?= e($proofUrl) ?>" target="_blank" rel="noopener">Lihat bukti</a>
                                        <?php endif; ?>
                                        <span class="proof-filename"><?= e(basename($proofPath)) ?></span>
                                    </div>
                                <?php else: ?>
                                    <span class="panel-text">-</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if ($purchase['status'] === 'pending'): ?>
                                    <div class="admin-actions">
                                        <form action="<?= e(url('/admin/purchases/verify')) ?>" method="post">
                                            <?= csrf() ?>
                                            <input type="hidden" name="purchase_id" value="<?= e((string) $purchase['id']) ?>">
                                            <button class="button-primary" type="submit">Verifikasi</button>
                                        </form>
                                        <form action="<?= e(url('/admin/purchases/reject')) ?>" method="post">
                                            <?= csrf() ?>
                                            <input type="hidden" name="purchase_id" value="<?= e((string) $purchase['id']) ?>">
                                            <button class="button-secondary" type="submit">Tolak</button>
                                        </form>
                                    </div>
                                <?php elseif ($purchase['status'] === 'verified'): ?>
                                    <form action="<?= e(url('/admin/purchases/complete')) ?>" method="post">
                                        <?= csrf() ?>
                                        <input type="hidden" name="purchase_id" value="<?= e((string) $purchase['id']) ?>">
                                        <button class="button-primary" type="submit">Selesai</button>
                                    </form>
                                <?php else: ?>
                                    -
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</section>
