<?php
require_once 'dbconnect.php';

// Insert Code
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $img_name = $_POST['img_name'];
    $img_file = $_FILES['img_file'];

    $folder = "uploads/";

    if (!is_dir($folder)) {
        mkdir($folder, 0777, true);
    }

    $filename = time() . "_" . basename($img_file["name"]);
    $target = $folder . $filename;

    if (move_uploaded_file($img_file["tmp_name"], $target)) {

        // FIXED: $conn → $con
        $stmt = $con->prepare("INSERT INTO images (name, filename) VALUES (?, ?)");
        $stmt->bind_param("ss", $img_name, $filename);

        if ($stmt->execute()) {
            echo "<script>alert('Image Uploaded Successfully');</script>";
        } else {
            echo "<script>alert('Database Insert Failed');</script>";
        }

    } else {
        echo "<script>alert('File Upload Error');</script>";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Demo Upload</title>
    <style>
        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(135deg, #1f1c2c, #928dab);
            margin: 0;
            padding: 0;
        }
        .logout-btn {
            background: #e74c3c;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            font-weight: bold;
            position: absolute;
            top: 20px;
            right: 20px;
            transition: 0.3s;
        }
        .logout-btn:hover {
            background: #c0392b;
        }
        .container {
            width: 90%;
            max-width: 480px;
            margin: 80px auto;
            background: rgba(255, 255, 255, 0.12);
            padding: 25px;
            border-radius: 15px;
            backdrop-filter: blur(8px);
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3);
            color: #fff;
        }
        h2 { text-align:center; font-weight: bold; }
        label { font-weight:bold; display:block; margin-top: 10px; }

        input[type=text], input[type=file] {
            width:100%; padding:12px; margin-top:8px;
            border: none;
            border-radius:8px;
            outline: none;
            background: rgba(255, 255, 255, 0.2);
            color: #fff;
        }
        input::placeholder { color: #eee; }

        button {
            width:100%; padding:14px; border:none;
            background: linear-gradient(135deg, #6a11cb, #2575fc);
            color:#fff; font-size:16px; border-radius:8px;
            cursor:pointer; font-weight:bold;
            margin-top: 15px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.3);
            transition: 0.3s;
        }
        button:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 20px rgba(0,0,0,0.4);
        }

        @media (max-width:480px) {
            .container { padding:20px; margin-top: 60px; }
            button { font-size:14px; }
        }
    </style>
</head>
<body>
    <button class="logout-btn" onclick="location.href='dashboard.php'">Logout</button>

    <div class="container">
        <h2>Image Upload</h2>
        <form action="demoupload.php" method="POST" enctype="multipart/form-data">
            <label>Image Name:</label>
            <input type="text" name="img_name" placeholder="Enter image name" required>

            <label>Select Image:</label>
            <input type="file" name="img_file" accept="image/*" required>

            <button type="submit">Upload Image</button>
        </form>
    </div>
</body>
</html>
