<?php

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


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

    if(isset($_POST['edithostel'])){
        $hosteltable_id = mysqli_real_escape_string($db, $_POST['hosteltable_id']);
        $sname =  mysqli_real_escape_string($db, $_POST['sname']);
        $othernames =  mysqli_real_escape_string($db, $_POST['othernames']);
        $department_name = mysqli_real_escape_string($db, $_POST['department_name']);
        $residential_address =  mysqli_real_escape_string($db, $_POST['residential_address']);
        $nameguardian = mysqli_real_escape_string($db, $_POST['nameguardian']);
        $phoneall = mysqli_real_escape_string($db, $_POST['phoneall']);
        $guardian = mysqli_real_escape_string($db, $_POST['guardian']);
        $approval = mysqli_real_escape_string($db, $_POST['approval']);
        

        // update hostel information in the database
        $update_query = "UPDATE hostel_table SET sname='$sname', othernames='$othernames', department_name='$department_name', residential_address='$residential_address', nameguardian='$nameguardian', phoneall='$phoneall', guardian='$guardian', approval='$approval' WHERE hosteltable_id = $hosteltable_id";

        if(mysqli_query($db, $update_query)){
            echo "<script>alert('hostel information updated successfully.')</script>";
            
            header("location:index.php?env=edit_hostel&hosteltable_id={$hosteltable_id}");
        }else{
            echo "<script>alert('Error updating hostel 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 hostel 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_hostel">
            <div class="form-group">
                <div class="row">
                    <div class="col-md-6">
                        <input type="hidden" name="hosteltable_id" value="<?php echo $hostel['hosteltable_id']; ?>">
                        <!-- input field for editing hostel information -->
                        <input type="text" name="sname" value="<?php echo $hostel['sname']; ?>" class="form-control"><br/>
                    </div>
                    <div class="col-md-6">
                        <input type="text" name="othernames"  value="<?php echo $hostel['othernames']; ?>" class="form-control"><br/>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="row">
                    <div class="col-md-6">
                    <input type="text" name="department_name" value="<?php echo $hostel['department_name']; ?>" class="form-control"><br/>
                    </div>
                    <div class="col-md-6">
                    <input type="text" name="residential_address" value="<?php echo $hostel['residential_address']; ?>" class="form-control"><br/>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="row">
                    <div class="col-md-6">
                    <input type="text" name="nameguardian" value="<?php echo $hostel['nameguardian']; ?>" class="form-control"><br/>
                    </div>
                    <div class="col-md-6">
                    <input type="text" name="phoneall" value="<?php echo $hostel['phoneall']; ?>" class="form-control"><br/>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="row">
                    <div class="col-md-6">
                    <input type="text" name="guardian" value="<?php echo $hostel['guardian']; ?>" class="form-control"><br/>
                    </div>
                    <div class="col-md-6">
                    <input type="text" name="approval" placeholder="Enter Approval Note"  class="form-control"><br/>
                    </div>
                </div>
            </div>
            
            <div class="form-group">
                <button type="submit" name="edithostel" 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"); ?>

    
                    
