<?php

if (!isset($_SESSION['user'])) {
    header("Location: /membership/login");
    exit;
}

require __DIR__ . '/../../app/config/database.php';

$id = $_SESSION['user']['id'];

/* ===============================
   TOTAL IURAN
================================ */
$total_iuran = 0;

$stmt = $conn->prepare("
    SELECT SUM(jumlah) 
    FROM iuran 
    WHERE member_id=? AND status='Lunas'
");

$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($total_iuran);
$stmt->fetch();
$stmt->close(); // 🔥 WAJIB (FIX ERROR)

/* ===============================
   EVENT MENDATANG
================================ */
$events = [];

$q = $conn->query("
    SELECT nama_event, tanggal, lokasi
    FROM events
    WHERE tanggal >= CURDATE()
    ORDER BY tanggal ASC
    LIMIT 5
");

if ($q) {
    while ($row = $q->fetch_assoc()) {
        $events[] = $row;
    }
}

$title = "Dashboard";
require __DIR__ . '/../layout/header.php';
require __DIR__ . '/../layout/sidebar.php';
?>

<div class="main">

    <div class="header">
        Dashboard
    </div>

    <!-- HERO -->
    <div class="hero">
        <h2>Selamat datang, <?= htmlspecialchars($_SESSION['user']['nama']) ?></h2>
        <p>Semua aktivitas member bisa kamu akses di sini</p>
    </div>

    <!-- GRID -->
    <div class="grid">

        <!-- STATUS -->
        <div class="card">
            <h3>Status Member</h3>
            <span class="badge <?= $_SESSION['user']['status'] == 'Approved' ? 'success' : 'warning' ?>">
                <?= $_SESSION['user']['status'] ?>
            </span>
        </div>

        <!-- TOTAL IURAN -->
        <div class="card">
            <h3>Total Iuran</h3>
            <p>Rp <?= number_format($total_iuran ?? 0, 0, ',', '.') ?></p>
        </div>

        <!-- 🔥 QR MEMBER -->
<div class="card">

    <h3 style="margin-bottom:15px;">Kartu Member Digital</h3>

    <?php
    $url = "https://hitasumut.org/membership/member-card?id=" . $_SESSION['user']['id'];
    ?>

    <!-- 🔥 WRAPPER -->
    <div class="member-wrapper">

        <!-- MINI CARD -->
       
        <!-- QR -->
        <div class="qr-box">
            <img src="https://api.qrserver.com/v1/create-qr-code/?size=130x130&data=<?= urlencode($url) ?>">
        </div>

        <!-- BUTTON -->
        <div class="btn-group">
            <a href="<?= $url ?>" target="_blank" class="btn btn-primary">
                Lihat Kartu
            </a>

            <button onclick="downloadMiniCard()" class="btn btn-success">
                Download PNG
            </button>
        </div>

    </div>

</div>

    </div>

    <!-- 🔥 EVENT -->
    <div class="card" style="margin-top:20px;">
        <h3>Event Mendatang</h3>

        <?php if (empty($events)): ?>
            <p>Tidak ada event</p>
        <?php else: ?>

            <table class="table">
                <tr>
                    <th>Event</th>
                    <th>Tanggal</th>
                    <th>Lokasi</th>
                </tr>

                <?php foreach ($events as $e): ?>
                <tr>
                    <td><?= htmlspecialchars($e['nama_event']) ?></td>
                    <td><?= date('d M Y', strtotime($e['tanggal'])) ?></td>
                    <td><?= htmlspecialchars($e['lokasi']) ?></td>
                </tr>
                <?php endforeach; ?>

            </table>

        <?php endif; ?>
    </div>

</div>

<?php require __DIR__ . '/../layout/footer.php'; ?>