<?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 dependencies
require_once 'vendor/autoload.php';
// Ensure session is valid
use Dotenv\Dotenv;

// Load environment variables
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
$school_name = $_ENV['SCHOOL_NAME'];

?>
<!DOCTYPE html>
<html lang="en">
<?php @include("includes/head.php"); ?>


<body class="hold-transition sidebar-mini layout-fixed">
    <div class="wrapper">
        <!-- Navbar -->
        <?php @include("includes/headerentrance.php"); ?>
        <!-- Main Sidebar Container -->
        <?php @include("includes/entrancesidebar.php"); ?>

        <!-- Content Wrapper -->
        <div class="content-wrapper">
            <!-- Page Header -->
            <div class="content-header">
                <div class="container-fluid">
                    <div class="row mb-3">
                        <div class="col-sm-12">
                            <h4 class="dashboard-header text-center text-lg font-black text-slate-600" style = "font-style:bold;">Welcome to <?php echo $school_name ?> Guest Portal</h4>
                        </div>
                        <hr>
                    </div>
                    <hr>
                </div>
                <hr>
            </div>

                        <!-- Main Content -->
            <section class="content px-6">
                <div class="container-fluid">
                    
                    <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-4 gap-4 mb-10">
                        <?php
    $dashboard_items = [
        // Students
        ["query" => null, "label" => "ENTRANCE FORM PAYMENT", "icon" => "fas fa-file-alt", "bg" => "bg-info", "link" => "entrance.php", "id" => "admittedStudents", "class" => "text-[12px] font-black text-slate-400 group-hover:text-slate-900 transition-colors tracking-tighter", "display_text" => "ENTRANCE FORM"],
        ["query" => null, "label" => "LESSON FEES PAYMENT", "icon" => "fas fa-book-open", "bg" => "bg-primary", "link" => "entrancelesson.php", "class" => "text-[12px] font-black text-slate-400 group-hover:text-slate-900 transition-colors tracking-tighter", "id" => "registeredStudents", "display_text" => "LESSON FEES"],
        ["query" => null, "label" => "CERTIFICATE FEES PAYMENT", "icon" => "fas fa-certificate", "bg" => "bg-success", "link" => "entrancecertificate.php", "id" => "activeTeachers", "class" => "text-[12px] font-black text-slate-400 group-hover:text-slate-900 transition-colors tracking-tighter", "display_text" => "CERTIFICATE"],
        ["query" => null, "label" => "PROVIDE FEEDBACK", "desc" => "Provide Your Feedback", "icon" => "fas fa-comments", "bg" => "bg-danger", "link" => "#", "id" => "feedbackBtn","class" => "text-[12px] font-black text-slate-400 group-hover:text-slate-900 transition-colors tracking-tighter feedbackModal", "display_text" => "FEEDBACK"],
        ];





                        $styleMap = [
                            "bg-primary"   => ["bg" => "bg-blue-50", "text" => "text-blue-600"],
                            "bg-success"   => ["bg" => "bg-emerald-50", "text" => "text-emerald-600"],
                            "bg-warning"   => ["bg" => "bg-orange-50", "text" => "text-orange-600"],
                            "bg-info"      => ["bg" => "bg-cyan-50", "text" => "text-cyan-600"],
                            "bg-danger"    => ["bg" => "bg-rose-50", "text" => "text-rose-600"],
                            "bg-secondary" => ["bg" => "bg-slate-50", "text" => "text-slate-600"]
                        ];

foreach ($dashboard_items as $item) {

    $count = null;

    if (!empty($item['query'])) {
        $res = mysqli_query($con, $item['query']);
        if ($res) {
            $count = mysqli_num_rows($res);
        }
    }

    $st = $styleMap[$item['bg']] ?? $styleMap["bg-secondary"];
?>
    <div class="bento-card group">
        <div class="flex justify-between items-start mb-3">
            <div class="w-10 h-10 <?php echo $st['bg']; ?> <?php echo $st['text']; ?> rounded-xl flex items-center justify-center text-base group-hover:scale-110 transition-transform">
                <i class="<?php echo $item['icon']; ?>"></i>
            </div>
            <a href="<?php echo $item['link']; ?>" class="<?php echo $item['class']; ?>" id="<?php echo $item['id']; ?>">
                <i class="fas fa-arrow-right"></i> CLICK HERE
            </a>
        </div>

        <h2 class="text-2xl font-black text-slate-800 tracking-tight">
            <?php
                echo $count !== null
                    ? number_format($count)
                    : strtoupper($item['display_text']);
            ?>
        </h2>

        <p class="text-[10px] font-bold text-slate-500 uppercase tracking-tight truncate">
            <?php echo $item['label']; ?>
        </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-', $st['text']); ?> opacity-40 w-full transform -translate-x-1/2 group-hover:translate-x-0 transition-transform duration-1000"></div>
        </div>
    </div>
<?php } ?>
</div>

                    <div class="mt-12">
                        <hr>
                         <h5 class="text-lg font-black text-slate-600 mb-6 flex items-center">
                            <span class="w-8 h-8 bg-white shadow-sm rounded-lg flex items-center justify-center mr-3 text-sm">📅</span>
                            School Calendar
                         </h5>
                         <hr>
                         <div id="calendar" class="bg-white p-6 rounded-3xl border border-slate-100 shadow-sm mb-8"></div>
                         

                    </div>

                </div>
            </section>


        </div>


        <!-- Footer -->
        <?php @include("includes/footer.php"); ?>
    </div>

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

<!-- Feedback Modal -->
<div id="feedbackModal" class="modal-custom" role="dialog" aria-modal="true" aria-labelledby="feedbackModalLabel">
  <div class="modal-dialog-custom">
    <div class="modal-content-custom">

      <div class="modal-header-custom">
        <div class="modal-title-custom" id="feedbackModalLabel">
          <i class="fas fa-comment-dots"></i> Share Your Feedback
        </div>
        <button id="closeModal" class="modal-close-btn approve" aria-label="Close">
          <i class="fas fa-times"></i>
        </button>
      </div>

      <!-- Form -->
      <form id="feedbackForm" action="includes/submit_feedback.php" method="post" enctype="multipart/form-data">

        <div class="form-row-custom">
          <div class="form-group-custom">
            <label class="form-label-custom" for="name">Your Name</label>
            <input type="text" class="form-control-custom" id="name" name="name" placeholder="e.g. John Doe" required>
          </div>
          <div class="form-group-custom">
            <label class="form-label-custom" for="email">Email Address</label>
            <input type="email" class="form-control-custom" id="email" name="email" placeholder="e.g. john@email.com" required>
          </div>
          <div class="form-group-custom">
            <label class="form-label-custom" for="stdname">Student's Name</label>
            <input type="text" class="form-control-custom" id="stdname" name="stdname" placeholder="Student's full name" required>
          </div>
          <div class="form-group-custom">
            <label class="form-label-custom" for="adno">Student's Class</label>
            <input type="text" class="form-control-custom" id="adno" name="adno" placeholder="e.g. JSS 2A" required>
          </div>
        </div>

        <div class="form-group-custom">
          <label class="form-label-custom" for="message">Message</label>
          <textarea class="form-control-custom" id="message" name="message" placeholder="Write your feedback here..." required></textarea>
          <!-- Honeypot -->
          <input type="text" name="honeypot" id="honeypot" style="display:none;" autocomplete="off">
        </div>

        <div class="form-group-custom">
          <label class="form-label-custom" for="file">Attach File <span style="color:#475569;font-weight:400;text-transform:none;">(optional &mdash; jpg, png, pdf, doc)</span></label>
          <div class="file-input-wrapper">
            <input type="file" id="file" name="file" accept=".jpg,.jpeg,.png,.pdf,.doc,.docx">
          </div>
        </div>

        <div class="modal-actions">
          <button type="button" id="cancelFeedback" class="btn-modal-cancel approve">Cancel</button>
          <button type="submit" class="btn-modal-submit">
            <i class="fas fa-paper-plane"></i> Submit Feedback
          </button>
        </div>

      </form>

      <!-- Success State -->
      <div class="feedback-success" id="feedbackSuccess">
        <div class="success-icon"><i class="fas fa-check"></i></div>
        <h5>Thank you!</h5>
        <p>Your feedback has been submitted successfully.</p>
      </div>

    </div>
  </div>
</div>

<script>
  (function () {
    const modal       = document.getElementById('feedbackModal');
    const openBtn     = document.getElementById('feedbackBtn');
    const closeBtn    = document.getElementById('closeModal');
    const cancelBtn   = document.getElementById('cancelFeedback');
    const form        = document.getElementById('feedbackForm');
    const successBox  = document.getElementById('feedbackSuccess');

    function openModal()  { modal.style.display = 'flex'; }
    function closeModal() { modal.style.display = 'none'; }

    openBtn.addEventListener('click', openModal);
    closeBtn.addEventListener('click', closeModal);
    cancelBtn.addEventListener('click', closeModal);

    window.addEventListener('click', e => { if (e.target === modal) closeModal(); });
    document.addEventListener('keydown', e => { if (e.key === 'Escape') closeModal(); });

    form.addEventListener('submit', function (e) {
      e.preventDefault();
      const formData = new FormData(this);
      const xhr = new XMLHttpRequest();
      xhr.open('POST', 'includes/submit_feedback.php', true);

      xhr.onload = function () {
        if (xhr.status === 200) {
          form.style.display = 'none';
          successBox.style.display = 'block';
          setTimeout(closeModal, 4000);
        } else {
          alert('An error occurred. Please try again.');
        }
      };

      xhr.send(formData);
    });
  })();
</script>

</body>

</html>