<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Termin hinzufügen</title>
    <link rel="stylesheet" href="style.css"> <!-- Link to the CSS file -->
</head>
<body>
    <header>
        <div class="container">
            <div id="branding">
                <img src="Logo.png" alt="Logo">
                <h1>Termin hinzufügen</h1>
            </div>
        </div>
    </header>

    <div class="container">
        <h2>Neuen Termin hinzufügen</h2>
        <form action="add.php" method="POST">
            <label for="title">Title:</label>
            <input type="text" id="title" name="title" required>

            <label for="melly">Melly Link:</label>
            <input type="text" id="melly" name="melly" required>

            <label for="date">Event Date:</label>
            <input type="date" id="date" name="date" required>

            <label for="signup_deadline">Anmeldung bis:</label>
            <input type="datetime-local" id="signup_deadline" name="signup_deadline" required>

            <label for="password">Password:</label>
            <input type="password" id="password" name="password" required>

            <button type="submit" class="btn">Hinzfügen</button>
        </form>

        <?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'];

        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            // Retrieve form data
            $title = $_POST['title'];
            $melly = $_POST['melly'];
            $date = $_POST['date'];
            $deadline = $_POST['signup_deadline'];

            $webform_password = $_POST['password'];

            if($webform_password != $config['webform_password']) {
                echo 'Invalid Password!';
            } else {

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

                    // Set error mode to exception for easier debugging
                    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                    // SQL query to insert a new dinner option
                    $sql = "INSERT INTO meals (title, link, event_date, registration_closes) VALUES (:title, :melly, :date, :deadline)";
                    $stmt = $pdo->prepare($sql);

                    // Bind parameters
                    $stmt->bindParam(':title', $title);
                    $stmt->bindParam(':melly', $melly);
                    $stmt->bindParam(':date', $date);
                    $stmt->bindParam(':deadline', $deadline);

                    // Execute the statement
                    $stmt->execute();

                    echo '<p class="success">Dinner option added successfully!</p>';

                } catch (PDOException $e) {
                    // Handle connection or query error
                    echo "Error: " . $e->getMessage();
                }
            }
        }
        ?>
    </div>
</body>
</html>
