<?php
  require_once("../../session_check.php");
  require_once("../../../classes/main_classes.php");

  //OBJECTS
  $retrieveObj=new RETRIEVE();

  $get_users=$retrieveObj->retriever("users 
			JOIN user_roles ON user_roles.user_id=users.user_id
			JOIN user_role ON user_roles.role_id=user_role.role_id
			WHERE users.status='deleted'
			GROUP BY users.user_id ORDER BY users.user_id DESC","DISTINCT *","fetchAll");
?>

<h4 class="page-title">
  <span class="la la-users"></span>
  Users
</h4>
<p><a href="#" class="bold" onclick="users_view()">Users</a> <span class="fas fa-angle-right"></span> Removed users</p>
<div class="row">
	<div class="col-md-12 mb-3">
		<div id="delete_user_response"></div>
		<table class="table table-head-bg-primary mt-4" id="users_table">
			<thead>
				<tr>
                  <th scope="col">Profile image</th>
                  <th scope="col">Firstname</th>
                  <th scope="col">Lastname</th>
                  <th scope="col">Email</th>
                  <th scope="col">User role</th>
                  <th scope="col"></th>
                </tr>
			</thead>
			<tbody>
				<?php
                    
                    if($get_users!="1020"){
                      $element_id="element_id";
                        foreach ($get_users as $user) {
                          $element_id=$element_id."Ot";
                  ?>  
                      <tr id="<?php echo $element_id ?>">         
                        <td class="text-center">
                          <?php
                            if(!empty($user->photo)){
                              echo $image='<img class="m-1" style="max-height:40px;" src="../img/profile_pictures/'.$user->photo.'?timestamp'.time().'" alt="Profile img"/>';
                            }else{
                              echo "<span class='fas fa-user fa-2x'></span>";
                            }
                          ?>
                        </td>
                        <td>
                          <?php 
                            echo $user->firstname;
                          ?>
                        </td>
                        <td>
                          <?php 
                            echo $user->lastname;
                          ?>
                        </td>
                        <td>
                          <?php 
                            echo $user->email;
                          ?>
                        </td>
                        <td>
                          <?php 
                            echo $user->user_role;
                          ?>
                        </td>
                        <td id="<?php echo $element_id.'Dbtn' ?>">
                        <?php
                          if($user_in_type=="su_admin" and !($user_id==$user->user_id)){
                        ?>
                            <div class="">
                              <form id="<?php echo 'R'.$element_id.'formI' ?>" class="mb-0 hand_cursor" style="display: inline;">
                                  <input type="hidden" name="restore_user_note" value="<?php echo $user->user_id; ?>">
                                  <button id="<?php echo $element_id.'Rbtn' ?>" onclick="restore_user('<?php echo 'R'.$element_id.'formI' ?>','<?php echo $element_id; ?>','delete_user_response')" type="submit" title="Restore" class="btn btn-success hand_cursor"><span class="fas fa-sync"></span> Restore</button>
                              </form>
                              <form id="<?php echo 'R'.$element_id.'form' ?>" class="mb-0 hand_cursor" style="display: inline;">
                                  <input type="hidden" name="permanently_delete_user_note" value="<?php echo $user->user_id; ?>">
                                  <button id="<?php echo $element_id.'Rbtn' ?>" onclick="permanently_delete_user('<?php echo 'R'.$element_id.'form' ?>','<?php echo $element_id; ?>','delete_user_response')" type="submit" title="Delete" class="btn btn-danger hand_cursor"><span class="fas fa-times "></span> Delete</button>
                              </form>
                              
                            </div>
                        <?php
                          }
                        ?>
                            
                        </td>
                      </tr>
                    <?php
                        }
                      }
                    ?>
				
			</tbody>
		</table>
	</div>
</div>
<script type="text/javascript">
  $(document).ready(function () {
    var table = $('#users_table').DataTable({
      columnDefs: [
        {bSortable: false, targets: [5]} 
      ]
    });
  })
</script>