<?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);

    $materials = mysqli_query($conf,"SELECT * FROM reports WHERE school = '".$det["school"]."'");
    $pupils = mysqli_query($conf,"SELECT * FROM participants WHERE school = '".$det["school"]."'");
    $categories = mysqli_query($conf,"SELECT * FROM categories");
    $level = mysqli_query($conf,"SELECT * FROM grade_section");
    


?>
<div class="school-container">
    <h2>PARTICIPANT REPORTS UPLOAD</h2>
    <p style="color:red">* only PDF files are allowed</p>
    <div class="add-school">
        <form method="POST" enctype="multipart/form-data">
            <select id="pupilSelect" name="pupil" required>
                <option value="" disabled selected>Select Participant</option>
                <?php
                    while($pupil = mysqli_fetch_array($pupils))
                    {
                ?>
                <option value="<?php echo $pupil["first_name"]." ".$pupil["last_name"] ?>"><?php echo $pupil["first_name"]." ".$pupil["last_name"] ?></option>
                <?php
                    }
                ?>
            </select>
            <select id="categorySelect" name="category" required>
                <option value="" disabled selected>Select Category</option>
                <?php
                    while($cat = mysqli_fetch_array($categories))
                    {
                ?>
                <option value="<?php echo $cat["category_name"] ?>"><?php echo $cat["category_name"] ?></option>
                <?php
                    }
                ?>
            </select>
            <select id="levelSelect" name="level" required>
                <option value="" disabled selected>Select Level</option>
                <?php
                    while($lev = mysqli_fetch_array($level))
                    {
                ?>
                <option value="<?php echo $lev["section_name"] ?>"><?php echo $lev["section_name"] ?></option>
                <?php
                    }
                ?>
            </select>
            <input type="file" placeholder="select file" name="material_file" accept=".pdf" required>
            <button  type="submit" name="add_report">Upload Document</button>
        </form>
    </div>
    <table id="schoolsTable">
        <thead>
            <tr>
                <th>Pupils Name</th>
                <th>Category</th>
                <th>Level</th>
                <th>Document</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <!-- Dummy data -->
            <?php
                while($reports = mysqli_fetch_array($materials))
                {
            ?>
            <tr>
                <td><?php echo $reports["full_name"] ?></td>
                <td><?php echo $reports["category"] ?></td>
                <td><?php echo $reports["section_name"] ?></td>
                <td><a href="<?php echo $reports["location"] ?>" style="text-decoration: none; color:black; text-align:center;"><img src="../images/icon.PNG" width="50"> <br> Read Document</a></td>
                <td>
                <button class="delete"><a  href="./deletions/delete_report.php?report_del=<?php echo $reports["id"] ?>" onclick="return confirm('Are you sure you want to remove this report?');" style="color:white; text-decoration:none;">Delete</a></button>   
                </td>
            </tr>
            <?php
                }
            ?>
        </tbody>
    </table>
</div>
