<?php
session_start();
ob_start(); // Start output buffering at the beginning
include("connection.php");
include("includes/header.php");
include("includes/sidebar.php");

$user_profile=$_SESSION['user_name'];
if($user_profile==true){

}else{
    // header('location:index.php');
}
?>

<?php
 $id = $_GET['update_id']; // Get the ID from the URL
 
 // Select specific fields from the `hostel` table
 $sql = "SELECT `id`, `category`,`title`, `subtitle`,`description`, `image` FROM `rdpage` WHERE id='$id'";
 
 $result = mysqli_query($conn, $sql);
 $row = mysqli_fetch_assoc($result); // Fetch the record

 $category = $row['category'];
 $image = $row['image'];
 $title = $row['title'];
 $subtitle = $row['subtitle'];
 $desc = $row['description'];
?>

 <div class="app-main__outer">
                    <div class="app-main__inner">

                        <div class="app-page-title">
                            <div class="page-title-wrapper">
                                <div class="page-title-heading">
                                    <div class="page-title-icon">
                                        <i class="pe-7s-car icon-gradient bg-mean-fruit">
                                        </i>
                                    </div>
                                    <div>Admin Dashboard
                                        
                                    </div>
                                </div>
                                <div class="page-title-actions">
                                    <button type="button" data-toggle="tooltip" title="Example Tooltip" data-placement="bottom" class="btn-shadow mr-3 btn btn-dark">
                                        <i class="fa fa-star"></i>
                                    </button>
                                 
                                </div>   
                             </div>
                         </div>

                         <div class="row">
    <div class="col-md-12">
        <div class="card mb-3">
            <div class="card-header">
                <strong>Update rdpage Information</strong>
            </div>
            <div class="card-body">
                <form action="" method="POST" enctype="multipart/form-data">
                
                <div class="form-group">
                        <label for="section_type">Category Type</label>
                        <select class="form-control" name="category" required>
                            <option value="<?php echo $category; ?>"><?php echo $category; ?></option>
                            <option value="about_school">About-School</option>
                            <option value="about_chairman">About-Chairman</option>
                            <option value="mission&vision">Mission & Vision</option>
                                            
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="image">Image</label>
                        <input type="file" class="form-control" name="image" accept="image/*">
                        <?php if ($image): ?>
                            <img src="upload/<?php echo $image; ?>" height="100px" width="100px">
                        <?php endif; ?>
                    </div>
                    <div class="form-group">
                        <label for="title">Title</label>
                        <input type="text" class="form-control" name="title" value="<?php echo $title; ?>" placeholder="Enter title">
                    </div>
                    <div class="form-group">
                        <label for="title">Subtitle</label>
                        <input type="text" class="form-control" name="subtitle" value="<?php echo $subtitle; ?>" placeholder="Enter Subtitle">
                    </div>
                    <div class="form-group">
                        <label for="desc">Content</label>
                        <textarea class="form-control" name="desc" rows="4" placeholder="Enter content"><?php echo $desc; ?></textarea>
                    </div>
                    <input type="submit" value="Update" name="update" class="btn btn-primary">
                </form>
            </div>
        </div>
    </div>
</div>

                    </div>

<?php
if (isset($_POST['update'])) {
    // Sanitize inputs
    $category = mysqli_real_escape_string($conn, $_POST['category']);
    $title = mysqli_real_escape_string($conn, $_POST['title']);
    $subtitle = mysqli_real_escape_string($conn, $_POST['subtitle']);
    $desc = mysqli_real_escape_string($conn, $_POST['desc']);


    // Handle second image upload
    if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
        $temp_name = $_FILES['image']['tmp_name'];
        $image_name = $_FILES['image']['name'];
        $target_path = 'upload/' . $image_name;

        move_uploaded_file($temp_name, $target_path);
    } else {
        $image_name = $image; // Keep the existing second image
    }

    // Update the record in the database
    $sql = "UPDATE rdpage SET 
      category='$category', 
                title='$title', 
                  subtitle='$subtitle', 
                description='$desc',     
                image='$image_name', 
                updated_at=NOW() 
            WHERE id='$id'";

    $result = mysqli_query($conn, $sql);

    if ($result) {
        header('location:view_r&dpage.php'); // Redirect after successful update
    } else {
        echo "Error updating record: " . mysqli_error($conn);
    }
}
?>



 </div>

<?php
  include("includes/footer.php");
  ob_end_flush(); // Send the buffered output to the client
  ?>
  