<?php
    if(isset($_POST['delete'])){
        $id = $_POST['delete_courses'];

        $all_courses = "DELETE FROM portal WHERE courses_id=$id";

        if($db->query($all_courses) == TRUE){
            echo "<script>alert('Course Deleted')</script>";
        }else{
            echo "<script>alert('Course not Deleted')</script>";
        }

    }

?>
            <!-- Recent Sales Start -->
            <div class="container-fluid pt-4 px-4">
                <div class="bg-secondary text-center rounded p-4">
                    <div class="d-flex align-items-center justify-content-between mb-4">
                        <h6 class="mb-0">All Registered courses</h6>
                        
                    </div>
                    <div class="table-responsive">
                        <table class="table text-start align-middle table-bordered table-hover mb-0">
                            <thead>
                                <tr class="text-white">
                                    <th scope="col"><input class="form-check-input" type="checkbox"></th>
                                    <th scope="col">S/N</th>
                                    <th scope="col">Surname</th>
                                    <th scope="col">Course Title</th>
                                    <th scope="col">Course Code</th>
                                    <th scope="col">Course Unit</th>
                                    <th scope="col">Course Status</th>
                                    <th scope="col">Action</th>
                                    <th scope="col">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $all_courses = mysqli_query($db, "SELECT * FROM registered_courses");

                                // fetch all courses 
                                if(mysqli_fetch_array($all_courses)){

                                    // use foreach to echo all datas
                                    foreach($all_courses as $courses){

                                ?>
                                <tr>
                                    <td><input class="form-check-input" type="checkbox"></td>
                                    <td><?php echo $courses['courses_id']; ?></td>
                                    <td><?php echo $courses['sname']; ?></td>
                                    <td><?php echo $courses['course_title']; ?></td>
                                    <td><?php echo $courses['course_code']; ?></td>
                                    <td><?php echo $courses['course_unit']; ?></td>
                                    <td><?php echo $courses['course_status']; ?></td>
                                    <td><a class="btn btn-sm btn-success" href="index.php?env=View_courses_record?courses_id=<?php echo $courses['courses_id']; ?>" target="_blank">View</a></td>   
                                    
                                    <td>
                                        <form method="POST">
                                            <input type='hidden' name='delete_courses' value='<?php echo $courses['courses_id'] ?>'>
                                            <input type='submit'class='btn btn-sm btn-danger' name='delete' value='Delete'>
                                        </form>
                                  
                                    </td>
                                </tr>

                                <?php
                                    }
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <!-- Recent Sales End -->

            <!-- Footer Start -->
            <?php include("inc/footer.php"); ?>