<?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'];




if (strlen($_SESSION['sid']) == 0) {

    header('location:logout.php');

}



// Handle deletion or status update

if (isset($_GET['res']) && $_GET['res'] == 'resolve') {

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

    $query = mysqli_query($con, "UPDATE message SET status = 0 WHERE id = '$id'");

    if ($query) {

        $_SESSION['delmsg'] = "Message marked as resolved!";

    } else {

        $_SESSION['errormsg'] = "Error: Unable to update message status!";

    }

}



if (isset($_GET['action']) && $_GET['action'] == 'delete') {

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



  // Retrieve the record to archive

  $query = mysqli_query($con, "SELECT * FROM message WHERE id = '$id'");

  if ($row = mysqli_fetch_assoc($query)) {

      // Insert into archived_messages table

      $archive_query = mysqli_query($con, "INSERT INTO archived_messages (regno, fullname, class, purpose, amount, method, status, date) 

                                           VALUES ('{$row['regno']}', '{$row['fullname']}', '{$row['class']}', '{$row['purpose']}', 

                                                   '{$row['amount']}', '{$row['method']}', '{$row['status']}', '{$row['date']}')");

      

      if ($archive_query) {

          // Delete from message table

          $delete_query = mysqli_query($con, "DELETE FROM message WHERE id = '$id'");

          if ($delete_query) {

              $_SESSION['delmsg'] = "Message archived successfully!";

          } else {

              $_SESSION['errormsg'] = "Error: Unable to delete message!";

          }

      } else {

          $_SESSION['errormsg'] = "Error: Unable to archive message!";

      }

  } else {

      $_SESSION['errormsg'] = "Error: Message not found!";

  }

}

?>

<!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">

        <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-wallet"></i>

                        </div>

                        <div>

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

                            Payment Management

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

                        </ol>

                    </nav>

                </div>

            </div>

        </div>

    </div>

</section> 

      <!-- Feedback Alert -->

      <?php if (!empty($_SESSION['delmsg'])) { ?>

        <div class="alert alert-primary alert-dismissible fade show" role="alert">

          <?php echo $_SESSION['delmsg']; ?>

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

          &times;

          </button>

        </div>

        <?php unset($_SESSION['delmsg']); ?>

      <?php } elseif (!empty($_SESSION['errormsg'])) { ?>

        <div class="alert alert-danger alert-dismissible fade show" role="alert">

          <?php echo $_SESSION['errormsg']; ?>

          <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">&times;</button>

        </div>

        <?php unset($_SESSION['errormsg']); ?>

      <?php } ?>



      <!-- Main content -->

      <section class="content">

        <div class="container-fluid">

          <div class="row">

            <div class="col-12">

              <div class="card card-primary">

                <div class="card-header">

                  <h5 class="card-title font-weight-bold"><i class="fas fa-exclamation-triangle"></i> Unresolved Payment Request</h5>

                  <div class="card-tools">

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

                  </button></a>

                    <a href="approve.php"><button type="button" class="btn btn-sm btn-primary"><span style="color: #fff;"><i class="ion ion-person-add"></i> Confirm Payment</span></button></a>

                  </div>

                </div>

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

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

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

                    <thead> 

                      <tr> 

                        <th>#</th>

                        <th>Admission No.</th>

                        <th>Fullname</th>

                        <th>Class</th>

                        <th>Payment Purpose</th>

                        <th>Amount</th>

                        <th>Payment Method</th>

                        <th>Action</th>

                      </tr> 

                    </thead> 

                    <tbody>

                      <?php

                      $query = mysqli_query($con, "SELECT * FROM message WHERE status = 3 ORDER BY date DESC");

                      $cnt = 1;

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

                        <tr>

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

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

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

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

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

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

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

                          <td>

  <a href="requestun.php?id=<?php echo $row['id']; ?>&res=resolve" onClick="return confirm('Are you sure you want to resolve?')" class="btn btn-primary btn-xs"><i class="fa fa-check"></i> Resolved</a>

  <a href="requestun.php?id=<?php echo $row['id']; ?>&action=delete" onClick="return confirm('Are you sure you want to delete?')" class="btn btn-danger btn-xs"><i class="fa fa-trash"></i> Delete</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"); ?>

</body>

</html>

