<?php session_start(); ?>
    
<form action="javascript:;" method="post" onsubmit="submitEvent(this)">
    <div class="eventBoxTitle">
        <div class="icon-content closeEventBoxDiv" onclick="closeOpacityBox('.eventBox')">
            <span class="icon closeEventBoxIcon" data-icon="close"></span>
        </div>
<?php

    include 'db_conn.php';

    if (isset($_POST['eventID'])) {
        $eventID = [
            'id' => $_POST['eventID']
        ];

        $query = "SELECT * FROM Events WHERE event_id=:id LIMIT 1";

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

        if ($result == false) {
            echo "No event found";
        }

        $event = [
            'title' =>  $result['title'],
            'allDay' => $result['all_day'],
            'startDate' => new DateTime($result['start_date']),
            'endDate' => new DateTime($result['end_date']),
            'description' => $result['event_text']
        ];

    } else if (isset($_POST['dateTime'])) {

        $startDate = new DateTime($_POST['dateTime']);
        $endDate = clone $startDate;
        $endDate->add(new DateInterval('PT1H'));

        $event = [
            'title' =>  '',
            'allDay' => '',
            'startDate' => $startDate,
            'endDate' => $endDate,
            'description' => ''
        ];
    }
    ?>
    
    <input type="text" name="title" placeholder="Title" maxlength="52" autocomplete="off" required="required" class="eventBoxTitle" value="<?php echo $event['title'];?>">
    </div>
    <textarea name="description" placeholder="Description" class="description" maxlength="333"><?php echo $event['description'];?></textarea>
        <span>
            <span class="icon create-icon" id="icon-mirrored" data-icon="restore">
                <p>All day</p>
            </span>
            <br>
            <input type="checkbox" class="allDayCheckbox" name="allDay" <?php echo (($event['allDay'] > 0)?'checked':'')?>>
        </span>
        <span class="icon create-icon" data-icon="schedule">
            <p>Start</p>
        </span>
        <br>
        <input type="date" name="startDate" required="required" value="<?php echo $event['startDate']->format("Y-m-d");?>">
        <input type="time" id="start-time" name="startTime" required="required" value="<?php echo $event['startDate']->format("H:i:s")?>">
        <br>
        <span class="icon create-icon" data-icon="watch_later">
            <p>End</p>
        </span>
        <br>
        <input type="date" name="endDate" required="required" value="<?php echo $event['endDate']->format("Y-m-d")?>">
        <input type="time" id="end-time" name="endTime" required="required" value="<?php echo $event['endDate']->format("H:i:s")?>">
        <br>
        <span class="icon create-icon" data-icon="label_outline">
            <p>Label</p>
        </span>
        <br>
        <div class="check-multiple">
            <?php include 'label_options.php'; ?>
        </div>
        <?php if (isset($_POST['eventID'])) { ?>
            <input type="hidden" name="existingEvent" value="<?php echo $_POST['eventID']; ?>">
        <?php } ?>
        <input type="submit" name="submit" value="event_available" class="button" id="eventBoxCreate">
    </form>