<?php

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


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

    if(isset($_POST['editcarryover'])){
        $carryover_id = mysqli_real_escape_string($db, $_POST['carryover_id']);
        $sname =  mysqli_real_escape_string($db, $_POST['sname']);
        $department =  mysqli_real_escape_string($db, $_POST['department']);
        $semesta = mysqli_real_escape_string($db, $_POST['semesta']);
        $courses =  mysqli_real_escape_string($db, $_POST['courses']);
        $coursecode = mysqli_real_escape_string($db, $_POST['coursecode']);
        $courseunit = mysqli_real_escape_string($db, $_POST['courseunit']);
        $coursestatus = mysqli_real_escape_string($db, $_POST['coursestatus']);
        

        // update carryover information in the database
        $update_query = "UPDATE carryover SET sname='$sname', department='$department', semesta='$semesta', courses='$courses', coursecode='$coursecode', courseunit='$courseunit', coursestatus='$coursestatus' WHERE carryover_id = $carryover_id";

        if(mysqli_query($db, $update_query)){
            echo "<script>alert('carryover information updated successfully.')</script>";
            
            header("location:index.php?env=edit_carryover&carryover_id={$carryover_id}");
        }else{
            echo "<script>alert('Error updating carryover 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 carryover 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 carryover information -->
        <form method="POST" action="index.php?env=edit_carryover">
            <div class="form-group">
                <div class="row">
                    <div class="col-md-6">
                        <input type="hidden" name="carryover_id" value="<?php echo $carryover['carryover_id']; ?>">
                        <!-- input field for editing carryover information -->
                        <input type="text" name="sname" value="<?php echo $carryover['sname']; ?>" class="form-control"><br/>
                    </div>
                    <div class="col-md-6">
                        <input type="text" name="department"  value="<?php echo $carryover['department']; ?>" class="form-control"><br/>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="row">
                    <div class="col-md-6">
                    <input type="text" name="semesta" value="<?php echo $carryover['semesta']; ?>" class="form-control"><br/>
                    </div>
                    <div class="col-md-6">
                    <input type="text" name="courses" value="<?php echo $carryover['courses']; ?>" class="form-control"><br/>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="row">
                    <div class="col-md-6">
                    <input type="text" name="coursecode" value="<?php echo $carryover['coursecode']; ?>" class="form-control"><br/>
                    </div>
                    <div class="col-md-6">
                    <input type="text" name="courseunit" value="<?php echo $carryover['courseunit']; ?>" class="form-control"><br/>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="row">
                    <div class="col-md-6">
                    <input type="text" name="coursestatus" value="<?php echo $carryover['coursestatus']; ?>" class="form-control"><br/>
                    </div>
                    <div class="col-md-6">
                    
                    </div>
                </div>
            </div>
            
            <div class="form-group">
                <button type="submit" name="editcarryover" 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"); ?>

    
                    
