<?php
include('session_validator.php');
include("includes/db_pdo.php");

// function calculateAge(\$dob) {
//     \$today = new DateTime('2081-11-12');
//     \$birthDate = new DateTime(\$dob);
//     \$age = \$today->diff(\$birthDate)->y;
//     return \$age;}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Student List</title>
    <link rel="stylesheet" href="../public/css/bootstrap.min.css" media="screen">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></script>
    <style>
        .table th, .table td { text-align: center; }
    </style>
</head>
<body>
    <div class="container mt-4">
        <h2 class="text-center">Student List</h2>
        <table id="studentTable" class="display table table-striped table-bordered">
            <thead>
                <tr>
                    <th>S.N.</th>
                    <th>Name of Student</th>
                    <th>Gender</th>
                    <th>Class</th>
                    <th>DOB</th>
                    <th>Age</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $query = "SELECT * FROM students INNER JOIN classes ON students.class = classes.Class_id WHERE classes.status='on'";
                $result = $conn->query($query);
                $sn = 1;
                
                if ($result->rowCount() > 0) {
                    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
                        // $age = calculateAge($row["dob"]);
                        $age="";
                        echo "<tr>";
                        echo "<td>" . $sn++ . "</td>";
                        echo "<td>" . htmlspecialchars($row["name"]) . "</td>";
                        echo "<td>" . htmlspecialchars($row["gender"]) . "</td>";
                        echo "<td>" . htmlspecialchars($row["class_name"]) . "-" . $row["section"] .  "</td>";
                        echo "<td>" . htmlspecialchars($row["dob"]) . "</td>";
                        echo "<td>" . $age . "</td>";
                        echo "</tr>";
                    }
                } else {
                    echo "<tr><td colspan='6'>No students found</td></tr>";
                }
                ?>
            </tbody>
        </table>
    </div>
    <script>
        $(document).ready(function () {
            $('#studentTable').DataTable({ "lengthMenu": [25, 50, 100, 200,500, 1000 , 2000] });
        });
    </script>
</body>
</html>
