<?php include "header.php";?>
<?php include "search_db.php";?>
<!-- ***** Breadcrumb Area Start ***** -->
<section class="section breadcrumb-area overlay-dark d-flex align-items-center">
    <div class="container">
        <div class="row">
            <div class="col-12">
                <!-- Breadcrumb Content -->
                <div class="breadcrumb-content d-flex flex-column align-items-center text-center">
                    <h2 class="text-white text-uppercase mb-3">نتایج نمرات امتحانات آزمایشی</h2>
                    <ol class="breadcrumb"></ol>
                </div>
            </div>
        </div>
    </div>
</section>
<!-- ***** Breadcrumb Area End ***** -->

<!-- ***** About Area Start ***** -->
<!-- ***** About Area Start ***** -->
<section class="section about-area ptb_100">
    <div class="container">
        <div class="row justify-content-center align-items-center">
            <div class="col-12">
                <!-- About Content -->
                <div class="about-content section-heading text-center">
<?php
include 'z_db.php';

$safe_id = '';
$student_result = [];

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["student_id"]) && trim($_POST["student_id"]) !== '') {
    $safe_id = mysqli_real_escape_string($con, $_POST["student_id"]);
    $q2 = "
        SELECT s.id AS student_id, s.name AS student_name, m.marks, e.title AS exam_title
        FROM marks m
        JOIN students s ON m.student_id = s.id
        JOIN exams e ON m.exam_id = e.id
        WHERE s.id = '$safe_id'
        ORDER BY m.id DESC
    ";
} else {
    $q2 = "
        SELECT s.id AS student_id, s.name AS student_name, m.marks, e.title AS exam_title
        FROM marks m
        JOIN students s ON m.student_id = s.id
        JOIN exams e ON m.exam_id = e.id
        ORDER BY m.id DESC
    ";
}


$result = $con->query($q2);
if ($result && $result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $student_result[] = $row;
    }
}
?>

<div class="container py-5">
    <h2 class="text-center mb-4">نتایج امتحانات آزمایشی</h2>

    <!-- Search Form -->
    <form method="POST" class="mb-4">
    <div class="row justify-content-center">
        <div class="col-md-8 col-12">
            <div class="input-group input-group-lg">
                <button type="submit" class="btn btn-primary">جستجو</button>
                <input type="text" name="student_id" class="form-control text-center" placeholder="ایدی محصل خود را وارد کنید" value="<?= htmlspecialchars($safe_id) ?>" required>
            </div>
        </div>
    </div>
</form>

    <?php if (!empty($student_result)) : ?>
        <!-- نمایش داده‌ها گروه‌بندی‌شده بر اساس عنوان امتحان -->
        <?php
        $grouped_results = [];
        foreach ($student_result as $row) {
            $grouped_results[$row['exam_title']][] = $row;
        }
        ?>

        <?php foreach ($grouped_results as $exam_title => $results): ?>
            <div class="text-center mt-4">
                <h5 class="text-primary"> <?= htmlspecialchars($exam_title) ?></h5>
            </div>
            <div class="table-responsive mt-3">
                <table class="table table-bordered table-striped" dir="rtl" style="color: #414465;">
    <thead style="background-color: #DEBD6B; color: #414465;">
        <tr>
            <th>ایدی محصل</th>
            <th>اسم محصل</th>
            <th>نمره</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($results as $row): ?>
            <tr>
                <td><?= htmlspecialchars($row["student_id"]) ?></td>
                <td><?= htmlspecialchars($row["student_name"]) ?></td>
                <td><?= htmlspecialchars($row["marks"]) ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

            </div>
        <?php endforeach; ?>
    <?php elseif ($_SERVER["REQUEST_METHOD"] == "POST") : ?>
        <div class="alert alert-danger text-center mt-4">
            هیچ نتیجه‌ای با این ایدی پیدا نشد!
        </div>
    <?php endif; ?>


<!-- Scripts -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
<script>
$(document).ready(function () {
    $('table').DataTable({
        "language": {
            "search": "جستجو:",
            "lengthMenu": "نمایش _MENU_ سطر",
            "zeroRecords": "هیچ نتیجه‌ای یافت نشد",
            "info": "نمایش _START_ تا _END_ از _TOTAL_ مورد",
            "infoEmpty": "بدون نتیجه",
            "infoFiltered": "(فیلتر شده از _MAX_ رکورد)",
            "paginate": {
                "first": "اولین",
                "last": "آخرین",
                "next": "بعدی",
                "previous": "قبلی"
            }
        },
        "paging": true,
        "searching": true
    });
});
</script>
                </div>
            </div>
        </div>
    </div>
</section>

<!-- Call To Action Area Start -->
<section class="section cta-area bg-overlay ptb_100">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-12 col-lg-10">
                <!-- Section Heading -->
                <div class="section-heading text-center m-0">
                    <h2 class="text-white"><?php print $enquiry_title; ?></h2>
                    <p class="text-white d-none d-sm-block mt-4"><?php print $enquiry_text; ?></p>
                    <a href="contact" class="btn btn-bordered-white mt-4">تماس</a>
                </div>
            </div>
        </div>
    </div>
</section>
<!-- Call To Action Area End -->
<?php include "footer.php"; ?>
