<?php
session_start();

if (
    !isset($_SESSION['role']) ||
    $_SESSION['role'] !== "organizer"
){
    die("No access");
}

include("../../../config/db.php");

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

/* ORGANIZER */
$q = mysqli_query($conn,"
    SELECT id
    FROM organizers
    WHERE user_id = $user_id
    LIMIT 1
");

$org = mysqli_fetch_assoc($q);

if(!$org){
    die("Organizer not found");
}

$organizer_id = (int)$org['id'];

/* EVENTS */
$events = mysqli_query($conn,"
    SELECT *
    FROM events
    WHERE organizer_id = $organizer_id
    AND status = 'upcoming'
    ORDER BY event_date ASC
");

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

<link rel="stylesheet" href="/SEAT/assets/css/organizer/event.css">
<link rel="stylesheet" href="/SEAT/assets/css/organizer/createEvent.css">
<link rel="stylesheet" href="/SEAT/assets/css/organizer/edit.css">
<link rel="stylesheet" href="/SEAT/assets/css/organizer/delete.css">

<div class="main">

    <!-- HEADER -->
    <div class="pageHead">

        <div>
            <h2>Event Management</h2>

            <p class="sub">
                Manage and update your events
            </p>
        </div>

        <button
            type="button"
            class="createBtn"
            onclick="openModal('createModal')">

            <i class="fa fa-plus"></i>
            Create New Event
        </button>

    </div>

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

<?php while($e = mysqli_fetch_assoc($events)):

$event_id = (int)$e['id'];

$r = mysqli_query($conn,"
    SELECT COUNT(*) total
    FROM event_registrations
    WHERE event_id = $event_id
");

$row = mysqli_fetch_assoc($r);

$total = (int)$row['total'];

$json = htmlspecialchars(
    json_encode($e),
    ENT_QUOTES,
    "UTF-8"
);
?>

        <div class="eventCard">

            <!-- POSTER -->
<?php if(!empty($e['poster'])): ?>

            <div
                class="eventThumbWrap"
                onclick="openPosterModal('/SEAT/uploads/event_posters/<?= $e['poster'] ?>')">

                <img
                    src="/SEAT/uploads/event_posters/<?= $e['poster'] ?>"
                    class="eventThumb">

            </div>

<?php else: ?>

            <div class="eventThumbWrap noImage">
                <span>No Poster</span>
            </div>

<?php endif; ?>

            <!-- INFO -->
            <div class="infoContainer">

                <div class="cardTop">

                    <h4>
                        <?= htmlspecialchars($e['title']) ?>
                    </h4>

                </div>

                <div class="infoRow">

                    <i class="fa fa-calendar-days icon calendar"></i>

                    <span>
                        <?= date(
                            "d M Y",
                            strtotime($e['event_date'])
                        ) ?>
                    </span>

                </div>

<?php if($e['mode'] === "Physical"): ?>

                <div class="infoRow">

                    <i class="fa fa-location-dot icon location"></i>

                    <span>
                        <?= $e['venue']
                            ? htmlspecialchars($e['venue'])
                            : 'No venue set' ?>
                    </span>

                </div>

<?php else: ?>

                <div class="infoRow">

                    <i class="fa fa-link icon linkIcon"></i>

<?php if(!empty($e['meeting_link'])): ?>

                    <a
                        href="<?= htmlspecialchars($e['meeting_link']) ?>"
                        target="_blank"
                        class="meetingLink">

                        Join Meeting
                    </a>

<?php else: ?>

                    <span>No meeting link</span>

<?php endif; ?>

                </div>

<?php endif; ?>

                <div class="infoRow">

                    <i class="fa fa-user-group icon users"></i>

                    <span>
                        <?= $total ?>
                        /
                        <?= (int)$e['max_participant'] ?>
                    </span>

                </div>

                <div class="infoRow">

                    <i class="fa fa-wallet icon money"></i>

                    <span>
                        <?= $e['fee_type'] === 'Free'
                            ? 'Free'
                            : 'RM '.number_format(
                                $e['fee_amount'],2
                            ) ?>
                    </span>

                </div>

                <hr class="infoSeparator">

                <div class="infoRow modeInfo">

                    MODE :
                    <strong>
                        <?= htmlspecialchars($e['mode']) ?>
                    </strong>

                </div>

            </div>

            <!-- ACTIONS -->
            <div class="btnRow">

                <button
                    type="button"
                    class="btn pillBtn"
                    onclick='openEditModal(<?= $json ?>)'>

                    <i class="fa fa-pen"></i>
                </button>

                <button
                    type="button"
                    class="btn pillBtn"
                    onclick="openPostponeModal(
                        <?= $event_id ?>,
                        '<?= $e['event_date'] ?>',
                        '<?= $e['start_time'] ?>',
                        '<?= $e['end_time'] ?>'
                    )">

                    Postpone
                </button>

                <button
                    type="button"
                    class="btn pillBtn"
                    onclick="openMessageModal(<?= $event_id ?>)">

                    <i class="fa fa-envelope"></i>
                </button>

                <button
                    type="button"
                    class="btn iconBtn deleteBtn"
                    onclick="openDeleteModal(<?= $event_id ?>)">

                    <i class="fa fa-trash"></i>
                </button>

            </div>

        </div>

<?php endwhile; ?>

    </div>

</div>

<?php
include("createEvent.php");
include("edit.php");
include("postpone.php");
include("message.php");
include("delete.php");
?>

<!-- POSTER MODAL -->
<div id="posterModal" class="imageModal">

    <div class="imageModalBox">

        <span
            class="imageClose"
            onclick="closePosterModal()">

            ×
        </span>

        <img
            id="posterModalImg"
            src="">

    </div>

</div>

<script src="/SEAT/modules/Event/organizer/global.js"></script>