<?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 = "username";
    $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_user_name'] != ''){
            if(strlen($filter) == 0){
                $filter = "WHERE username LIKE '" . strip_tags($_POST['filter_user_name']) . "%' ";
            } else {
                $filter = $filter . "AND username LIKE '" .  strip_tags($_POST['filter_user_name']) . "%'";
            }
        }

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

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

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

/* 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(user_id) FROM Users $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 username, email, privilege, user_id, privilege_id, 'Labels.label_id'
                    FROM Users
                    LEFT JOIN User_Privileges USING (privilege_id)
                    LEFT JOIN User_labels Using (user_id)
                    $filter
                	GROUP BY (user_id)
                    ORDER BY $column_name $sort
                    LIMIT $beginpunt,$rows_page;");


/* Haalt een lijst met privileges op. */
$stat = $db->prepare("SELECT privilege, privilege_id FROM User_Privileges;");
$lab = $db->prepare("SELECT label, label_id FROM Labels;");

$stat->execute();
$lab->execute();

$statuses = $stat->fetchAll(PDO::FETCH_ASSOC);
$labels = $lab->fetchAll(PDO::FETCH_ASSOC);

// Th van de tabel
echo '<table id="tabel">
    <tr>
        <td colspan="42" id="wijzig_knop">';
            include "../hulp_files/wijzig.php";
        echo '</td>
    </tr>
    <tr>
    <tr>
        <th>
            <button onclick="formSort(this)" clicked="false" type="submit" name="column_name" class="sort" value="username">User ';
                if($column_name == "username") {
                    if($sort == "DESC"){
                        echo '^';
                    } else {
                        echo "V";
                    }
                } else {
                    echo "-";
                }
            echo '</button>
            <input type="hidden" name="sort" value="ASC"/>
        </th>
        <th>
            <button onclick="formSort(this)" clicked="false" type="submit" name="column_name" class="sort" value="email">E-mail ';
                if($column_name == "email") {
                    if($sort == "DESC"){
                        echo '^';
                    } else {
                        echo "V";
                    }
                } else {
                    echo "-";
                }
            echo '</button>
            <input type="hidden" name="sort" value="ASC"/>
        </th>
        <th>Label</th>
        <th id=laatste_th>
            <button onclick="formSort(this)" clicked="false" type="submit" name="column_name" class="sort" value="privilege">Privilege ';
                if($column_name == "privilege") {
                    if($sort == "DESC"){
                        echo '^';
                    } else {
                        echo "V";
                    }
                } else {
                    echo "-";
                }
            echo '</button>
            <input type="hidden" name="sort" value="ASC"/>
        </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="gebruikers_id" type="hidden" name="user_id[]" value="' . $results['user_id'] . '">
        <td class="gebruiker">
            <span class="gebruikers_title" >' . $results['username'] . '</span>
            <input class="edit_gebruiker" type="text" name="username[]" action="" value="' . $results['username'] . '">
        </td>
        <td class="email">' . $results['email'] . '</td>
        <div class="dropdown-doelgroep">
        <td class="labels_db">';

        // Loopt door de labels die aan het event gekopppelt zijn en print ze uit.
        $user = $results['user_id'];
        $user_lab = $db->prepare("SELECT label_id, label
                                FROM User_labels
                                LEFT JOIN Labels USING (label_id)
                                WHERE user_id = $user");
        $user_lab->execute();
        $user_labels = $user_lab->fetchAll(PDO::FETCH_ASSOC);
        foreach ($user_labels as $key => $value) {
            echo '<div class="doelgroep"><input class="label_user" 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 class="status laatste_td">
            <input class="status_input" type="hidden" name="privilege[]" value="' . $results['privilege_id'] . '">
                <span class="status_text">' . $results['privilege'] . '</span>
                    <div class="dropdown-content">';
                        foreach ($statuses as $key => $value) {
                            echo '<div class="status_choose">
                                <input class="privilege_id" type="hidden" value="' . $value['privilege_id'] . '">' . $value['privilege'] .
                                '</div>';
                        }

                    echo '</div>
        </div>
        </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>';
?>
