    <?php

/* Kijkt op wat de tabel gesorteerd wordt als er de pagina voor het eerst
 * geladen wordt dan wordt er standaard gesorteerd op begin datum.
 */
if(isset($_POST['current_sort'])){
    $sort = $_POST['current_sort'];
    $column_name = $_POST['current_column'];
} else {
    $column_name = "start_date";
    $sort = "ASC";
}

/* Kijkt of de filter al een keer gezet is. Is dit niet het geval dan is hij
 * leeg.
*/
if(isset($_POST['filter'])){
    $filter = $_POST['filter'];
} else {
    $filter = "";
}

/* Zet de filter variabele als de filter knop is ingedrukt. */
if(!empty($_POST)){
    if (isset($_POST['column_name'])){

        if($column_name == $_POST['column_name']){
            if($sort == "ASC"){
                $sort = "DESC";
            } else {
                $sort = "ASC";
            }
        } else {
            $sort = "ASC";
        }

        if(isset($_POST['column_name'])){
            $column_name = $_POST['column_name'];
        }
    } else if($_POST['submit'] == 'filter'){
        $filter = "";

        if($_POST['filter_event_title'] != ''){
            if(strlen($filter) == 0){
                $filter = "WHERE title LIKE '" . strip_tags($_POST['filter_event_title']) . "%' ";
            } else {
                $filter = $filter . "AND title LIKE '" .  strip_tags($_POST['filter_event_title']) . "%' ";
            }
        }

        if($_POST['filter_status'] != ''){
            if(strlen($filter) == 0){
                $filter = "WHERE status_id LIKE '" . strip_tags($_POST['filter_status']) . "%' ";
            } else {
                $filter = $filter . "AND status_id LIKE '" .  strip_tags($_POST['filter_status']) . "%' ";
            }
        }

        if($_POST['filter_datum'] != ''){
            if(strlen($filter) == 0){
                $filter = "WHERE start_date LIKE '" . strip_tags($_POST['filter_datum']) . "%' ";
            } else {
                $filter = $filter . "AND start_date LIKE '" .  strip_tags($_POST['filter_datum']) . "%' ";
            }
        }

        foreach ($_POST['label_filters'] as $key => $value) {
            if($_POST['label_filters_selected'][$key] == 'selected'){
                if(strlen($filter) == 0){
                    $filter = "WHERE label_id  LIKE '" . $value . "' ";
                } else {
                    $filter = $filter . "AND label_id LIKE '" .  $value. "' ";
                }
            }
        }
    }
}

/* Houdt waar opgesorteerd wordt en de filters bij. */
echo '<input type="hidden" name="current_sort" value="' . $sort . '"/>
      <input type="hidden" name="current_column" value="' . $column_name . '"/>
      <input type="hidden" name="filter" value="' . $filter . '"/>';

/* Zorgt ervoor dat de nummers onder de tabel kloppen. */
$beginpunt = 0;
$rows_page_begin = 5;
$rows_page = $rows_page_begin;
$count = $db->prepare("SELECT COUNT(event_id) FROM Events LEFT JOIN Event_labels Using (event_id) $filter");
$count->execute();
$aantal_rows = $count->fetch()[0];
if($aantal_rows < $rows_page_begin){
  $rows_page = $aantal_rows;
}

include "../hulp_files/bladeren_POST.php";

/* Haalt uit de database gebruikersnaam, e-mail en privilege op. */
$stmt = $db->query("SELECT title, start_date, end_date, event_text, status, event_id, label_id
                    FROM Events
                    LEFT JOIN Statuses USING (status_id)
                    LEFT JOIN Event_labels Using (event_id)
                    $filter
					GROUP BY (event_id)
                    ORDER BY $column_name $sort
                    LIMIT $beginpunt,$rows_page");

// Haalt alle labels op.
$lab = $db->prepare("SELECT label, label_id FROM Labels;");
$lab->execute();
$labels = $lab->fetchAll(PDO::FETCH_ASSOC);

echo '<table id="tabel">
        <tr>
            <td colspan="42" id="wijzig_knop">';
                include "../hulp_files/wijzig.php";
            echo '</td>
        </tr>
        <tr>
            <th>
                <button onclick="formSort(this)" clicked="false" type="submit" name="column_name" class="sort" value="title">Event Title ';
                    if($column_name == "title") {
                        if($sort == "DESC"){
                            echo '^';
                        } else {
                            echo "V";
                        }
                    } else {
                        echo "-";
                    }
                echo '</button>
                <input type="hidden" name="sort" value="ASC"/>
            </th>
            <th>Label</th>
            <th>
                <button onclick="formSort(this)" clicked="false" type="submit" name="column_name" class="sort" value="start_date">Date ';
                    if($column_name == "start_date") {
                        if($sort == "DESC"){
                            echo '^';
                        } else {
                            echo "V";
                        }
                    } else {
                        echo "-";
                    }
                echo '</button>
                <input type="hidden" name="sort" value="ASC"/>
            </th>
            <th>Begin_tijd</th>
            <th id="laatste_th">Eind Tijd</th>
        </tr>';


// Maakt een rij aan als er nog een rij in de tabel zit.
while($results = $stmt->fetch()){
    echo '<tr class="table_row">
        <input class="event_id" type="hidden" name="event_id[]" value="' . $results['event_id'] . '">
        <td class="event_td">
            <div class = "event_box">

                <span class="uitbreiden">V</span>
                <span class="event_title" > ' . $results['title'] . '</span>
                <input class="edit_event" type="text" name="title[]" action="" value="' . $results['title'] . '">
                <div class="event_informatie">
                    <textarea placeholder="Description" class="edit_event_text" type="text" name="event_text[]" action="" value="' . $results['event_text'] .'"></textarea>
                    <p placeholder="No description added" class="event_text">' . $results['event_text'] . '</p>
                </div>
            </div>
        </td>
        <div class="dropdown-doelgroep">
        <td class="labels_db">';

        // Loopt door de labels die aan het event gekopppelt zijn en print ze uit.
        $event = $results['event_id'];
        $ev_lab = $db->prepare("SELECT label_id, label
                                FROM Event_labels
                                LEFT JOIN Labels USING (label_id)
                                WHERE event_id = $event");
        $ev_lab->execute();
        $event_labels = $ev_lab->fetchAll(PDO::FETCH_ASSOC);
        foreach ($event_labels as $key => $value) {
            echo '<div class="doelgroep"><input class="label_event" name="label[]" type="hidden" value="' . $value['label_id'] . '">' . $value['label'] . ' </div>';
        }

        echo '<div class="doelgroepen">';

            // Loopt door alle labels in de database en laat ze in een dropdown menu zien.
            foreach ($labels as $key => $value) {
                echo '<div class="doelgroep_edit">
                            <input class="label_id" type="hidden" name="label_db[]" value="' . $value['label_id'] . '">' . $value['label'] . '
                            <input class="label_selected" type="hidden" name="label_selected[]" value="">
                      </div>';
            }

            echo '</div>

        </td>
    </div>';

        $start_date = new DateTime($results["start_date"]);
        $end_date = new DateTime($results['end_date']);
        echo '
        <td class="datum"><span class="datum_text">' . $start_date->format("m-d-Y") . '</span>
            <input class="edit_datum" name="datum[]" type="date" value="' . $start_date->format("Y-m-d") . '">
        </td>
        <td class="begin_tijd"><span class="begin_tijd_text">' . $start_date->format("H:i") . '</span>
            <input class="edit_begin_tijd" name="begin_tijd[]" type="time" value="' . $start_date->format("H:i") . '">
        </td>
        <td class="laatste_td eind_tijd"><span class="eind_tijd_text">' . $end_date->format("H:i") . '</span>
            <input class="edit_eind_tijd" name="eind_tijd[]" type="time" value="' . $end_date->format("H:i") . '">
        </td>
    </tr>';
}

/* Zorgt ervoor dat je door tabellen kunt bladeren */
echo '<tr>
        <td colspan="42" id="submit_button"><div><input onclick="formSubmit(this)" clicked="false"  class="submit" name="submit" type="submit" value="submit"></div></td>
      </tr>
      <tr>
        <td colspan="42" id="bladeren_knoppen">';
            include "../hulp_files/bladeren.php";
        echo '</td>
      </tr>
</table>';
?>
