<?php    
    session_start();

    if (!isset($_SESSION['logged_in']))
    {
        http_response_code(401);
        die("You're not logged in.");
    }

    include '../db_conn.php';

    $eventID = [
        'id' => $_POST['id']
    ];

    $statement = $db->prepare("SELECT user_id FROM Events WHERE event_id=:id");
    $statement->execute(array("id" => $eventID['id']));

    $result = $statement->fetch(PDO::FETCH_ASSOC);
    if ($result['user_id'] != $_SESSION['user_id'] && $_SESSION['privilege_id'] == 4)
    {
        http_response_code(401);
        die("That event is not yours to delete.");
    }

    $query = "DELETE FROM Events WHERE event_id=:id";

    $statement = $db->prepare($query);

    try {
        $statement->execute(array(
            "id" => $eventID['id']
        ));
    } catch (PDOException $ex2) {
        echo $query . "<br>" . $ex2;
        die("Error connecting to SQL server.");
    }

    // Use js to go back 1 page
    //header("location:javascript://history.go(-1)");

    // Then execute the query, give user feedback, and updateEvents();

    //echo '<script>updateEvents();</script>';
?>
