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

        $all_matricnumber = "DELETE FROM newmatric WHERE newmatric_id=$id";

        if($db->query($all_matricnumber) == TRUE){
            echo "<script>alert('Matric Deleted')</script>";
        }else{
            echo "<script>alert('Matric 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">Add New Student Matric Number</h6>
                        
                    </div>
                    <div class="table-responsive">
                      <div class="container">
                        <div class="row">
                            <div class="col-md-2"></div>
                            <div class="col-md-8">
                                <form method="POST" action="">
                                    <div class="form-group">
                                        <label>Matric Number</label>
                                        <input type="text" name="matricnumber" class="form-control" placeholder="Input Matric Number" required><br/>
                                    </div>
                                    <div class="form-group">
                                        <button type="submit" name="addmatric" class="btn btn-outline-primary btn-block">Add Matric Number</button>
                                    </div>
                                </form>

                                
                            </div>
                            <div class="col-md-2"></div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <!-- view all matric number -->
                                <table class="table text-start align-middle table-bordered table-hover mb-0" style="margin-top:50px;">
                                    <thead>
                                        <tr>
                                            <th scope="col">S/N</th>
                                            <th scope="col">Matric Number</th>
                                            <th scope="col">Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                            $all_matricnumber = mysqli_query($db, "SELECT * FROM newmatric");

                                            // fetch all matric Number
                                            if(mysqli_fetch_array($all_matricnumber)){
                                                
                                                // use foreach loop to echo all data
                                                foreach($all_matricnumber as $matricnumber){
                                        ?>
                                        <tr>
                                                <td><?php echo $matricnumber['newmatric_id']; ?></td>
                                                <td><?php echo $matricnumber['matricnumber']; ?></td>
                                                <td>
                                                    <form method="POST">
                                                        <input type='hidden' name='delete_matric' value='<?php echo $matricnumber['newmatric_id'] ?>'>
                                                        <input type='submit'class='btn btn-sm btn-danger' name='delete' value='Delete'>
                                                    </form>
                                          
                                                </td>
                                        </tr>
                                        <?php
                                            }
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                      </div>
                    </div>
                </div>
            </div>
            <!-- Recent Sales End -->

            <!-- Footer Start -->
            <div class="container-fluid pt-4 px-4">
                <div class="bg-secondary rounded-top p-4">
                    <div class="row">
                        <div class="col-12 col-sm-6 text-center text-sm-start">
                            &copy; <a href="#">Envillepoly</a>, All Right Reserved. 
                        </div>
                        <div class="col-12 col-sm-6 text-center text-sm-end">
                            <!--/*** This template is free as long as you keep the footer author’s credit link/attribution link/backlink. If you'd like to use the template without the footer author’s credit link/attribution link/backlink, you can purchase the Credit Removal License from "https://htmlcodex.com/credit-removal". Thank you for your support. ***/-->
                            Designed By <a href="">Eagertech</a>
                           
                        </div>
                    </div>

<?php
if(isset($_POST['addmatric'])){
    $addmatric = mysqli_real_escape_string($db, $_POST['matricnumber']);

    $checkmatric = "SELECT * FROM newmatric WHERE matricnumber = '$addmatric'";
    $checkresult = mysqli_query($db, $checkmatric);

    if(mysqli_num_rows($checkresult) > 0){
        echo "<script>alert('Matric number Already Exist')</script>";
    }else{
    $query = "INSERT INTO newmatric(matricnumber) VALUES('$addmatric')";

    if(mysqli_query($db, $query)){
        echo "<script>alert('Matric Number Added')</script>";
    }else{
        echo "<script>alert('Failed to add Matric Number')</script>";
    }
}
}
?>