<?php

//define $student as an empty array
$student = [];


// Retrieve student information based on the provided student_id
if(isset($_GET['student_id'])){
    $student_id = $_GET['student_id'];
    $get_student_query = "SELECT * FROM portal WHERE student_id=$student_id";
    $result = mysqli_query($db, $get_student_query);
    $student = mysqli_fetch_assoc($result);
}

    if(isset($_POST['editstudent'])){
        $student_id = mysqli_real_escape_string($db, $_POST['student_id']);
        $sname =  mysqli_real_escape_string($db, $_POST['sname']);
        $fname =  mysqli_real_escape_string($db, $_POST['fname']);
        $mat = mysqli_real_escape_string($db, $_POST['mat']);
        $program =  mysqli_real_escape_string($db, $_POST['program']);
        $stpassword = mysqli_real_escape_string($db, $_POST['stpassword']);
        $Dept = mysqli_real_escape_string($db, $_POST['Dept']);
        $leveld = mysqli_real_escape_string($db, $_POST['leveld']);
        $sex = mysqli_real_escape_string($db, $_POST['sex']);
        $email = mysqli_real_escape_string($db, $_POST['email']);
        $phone = mysqli_real_escape_string($db, $_POST['phone']);

        // update student information in the database
        $update_query = "UPDATE portal SET sname='$sname', fname='$fname', mat='$mat', program='$program', stpassword='$stpassword', Dept='$Dept', leveld='$leveld', sex='$sex', email='$email', phone='$phone' WHERE student_id = $student_id";

        if(mysqli_query($db, $update_query)){
            echo "<script>alert('Student information updated successfully.')</script>";
            
            header("location:index.php?env=edit_student&student_id={$student_id}");
        }else{
            echo "<script>alert('Error updating student information:".mysqli_error($db)."')</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">Edit Student Information</h6>
                        
                    </div>
                    <div class="table-responsive">
                      <div class="container">
                        <div class="row">
                            <div class="col-md-2"></div>
                            <div class="col-md-8">
                    
        <!-- form for editing student information -->
        <form method="POST" action="index.php?env=edit_student">
            <div class="form-group">
                <div class="row">
                    <div class="col-md-6">
                        <input type="hidden" name="student_id" value="<?php echo $student['student_id']; ?>">
                        <!-- input field for editing student information -->
                        <input type="text" name="sname" value="<?php echo $student['sname']; ?>" class="form-control"><br/>
                    </div>
                    <div class="col-md-6">
                        <input type="text" name="fname"  value="<?php echo $student['fname']; ?>" class="form-control"><br/>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="row">
                    <div class="col-md-6">
                    <input type="text" name="mat" value="<?php echo $student['mat']; ?>" class="form-control"><br/>
                    </div>
                    <div class="col-md-6">
                    <input type="text" name="program" value="<?php echo $student['program']; ?>" class="form-control"><br/>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="row">
                    <div class="col-md-6">
                    <input type="text" name="Dept" value="<?php echo $student['Dept']; ?>" class="form-control"><br/>
                    </div>
                    <div class="col-md-6">
                    <input type="text" name="leveld" value="<?php echo $student['leveld']; ?>" class="form-control"><br/>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="row">
                    <div class="col-md-6">
                    <input type="text" name="sex" value="<?php echo $student['sex']; ?>" class="form-control"><br/>
                    </div>
                    <div class="col-md-6">
                    <input type="text" name="email" value="<?php echo $student['email']; ?>" class="form-control"><br/>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="row">
                    <div class="col-md-6">
                    <input type="text" name="phone" value="<?php echo $student['phone']; ?>" class="form-control"><br/>
                    </div>
                    <div class="col-md-6">
                    <input type="text" name="stpassword" value="<?php echo $student['stpassword']; ?>" class="form-control">
                    <p><b>Change Student Unique Code Here</b></p>
                </div>
                </div>
            </div>
            <div class="form-group">
                <button type="submit" name="editstudent" class="btn btn-outline-primary btn-block">Save Changes</button>
            </div>

        </form>
 

    </div>
        <div class="col-md-2"></div>
                    </div>            
                </div>
            </div>
        </div>
    </div>
            
    <!-- footer -->
    <?php include("footer.php"); ?>

    
                    
