<?php
session_start();
require_once 'config.php';

// Check if logged in
if (!isset($_SESSION['user_logged_in']) || $_SESSION['user_logged_in'] !== true) {
    header('Location: login.php');
    exit;
}

$user_id = $_SESSION['user_id'] ?? 0;

// Get user's events from database (exclude expired events - 7 days after event date)
$events = [];
$conn = getDBConnection();
// Get all events, we'll filter expired ones in PHP based on event_date + 7 days
$stmt = $conn->prepare("SELECT id, event_slug, event_name, event_type, celebrant1, celebrant2, event_date, created_at FROM events WHERE user_id = ? ORDER BY created_at DESC");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result && $result->num_rows > 0) {
    $now = new DateTime();
    while ($row = $result->fetch_assoc()) {
        // Filter out expired events (7 days after event_date)
        if (!empty($row['event_date'])) {
            $event_date = new DateTime($row['event_date']);
            $expiry_date = (clone $event_date)->modify('+7 days');
            if ($expiry_date >= $now) {
                $events[] = $row;
            }
        } else {
            // Events without event_date don't expire (or use created_at as fallback)
            // For now, include them
            $events[] = $row;
        }
    }
}
$stmt->close();
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Events - Evently</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        html, body {
            height: 100%;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
            background-color: #000000;
            color: #333;
            padding: 0;
            margin: 0;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        .navbar {
            background-color: #ffffff;
            padding: 15px 20px;
            border-bottom: 1px solid #e0e0e0;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .navbar-brand {
            color: #000000;
            font-family: 'Inter', sans-serif;
            font-size: 20px;
            font-weight: 700;
            text-decoration: none;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .navbar-brand img {
            height: 30px;
            width: auto;
        }

        .navbar-nav {
            display: flex;
            align-items: center;
            gap: 20px;
        }

        .navbar-nav a {
            color: #000000;
            text-decoration: none;
            font-size: 14px;
            transition: opacity 0.2s;
        }

        .navbar-nav a:hover {
            opacity: 0.7;
        }

        .navbar-nav span {
            color: #000000;
        }

        .hamburger {
            display: none;
            flex-direction: column;
            cursor: pointer;
            padding: 5px;
            gap: 4px;
        }

        .hamburger span {
            width: 25px;
            height: 3px;
            background-color: #000000;
            transition: all 0.3s;
        }

        .hamburger.active span:nth-child(1) {
            transform: rotate(45deg) translate(5px, 5px);
        }

        .hamburger.active span:nth-child(2) {
            opacity: 0;
        }

        .hamburger.active span:nth-child(3) {
            transform: rotate(-45deg) translate(7px, -6px);
        }

        @media (max-width: 768px) {
            .hamburger {
                display: flex;
            }

            .navbar-nav {
                position: fixed;
                top: 60px;
                left: 0;
                right: 0;
                background-color: #ffffff;
                flex-direction: column;
                padding: 20px;
                border-bottom: 1px solid #e0e0e0;
                box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
                transform: translateY(-100%);
                opacity: 0;
                visibility: hidden;
                transition: all 0.3s ease;
                z-index: 1000;
            }

            .navbar-nav.active {
                transform: translateY(0);
                opacity: 1;
                visibility: visible;
            }

            .navbar-nav a,
            .navbar-nav span {
                padding: 12px 0;
                border-bottom: 1px solid #f0f0f0;
                width: 100%;
            }

            .navbar-nav a:last-child,
            .navbar-nav span:last-child {
                border-bottom: none;
            }
        }

        .content-wrapper {
            padding: 20px;
            flex: 1;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: #ffffff;
            border-radius: 0;
            padding: 40px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
        }

        h1 {
            font-size: 28px;
            font-weight: 400;
            margin-bottom: 30px;
            color: #2c3e50;
        }

        .events-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        .events-table th {
            background-color: #f8f9fa;
            padding: 12px;
            text-align: left;
            font-weight: 600;
            color: #495057;
            border-bottom: 2px solid #dee2e6;
            font-size: 14px;
        }

        .events-table td {
            padding: 15px 12px;
            border-bottom: 1px solid #e9ecef;
            color: #2c3e50;
            font-size: 14px;
        }

        .events-table tr:hover {
            background-color: #f8f9fa;
        }

        /* Mobile card layout */
        .events-list-mobile {
            display: none;
        }

        .event-card {
            background-color: #ffffff;
            border: 1px solid #e0e0e0;
            border-radius: 0;
            padding: 20px;
            margin-bottom: 15px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .event-card-header {
            display: flex;
            justify-content: space-between;
            align-items: start;
            margin-bottom: 12px;
        }

        .event-card-title {
            font-size: 18px;
            font-weight: 500;
            color: #2c3e50;
            margin: 0;
        }

        .event-card-row {
            display: flex;
            padding: 8px 0;
            border-bottom: 1px solid #f0f0f0;
        }

        .event-card-row:last-child {
            border-bottom: none;
        }

        .event-card-label {
            font-weight: 600;
            color: #495057;
            min-width: 100px;
            font-size: 13px;
        }

        .event-card-value {
            color: #2c3e50;
            font-size: 14px;
            flex: 1;
        }

        @media (max-width: 768px) {
            .events-table {
                display: none;
            }

            .events-list-mobile {
                display: block;
            }

            .container {
                padding: 20px 15px;
            }

            h1 {
                font-size: 24px;
                margin-bottom: 20px;
            }
        }

        .event-name {
            font-weight: 500;
            color: #2c3e50;
        }

        .event-type {
            font-size: 12px;
            color: #6c757d;
            background-color: #e9ecef;
            padding: 4px 8px;
            border-radius: 0;
            display: inline-block;
        }

        .event-date {
            color: #6c757d;
            font-size: 13px;
        }

        .event-link {
            color: #000000;
            background-color: #000000;
            color: #ffffff;
            padding: 8px 16px;
            text-decoration: none;
            border-radius: 0;
            font-size: 14px;
            display: inline-block;
            transition: background-color 0.2s;
        }

        .event-link:hover {
            background-color: #333333;
        }

        .no-events {
            text-align: center;
            padding: 40px;
            color: #6c757d;
        }

        .expiry-info {
            font-size: 12px;
            color: #6c757d;
        }

        .expiry-warning {
            color: #dc3545;
            font-weight: 500;
        }

        .footer {
            background-color: #ffffff;
            padding: 20px;
            border-top: 1px solid #e0e0e0;
            text-align: center;
            color: #666;
            font-size: 14px;
            margin-top: auto;
        }
    </style>
</head>
<body>
    <nav class="navbar">
        <a href="home.php" class="navbar-brand">
            <img src="assets/img/logo.svg" alt="Evently">
            <span>Evently</span>
        </a>
        <div class="hamburger" onclick="toggleMenu()">
            <span></span>
            <span></span>
            <span></span>
        </div>
        <div class="navbar-nav" id="navbarNav">
            <a href="home.php">Create Event</a>
            <a href="my_events.php">My Events</a>
            <span><?php echo htmlspecialchars($_SESSION['user_username'] ?? 'User'); ?></span>
            <a href="home.php?logout=1">Logout</a>
        </div>
    </nav>
    <div class="content-wrapper">
        <div class="container">
            <h1>My Events</h1>
            
            <?php if (empty($events)): ?>
                <div class="no-events">
                    <p>No events available yet. <a href="home.php" style="color: #000000; text-decoration: underline;">Create your first event</a></p>
                </div>
            <?php else: ?>
                <?php 
                $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
                $host = $_SERVER['HTTP_HOST'];
                $base_url = $protocol . '://' . $host . dirname($_SERVER['SCRIPT_NAME']);
                ?>
                <table class="events-table">
                    <thead>
                        <tr>
                            <th>Event Name</th>
                            <th>Type</th>
                            <th>Names</th>
                            <th>Event Date</th>
                            <th>Expires</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($events as $event): 
                            $event_url = $base_url . '/index.php?event=' . urlencode($event['event_slug']);
                            
                            // Calculate expiry date (7 days after event_date)
                            $now = new DateTime();
                            if (!empty($event['event_date'])) {
                                $event_date = new DateTime($event['event_date']);
                                $expiry_date = (clone $event_date)->modify('+7 days');
                                $days_remaining = $now->diff($expiry_date)->days;
                                $is_expired = $expiry_date < $now;
                            } else {
                                // Events without event_date don't expire
                                $expiry_date = null;
                                $days_remaining = null;
                                $is_expired = false;
                            }
                        ?>
                            <tr>
                                <td class="event-name"><?php echo htmlspecialchars($event['event_name']); ?></td>
                                <td>
                                    <span class="event-type"><?php echo htmlspecialchars($event['event_type'] ?? 'Other'); ?></span>
                                </td>
                                <td>
                                    <?php echo htmlspecialchars($event['celebrant1']); ?>
                                    <?php if (!empty($event['celebrant2'])): ?>
                                        & <?php echo htmlspecialchars($event['celebrant2']); ?>
                                    <?php endif; ?>
                                </td>
                                <td class="event-date">
                                    <?php if ($event['event_date']): ?>
                                        <?php echo date('M j, Y', strtotime($event['event_date'])); ?>
                                    <?php else: ?>
                                        <span style="color: #adb5bd;">—</span>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <?php if (empty($event['event_date'])): ?>
                                        <span style="color: #6c757d; font-size: 13px;">No expiry</span>
                                    <?php elseif ($is_expired): ?>
                                        <span class="expiry-warning">Expired</span>
                                    <?php else: ?>
                                        <div class="expiry-info">
                                            <div><?php echo $days_remaining; ?> day<?php echo $days_remaining != 1 ? 's' : ''; ?> left</div>
                                            <div style="font-size: 11px; color: #adb5bd;"><?php echo $expiry_date->format('M j, Y'); ?></div>
                                        </div>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <a href="index.php?event=<?php echo urlencode($event['event_slug']); ?>" class="event-link" target="_blank">View Event</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>

                <!-- Mobile Card View -->
                <div class="events-list-mobile">
                    <?php foreach ($events as $event): 
                        $event_url = $base_url . '/index.php?event=' . urlencode($event['event_slug']);
                        
                        // Calculate expiry date (7 days after event_date)
                        $now = new DateTime();
                        if (!empty($event['event_date'])) {
                            $event_date = new DateTime($event['event_date']);
                            $expiry_date = (clone $event_date)->modify('+7 days');
                            $days_remaining = $now->diff($expiry_date)->days;
                            $is_expired = $expiry_date < $now;
                        } else {
                            // Events without event_date don't expire
                            $expiry_date = null;
                            $days_remaining = null;
                            $is_expired = false;
                        }
                    ?>
                        <div class="event-card">
                            <div class="event-card-header">
                                <h3 class="event-card-title"><?php echo htmlspecialchars($event['event_name']); ?></h3>
                                <span class="event-type"><?php echo htmlspecialchars($event['event_type'] ?? 'Other'); ?></span>
                            </div>
                            <div class="event-card-row">
                                <span class="event-card-label">Names:</span>
                                <span class="event-card-value">
                                    <?php echo htmlspecialchars($event['celebrant1']); ?>
                                    <?php if (!empty($event['celebrant2'])): ?>
                                        & <?php echo htmlspecialchars($event['celebrant2']); ?>
                                    <?php endif; ?>
                                </span>
                            </div>
                            <div class="event-card-row">
                                <span class="event-card-label">Event Date:</span>
                                <span class="event-card-value">
                                    <?php if ($event['event_date']): ?>
                                        <?php echo date('M j, Y', strtotime($event['event_date'])); ?>
                                    <?php else: ?>
                                        <span style="color: #adb5bd;">—</span>
                                    <?php endif; ?>
                                </span>
                            </div>
                            <div class="event-card-row">
                                <span class="event-card-label">Expires:</span>
                                <span class="event-card-value">
                                    <?php if (empty($event['event_date'])): ?>
                                        <span style="color: #6c757d;">No expiry</span>
                                    <?php elseif ($is_expired): ?>
                                        <span class="expiry-warning">Expired</span>
                                    <?php else: ?>
                                        <div class="expiry-info">
                                            <div><?php echo $days_remaining; ?> day<?php echo $days_remaining != 1 ? 's' : ''; ?> left</div>
                                            <div style="font-size: 11px; color: #adb5bd;"><?php echo $expiry_date->format('M j, Y'); ?></div>
                                        </div>
                                    <?php endif; ?>
                                </span>
                            </div>
                            <div class="event-card-row" style="padding-top: 15px; border-top: 1px solid #e0e0e0; margin-top: 10px;">
                                <a href="index.php?event=<?php echo urlencode($event['event_slug']); ?>" class="event-link" target="_blank" style="width: 100%; text-align: center; display: block;">View Event</a>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
    <footer class="footer">
        <p>&copy; <?php echo date('Y'); ?> Evently. All rights reserved.</p>
    </footer>
</body>
</html>

