<?php

include('includes/session.php');

error_reporting(0);

include('includes/dbconnection.php');

require_once __DIR__ . '/includes/session_term_helper.php';
// Fetch current session and term (with gap/fallback support)
$stResult = resolveCurrentSessionTerm($con);
$currentSessionId   = $stResult['session_id'];
$currentSessionName = $stResult['session_name'];
$currentTermId      = $stResult['term_id'];
$currentTermName    = $stResult['term_name'];


// Set student details (replace with session values if available)

$cls = 'All student';

$st = 'All student';

$stdt = 'All student';



$query = "SELECT * FROM fees WHERE class ='$cls' AND stream ='$st' AND student_type = '$stdt' AND name LIKE 'ASI%' ";

$result = mysqli_query($con, $query);



// Fetch the current session

// Fetch active session ordered by earliest start_date

$currentSessionQuery = "

    SELECT id, session_name 

    FROM school_sessions 

    WHERE start_date <= CURDATE() 

      AND end_date >= CURDATE() 

    ORDER BY start_date ASC 

    LIMIT 1";

$currentSessionResult = $con->query($currentSessionQuery);



if ($currentSessionResult && $currentSessionResult->num_rows > 0) {

  $currentSession = $currentSessionResult->fetch_assoc();

  $currentSessionId = $currentSession['id'];

  $currentSessionName = $currentSession['session_name'];

} else {

  $currentSessionId = null;

  $currentSessionName = null;

}



// Fetch the current term

$termQuery = "SELECT id, term_name FROM school_terms WHERE session_id = '$currentSessionId'

              AND start_date <= CURDATE() 

              AND end_date >= CURDATE() 

              ORDER BY start_date ASC 

              LIMIT 1";

$termResult = mysqli_query($con, $termQuery);

$termRow = mysqli_fetch_assoc($termResult);

$currentTermId = $termRow['id'];

$currentTermName = $termRow['term_name'];

?>

<!DOCTYPE html>

<html>

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



<style>

  /* Cards Styling */

  .small-box {

    border-radius: 12px;

    transition: 0.3s ease-in-out;

    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);

    overflow: hidden;

    background: white;

  }



  .small-box:hover {

    transform: translateY(-4px);

    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);

  }



  .small-box .inner {

    padding: 20px;

  }



  .small-box p {

    font-size: 1rem;

    font-weight: bold;

    color: whitesmoke;

  }



  .small-box span {

    font-size: 1rem;

    color: #666;

    font-weight: bold;

  }



  .small-box .icon {

    position: absolute;

    right: 20px;

    top: 20px;

    font-size: 3rem;

    color: rgba(255, 255, 255, 0.8);

  }



  .small-box-footer {

    display: block;

    padding: 12px;

    font-weight: 600;

    font-size: 1rem;

    text-align: center;

    background: rgba(0, 0, 0, 0.1);

    border-top: 1px solid rgba(255, 255, 255, 0.2);

  }



  .small-box-footer:hover {

    background: rgba(0, 0, 0, 0.15);

  }



  /* Alert Box Styling */

  .alert-overlay {

    position: fixed;

    top: 0;

    left: 0;

    width: 100%;

    height: 100%;

    background: rgba(0, 0, 0, 0.5);

    display: flex;

    justify-content: center;

    align-items: center;

    z-index: 1050;

  }



  .dashboard-header {

    font-size: 1.3rem;

    font-weight: 400;

    color: #333;

    text-align: center;

    margin-bottom: 10px;

  }



  .alert-box {

    background: white;

    padding: 25px;

    border-radius: 10px;

    text-align: center;

    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);

    max-width: 400px;

    width: 90%;

    animation: fadeIn 0.3s ease-in-out;

  }



  .alert-box p {

    font-size: 1.2rem;

    color: #444;

    margin-bottom: 15px;

  }



  .alert-box button {

    margin-top: 15px;

    padding: 10px 18px;

    border: none;

    border-radius: 5px;

    cursor: pointer;

    font-size: 1rem;

    transition: 0.3s;

  }



  .btn-update {

    background-color: #007bff;

    color: white;

  }



  .btn-update:hover {

    background-color: #0056b3;

  }



  .btn-close {

    background-color: #dc3545;

    color: white;

  }



  .btn-close:hover {

    background-color: #bd2130;

  }



  @keyframes fadeIn {

    from {

      opacity: 0;

      transform: scale(0.9);

    }



    to {

      opacity: 1;

      transform: scale(1);

    }

  }

</style>



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

  <div class="wrapper">

    <!-- Navbar -->

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

    <!-- /.navbar -->



    <!-- Main Sidebar Container -->

    <?php @include("includes/sidebaroldstd.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-wallet"></i>

                  </div>

                  <div>

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

                      Payment Management

                      <span class="premium-subtitle text-secondary"><?php echo $currentTermName; ?> |

                        <?php echo $currentSessionName; ?></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="oldstdday.php" class="text-primary">Home</a></li>

                    <li class="breadcrumb-item active" aria-current="page">Pay ASI</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> Secure Your Spot – Pay for Skill-Up

                  </h5>

                </div>

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

                <!-- form start -->

                <form role="form" method="" enctype="multipart/form-data">

                  <div class="card-body">

                    <span style="color: brown">

                      <h5>Select what you are paying for</h5>

                    </span>

                    <br>

                    <hr>

                    <br>

                    <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-4 gap-4 mb-10">

                      <?php

                      if (mysqli_num_rows($result) > 0) {



                        while ($row = mysqli_fetch_assoc($result)) {



                          $feeType = htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8');

                          $amount = (float) $row['amount'];

                          $class = htmlspecialchars($row['class'], ENT_QUOTES, 'UTF-8');

                          $feeId = (int) $row['id'];



                          /* -------------------------------

                             STYLE MAPPING (SECURE + CLEAN)

                          -------------------------------- */

                          // Default fallback style (Green/Emerald)

                          $style = ["bg" => "bg-emerald-50", "text" => "text-emerald-600"];



                          if (stripos($feeType, 'UNIFORM') !== false) {

                            $style = ["bg" => "bg-cyan-50", "text" => "text-cyan-600"];

                          } elseif (stripos($feeType, 'CARDIGAN') !== false) {

                            $style = ["bg" => "bg-orange-50", "text" => "text-orange-600"];

                          } elseif (stripos($feeType, 'EXCURSION') !== false) {

                            $style = ["bg" => "bg-rose-50", "text" => "text-rose-600"];

                          } elseif (stripos($feeType, 'SUMMER') !== false) {

                            $style = ["bg" => "bg-blue-50", "text" => "text-blue-600"];

                          } elseif (stripos($feeType, 'Robotics') !== false || stripos($feeType, 'Photography') !== false || stripos($feeType, 'Videography') !== false) {

                            $style = ["bg" => "bg-sky-50", "text" => "text-sky-600"]; // Was 'info'

                          } elseif (stripos($feeType, 'Coding') !== false || stripos($feeType, 'Web') !== false || stripos($feeType, 'App') !== false) {

                            $style = ["bg" => "bg-blue-100", "text" => "text-blue-700"]; // Was 'primary'

                          } elseif (stripos($feeType, 'Graphics') !== false || stripos($feeType, 'Design') !== false || stripos($feeType, 'Handcrafts') !== false || stripos($feeType, 'Bead') !== false) {

                            $style = ["bg" => "bg-green-50", "text" => "text-green-600"]; // Was 'success'

                          } elseif (stripos($feeType, 'Fashion') !== false || stripos($feeType, 'Hairdressing') !== false) {

                            $style = ["bg" => "bg-slate-100", "text" => "text-slate-600"]; // Was 'secondary'

                          } elseif (stripos($feeType, 'Makeup') !== false || stripos($feeType, 'Beauty') !== false) {

                            $style = ["bg" => "bg-red-50", "text" => "text-red-600"]; // Was 'danger'

                          } elseif (stripos($feeType, 'Baking') !== false || stripos($feeType, 'Catering') !== false) {

                            $style = ["bg" => "bg-lime-50", "text" => "text-lime-700"]; // Was 'success' variant

                          } elseif (stripos($feeType, 'Event') !== false || stripos($feeType, 'Decoration') !== false) {

                            $style = ["bg" => "bg-purple-50", "text" => "text-purple-600"]; // Was 'purple'

                          }

                          ?>

                          <div class="bento-card group">

                            <div class="flex justify-between items-start mb-4">

                              <div

                                class="w-10 h-10 <?php echo $style['bg']; ?> <?php echo $style['text']; ?> rounded-xl flex items-center justify-center transition-transform group-hover:scale-110">

                                <i class="fas fa-money-bill-wave"></i>

                              </div>



                              <a href="confirmpay.php?fee_id=<?php echo $feeId; ?>&pay_amount=<?php echo $amount; ?>"

                                class="text-[12px] font-black text-slate-400 group-hover:text-slate-900 transition-colors tracking-tighter">

                                <i class="fas fa-arrow-right"></i> PAY

                              </a>

                            </div>



                            <h5 class="text-[14px] font-bold text-slate-600 truncate">

                              <?php echo $class; ?>

                            </h5>



                            <p class="text-[11px] font-bold uppercase text-slate-500 truncate">

                              <?php echo $feeType; ?>

                            </p>



                            <p class="mt-2 text-[13px] font-extrabold <?php echo $style['text']; ?>">

                              ₦<?php echo number_format($amount, 2); ?>

                            </p>



                            <div class="mt-4 w-full h-1 bg-slate-200/50 rounded-full overflow-hidden">

                              <div

                                class="h-full <?php echo str_replace('text-', 'bg-', $style['text']); ?> opacity-40 w-full transform -translate-x-1/2 group-hover:translate-x-0 transition-transform duration-1000">

                              </div>

                            </div>

                          </div>

                          <?php

                        }

                      } else {

                        echo "<p class='text-red-500 col-span-full text-sm'>No lesson fee available at the moment.</p>";

                      }

                      ?>

                    </div>

                  </div>

                </form>

              </div>

            </div>

          </div>

        </div>

      </section>

    </div>



    <!-- ./wrapper -->

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



  </div>



  <!-- ./wrapper -->

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



</body>



</html>