<?php
require_once 'dbconnect.php'; 
$con = $con; // DB connection

// Check valid ID
if (!isset($_GET['id'])) {
    die("Invalid Request!");
}

$id = $_GET['id'];

// Fetch existing record
$stmt = $con->prepare("SELECT * FROM images WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$data = $stmt->get_result()->fetch_assoc();

if (!$data) {
    die("Record Not Found!");
}

// Update Process
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $name = $_POST['img_name'];
    $old_img = $data['filename'];
    $new_img = $old_img;

    // New Image Upload
    if (!empty($_FILES['img_file']['name'])) {

        $file_name = time() . "_" . basename($_FILES['img_file']['name']);
        $target = "uploads/" . $file_name;

        if (move_uploaded_file($_FILES['img_file']['tmp_name'], $target)) {

            // Delete old image
            if (file_exists("uploads/" . $old_img)) {
                unlink("uploads/" . $old_img);
            }

            $new_img = $file_name;
        }
    }

    // Update database
    $update = $con->prepare("UPDATE images SET name=?, filename=? WHERE id=?");
    $update->bind_param("ssi", $name, $new_img, $id);

    if ($update->execute()) {
        echo "<script>alert('Updated Successfully!'); window.location='demouploaddisplay.php';</script>";
    } else {
        echo "<script>alert('Error updating record!');</script>";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Edit Image</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
    body {
        font-family: Poppins, sans-serif;
        background: linear-gradient(135deg, #090979, #4b6cb7);
        margin: 0;
        padding: 0;
        color: white;
    }

    .container {
        width: 92%;
        max-width: 480px;
        margin: 60px auto;
        background: rgba(255,255,255,0.10);
        padding: 28px;
        border-radius: 18px;
        backdrop-filter: blur(10px);
        box-shadow: 0 8px 30px rgba(0,0,0,0.35);
    }

    h2 {
        text-align: center;
        font-weight: 600;
        letter-spacing: 1px;
    }

    label {
        display: block;
        margin-top: 14px;
        font-weight: 600;
    }

    input[type=text],
    input[type=file] {
        width: 100%;
        padding: 12px;
        margin-top: 7px;
        border: none;
        border-radius: 10px;
        background: rgba(255,255,255,0.20);
        color: #fff;
        font-size: 15px;
        outline: none;
    }

    .preview-img {
        width: 100%;
        max-height: 260px;
        object-fit: cover;
        border-radius: 12px;
        margin-top: 12px;
        border: 2px solid rgba(255,255,255,0.4);
    }

    button {
        width: 100%;
        padding: 15px;
        background: linear-gradient(135deg, #ff416c, #ff4b2b);
        color: white;
        border: none;
        border-radius: 10px;
        font-size: 18px;
        cursor: pointer;
        margin-top: 22px;
        font-weight: bold;
        box-shadow: 0 4px 15px rgba(0,0,0,0.35);
        transition: 0.3s;
    }

    button:hover {
        transform: translateY(-3px);
        box-shadow: 0 7px 22px rgba(0,0,0,0.5);
    }

    .back-btn {
        display: block;
        width: 100%;
        text-align: center;
        margin-top: 18px;
        padding: 12px;
        border-radius: 10px;
        background: rgba(0,0,0,0.35);
        text-decoration: none;
        color: white;
        font-weight: bold;
        transition: .3s;
    }

    .back-btn:hover {
        background: rgba(0,0,0,0.55);
    }
</style>

</head>
<body>

<div class="container">
    <h2>Edit Image</h2>

    <form method="POST" enctype="multipart/form-data">

        <label>Image Name:</label>
        <input type="text" name="img_name" value="<?= $data['name']; ?>" required>

        <label>Current Image:</label>
        <img src="uploads/<?= $data['filename']; ?>" class="preview-img">

        <label>Change Image (optional):</label>
        <input type="file" name="img_file" accept="image/*">

        <button type="submit">Update Image</button>

    </form>

    <a href="demouploaddisplay.php" class="back-btn">← Back to List</a>
</div>

</body>
</html>
