<?php 
session_name('super_admin_session');
if (!is_dir(dirname(__DIR__) . '/session_data/super')) { mkdir(dirname(__DIR__) . '/session_data/super', 0700, true); } session_save_path(dirname(__DIR__) . '/session_data/super');
session_set_cookie_params(['path' => '/', 'httponly' => true, 'samesite' => 'Strict']);
session_start();
error_reporting(0);
include('includes/dbconnection.php');
require_once 'includes/session_term_helper.php';
$stResult = resolveCurrentSessionTerm($con);
$currentSessionName = $stResult['session_name'];
$currentTermName    = $stResult['term_name'];
include('includes/flash.php');

if (isset($_GET['blockid'])) {
    $blockedid = intval($_GET['blockid']);

    // Fetch permission of the blocked user
    $sql = "SELECT permission FROM tblusers WHERE id = :blockedid LIMIT 1";
    $query = $dbh->prepare($sql);
    $query->bindParam(':blockedid', $blockedid, PDO::PARAM_INT);
    $query->execute();
    $user = $query->fetch(PDO::FETCH_OBJ);

    if ($user) {
        // Restore status depending on permission
        $newStatus = ($user->permission === 'Teacher') ? 2 : 1;

        $sql = "UPDATE tblusers SET status = :status WHERE id = :blockedid";
        $query = $dbh->prepare($sql);
        $query->bindParam(':status', $newStatus, PDO::PARAM_INT);
        $query->bindParam(':blockedid', $blockedid, PDO::PARAM_INT);
        $query->execute();

        flash_alert('Restored successfully'); 
        header('Location: userregister.php'); exit;
    }
}
?>

<div class="card-body">
 <table class="table table-bordered table-striped">
  <thead>
    <tr>
      <th class="text-center">Names</th>
      <th class="text-center">Mobile</th>
      <th class="text-center">Email</th>
      <th class="text-center">Permission</th>
      <th class="text-center">Action</th>
    </tr>
  </thead>
  <tbody>
   <?php
   // Show only users that are blocked
   $sql = "SELECT * FROM tblusers WHERE status = 3";
   $query = $dbh->prepare($sql);
   $query->execute();
   $results = $query->fetchAll(PDO::FETCH_OBJ);

   if ($query->rowCount() > 0) {
       foreach ($results as $row) { ?>
       <tr>
         <td><a href="#"><?php echo htmlentities($row->name); ?> <?php echo htmlentities($row->lastname); ?></a></td>
         <td class="text-left">0<?php echo htmlentities($row->mobile); ?></td>
         <td class="text-left"><?php echo htmlentities($row->email); ?></td>
         <td class="text-left"><?php echo htmlentities($row->permission); ?></td>
         <td class="text-left">
           <a href="blockedusers.php?blockid=<?php echo $row->id; ?>" 
              onclick="return confirm('Do you really want to unblock this user?');" 
              title="Restore this User">Unblock</a>
         </td>
       </tr>
   <?php }
   } ?>
 </tbody>
</table>
</div>
<!-- /.card-body -->
