<?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);



ini_set('display_errors', 1);

error_reporting(E_ALL);



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');

} 

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

  $id = intval($_GET['id']); // Securely get the id



  // Make sure the columns are explicitly listed

  $restoreQuery = "INSERT INTO document (id, File_Name, Term, session, class, student_type, filename, file_type, file_size, 

  file_data, Date) 

  SELECT 

      id, File_Name, Term, session, class, student_type, filename, file_type, file_size, file_data, Date

  FROM deleted_document WHERE id = ?";



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

      mysqli_stmt_bind_param($stmt, "i", $id); // Bind the id to the prepared statement

      if (mysqli_stmt_execute($stmt)) {

          // Remove the record from the backup table after restoration

          $deleteBackupQuery = "DELETE FROM deleted_document WHERE id = ?";

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

              mysqli_stmt_bind_param($stmtDelete, "i", $id); // Bind the id to the delete statement

              if (mysqli_stmt_execute($stmtDelete)) {

                flash_alert('Document details restored successfully.'); 

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

              } else {

                flash_alert('Error removing document from backup table.'); 

                header('Location: restore_document.php'); exit;               }

          } else {

            flash_alert('Error preparing delete query.'); 

            header('Location: restore_document.php'); exit;           }

      } else {

        flash_alert('Error restoring document details.'); 

        header('Location: restore_document.php'); exit;       }

      mysqli_stmt_close($stmt);

  } else {

    flash_alert('Error preparing restore query.'); 

    header('Location: restore_document.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-folder-open"></i>

                        </div>

                        <div>

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

                            Document Management

                                <span class="premium-subtitle text-secondary">Manage Document</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 Document</li>

                        </ol>

                    </nav>

                </div>

            </div>

        </div>

    </div>

</section> 



      <!-- Main content -->

      <section class="content">

        <div class="container-fluid">

          <div class="row">

            <div class="col-12">

              <div class="card">

                <div class="card-header">

                  <h3 class="card-title">Deleted Document Information</h3>

                  <div class="card-tools">

                  <a href="restore_document.php"><button type="button" class="btn btn-sm btn-warning"  ><span style="color: #fff;"><i class="fas fa-undo" ></i> Restore document</span>

                  </button></a>

                    <a href="document_list.php"><button type="button" class="btn btn-sm btn-primary"  ><span style="color: #fff;"><i class="fas fa-list" ></i> document List</span>

                    </button> </a>                  

                  </div>

                </div>

                <!-- /.card-header -->

                <div id="editData" class="modal fade">

                  <div class="modal-dialog modal-xl">

                    <div class="modal-content">

                      <div class="modal-header">

                        <h5 class="modal-title">Document details</h5>

                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">

                          <span aria-hidden="true">&times;</span>

                        </button>

                      </div>

                      <div class="modal-body" id="info_update">
<!-- loaded via AJAX -->
</div>

                      <div class="modal-footer ">

                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>

                      </div>

                      <!-- /.modal-content -->

                    </div>

                    <!-- /.modal-dialog -->

                  </div>

                  <!-- /.modal -->

                </div>

                <!--   end modal -->

               

                <div id="editData2" class="modal fade">

                  <div class="modal-dialog ">

                    <div class="modal-content">

                      <div class="modal-header">

                        <h5 class="modal-title">document Details</h5>

                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">

                          <span aria-hidden="true">&times;</span>

                        </button>

                      </div>

                      <div class="modal-body" id="info_update2">
<!-- loaded via AJAX -->
</div>

                      <div class="modal-footer ">

                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>

                      </div>

                      <!-- /.modal-content -->

                    </div>

                    <!-- /.modal-dialog -->

                  </div>

                  <!-- /.modal -->

                </div>

                <!--   end modal -->



                <div class="card-body mt-2 " >

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

                    <thead> 

                      <tr> 

                      <th>#</th>

                        <th>File Name</th>

                        <th>Term</th>

                        <th>Session</th>

                        <th>Class </th>

                        <th>Date</th>

                        <th>Action</th>

                      </tr> 

                    </thead> 

                    <tbody>

                      <?php $query=mysqli_query($con,"select * from deleted_document");

                      $cnt=1;

                      while($row=mysqli_fetch_array($query))

                      {

                        ?>                  

                        <tr>

                        <td><?php echo htmlentities($cnt);?></td>

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

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

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

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

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

                          <td>

                            <a href="restore_document.php?id=<?php echo $row['id']?>&res=restore" onClick="return confirm('Are you sure you want to restore this document?')" class=" btn btn-danger btn-xs "><i class = " fas fa-undo"> Restore</i></a>



                          </td>

                        </tr>

                        <?php $cnt=$cnt+1;

                      } ?>

                    </tbody>

                  </table>

                </div>

                <!-- /.card-body -->

              </div>

              <!-- /.card -->

            </div>

            <!-- /.col -->

          </div>

          <!-- /.row -->

        </div>

        <!-- /.container-fluid -->

      </section>

      <!-- /.content -->

    </div>

    <!-- /.content-wrapper -->

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



    <!-- Control Sidebar -->

    <aside class="control-sidebar control-sidebar-dark">

      <!-- Control sidebar content goes here -->

    </aside>

    <!-- /.control-sidebar -->

  </div>



  <!-- ./wrapper -->

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

  <script type="text/javascript">

    $(document).ready(function(){

      $(document).on('click','.edit_data',function(){

        var edit_id=$(this).attr('id');

        $.ajax({

          url:"edit_document.php",

          type:"post",

          data:{edit_id:edit_id},

          success:function(data){

            $("#info_update").html(data);

            $("#editData").data('ajax-src', 'edit_document.php').modal('show');
            $("#editData").find('form:not([action])').attr('data-ajax-action', 'edit_document.php');

          }

        });

      });

    });

  </script>

  <script type="text/javascript">

    $(document).ready(function(){

      $(document).on('click','.edit_data2',function(){

        var edit_id2=$(this).attr('id');

        $.ajax({

          url:"view_document_info.php",

          type:"post",

          data:{edit_id2:edit_id2},

          success:function(data){

            $("#info_update2").html(data);

            $("#editData2").data('ajax-src', 'view_document_info.php').modal('show');
            $("#editData2").find('form:not([action])').attr('data-ajax-action', 'view_document_info.php');

          }

        });

      });

    });

  </script>

</body>

</html>

