<?php
include 'auth.php';
include '../includes/header.php';

// Handle Upload
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['gallery_img'])) {
    $title = sanitize($conn, $_POST['title']);
    if (!empty($_FILES['gallery_img']['name'])) {
        $img_name = time() . '_' . $_FILES['gallery_img']['name'];
        move_uploaded_file($_FILES['gallery_img']['tmp_name'], "../uploads/" . $img_name);
        $conn->query("INSERT INTO gallery (title, image) VALUES ('$title', '$img_name')");
        echo "<script>alert('Image added to gallery!'); window.location='manage-gallery.php';</script>";
    }
}

// Handle Delete
if (isset($_GET['delete'])) {
    $del_id = (int)$_GET['delete'];
    $conn->query("DELETE FROM gallery WHERE id = $del_id");
    header("Location: manage-gallery.php");
    exit();
}

$gallery = $conn->query("SELECT * FROM gallery ORDER BY id DESC");
?>

<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="fw-bold mb-0">General Gallery Management</h2>
        <a href="index.php" class="btn btn-outline-secondary rounded-pill px-4">
            <i class="fas fa-arrow-left me-2"></i> Back to Dashboard
        </a>
    </div>

    <div class="row g-4">
        <!-- Add New Image -->
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm p-4" style="border-radius: 20px;">
                <h5 class="fw-bold mb-4">Add New Image</h5>
                <form method="POST" enctype="multipart/form-data">
                    <div class="mb-3">
                        <label class="form-label small fw-bold text-muted">Title (Optional)</label>
                        <input type="text" name="title" class="form-control border-0 bg-light" placeholder="e.g. Beautiful Sunset" style="border-radius: 10px;">
                    </div>
                    <div class="mb-4">
                        <label class="form-label small fw-bold text-muted">Select Image</label>
                        <input type="file" name="gallery_img" class="form-control border-0 bg-light" accept="image/*" required style="border-radius: 10px;">
                    </div>
                    <button type="submit" class="btn btn-primary w-100 py-3 rounded-pill fw-bold shadow-sm">
                        <i class="fas fa-upload me-2"></i> UPLOAD TO GALLERY
                    </button>
                </form>
            </div>
        </div>

        <!-- Gallery List -->
        <div class="col-lg-8">
            <div class="card border-0 shadow-sm p-4" style="border-radius: 20px;">
                <h5 class="fw-bold mb-4">Current Gallery Images</h5>
                <div class="row g-3">
                    <?php if($gallery && $gallery->num_rows > 0): ?>
                        <?php while($img = $gallery->fetch_assoc()): ?>
                            <div class="col-md-4">
                                <div class="position-relative overflow-hidden rounded-3 shadow-sm group" style="height: 150px;">
                                    <img src="../uploads/<?= $img['image'] ?>" class="w-100 h-100" style="object-fit: cover;">
                                    <div class="position-absolute top-0 end-0 p-2">
                                        <a href="manage-gallery.php?delete=<?= $img['id'] ?>" class="btn btn-danger btn-sm rounded-circle shadow-sm" onclick="return confirm('Delete this image?')">
                                            <i class="fas fa-times"></i>
                                        </a>
                                    </div>
                                    <?php if($img['title']): ?>
                                        <div class="position-absolute bottom-0 start-0 w-100 p-2 bg-dark bg-opacity-50 text-white small">
                                            <?= htmlspecialchars($img['title']) ?>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <div class="col-12 text-center py-5">
                            <p class="text-muted">No images in the general gallery yet.</p>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include '../includes/footer.php'; ?>
