<?php

$afgekeurd_id = 1;
$goedgekeurd_id = 2;
$in_behandeling_id = 3;

/* Kijkt of de geodgekeurd of afgekeurd knop van een rij gehighlight
 *  is is dit het geval dan zal hij de status-id van dat evenementen
 * veranderen naar wat op de knop staat.
 */
if(isset($_POST['answer'])){
        for ($i=0; $i < count($_POST['event_id']); $i++) {
            $event_id = strip_tags($_POST['event_id'][$i]);
            $answer = strip_tags($_POST['answer'][$i]);


            if($answer == 'goedgekeurd'){
                $update_status = $db->prepare("UPDATE Events SET status_id = ? WHERE event_id = ?");
                $update_status->bindValue(1, $goedgekeurd_id, PDO::PARAM_INT);
                $update_status->bindValue(2, $event_id, PDO::PARAM_INT);

                $update_status->execute();
            } else if($answer == 'afgekeurd') {
                $update_status = $db->prepare("UPDATE Events SET status_id = ? WHERE event_id = ?");
                $update_status->bindValue(1, $afgekeurd_id, PDO::PARAM_INT);
                $update_status->bindValue(2, $event_id, PDO::PARAM_INT);

                $update_status->execute();
            }
        }
    }

/* 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'] != ''){
            $filter = $filter . "AND title LIKE '" .  strip_tags($_POST['filter_event_title']) . "%'";
        }

        if($_POST['filter_datum'] != ''){
            $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'){
                    $filter = $filter . "AND label_id LIKE '" .  $value. "' ";
            }
        }
    }
}

/* Zorgt dat het bladeren door tabellen werkt. */
$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) WHERE status_id = $in_behandeling_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';

/* 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 . '"/>';

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

echo '<table id="tabel">
<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">Datum';
            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>
                <div class="event_informatie">
                    <p class="event_text">
                        ' . $results['event_text'] . '
                    </p>
                </div>
            </div>
        </td>
        <td class="labels_db">';
            $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 '</td>';
        $start_date = new DateTime($results["start_date"]);
        $end_date = new DateTime($results['end_date']);

        echo '
        <td class="datum">' . $start_date->format("Y-m-d") . '</td>
        <td class="begin_tijd">' . $start_date->format("H:i") . '</td>
        <td class="eind_tijd">' . $end_date->format("H:i") . '</td>
        <td class="goedgekeurd"">
            Goedgekeurd
        </td>
        <td class="laatste_td afgekeurd">
            Afgekeurd
        </td>
        <input class="answer" type="hidden" name="answer[]" value="">
    </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>';
?>
