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

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



$BASE="/SEAT";

/* ================= GET ORGANIZERS ================= */
$orgs = $conn->query("SELECT id, club_name FROM organizers");

/* ================= GET EVENTS ================= */
$events = $conn->query("
SELECT 
    e.*, 
    o.club_name
FROM events e
LEFT JOIN organizers o ON e.organizer_id = o.id
ORDER BY e.event_date DESC
");
?>




<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin - Events</title>
<link rel="stylesheet" href="<?= $BASE ?>/assets/css/admin/viewEvent.css">
</head>

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

<div class="content">
    
    <!-- ================= PAGE TITLE ================= -->

<div class="pageHeader">

  
        <h1 class="pageTitle">
            Organizer Events
        </h1>

</div>



    <!-- ================= FILTER ================= -->
    <div class="filters">
        <div class="filter-group">
            <label>Search</label>
            <input type="text" id="searchInput" placeholder="Search event, club, venue..." onkeyup="filterEvent()">
        </div>

        <div class="filter-group">
            <label>Status</label>
            <select id="statusFilter" onchange="filterEvent()">
                <option value="">All Status</option>
                <option value="upcoming">Upcoming</option>
                <option value="completed">Completed</option>
                <option value="cancelled">Cancelled</option>
            </select>
        </div>

        <div class="filter-group">
            <label>Organizer</label>
            <select id="organizerFilter" onchange="filterEvent()">
                <option value="">All Organizer</option>
                <?php while($o=$orgs->fetch_assoc()){ ?>
                    <option value="<?= $o['id'] ?>"><?= htmlspecialchars($o['club_name']) ?></option>
                <?php } ?>
            </select>
        </div>

        <div class="filter-group">
            <label>&nbsp;</label>
            <button class="btn resetBtn" onclick="resetFilter()">Reset</button>
        </div>
    </div>

    
    <!-- ================= EVENT GRID ================= -->
    <div class="grid" id="eventGrid">
        <?php while($e = $events->fetch_assoc()){ 
            $eventJSON = htmlspecialchars(
                json_encode($e, JSON_HEX_APOS | JSON_HEX_QUOT),
                ENT_QUOTES,'UTF-8'
            );
        ?>
        <div class="card eventCard"
            data-title="<?= strtolower($e['title']) ?>"
            data-club="<?= strtolower($e['club_name']) ?>"
            data-venue="<?= strtolower($e['venue']) ?>"
            data-status="<?= strtolower($e['status']) ?>"
            data-organizer="<?= $e['organizer_id'] ?>"
        >
            <span class="tag <?= strtolower($e['status']) ?>"><?= strtoupper($e['status']) ?></span>

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

            <div class="eventInfo">
                <div class="infoRow"><span>🏢</span><span><?= htmlspecialchars($e['club_name']) ?></span></div>
        <div class="infoRow">
    <span>📅</span>
    <span>
        <?= date("D, d M Y", strtotime($e['event_date'])) ?>
    </span>
</div>
                <div class="infoRow"><span>⏰</span><span><?= date("H:i",strtotime($e['start_time'])) ?> - <?= date("H:i",strtotime($e['end_time'])) ?></span></div>
                <div class="infoRow"><span>📍</span><span><?= htmlspecialchars($e['venue']) ?></span></div>
            </div>

      
        </div>
        <?php } ?>
    </div>

    <p id="noResult" class="noResult">No event found</p>
</div>



</script>
</body>
</html>