<?php
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../includes/auth.php';
requireMember();

$userId = $_SESSION['user_id'];
$stmt = $pdo->prepare("SELECT t.*, (SELECT COUNT(*) FROM detail_transaksi WHERE transaksi_id = t.id) as jumlah_kelas FROM transaksi t WHERE t.user_id = ? ORDER BY t.created_at DESC");
$stmt->execute([$userId]);
$transaksiList = $stmt->fetchAll();

require_once __DIR__ . '/../includes/header.php';
?>
<div class="max-w-5xl mx-auto px-4 py-8">
    <div class="mb-8 animate-fadeUp"><h1 class="text-3xl font-black mb-2">Riwayat <span class="text-volt">Transaksi</span></h1><p class="text-muted text-sm">Semua riwayat booking kelas Anda</p></div>

    <?php if (empty($transaksiList)): ?>
        <div class="empty-state"><div class="emoji">📋</div><p class="text-lg font-bold mb-2">Belum Ada Transaksi</p><p class="text-sm text-muted mb-4">Anda belum pernah melakukan booking</p><a href="<?= BASE_URL ?>/member/katalog.php" class="btn-volt">Lihat Kelas</a></div>
    <?php else: ?>
        <div class="space-y-4">
        <?php foreach ($transaksiList as $i => $trx):
            $delay = 'd' . min($i+1, 12);
            $detailStmt = $pdo->prepare("SELECT dt.*, k.nama_kelas FROM detail_transaksi dt JOIN kelas k ON dt.kelas_id = k.id WHERE dt.transaksi_id = ?");
            $detailStmt->execute([$trx['id']]);
            $details = $detailStmt->fetchAll();
        ?>
        <div class="card animate-fadeUp <?= $delay ?>">
            <div class="flex flex-wrap items-start justify-between gap-4 mb-4">
                <div>
                    <span class="text-xs text-muted">Transaksi #<?= $trx['id'] ?></span>
                    <p class="text-sm text-muted"><?= date('d M Y, H:i', strtotime($trx['created_at'])) ?></p>
                </div>
                <span class="badge-<?= $trx['status'] ?>"><?= ucfirst($trx['status']) ?></span>
            </div>
            <div class="space-y-2 mb-4">
                <?php foreach ($details as $d): ?>
                <div class="flex justify-between text-sm"><span class="text-muted"><?= htmlspecialchars($d['nama_kelas']) ?></span><span>Rp <?= number_format($d['harga_satuan'],0,',','.') ?></span></div>
                <?php endforeach; ?>
            </div>
            <div class="flex items-center justify-between pt-4 border-t border-border">
                <span class="font-bold text-volt text-lg">Rp <?= number_format($trx['total_harga'],0,',','.') ?></span>
                <?php if ($trx['status'] === 'pending' && empty($trx['bukti_bayar'])): ?>
                    <a href="<?= BASE_URL ?>/member/upload_bukti.php?transaksi_id=<?= $trx['id'] ?>" class="btn-volt text-xs px-4 py-2">Upload Bukti</a>
                <?php endif; ?>
            </div>
        </div>
        <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>
<?php require_once __DIR__ . '/../includes/footer.php'; ?>
