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

require_once 'vendor/autoload.php'; // Load Composer's autoloader for mPDF
require_once __DIR__ . '/vendor/autoload.php'; // Adjust the path if necessary
// Include the PHP QR Code library
include('phpqrcode/qrlib.php'); // Adjust path as neede

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use Mpdf\Mpdf; // Import the mPDF class
use Dotenv\Dotenv;

// required files
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';

// Load environment variables
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$school_name = $_ENV['SCHOOL_NAME'];
$school_address = $_ENV['SCHOOL_ADDRESS'];
$logo = $_ENV['WEBSITE_LOGO'];


function convertAmountToWords($number)
{
  $hyphen = '-';
  $conjunction = ' and ';
  $separator = ', ';
  $negative = 'negative ';
  $decimal = ' point ';
  $dictionary = array(
    0 => 'Zero',
    1 => 'One',
    2 => 'Two',
    3 => 'Three',
    4 => 'Four',
    5 => 'Five',
    6 => 'Six',
    7 => 'Seven',
    8 => 'Eight',
    9 => 'Nine',
    10 => 'Ten',
    11 => 'Eleven',
    12 => 'Twelve',
    13 => 'Thirteen',
    14 => 'Fourteen',
    15 => 'Fifteen',
    16 => 'Sixteen',
    17 => 'Seventeen',
    18 => 'Eighteen',
    19 => 'Nineteen',
    20 => 'Twenty',
    30 => 'Thirty',
    40 => 'Forty',
    50 => 'Fifty',
    60 => 'Sixty',
    70 => 'Seventy',
    80 => 'Eighty',
    90 => 'ninety',
    100 => 'Hundred',
    1000 => 'Thousand',
    1000000 => 'Million',
    1000000000 => 'Billion'
  );

  if (!is_numeric($number)) {
    return false;
  }

  if ($number < 0) {
    return $negative . convertAmountToWords(abs($number));
  }

  $string = $fraction = null;

  if (strpos((string) $number, '.') !== false) {
    list($number, $fraction) = explode('.', (string) $number);
  }

  switch (true) {
    case $number < 21:
      $string = $dictionary[$number];
      break;
    case $number < 100:
      $tens = ((int) ($number / 10)) * 10;
      $units = $number % 10;
      $string = $dictionary[$tens];
      if ($units) {
        $string .= $hyphen . $dictionary[$units];
      }
      break;
    case $number < 1000:
      $hundreds = (int) ($number / 100);
      $remainder = $number % 100;
      $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
      if ($remainder) {
        $string .= $conjunction . convertAmountToWords($remainder);
      }
      break;
    default:
      $baseUnit = pow(1000, floor(log($number, 1000)));
      $numBaseUnits = (int) ($number / $baseUnit);
      $remainder = $number % $baseUnit;
      $string = convertAmountToWords($numBaseUnits) . ' ' . $dictionary[$baseUnit];
      if ($remainder) {
        $string .= $remainder < 100 ? $conjunction : $separator;
        $string .= convertAmountToWords($remainder);
      }
      break;
  }

  if ($fraction !== null && is_numeric($fraction)) {
    $string .= $decimal;
    $words = array();
    foreach (str_split((string) $fraction) as $digit) {
      $words[] = $dictionary[$digit];
    }
    $string .= implode(' ', $words);
  }

  return $string;
}

$message_id = $_GET['id'];
$messageData = $_SESSION['message_data'];
if (strlen($_SESSION['sid'] == 0)) {
  header('location:logout.php');
} else {
  if (isset($_POST['disapprove'])) {

    $regno = $_POST['fullname'];
    $adno = $_POST['fullname'];
    $term = $_POST['term'];
    $session = $_POST['session'];
    $class = $_POST['class'];
    $school_type = $_POST['school_type'];
    $payment_type = $_POST['payment_type'];
    $pay_id = $_POST['pay_id'];
    $amount = $_POST['amount'];
    $payment_for = $_POST['pay_for'];
    $studentname = $_POST['fullname'];
    $email = $_POST['email'];

    try {
      // Update the message status to 'disapproved' (set status = 2, assuming 2 indicates disapproval)
      $stmtDisapprove = $dbh->prepare("UPDATE message SET status = 2 WHERE id = :message_id AND status = 0 LIMIT 1");
      $stmtDisapprove->bindParam(':message_id', $message_id);
      $stmtDisapprove->execute();

      // Notify user via email about disapproval
      try {
        $mail = new PHPMailer(true);
        // SMTP configuration from .env
        $mail->isSMTP();
        $mail->Host = $_ENV['SMTP_HOST'];
        $mail->SMTPAuth = true;
        $mail->Username = $_ENV['SMTP_USERNAME'];
        $mail->Password = $_ENV['SMTP_PASSWORD'];
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
        $mail->Port = $_ENV['SMTP_PORT'];
        $mail->Timeout = 10;

        $mail->setFrom($_ENV['SMTP_USERNAME'], $_ENV['SCHOOL_NAME']);
        $mail->addAddress($email, $studentname);

        $mail->isHTML(true);
        $mail->Subject = $school_name . ' ' . 'Payment Disapproval Notification';
        $mail->Body = "
            <html>
            <body>
                <div style='max-width: 600px; margin: auto; font-family: Arial, sans-serif; background-color: #f4f4f4; padding: 20px;'>
                    <h1 style='color: #ffffff; text-align: center; padding: 10px 0; background-color: #f44336;'>Payment Disapproved</h1>
                    <p>Dear $studentname,</p>
                    <p>We regret to inform you that your payment of NGN $amount has been disapproved.</p>
                    <p>The payment was disapproved due to uncertainity of the payment</p>
                    <p>Kindly wait for two days to confirm if your bank has reversed the transaction made. Then, make the payment again.</p>
                    <p>For more questions and clarifications, please fill the feedback form at the bottom right of the student portal and you will be responded to.</p>
                    <p>Thank you!</p>
                    <hr>
                    <center>
                        <p>$school_name &copy; " . date("Y") . ". All Rights Reserved.</p>
                    </center>
                </div>
            </body>
            </html>";

        $mail->send();

        $mail->send();
        flash_alert('Payment disapproved and Email sent successfully!', 'entrancepay1.php');
        header('Location: entrancepay1.php');
        exit;
      } catch (Exception $e) {
        flash_alert('Payment disapproved, but email could not be sent. Please try again.', 'entrancepay1.php');
        header('Location: entrancepay1.php');
        exit;
      }
    } catch (Exception $e) {
      flash_alert('Something went wrong. Payment disapproval failed. Please try again.');
    }
  }
  if (isset($_POST['unresolved'])) {
    try {
      $dbh->beginTransaction();

      // Update the message status to 2 for unresolved payments
      if (!empty($message_id)) {
        $stmtUpdate = $dbh->prepare("UPDATE message SET status = 3 WHERE id = :message_id AND status = 0 LIMIT 1");
        $stmtUpdate->bindParam(':message_id', $message_id, PDO::PARAM_INT);
        $stmtUpdate->execute();
      }

      $dbh->commit();
      unset($_SESSION['id']);
      unset($_SESSION['message_data']);
      flash_alert('Payment marked as unresolved, and message status updated successfully!', 'pay_tuition.php');
      header('Location: pay_tuition.php');
      exit;
    } catch (Exception $e) {
      $dbh->rollBack();
      flash_alert('Failed to mark payment as unresolved. Please try again.', 'pay_tuition.php');
      header('Location: pay_tuition.php');
      exit;
    }
  }
  if (isset($_POST['unrelated'])) {
    try {
      $dbh->beginTransaction();

      // Update the message status to 2 for unresolved payments
      if (!empty($message_id)) {
        $stmtUpdate = $dbh->prepare("UPDATE message SET status = 5 WHERE id = :message_id AND status = 0 LIMIT 1");
        $stmtUpdate->bindParam(':message_id', $message_id, PDO::PARAM_INT);
        $stmtUpdate->execute();
      }

      $dbh->commit();
      unset($_SESSION['id']);
      unset($_SESSION['message_data']);
      flash_alert('Payment marked as unrelated, and message status updated successfully!', 'entrancepay1.php');
      header('Location: entrancepay1.php');
      exit;
    } catch (Exception $e) {
      $dbh->rollBack();
      flash_alert('Failed to mark payment as unrelated. Please try again.', 'entrancepay1.php');
      header('Location: entrancepay1.php');
      exit;
    }
  }


  if (isset($_POST['submit'])) {
    $adno = $_POST['fullname'];
    $regno = $_POST['email'];
    $term = $_POST['term'];
    $session = $_POST['session'];
    $class = $_POST['class'];
    $school_type = $_POST['school_type'];
    $payment_type = $_POST['payment_type'];
    $pay_id = $_POST['pay_id'];
    $amount = $_POST['amount'];
    $payment_for = $_POST['pay_for'];
    $studentname = $_POST['fullname'];
    $email = $_POST['email'];
    $method = $_POST['method'];

    // Check for duplicate payment before proceeding
    $duplicateCheck = mysqli_query($con, "SELECT id FROM payment WHERE studentno = '$adno' AND pay_id = '$pay_id' LIMIT 1");
    if (mysqli_num_rows($duplicateCheck) > 0) {
      flash_alert('Duplicate payment detected! This payment has already been approved.', 'entrancepay1.php');
      header('Location: entrancepay1.php');
      exit;
      exit;
    }



    $query = mysqli_query($con, "INSERT INTO payment(studentno, studentName, Term, session, class, school_type, payment_type, pay_id, amount, pay_for,method) VALUES ('$adno', '$studentname', '$term', '$session', '$class', '$school_type', '$payment_type', '$pay_id', '$amount', '$payment_for','$method')");

    if ($query) {



      $payment_date = date('Y-m-d');
      $payment_time = date('H:i:s');
      $qrContent = 'Student Name: ' . $studentname . "\n" .
        'Receipt No: R000' . $pay_id . "\n" .
        'Amount Paid: ' . number_format($amount) . "\n" .
        'Payment For: ' . $pay_for . "\n" .
        'Term: ' . $Term . "\n" .
        'Session: ' . $session . "\n" .
        'Payment Date: ' . $payment_date;
      $qrFile = 'qrcodes/payment_' . $studentname . '.png';
      QRcode::png($qrContent, $qrFile, QR_ECLEVEL_L, 4); // Adjust size if needed

      $amountInWords = convertAmountToWords($amount);


      // Update message status using the passed id
      if (!empty($message_id)) {
        $stmtUpdate = $dbh->prepare("UPDATE message SET status = 1 WHERE id = :message_id AND status = 0 LIMIT 1");
        $stmtUpdate->bindParam(':message_id', $message_id);
        $stmtUpdate->execute();
      }

      // Generate PDF receipt
      $mpdf = new Mpdf();
      // Set the watermark image
      $mpdf->SetWatermarkImage($logo, 0.1); // 0.1 for light opacity, 'F' for fit to page
      $mpdf->showWatermarkImage = true;
      $html = "
             <style>
     body {
        font-family: Times New Roman, serif;
        margin: 0;
        padding: 0;
    }
        .details {
     
       
        width: 100%;
    
    }
    .child{
    float: left;
    width: 50%;
    
    }
    .left {
    margin-left: 100%;

        font-size: 14px;
        font-weight: bold;
    }
         .right {
    margin-left: 50%;
    
        font-size: 14px;
        font-weight: bold;
    }
        .text-center {
            text-align: center;
        }
        .text-right {
            text-align: right;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        table, th, td {
            border: 1px solid black;
        }
        th, td {
            padding: 8px;
            font-size: 12px;
            text-align: left;
        }
        p {
            margin: 5px 0;
            font-size: 12px;
        }
        .header, .footer {
            font-size: 14px;
        }
        .flex-container {
            display: flex;
            justify-content: space-between;
            width: 100%;
        }
        .flex-container > div {
            width: 45%; /* Adjust as needed */
        }
        .qr-code {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-left: 20px; /* Space between the text and QR code */
        }
    </style>
    <body>
    <div class='text-center header'>
        <h1>" . strtoupper($school_name) . "</h1>
    <b><i>$school_address</i></b><br>
    <img src=$logo alt='logo' width ='80' height ='100'><br>
        <hr>
        <strong>Receipt</strong>
    </div>
    <hr>
     <!-- Details Section -->
<div class='details'>
<div class='child'>
    <div class='left'> <p>Student Name: <strong>$adno</strong></p>
            <p>Transaction ID: <strong>$pay_id</strong></p>
            <p>Class: <strong>$class</strong></p>
            <p>Payment Status: <strong>Approved</strong></p>
            </div>
    </div>
    <div class='child'>
    <div class='right'><img src='$qrFile'alt='QR Code' width='100' height='100'></div>
</div>
</div>

    <hr>
    <div>
        <p class='text-center'><strong>Payment Summary</strong></p>
        <table>
            <tr>
                <th>Fee Details</th>
                <th>Payment Details</th>
            </tr>
            <tr>
                <td>
                    <p>Payment Received from <strong>$adno</strong> </p><br><p>The sum of <strong>NGN $amount</strong> (<strong> $amountInWords Naira only</strong>)</p><br>
                    <p>Being paid for <strong>$payment_for</strong> </p><br>
                    <p>Term: <strong>$term</strong></p><br>
                    <p>Session: <strong>$session</strong> academic session</p><br>
                    
                </td>
                <td>
                    <table style='width: 100%; border-collapse: collapse;'>
                        <tr>
                            <td><strong>Date</strong></td>
                             <td class='text-right'><strong>$payment_date</strong></td>
                            
                        </tr>
                        <tr>
                                <td><strong>Time</strong></td>
                                <td class='text-right'><strong>$payment_time</strong></td>
                        </tr>
                        <tr>
                            <th>Amount (NGN)</th>
                            <th class='text-right'><strong>" . number_format($amount) . "</strong></th>
                        </tr>
                        
                    </table>
                </td>
            </tr>
            <tr>
                <td>
                <center>
                <em> Thank you for your payment</em>
                </center>
                </td>
                <td class ='text-right'>
                <center>
                <em>No Refund Policy</em>
                </center>
                </td>
                </tr>
        </table>
        <div class='text-center footer'>
            <hr>
            <strong>$school_name &copy; " . date('Y') . "</strong>
            <hr>
        </div>
    </div>
    <body>";
      $mpdf->WriteHTML($html);
      $pdfContent = $mpdf->Output('', 'S'); // Output to string (do not save to file)



      // Send Email Notification
      try {
        $mail = new PHPMailer(true);
        $mail->isSMTP();
        $mail->Host = $_ENV['SMTP_HOST'];
        $mail->SMTPAuth = true;
        $mail->Username = $_ENV['SMTP_USERNAME'];
        $mail->Password = $_ENV['SMTP_PASSWORD'];
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
        $mail->Port = $_ENV['SMTP_PORT'];
        $mail->Timeout = 10;


        $mail->setFrom($_ENV['SMTP_USERNAME'], $_ENV['SCHOOL_NAME']);
        $mail->addAddress($email, $studentname);

        // Email content
        $mail->isHTML(true);
        $mail->Subject = $school_name . ' ' . 'Payment Approval Notification';
        $mail->Body = "
         <html>
         <body>
             <div style='max-width: 600px; margin: auto; font-family: Arial, sans-serif; background-color: #f4f4f4; padding: 20px;'>
                 <h1 style='color: #ffffff; text-align: center; padding: 10px 0; background-color: #4CAF50;'>Payment Approved</h1>
                 <p>Dear $studentname,</p>
                 <p>We are pleased to inform you that your payment of NGN $amount has been confirmed successfully.</p>
                 <p>Please find your receipt attached.</p>
                 <p>Thank you!</p>
                 <hr>
                 <p>If you have any questions or need further assistance, please feel free to reach out to us. Thank you!</p>
                                <hr>
                                <center>
                            <p>$school_name &copy; " . date("Y") . ". All Rights Reserved.</p>
                                </center>
                 <hr>
             </div>
         </body>
         </html>";

        // Attach PDF receipt
        $mail->addStringAttachment($pdfContent, 'receipt.pdf', 'base64', 'application/pdf');


        $mail->send();
        unset($_SESSION['id']);
        unset($_SESSION['message_data']);
        flash_alert('Payment approved and Email sent successfully!', 'entrancepay1.php');
        header('Location: entrancepay1.php');
        exit;
      } catch (Exception $e) {
        flash_alert('Payment approved, but email could not be sent. Please try again.', 'entrancepay1.php');
        header('Location: entrancepay1.php');
        exit;
      }
    } else {
      flash_alert('Something went wrong. Payment was not approved. Please try again.');
    }
  }
}
?>
<!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-credit-card"></i>
                  </div>
                  <div>
                    <h1 class="premium-title text-primary">
                      Payment Management
                      <span class="premium-subtitle text-secondary">Approve Lesson Fee (External)</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"><a href="approve.php" class="text-primary">Approve Payment</a></li>
                    <li class="breadcrumb-item active" aria-current="page">Pay Lesson Fee</li>
                  </ol>
                </nav>
              </div>
            </div>
          </div>
        </div>
      </section>

      <!-- Main content -->
      <section class="content">
        <div class="container-fluid">
          <div class="row ">
            <div class="col-md-12">
              <!-- general form elements -->
              <div class="card card-primary">
                <div class="card-header">
                  <h5 class="card-title font-weight-bold"><i class="fas fa-wallet"></i> Lesson Fees Payment (External
                    Student only)</h5>
                </div>
                <!-- /.card-header -->
                <!-- form start -->
                <form role="form" method="post" enctype="multipart/form-data">
                  <div class="card-body">
                    <span style="color: brown">
                      <h5>Student's Details</h5>
                    </span>
                    <hr>
                    <div class="row">

                      <div class="form-group col-md-4">
                        <label for="sex">Student's Fullname</label>
                        <input type="text" class="form-control" id="fullname" name="fullname"
                          value="<?php echo $messageData['fullname']; ?>" required>
                      </div>
                      <div class="form-group col-md-4">
                        <label for="sex">Email</label>
                        <input type="text" class="form-control" id="email" name="email"
                          value="<?php echo $messageData['email'] ?? $messageData['regno']; ?>" required>
                      </div>
                      <div class="form-group col-md-4">
                        <label for="age">Term</label>
                        <select name="term" id="term" class="form-control" required>
                          <?php
                          $termResult = $con->query("SELECT DISTINCT id, term_name FROM school_terms ORDER BY term_name ASC");
                          while ($row = $termResult->fetch_assoc()) {
                            $selected = ($messageData['term'] ?? '') == $row['term_name'] ? 'selected' : '';
                            echo "<option value='{$row['term_name']}' $selected>{$row['term_name']}</option>";
                          }
                          ?>
                          <option value="Fourth Term">Fourth Term</option>
                        </select>
                      </div>
                      <div class="form-group col-md-4">
                        <label for="sex">Session</label>
                        <!-- option for session using preselect -->
                        <select name="session" id="session" class="form-control" required>
                          <option value="">-- Select Session --</option>

                          <?php
                          $sessionResult = $con->query("SELECT DISTINCT id, session_name FROM school_sessions ORDER BY session_name DESC");
                          while ($row = $sessionResult->fetch_assoc()) {
                            $selected = ($messageData['session'] ?? '') == $row['session_name'] ? 'selected' : '';
                            echo "<option value='{$row['session_name']}' $selected>{$row['session_name']}</option>";
                          }
                          ?>
                        </select>

                      </div>
                      <div class="form-group col-md-4">
                        <label for="age">Class</label>
                        <input type="text" class="form-control" id="class" name="class"
                          value="<?php echo $messageData['class']; ?>" required>
                      </div>

                      <div class="form-group col-md-4">
                        <label for="age">School Type</label>
                        <input type="text" class="form-control" id="school_type" name="school_type"
                          value="<?php echo $messageData['school_type']; ?>" required>
                      </div>



                    </div>
                    <div class="row">
                      <div class="form-group col-md-4">
                        <label for="age">Payment Type</label>
                        <input type="text" class="form-control" id="payment_type" name="payment_type"
                          value="Full Payment" required>
                      </div>
                      <div class="form-group col-md-4">
                        <label for="age">Payment ID</label>
                        <input type="text" class="form-control" name="pay_id" id="pay_id"
                          value="<?php
                          function generate_pay_ID($length = 15)
                          {
                            $char = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSQTUVWXYZ';
                            $charlength = strlen($char);
                            $randomString = '';
                            for ($i = 0; $i < $length; $i++) {
                              # code...
                              $randomString .= $char[rand(0, $charlength - 1)];
                            }
                            return $randomString;
                          }
                          $tid = generate_pay_ID();
                          echo isset($pay_id) ? $pay_id : $tid ?>" required readonly>
                      </div>
                      <div class="form-group col-md-4">
                        <label for="age">Payment For</label>
                        <input type="text" class="form-control" id="pay_for" name="pay_for"
                          value="<?php echo $messageData['purpose']; ?>" required>
                      </div>

                      <div class="form-group col-md-4">
                        <label for="age">Payment Method</label>
                        <input type="text" class="form-control" id="method" name="method"
                          value="<?php echo $messageData['method']; ?>" required>
                      </div>

                      <div class="form-group col-md-4">
                        <label for="amount_display">Amount Paid (NGN)</label>
                        <!-- Visible, formatted for user -->
                        <input type="text" class="form-control" id="amount_display"
                          value="<?php echo number_format($messageData['amount']); ?>" readonly
                          oninput="updateHiddenAmount(this)">

                        <!-- Hidden, real value for DB -->
                        <input type="hidden" id="amount" name="amount" value="<?php echo $messageData['amount']; ?>">
                      </div>

                    </div>

                    <!-- /.card-body -->
                    <div class="card-footer">
                      <center>
                        <button type="submit" name="unrelated" id="unrelated" class="btn btn-warning mb-3"><i
                            class="fas fa-exclamation-circle"></i> Mark as Unrelated</button>
                        <button type="submit" name="submit" id="submit" class="btn btn-success mb-3"> <i
                            class="fas fa-check-circle"></i> Approve</button>
                        <button type="submit" name="disapprove" id="disapprove" class="btn btn-danger mb-3"><i
                            class="fas fa-times-circle"></i> Disapprove</button>
                        <button type="submit" name="unresolved" id="unresolved" class="btn btn-info mb-3"><i
                            class="fas fa-question-circle"></i> Mark as Unresolved</button>
                      </center>
                    </div>
                </form>
              </div>
              <!-- /.card -->
            </div>
            <!-- /.col -->
          </div>
          <!-- /.row -->
        </div>
        <!-- /.container-fluid -->
      </section>
      <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
    <?php @include("includes/footer.php"); ?>

  </div>


  <!-- ./wrapper -->
  <?php @include("includes/foot.php"); ?>
  <script>
    $(document).ready(function () {
      // Initialize Select
      $('#session').select2();
      $('#term').select2();
    });
    function updateHiddenAmount(displayInput) {
      // 1. Remove everything except numbers (and one decimal point if needed)
      // This turns "1,500.00" into "1500.00"
      let cleanValue = displayInput.value.replace(/[^0-9.]/g, '');

      // 2. Update the hidden field with the raw number
      document.getElementById('amount').value = cleanValue;

      // 3. Optional: Re-format the display field (so user sees commas as they type)
      // Note: To prevent cursor jumping, we only format on "blur" usually, 
      // but here is a simple way to keep the hidden field in sync:
      console.log("Hidden value is now: " + document.getElementById('amount').value);
    }
  </script>
</body>

</html>