<?php
$title = 'Status Pembelian';
$description = 'Lihat semua status pembelian milik client.';
$paymentSettings = $paymentSettings ?? [];
$selectedPurchase = flash('purchase_selected');
$latestPurchase = is_array($latestPurchase ?? null) ? $latestPurchase : null;
$statusFilter = (string) ($statusFilter ?? 'all');
$statusFilterOptions = [
    'all' => 'Semua status',
    'active' => 'Aktif',
    'inactive' => 'Tidak aktif',
    'pending' => 'Pending',
    'verified' => 'Diverifikasi',
    'completed' => 'Selesai',
    'rejected' => 'Ditolak',
];
$latestStatus = is_array($latestPurchase) ? (string) ($latestPurchase['status'] ?? '') : '';
$latestProductName = is_array($latestPurchase) ? (string) ($latestPurchase['product_name'] ?? '-') : '-';
$latestHasProof = is_array($latestPurchase) && ! empty($latestPurchase['bukti_transfer']);
$showUploadAction = is_array($selectedPurchase) && ! $latestHasProof;
$noticeTitle = match ($latestStatus) {
    'completed' => 'Pembelian Anda telah selesai.',
    'verified' => 'Pembayaran Anda telah diverifikasi.',
    default => flash('purchase_notice_title') ?: 'Konfirmasi pembelian berhasil.',
};
$noticeMessage = match ($latestStatus) {
    'completed' => 'Pembelian Anda dengan paket ' . $latestProductName . ' telah selesai. Terima kasih, layanan Anda sudah diproses.',
    'verified' => 'Pembayaran Anda sudah diverifikasi. Admin sedang melanjutkan proses instalasi jaringan di handphone Anda.',
    default => flash('status') ?: 'Bukti pembayaran sudah diterima. Mohon tunggu, admin sedang memverifikasi pembayaran dan menyiapkan proses instalasi jaringan di handphone Anda.',
};
?>
<section class="client-section">
    <?php if ($latestHasProof || in_array($latestStatus, ['verified', 'completed'], true)): ?>
        <div class="alert alert-success client-status-alert">
            <strong><?= e($noticeTitle) ?></strong>
            <span><?= e($noticeMessage) ?></span>
        </div>
    <?php endif; ?>

    <div class="card panel-stack client-next-step">
        <div class="section-head">
            <div>
                <span class="eyebrow">Langkah Berikutnya</span>
                <?php if ($latestStatus === 'completed'): ?>
                    <h2 class="panel-title">Pembelian paket Anda telah selesai</h2>
                    <p class="panel-text">
                        Pembelian Anda dengan paket <?= e($latestProductName) ?> telah selesai. Informasi detailnya tetap bisa dipantau dari tabel status pembelian.
                    </p>
                <?php elseif ($latestStatus === 'verified' || $latestHasProof): ?>
                    <h2 class="panel-title">Mohon tunggu, kami sedang memproses instalasi jaringan di handphone Anda</h2>
                    <p class="panel-text">
                        Bukti pembayaran sudah diterima. Admin akan memverifikasi pembayaran dan melanjutkan proses instalasi jaringan.
                    </p>
                <?php elseif ($showUploadAction): ?>
                    <h2 class="panel-title">Selesaikan pembayaran paket Anda</h2>
                    <p class="panel-text">
                        Gunakan nomor rekening atau barcode di bawah ini, lalu unggah bukti pembayaran agar admin bisa memproses pesanan.
                    </p>
                <?php else: ?>
                    <h2 class="panel-title">Pantau status pembelian Anda</h2>
                    <p class="panel-text">
                        Riwayat pembelian dan perubahan status akan muncul di halaman ini.
                    </p>
                <?php endif; ?>
            </div>
        </div>

        <?php if ($selectedPurchase): ?>
            <div class="next-step-product">
                <strong>Paket dipilih:</strong>
                <span><?= e($selectedPurchase['product_name'] ?? '-') ?></span>
            </div>
        <?php endif; ?>
    </div>

    <div class="card panel-stack">
        <div class="section-head">
            <div>
                <h2 class="panel-title">Instruksi Pembayaran</h2>
                <p class="panel-text">Gunakan rekening atau barcode yang sudah diatur admin agar transaksi selaras dengan pembelian.</p>
            </div>
        </div>

        <div class="payment-layout">
            <div class="payment-method">
                <span class="settings-meta-label">Nama Perusahaan</span>
                <strong><?= e($paymentSettings['company_name'] ?? app_name()) ?></strong>
            </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>
        </div>

        <?php if ($showUploadAction): ?>
            <div class="cta-row">
                <a class="button-primary" href="<?= e(url('/dashboard/upload')) ?>">Upload bukti pembayaran</a>
            </div>
        <?php endif; ?>
    </div>

    <div class="card panel-stack">
        <div class="section-head">
            <div>
                <h2 class="panel-title">Status pembelian</h2>
                <p class="panel-text">Filter transaksi berdasarkan status agar lebih mudah dipantau.</p>
            </div>
        </div>

        <form class="status-filter-form" action="<?= e(url('/dashboard/status')) ?>" method="get">
            <label for="status_filter">Filter status</label>
            <select id="status_filter" name="status" class="field-select">
                <?php foreach ($statusFilterOptions as $value => $label): ?>
                    <option value="<?= e($value) ?>" <?= $statusFilter === $value ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
            <button class="button-primary" type="submit">Terapkan</button>
            <?php if ($statusFilter !== 'all'): ?>
                <a class="button-secondary" href="<?= e(url('/dashboard/status')) ?>">Reset</a>
            <?php endif; ?>
        </form>

        <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>Dibuat</th>
                    <th>Diperbarui</th>
                </tr>
                </thead>
                <tbody>
                <?php if ($purchases === []): ?>
                    <tr>
                        <td colspan="6">Belum ada pembelian untuk filter ini.</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>
                            <td><?= e($purchase['updated_at']) ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>
