<?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 (strlen($_SESSION['sid'] == 0)) {

    header('location:logout.php');

}



// Handle restore request

if (isset($_GET['res'])) {

    $id = intval($_GET['id']);



    // Step 1: Reactivate teacher in tblusers

    $restoreQuery = "UPDATE tblusers SET status = 2 WHERE id = ?";

    if ($stmt = mysqli_prepare($con, $restoreQuery)) {

        mysqli_stmt_bind_param($stmt, "i", $id);

        if (mysqli_stmt_execute($stmt)) {

            // Step 2: Remove from backup (optional clean-up)

            $deleteBackupQuery = "DELETE FROM deleted_teachers WHERE original_id = ?";

            if ($stmtDelete = mysqli_prepare($con, $deleteBackupQuery)) {

                mysqli_stmt_bind_param($stmtDelete, "i", $id);

                mysqli_stmt_execute($stmtDelete);

            }

            flash_alert('Teacher restored successfully.');

        } else {

            flash_alert('Error restoring teacher.');

        }

        mysqli_stmt_close($stmt);

    } else {

        flash_alert('Error preparing restore query.');

    }



    header('Location: restore_teacher.php'); exit;

}

?>



<!DOCTYPE html>

<html>

<?php @include("includes/head.php"); ?>



<body class="hold-transition sidebar-mini layout-fixed">

<div class="wrapper">

    <!-- Navbar -->

    <?php @include("includes/header.php"); ?>

    <!-- /.navbar -->



    <!-- Main Sidebar Container -->

    <?php @include("includes/sidebar.php"); ?>



    <!-- Content Wrapper. Contains page content -->

    <div class="content-wrapper">

        <!-- Content Header (Page header) -->

        <section class="content-header premium-header">

    <div class="container-fluid">

        <div class="header-glass-card">

            <div class="row align-items-center">

                <div class="col-md-8">

                    <div class="title-section">

                        <div class="icon-box bg-primary-gradient shadow-sm">

                            <i class="fas fa-user-tie"></i>

                        </div>

                        <div>

                            <h1 class="premium-title text-primary">

                            Teacher Information Management

                                <span class="premium-subtitle text-secondary">Manage Teacher</span>

                            </h1>
                            <div class="mt-1">
                              <?php if ($currentSessionName): ?>
                                <span class="badge badge-primary mr-1">
                                  <i class="fas fa-calendar-alt"></i>
                                  <?= htmlspecialchars($currentSessionName) ?>
                                </span>
                              <?php endif; ?>
                              <?php if ($currentTermName): ?>
                                <span class="badge badge-success">
                                  <i class="fas fa-bookmark"></i>
                                  <?= htmlspecialchars($currentTermName) ?>
                                </span>
                              <?php else: ?>
                                <span class="badge badge-warning">
                                  <i class="fas fa-exclamation-triangle"></i> No active term
                                </span>
                              <?php endif; ?>
                            </div>

                        </div>

                    </div>

                </div>

                <div class="col-md-4">

                    <nav aria-label="breadcrumb">

                        <ol class="breadcrumb premium-breadcrumb">

                            <li class="breadcrumb-item"><a href="dashboard.php" class="text-primary">Home</a></li>

                            <li class="breadcrumb-item active" aria-current="page">Restore Teacher</li>

                        </ol>

                    </nav>

                </div>

            </div>

        </div>

    </div>

</section> 



        <!-- Deleted Teachers List -->

        <section class="content">

            <div class="container-fluid">

                <div class="card card-primary">

                    <div class="card-header">

                        <h5 class="card-title font-weight-bold">

                            <i class="fas fa-list"></i> Deleted Teachers

                        </h5>

                        <div class="card-tools">

                            <a href="teacher_list.php">

                                <button class="btn btn-sm btn-primary">

                                    <i class="fas fa-list"></i> Teacher List

                                </button>

                            </a>

                        </div>

                    </div>

                    <div class="card-body">

                        <table id="example1" class="table table-bordered table-hover">

                            <thead>

                                <tr>

                                    <th>#</th>

                                    <th>Teacher Image</th>

                                    <th>Full Name</th>

                                    <th>Username</th>

                                    <th>Email</th>

                                    <th>Mobile</th>

                                    <th>Action</th>

                                </tr>

                            </thead>

                            <tbody>

                                <?php

                                // ✅ Fetch deleted teachers directly from tblusers

                                $query = mysqli_query($con, "SELECT * FROM tblusers WHERE status = 0");

                                $cnt = 1;

                                while ($row = mysqli_fetch_array($query)) {

                                ?>

                                    <tr>

                                        <td><?php echo $cnt++; ?></td>

                                        <td><img src="staff_images/<?php echo htmlentities($row['userimage']); ?>" width="40" height="40"></td>

                                        <td><?php echo htmlentities($row['name'] . ' ' . $row['lastname']); ?></td>

                                        <td><?php echo htmlentities($row['username']); ?></td>

                                        <td><?php echo htmlentities($row['email']); ?></td>

                                        <td><?php echo htmlentities($row['mobile']); ?></td>

                                        <td>

                                            <a href="restore_teacher.php?id=<?php echo $row['id']; ?>&res=restore"

                                               class="btn btn-danger btn-xs"

                                               onClick="return confirm('Are you sure you want to restore this teacher?')">

                                                <i class="fas fa-undo"></i> Restore

                                            </a>

                                        </td>

                                    </tr>

                                <?php } ?>

                            </tbody>

                        </table>

                    </div>

                </div>

            </div>

        </section>

    </div>



    <?php @include("includes/footer.php"); ?>

</div>

<?php @include("includes/foot.php"); ?>

</body>

</html>

