<?php
/**
 * Keranjang (Cart) — Member Page
 */
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../includes/auth.php';
requireMember();

$userId = $_SESSION['user_id'];

// Fetch cart items
$stmt = $pdo->prepare("
    SELECT kr.id as cart_id, kr.kelas_id, k.nama_kelas, k.jenis_olahraga, k.instruktur, 
           k.hari, k.jam_mulai, k.jam_selesai, k.harga, k.intensitas
    FROM keranjang kr 
    JOIN kelas k ON kr.kelas_id = k.id 
    WHERE kr.user_id = ?
    ORDER BY kr.created_at DESC
");
$stmt->execute([$userId]);
$items = $stmt->fetchAll();

$total = array_sum(array_column($items, 'harga'));

$imageMap = [
    'yoga'      => '/assets/images/yoga.png',
    'zumba'     => '/assets/images/zumba.png',
    'muay thai' => '/assets/images/muay_thai.png',
    'gym'       => '/assets/images/gym.png',
    'pilates'   => '/assets/images/pilates.png',
    'spinning'  => '/assets/images/spinning.png',
    'crossfit'  => '/assets/images/gym.png',
];

function getClassImage($jenis) {
    global $imageMap;
    return $imageMap[strtolower($jenis)] ?? '/assets/images/gym.png';
}

require_once __DIR__ . '/../includes/header.php';
?>

<div class="max-w-4xl mx-auto px-4 py-8">
    <div class="mb-8" style="opacity:0;animation:fadeUp 0.5s ease forwards;">
        <h1 class="text-3xl font-black mb-2">Keranjang <span class="text-volt">Kelas</span></h1>
        <p class="text-muted text-sm"><?= count($items) ?> kelas dipilih</p>
    </div>

    <div id="cart-list">
        <?php if (empty($items)): ?>
            <div class="empty-state">
                <div class="emoji">🛒</div>
                <p class="text-lg font-bold mb-2">Keranjang Kosong</p>
                <p class="text-sm text-muted mb-4">Belum ada kelas yang dipilih</p>
                <a href="<?= BASE_URL ?>/member/katalog.php" class="btn-volt">Lihat Kelas</a>
            </div>
        <?php else: ?>
            <div class="space-y-4">
                <?php foreach ($items as $i => $item): 
                    $imgSrc = getClassImage($item['jenis_olahraga']);
                    $delay = 'd' . min($i + 1, 12);
                ?>
                <div class="cart-item card flex items-center gap-4 animate-fadeUp <?= $delay ?>" id="cart-row-<?= $item['kelas_id'] ?>">
                    <!-- Class Image Thumbnail -->
                    <img src="<?= $imgSrc ?>" class="w-16 h-16 object-cover rounded-xl border border-border" alt="<?= htmlspecialchars($item['nama_kelas']) ?>">

                    <!-- Info -->
                    <div class="flex-1 min-w-0">
                        <h3 class="font-bold text-white truncate"><?= htmlspecialchars($item['nama_kelas']) ?></h3>
                        <p class="text-xs text-muted">👤 <?= htmlspecialchars($item['instruktur']) ?> · 📅 <?= htmlspecialchars($item['hari']) ?> · <?= date('H:i', strtotime($item['jam_mulai'])) ?>-<?= date('H:i', strtotime($item['jam_selesai'])) ?></p>
                    </div>

                    <!-- Price -->
                    <div class="text-right flex-shrink-0">
                        <span class="cart-item-price text-lg font-bold text-volt" data-harga="<?= $item['harga'] ?>">
                            Rp <?= number_format($item['harga'], 0, ',', '.') ?>
                        </span>
                    </div>

                    <!-- Remove button -->
                    <button onclick="removeFromCart(<?= $item['kelas_id'] ?>, document.getElementById('cart-row-<?= $item['kelas_id'] ?>'))"
                            class="flex-shrink-0 w-10 h-10 flex items-center justify-center text-red-400 hover:bg-red-500/10 rounded-lg transition-colors"
                            title="Hapus">
                        ✕
                    </button>
                </div>
                <?php endforeach; ?>
            </div>

            <!-- Total & Checkout -->
            <div class="mt-8 card" style="opacity:0;animation:fadeUp 0.5s ease 0.3s forwards;">
                <div class="flex items-center justify-between mb-6">
                    <span class="text-muted font-bold">Total Biaya</span>
                    <span class="text-2xl font-black text-volt" id="cart-total">Rp <?= number_format($total, 0, ',', '.') ?></span>
                </div>
                <a href="<?= BASE_URL ?>/member/checkout.php" class="btn-volt w-full block text-center text-base py-4 pulse">
                    Lanjut Checkout →
                </a>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php require_once __DIR__ . '/../includes/footer.php'; ?>
