<?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) {
//     header('location:index.php');
// }

$id = $_GET['update_id'];
$sql = "SELECT * FROM gallery WHERE id='$id'";
$result = mysqli_query($conn, $sql);
$total = mysqli_num_rows($result);
$row = mysqli_fetch_assoc($result);

$image = $row['image'];
// $title = $row['title'];
?>

<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 Gallery</strong>
                    </div>
                    <div class="card-body">
                        <form action="" method="POST" enctype="multipart/form-data">
                            <div class="form-group">
                                <label for="smallImage">Image</label>
                                <input type="file" class="form-control" name="image" accept="image/*">
                                <img src="upload/<?php echo $image; ?>" height="100px" width="100px">
                            </div>
                           
                            <!--<div class="form-group">-->
                            <!--    <label for="title">Title</label>-->
                            <!--    <input type="text" class="form-control" name="title" value="<?php echo $title; ?>" placeholder="Enter image title">-->
                            <!--</div>-->
                            <input type="submit" value="Update" name="update" class="btn btn-primary">
                        </form>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <?php
    if (isset($_POST['update'])) {
        // $title = $_POST['title'];

        // Handle Small Image Upload
        if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
            $temp_name = $_FILES['image']['tmp_name'];
            $image_name = $_FILES['image']['name'];
            $upload_directory = 'upload/';
            $target_path_small = $upload_directory . $small_image_name;

            if (move_uploaded_file($temp_name, $target_path_small)) {
                // Small image uploaded successfully
                $sql = "UPDATE gallery SET image='$image_name' WHERE id='$id'";
                $result = mysqli_query($conn, $sql);
            } else {
                echo "Error uploading  image.";
            }
        }

       
        if ($result) {
            header('location:view_gallery.php');
        } else {
            echo "Error updating record: " . mysqli_error($conn);
        }
    }
    ?>

</div>

<?php
include("includes/footer.php");
ob_end_flush(); // Send the buffered output to the client
?>
