<?php

require_once '../models/db_connection.php';
require_once '../models/student_model.php';
require_once '../models/fee_model.php';
$fee = new Fee($conn);
$studentFeeData =  $fee->getAllStudentFees();
$student =  new Student($conn);
$studentsData = $student->getAllStudents();
?>
<nav class="navbar col-lg-12 col-12 p-0 fixed-top d-flex flex-row">
  <div class="text-center navbar-brand-wrapper d-flex align-items-center justify-content-center" style="background-color:#552400">
    <a class="navbar-brand brand-logo" href="../index.php"><span style='color:white'>KIMARA VTC</span></a>
    <a class="navbar-brand brand-logo-mini" href="../index.php"><span style='color:white'>KIMARA VTC</span></a>
  </div>
  <div class="navbar-menu-wrapper d-flex align-items-center justify-content-end">
    <button class="navbar-toggler navbar-toggler align-self-center" type="button" data-toggle="minimize">
      <span class="icon-menu"></span>
    </button>
    <ul class="navbar-nav mr-lg-2">
      <li class="nav-item nav-search d-none d-lg-block">
        <div class="input-group">
          <div class="input-group-prepend">

          </div>

        </div>
      </li>
    </ul>
    <ul class="navbar-nav navbar-nav-right">
      <?php
      $unreadCount = 0;
      if (isset($_SESSION['user'])) {
        $user_id = $_SESSION['user'];
        $result = mysqli_query($conn, "SELECT COUNT(*) as unreadCount FROM messages WHERE receiver_id = '$user_id' AND read_status = 0");
        if ($result) {
          $row = mysqli_fetch_assoc($result);
          $unreadCount = $row['unreadCount'];
        }
      }
      ?>
      <!-- Notification Icon with Red Dot for New Messages -->
      <li class="nav-item dropdown d-flex mr-4 ">
        <a class="nav-link count-indicator dropdown-toggle d-flex align-items-center justify-content-center" id="notificationDropdown" href="#" data-toggle="dropdown">
          <i class="fa fa-bell"></i>
          <?php if ($unreadCount > 0): ?>
            <sup><span class="badge badge-danger"><?= $unreadCount ?></span></sup>
          <?php endif; ?>
        </a>
        <div class="dropdown-menu dropdown-menu-right navbar-dropdown preview-list" aria-labelledby="notificationDropdown">
          <p class="mb-0 font-weight-normal float-left dropdown-header">Notifications</p>
          <?php if ($unreadCount > 0): ?>
            <p class="dropdown-item" onclick="window.location.href='../messages/index.php'" style="cursor: pointer;">You have <?= $unreadCount ?> new message(s).</p>
          <?php else: ?>
            <p class=" dropdown-item" style="cursor: not-allowed;">No new messages.</p>
          <?php endif; ?>
        </div>
      </li>
      <li class="nav-item dropdown d-flex">
        <a class="nav-link count-indicator dropdown-toggle d-flex justify-content-center align-items-center" id="messageDropdown" href="#" data-toggle="dropdown">
          <i class="icon-air-play mx-0"></i>
        </a>
        <div class="dropdown-menu dropdown-menu-right navbar-dropdown preview-list" aria-labelledby="messageDropdown">
          <p class="mb-0 font-weight-normal float-left dropdown-header">Dues</p>

          <?php
          foreach ($studentFeeData as $studentValue) {
            $studentPayment = $fee->getTotalPaymentsForStudent($studentValue['student_id']);
            if (($studentValue['ammount'] - $studentPayment[0]['totalAmmount']) <= 0) {
              continue;
            }
          ?>
            <a class="dropdown-item preview-item">
              <div class="preview-thumbnail">
                <img src="../uploads/<?= $studentValue['filename'] ?>" alt="image" class="profile-pic">
              </div>
              <div class="preview-item-content flex-grow">
                <h6 class="preview-subject ellipsis font-weight-normal"><?= $studentValue['fullname'] ?>
                </h6>
                <p class="font-weight-light  small-text text-muted mb-0">
                  <?php echo (number_format($studentValue['ammount'] - $studentPayment[0]['totalAmmount'])) ?>
                </p>
              </div>
            </a>
          <?php }
          ?>



        </div>
      </li>
      <li class="nav-item dropdown d-flex mr-4 ">
        <a class="nav-link count-indicator dropdown-toggle d-flex align-items-center justify-content-center" id="notificationDropdown" href="#" data-toggle="dropdown">
          <i class="fa fa-user"></i>
          <?php
          $username = mysqli_query($conn, "SELECT * FROM users WHERE id = '$_SESSION[user]'");
          $sel = mysqli_fetch_array($username);
          echo "Welcome, " . $sel['username'] . " ";
          ?>

        </a>
        <div class="dropdown-menu dropdown-menu-right navbar-dropdown preview-list" aria-labelledby="notificationDropdown">
          <p class="mb-0 font-weight-normal float-left dropdown-header">Settings</p>
          <a class="dropdown-item preview-item" href="../auth/logout.php">
            <i class="icon-inbox"></i> Logout
          </a>
          <a class="dropdown-item preview-item" href="../user/user_profile.php">
            <i class="icon-inbox"></i> Profile
          </a>
        </div>
      </li>

    </ul>
    <button class="navbar-toggler navbar-toggler-right d-lg-none align-self-center" type="button" data-toggle="offcanvas">
      <span class="icon-menu"></span>
    </button>
  </div>
</nav>