<?php
    include '../../needs_login.php';

    include '../../mail.php';

    // Zorgt dat een users privilige wordt upgedate
    if(!empty($_POST)){
        if($_POST['submit'] == "submit"){
            /* Update de database als de rijen gewijzigd zijn*/
            for ($i=0; $i < count($_POST['user_id']); $i++) {
                $privilege = strip_tags($_POST['privilege'][$i]);
                $id = strip_tags($_POST['user_id'][$i]);

                $update_status = $db->prepare("UPDATE Users SET privilege_id = ? WHERE user_id = ?");
                $update_status->bindValue(1, $privilege, PDO::PARAM_INT);
                $update_status->bindValue(2, $id, PDO::PARAM_INT);

                $update_status->execute();


            }
        }
    }

    $afgekeurd_id = 1;
    $goedgekeurd_id = 2;

    /* 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['user_id']); $i++) {
           $user_id = strip_tags($_POST['user_id'][$i]);
           $answer = strip_tags($_POST['answer'][$i]);

           $query = $db->prepare("SELECT email, username FROM Users WHERE user_id=:uid");
           $query->execute(array("uid" => $user_id));
           $userdata = $query->fetch(PDO::FETCH_ASSOC);

           if($answer == 'goedgekeurd'){
               echo $user_id . " " . $goedgekeurd_id;
               $update_status = $db->prepare("UPDATE Users SET status_user_id = ? WHERE user_id = ?");
               $update_status->bindValue(1, $goedgekeurd_id, PDO::PARAM_INT);
               $update_status->bindValue(2, $user_id, PDO::PARAM_INT);

               $update_status->execute();
               send_registration_mail($userdata['email'], $userdata['username']);
           } else if($answer == 'afgekeurd') {
               $update_status = $db->prepare("UPDATE Users SET status_user_id = ? WHERE user_id = ?");
               $update_status->bindValue(1, $afgekeurd_id, PDO::PARAM_INT);
               $update_status->bindValue(2, $user_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 = "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'] != ''){
               $filter = $filter . "AND username LIKE '" .  strip_tags($_POST['filter_user_name']) . "%'";
           }

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

           if($_POST['filter_privilege'] != ''){
               $filter = $filter . "AND privilege_id = '" .  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 dat het bladeren door tabellen werkt. */
   $beginpunt = 0;
   $rows_page_begin = 5;
   $rows_page = $rows_page_begin;
   $count = $db->prepare("SELECT COUNT(user_id) FROM Users LEFT JOIN User_labels Using (user_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 username, email, privilege, user_id, privilege_id, 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;");
    $stat->execute();
    $statuses = $stat->fetchAll(PDO::FETCH_ASSOC);


    // Th van de tabel
    echo '<table id="tabel">
        <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>
            </td>
            <td>' . $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>
            <td class="status status_screen_user">
            <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>
                    <td class="goedgekeurd"">
                    Goedgekeurd
                </td>
                <td class="laatste_td afgekeurd">
                    Afgekeurd
                </td>
                <input class="answer" type="hidden" name="answer[]" value="">
            </div>
            </tr>';
    }

    // Zorgt dat het bladeren van tabellen wordt geladen.
    echo '<tr>
            <td colspan="42" id="submit_button"><div><input  onclick="formSubmit(this)" type="submit" 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>';
    ?>
