<?php
include('includes/session2.php');
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'];



// Load Composer dependencies
require_once 'vendor/autoload.php';
// Required files
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use Dotenv\Dotenv;

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

$school_name =$_ENV['SCHOOL_NAME'];

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $regno = $_POST['email'];
    $fullname = $_POST['fullname'];
    $class = $_POST['class'];
    $term = $_POST['term'];
    $session = $_POST['session'];
    $school_type = $_POST['school_type'];
    $amount = $_POST['amount'];
    $purpose = $_POST['purpose'];
    $method = $_POST['method'];



// Check the last request time for this student and payment purpose
$query = "SELECT UNIX_TIMESTAMP(timestamp) AS lastRequestTime 
          FROM message 
          WHERE regno = :regno AND purpose = :purpose 
          ORDER BY timestamp DESC 
          LIMIT 1";
$stmt = $dbh->prepare($query);
$stmt->bindParam(':regno', $regno);
$stmt->bindParam(':purpose', $purpose);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);


    // Insert payment details into the payment table
    $sql = "INSERT INTO message (regno, fullname, class, term, session, school_type, amount, purpose, method) VALUES (:regno, :fullname, :class, :term, :session, :school_type, :amount, :purpose, :method)";
    $stmt = $dbh->prepare($sql);
    $stmt->bindParam(':regno', $regno);
    $stmt->bindParam(':fullname', $fullname);
    $stmt->bindParam(':class', $class);
    $stmt->bindParam(':term', $term);
    $stmt->bindParam(':session', $session);
    $stmt->bindParam(':school_type', $school_type);
    $stmt->bindParam(':amount', $amount);
    $stmt->bindParam(':purpose', $purpose);
    $stmt->bindParam(':method', $method);
    $stmt->execute();

    // Send Email Notification
    $mail = new PHPMailer(true);

    try {
          // 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'], $fullname);
          $mail->addAddress($_ENV['ADMIN_EMAIL'], 'Administrator');

        // Content
        $mail->isHTML(true);
        $mail->Subject = 'REQUEST FOR PAYMENT APPROVAL';
        $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;'>Request for Payment Approval</h1>
                        <p>Dear Administrator,</p>
                        <p>This is to notify you that $fullname has successfully requested for approval of his/her lesson fees payment.</p>
                        <hr>
                        <h3>Payment Details</h3>
                        <hr>
                        <p><strong>Amount Paid:</strong> $amount</p>
                        <p><strong>Class:</strong> $class</p>
                        <p><strong>Term:</strong> $term</p>
                        <p><strong>Session:</strong> $session</p>
                        <p><strong>Purpose of Payment:</strong> $purpose</p>
                        <p><strong>Payment Method:</strong> $method</p>
                        <p><strong>Thank you!</strong></p>
                        <hr>
                        <center>
                            <p>$school_name &copy; " . date("Y") . ". All Rights Reserved.</p>
                        </center>
                        <hr>
                    </div>
                </body>
            </html>
        ";

        $mail->send();
        echo "
            <script>
                alert('Message was sent successfully. Thank you for the payment made!');
                document.location.href = 'confirmpaymententrancelesson.php';
            </script>
        ";
    } catch (Exception $e) {
        echo "
            <script>
                alert('Message was sent successfully. Thank you for the payment made!');
                document.location.href = 'confirmpaymententrancelesson.php';
            </script>
        ";
    }
}

?>

    <!DOCTYPE html>
  <html lang="en">
  <?php @include("includes/head.php"); ?>
  <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
  <style>
  .cpf-wrapper { max-width: 900px; margin: 0 auto; padding: 0 0 40px; }
  .form-card { background:#fff; border-radius:20px; box-shadow:0 4px 30px rgba(15,23,42,.07); overflow:hidden; border:1px solid #e8ecf4; }
  .form-card-header { padding:18px 24px; border-bottom:1px solid #f1f5f9; display:flex; align-items:center; gap:10px; background:linear-gradient(135deg,#0f172a,#1e293b); }
  .form-card-header-icon { width:34px; height:34px; background:rgba(255,255,255,.1); border-radius:10px; display:flex; align-items:center; justify-content:center; color:#fff; font-size:.82rem; }
  .form-card-header h5 { margin:0; font-size:.92rem; font-weight:700; color:#f8fafc; }
  .form-card-body { padding:24px; }
  .field-label { font-size:.72rem; font-weight:600; text-transform:uppercase; letter-spacing:.06em; color:#64748b; margin-bottom:6px; display:block; }
  .wpf-input { width:100%; height:44px; border:1.5px solid #e2e8f0; border-radius:12px; padding:0 14px; font-size:.88rem; color:#0f172a; background:#f8fafc; transition:border-color .2s,box-shadow .2s; outline:none; }
  .wpf-input:focus { border-color:#6366f1; box-shadow:0 0 0 4px rgba(99,102,241,.1); background:#fff; }
  .wpf-input[readonly] { background:#f1f5f9; color:#64748b; cursor:default; }
  .select2-container { width:100% !important; }
  .select2-container--default .select2-selection--single { height:44px !important; border:1.5px solid #e2e8f0 !important; border-radius:12px !important; background:#f8fafc !important; display:flex; align-items:center; padding:0 14px; }
  .select2-container--default.select2-container--open .select2-selection--single,
  .select2-container--default.select2-container--focus .select2-selection--single { border-color:#6366f1 !important; box-shadow:0 0 0 4px rgba(99,102,241,.1) !important; background:#fff !important; }
  .select2-container--default .select2-selection--single .select2-selection__rendered { color:#0f172a; font-size:.88rem; line-height:44px !important; padding:0; }
  .select2-container--default .select2-selection--single .select2-selection__arrow { height:44px !important; right:12px; }
  .select2-dropdown { border:1.5px solid #e2e8f0 !important; border-radius:12px !important; box-shadow:0 10px 40px rgba(15,23,42,.12) !important; overflow:hidden; }
  .select2-container--default .select2-results__option--highlighted { background:#6366f1 !important; }
  .btn-submit { color:#fff; border:none; height:46px; padding:0 32px; border-radius:12px; font-size:.9rem; font-weight:700; display:inline-flex; align-items:center; gap:8px; cursor:pointer; background:linear-gradient(135deg,#6366f1,#8b5cf6); box-shadow:0 6px 20px rgba(99,102,241,.35); transition:transform .15s; }
  .btn-submit:hover { transform:translateY(-2px); }
  .section-divider { height:1px; background:#f1f5f9; margin:20px 0; }
  </style>
  <body class="hold-transition sidebar-mini layout-fixed">
    <div class="wrapper">
      <?php @include("includes/headerentrance.php"); ?>
      <?php @include("includes/entrancesidebar.php"); ?>

      <div class="content-wrapper" style="background:#f0f2f8;">
<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">Approval Request</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="entrance.php" class="text-primary">Home</a></li>
                            <li class="breadcrumb-item active" aria-current="page">Confirm Payment</li>
                        </ol>
                    </nav>
                </div>
            </div>
        </div>
    </div>
</section>  

        <section class="content" style="padding:16px 12px 40px;">
          <div class="container-fluid" style="padding-left:0;padding-right:0;">
            <div class="cpf-wrapper">
              <div class="form-card">
                <div class="form-card-header">
                  <div class="form-card-header-icon"><i class="fas fa-check-circle"></i></div>
                  <h5>Payment Status Confirmation  Lesson Fees</h5>
                </div>
                <form role="form" method="post" enctype="multipart/form-data">
                  <div class="form-card-body">
                    <div class="row">
                      <div class="form-group col-md-4" style="margin-bottom:18px;">
                        <label class="field-label">Student's Fullname</label>
                        <input type="text" class="wpf-input" id="fullname" name="fullname" placeholder="Surname | Firstname | Other name" required>
                      </div>
                      <div class="form-group col-md-4" style="margin-bottom:18px;">
                        <label class="field-label">Email</label>
                        <input type="email" class="wpf-input" id="email" name="email" required>
                      </div>
                      <div class="form-group col-md-4" style="margin-bottom:18px;">
                        <label class="field-label">Class</label>
                        <select class="select2-field" id="class" name="class" required>
                          <option value=""></option>
                          <option value="JSS 1">JSS 1</option>
                          <option value="JSS 2">JSS 2</option>
                          <option value="JSS 3">JSS 3</option>
                          <option value="SS 1">SS 1</option>
                          <option value="SS 2">SS 2</option>
                          <option value="SS 3">SS 3</option>
                        </select>
                      </div>
                      <div class="form-group col-md-4" style="margin-bottom:18px;">
                        <label class="field-label">Term</label>
                        <select class="select2-field" id="term" name="term">
                          <option value=""></option>
                          <option value="First Term">First Term</option>
                          <option value="Second Term">Second Term</option>
                          <option value="Third Term">Third Term</option>
                          <option value="Fourth Term">Fourth Term</option>
                          <option value="Full Session">Full Session</option>
                          <option value="First and Second Term">First and Second Term</option>
                          <option value="Second and Third Term">Second and Third Term</option>
                        </select>
                      </div>
                      <div class="form-group col-md-4" style="margin-bottom:18px;">
                        <label class="field-label">Session</label>
                        <select class="select2-field" id="session" name="session" required>
                          <option value=""></option>
                          <option value="2023/2024">2023/2024</option>
                          <option value="2024/2025">2024/2025</option>
                          <option value="2025/2026">2025/2026</option>
                          <option value="2026/2027">2026/2027</option>
                          <option value="2027/2028">2027/2028</option>
                          <option value="2028/2029">2028/2029</option>
                          <option value="2029/2030">2029/2030</option>
                          <option value="2030/2031">2030/2031</option>
                          <option value="2031/2032">2031/2032</option>
                          <option value="2032/2033">2032/2033</option>
                          <option value="2033/2034">2033/2034</option>
                          <option value="2034/2035">2034/2035</option>
                        </select>
                      </div>
                      <div class="form-group col-md-4" style="margin-bottom:18px;">
                        <label class="field-label">School Type</label>
                        <input type="text" class="wpf-input" id="school_type" name="school_type" value="Day" required readonly>
                      </div>
                      <div class="form-group col-md-4" style="margin-bottom:18px;">
                        <label class="field-label">Amount Paid</label>
                        <input type="number" class="wpf-input" id="amount" name="amount" placeholder="Amount Paid" required>
                      </div>
                      <div class="form-group col-md-4" style="margin-bottom:18px;">
                        <label class="field-label">Purpose of Payment</label>
                        <input type="text" class="wpf-input" id="purpose" name="purpose" value="Lesson Fees" required readonly>
                      </div>
                      <div class="form-group col-md-4" style="margin-bottom:18px;">
                        <label class="field-label">Payment Method</label>
                        <select class="select2-field" id="method" name="method" required>
                          <option value=""></option>
                          <option value="Web Payment">Website / Portal Payment</option>
                          <option value="Others">Others</option>
                        </select>
                      </div>
                    </div>

                    <div class="section-divider"></div>
                    <div style="text-align:center;">
                      <button type="submit" name="submit" id="submit" class="btn-submit">
                        <i class="fas fa-check-circle"></i> Submit
                      </button>
                    </div>
                  </div>
                </form>
              </div>
            </div>
          </div>
        </section>
      </div>
      <?php @include("includes/footer.php"); ?>
    </div>

    <?php @include("includes/foot.php"); ?>
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
    <script>
      $(document).ready(function() {
        $('.select2-field').select2({ width: '100%' });
      });
    </script>

  </body>
  </html>
   
</div>
