<?php
    session_start();
    if(!isset($_SESSION["school_admin"]))
    {
        header("location:./index.php");
    }

    include "../backend/connection.php";
    $configure = new DefaultDatabase();
    $conf = $configure->connect();
    //fetch school detailss
    $details = mysqli_query($conf,"SELECT * FROM school_admin WHERE username = '".$_SESSION["school_admin"]."'");
    $det = mysqli_fetch_array($details);

    $section = mysqli_query($conf,"SELECT * FROM grade_section");
    $category = mysqli_query($conf, "SELECT * FROM categories");
    $pupils = mysqli_query($conf, "SELECT * FROM participants");

?>
<div class="school-container">
    <h2>MANAGE PUPILS</h2>
    <div class="add-school">
        <form method="POST">
            <input type="text" placeholder="First Name" name="first_name" required>
            <input type="text" placeholder="Last Name" name="last_name" required>
            <input type="date" placeholder="Date Of Birth" name="date" required>
            <select id="sectionSelect" name="section" required>
                <option value="" disabled selected>Select Section</option>
                <?php
                    while($sec = mysqli_fetch_array($section))
                    {
                ?>
                <option value="<?php echo $sec["section_name"] ?>"><?php echo $sec["section_name"] ?></option>
                <?php
                    }
                ?>
            </select>
            <select id="categorySelect" name="category" required>
                <option value="" disabled selected>Select Category</option>
                <?php
                    while($cat = mysqli_fetch_array($category))
                    {
                ?>
                <option value="<?php echo $cat["category_name"] ?>"><?php echo $cat["category_name"] ?></option>
                <?php
                    }
                ?>
            </select>
            
            <button  type="submit" name="register_pupil">Register Participant</button>
        </form>
    </div>
    <table id="schoolsTable">
        <thead>
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Date of Birth</th>
                <th>Section</th>
                <th>Category</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <!-- Dummy data -->
            <?php
                while($pat = mysqli_fetch_array($pupils))
                {
            ?>
            <tr>
                <td><?php echo $pat["first_name"] ?></td>
                <td><?php echo $pat["last_name"] ?></td>
                <td><?php echo $pat["date_of_birth"] ?></td>
                <td><?php echo $pat["grade_section"] ?></td>
                <td><?php echo $pat["category"] ?></td>
                <td>
                <button class="delete"><a  href="./deletions/delete_pupil.php?pupil_del=<?php echo $pat["id"] ?>" onclick="return confirm('Are you sure you want to remove this participant?');" style="color:white; text-decoration:none;">Delete</a></button>   
                </td>
            </tr>
            <?php
                }
            ?>
        </tbody>
    </table>
</div>
