<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Allmende-Essen</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <header>
        <div class="container">
            <div id="branding">
                <img src="Logo.png" alt="Logo"/>
                <h1>Gemeinsames Essen in der Allmende</h1>
            </div>
        </div>
    </header>
    <?php
    
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    $config = require 'config.php';

    // Database connection parameters
    $host = $config['db']['host'];
    $dbname = $config['db']['dbname'];
    $username = $config['db']['username'];
    $password = $config['db']['password'];

    // Create connection
    $dsn = "pgsql:host=$host;dbname=$dbname";
    $pdo = new PDO($dsn, $username, $password);

    // Query to fetch future dinner options
    $sql = "SELECT id, title, link, event_date, registration_closes FROM meals WHERE event_date >= now()::date order by registration_closes < now(), event_date";
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

    $days = ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"];
    
    $today = strtotime(date('Y-m-d'));
    $now = strtotime(date("Y-m-d H:i:s"));
    ?>

    <div class="container"> 
    <?php // Display dinner options
    for($i = 0; $i < sizeof($result); $i++) {
        $row = $result[$i];
        
        $event_date = strtotime($row["event_date"]);

        $weekday = date("w", $event_date);
        $date = new DateTimeImmutable($row["event_date"]);

        $end_of_registration = strtotime($row["registration_closes"]);
        echo '<div class="dinner-option">';
        echo '<h2>' . $days[$weekday] . " " . $date->format('d.m.Y') .'</h2>';
        echo '<p>' . htmlspecialchars($row["title"]) . '</p>';
        if ($end_of_registration > $now) {
            echo '<a href="' . $row["link"] . '" class="btn">Zur Anmeldung</a>';
        } else {
            echo '<a href="' . $row["link"] . '" class="btn btn-grey">Anmeldungen ansehen</a>';            
        }
        echo '</div>';
    }
    // close container
    echo '</div>';

    // Query to fetch past dinner options
    $sql = "SELECT id, title, link, event_date FROM meals WHERE event_date < now()::date order by event_date";
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

    if(sizeof($result) > 0) {
    ?>
    
    <div class="container"> 
    <h2>Vergangene Essen</h2>
    
    <?php
        for($i = 0; $i < sizeof($result); $i++) {
            $row = $result[$i];
            
            $event_date = strtotime($row["event_date"]);

            $weekday = date("w", $event_date);
            $date = new DateTimeImmutable($row["event_date"]);

            echo '<div class="dinner-option">';
            echo '<h2>' . $days[$weekday] . " " . $date->format('d.m.Y') .'</h2>';
            echo '<p>' . htmlspecialchars($row["title"]) . '</p>';
            echo '<a href="' . $row["link"] . '" class="btn btn-grey">Anmeldungen ansehen</a>';            
            echo '</div>';
        }
        // close container
        echo '</div>';
    }
    ?>
    
</body>
</html>

