<?php
session_start();
include($_SERVER['DOCUMENT_ROOT']."/SEAT/config/db.php");

/* ================= SESSION CHECK ================= */
if(!isset($_SESSION['id'], $_SESSION['role'])){
    exit('<div class="regCard"><div class="msg error">Session expired</div></div>');
}

$user_id = (int)$_SESSION['id'];
$role    = $_SESSION['role'];
$BASE    = "/SEAT";

$event_id = (int)($_GET['event_id'] ?? 0);

if($event_id <= 0){
    exit('<div class="regCard"><div class="msg error">Invalid event</div></div>');
}

/* ================= EVENT ================= */
$stmt = $conn->prepare("
SELECT 
    e.*,
    o.club_name,
    b.account_holder_name,
    b.bank_name,
    b.account_number
FROM events e
LEFT JOIN organizers o ON e.organizer_id = o.id
LEFT JOIN users u ON o.user_id = u.id
LEFT JOIN bank_accounts b ON b.user_id = u.id
WHERE e.id = ?
LIMIT 1
");

$stmt->bind_param("i", $event_id);
$stmt->execute();
$event = $stmt->get_result()->fetch_assoc();

if(!$event){
    exit('<div class="regCard"><div class="msg error">Event not found</div></div>');
}

/* =====================================================
   PARTICIPANT (FIXED JOIN + SAFE DEBUG)
===================================================== */

$userQ = $conn->prepare("
SELECT 
    p.full_name,
    p.matric_no,
    p.staff_no,
    p.phone,
    u.email,
    u.id AS user_id_check
FROM users u
LEFT JOIN participants p ON p.user_id = u.id
WHERE u.id = ?
LIMIT 1
");

$userQ->bind_param("i", $user_id);
$userQ->execute();
$user = $userQ->get_result()->fetch_assoc();

/* DEBUG (TEMP - REMOVE LATER) */
// var_dump($user); exit;

if(!$user || !$user['email']){
    exit('<div class="regCard"><div class="msg error">User profile not found</div></div>');
}
?>

<link rel="stylesheet" href="<?= $BASE ?>/assets/css/participant/register3.css">
<link rel="stylesheet" href="<?= $BASE ?>/assets/css/participant/displayTicket.css">

<div class="regCard" id="mainCard">

    <div class="regClose" onclick="closeModal()">×</div>

    <div class="modalHeader">
        <h2 class="regTitle">Event Registration</h2>
        <p class="regClub"><?= htmlspecialchars($event['club_name'] ?? '-') ?></p>
    </div>

    <div class="infoWrap">

        <!-- EVENT -->
        <div class="infoCard">
            <div class="cardHead">Event Information</div>

            <div class="infoGrid">

                <div class="infoRow fullRow">
                    <span>Event</span>
                    <b><?= htmlspecialchars($event['title']) ?></b>
                </div>

                <div class="infoRow">
                    <span>Date</span>
                    <b><?= date("d M Y", strtotime($event['event_date'])) ?></b>
                </div>

                <div class="infoRow">
                    <span>Time</span>
                    <b><?= date("h:i A", strtotime($event['start_time'])) ?></b>
                </div>

                <div class="infoRow">
                    <span>Venue</span>
                    <b><?= htmlspecialchars($event['venue'] ?: $event['mode']) ?></b>
                </div>

                <div class="infoRow">
                    <span>Fee</span>
                    <b>
                        <?= $event['fee_type'] === "Free"
                            ? "Free Event"
                            : "RM " . number_format($event['fee_amount'], 2) ?>
                    </b>
                </div>

            </div>
        </div>

        <!-- USER -->
        <div class="infoCard">
            <div class="cardHead">Participant Info</div>

            <input class="regInput" readonly
                   value="<?= htmlspecialchars($user['full_name'] ?? '-') ?>">

            <input class="regInput" readonly
                   value="<?= htmlspecialchars($user['matric_no'] ?? $user['staff_no'] ?? '-') ?>">

            <input class="regInput" readonly
                   value="<?= htmlspecialchars($user['email'] ?? '-') ?>">

        </div>

    </div>

    <div id="regResult"></div>

    <div class="regActions">

        <button class="regBtn primary"
                id="mainBtn"
                onclick="submitRegisterFlow(<?= $event_id ?>)">

            <?= $event['fee_type'] === "Free"
                ? "✔ Confirm Registration"
                : "💳 Proceed Payment" ?>

        </button>

        <button class="regBtn light" onclick="closeModal()">Cancel</button>

    </div>

</div>

<script src="<?= $BASE ?>/modules/Event/participant/register.js"></script>