<?php
session_start();

error_reporting(E_ALL);
ini_set('display_errors',1);

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

if(!isset($_SESSION['id'])){
    die("Session expired");
}

$user_id = (int)$_SESSION['id'];

/* =========================================
   GET EVENTS (LEFT PANEL)
========================================= */

$stmt = $conn->prepare("
SELECT
    e.id,
    e.title,
    e.event_date,
    e.start_time,
    o.club_name
FROM event_registrations r
JOIN events e ON r.event_id = e.id
LEFT JOIN organizers o ON e.organizer_id = o.id
WHERE r.user_id = ?
ORDER BY e.event_date DESC
");

$stmt->bind_param("i",$user_id);
$stmt->execute();

$events = $stmt->get_result();

/* =========================================
   SELECT EVENT
========================================= */

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

if($event_id <= 0 && $events->num_rows > 0){
    $events->data_seek(0);
    $first = $events->fetch_assoc();
    $event_id = $first['id'];
    $events->data_seek(0);
}

/* =========================================
   SECURITY CHECK
========================================= */

if($event_id > 0){

    $check = $conn->prepare("
    SELECT 1
    FROM event_registrations
    WHERE user_id = ?
    AND event_id = ?
    LIMIT 1
    ");

    $check->bind_param("ii",$user_id,$event_id);
    $check->execute();

    if($check->get_result()->num_rows === 0){
        die("Unauthorized access");
    }
}

/* =========================================
   GET ANNOUNCEMENTS
========================================= */

$messages = [];

if($event_id > 0){

    $msgStmt = $conn->prepare("
    SELECT
        m.*,
        e.title,
        o.club_name
    FROM event_messages m
    JOIN events e ON m.event_id = e.id
    LEFT JOIN organizers o ON e.organizer_id = o.id
    WHERE m.event_id = ?
    ORDER BY m.created_at DESC
    ");

    $msgStmt->bind_param("i",$event_id);
    $msgStmt->execute();

    $messages = $msgStmt->get_result();
}

include("../../templates/header.php");
include("../../templates/sidebar.php");
?>

<link rel="stylesheet" href="/SEAT/assets/css/participant/announcement.css">

<div class="main">

    <div class="layout">

        <!-- ================= LEFT PANEL ================= -->
        <div class="leftPanel">

            <div class="panelHeader">
                <h2>Announcements</h2>
                <p>Your registered events</p>
            </div>

            <div class="eventList">

                <?php while($e = $events->fetch_assoc()): ?>

                    <a href="?event_id=<?= $e['id'] ?>"
                       class="eventCard <?= ($event_id == $e['id']) ? 'active' : '' ?>">

                        <div class="eventContent">

                            <div class="title">
                                <?= htmlspecialchars($e['title']) ?>
                            </div>

                            <div class="club">
                                <?= htmlspecialchars($e['club_name'] ?? 'Organizer') ?>
                            </div>

                            <div class="date">
                                <?= date("d M Y", strtotime($e['event_date'])) ?>
                                •
                                <?= date("h:i A", strtotime($e['start_time'])) ?>
                            </div>

                        </div>

                    </a>

                <?php endwhile; ?>

            </div>

        </div>

        <!-- ================= RIGHT PANEL ================= -->
        <div class="rightPanel">

            <div class="header">
                <h2>Event Announcements</h2>
                <p>Official updates from organizers</p>
            </div>

            <div class="body">

                <?php if(empty($messages) || $messages->num_rows == 0): ?>

                    <div class="empty">
                        No announcements yet
                    </div>

                <?php else: ?>

                    <?php while($m = $messages->fetch_assoc()): ?>

                        <div class="msgItem">

                            <div class="msgCard">

                                <div class="meta">
                                    <span class="sender">
                                        <?= htmlspecialchars($m['club_name'] ?? 'Organizer') ?>
                                    </span>

                                    <span class="time">
                                        <?= date("d M Y • h:i A", strtotime($m['created_at'])) ?>
                                    </span>
                                </div>

                                <?php if(!empty($m['message'])): ?>
                                    <div class="text">
                                        <?= nl2br(htmlspecialchars($m['message'])) ?>
                                    </div>
                                <?php endif; ?>

                                <?php if(!empty($m['attachment'])): ?>

                                    <div class="attach">

                                        <a href="/SEAT/uploads/event_messages/<?= $m['attachment'] ?>"
                                           target="_blank"
                                           class="file">

                                            <i class="fa fa-paperclip"></i>
                                            View Attachment

                                        </a>

                                    </div>

                                <?php endif; ?>

                            </div>

                        </div>

                    <?php endwhile; ?>

                <?php endif; ?>

            </div>

        </div>

    </div>

</div>

<script>

function closeModal(){
    document.getElementById("imgModal")?.remove();
}

function openImage(src){

    let modal = document.createElement("div");
    modal.id = "imgModal";
    modal.className = "imgModal";

    modal.innerHTML = `
        <img src="${src}">
    `;

    modal.onclick = () => modal.remove();

    document.body.appendChild(modal);
}

</script>