<?php
$pageTitle = "Gallery & Video";
require_once 'sidebar.php';

if (!$myHall) {
    redirect(BASE_URL.'/vendor/my-hall.php', 'Please add your hall first.', 'info');
}

$hid = $myHall['id'];

if (isset($_POST['upload_gallery']) && isset($_FILES['images'])) {
    $uploaded = 0;
    foreach ($_FILES['images']['name'] as $k => $name) {
        if ($_FILES['images']['error'][$k] === 0) {
            $file = [
                'name' => $_FILES['images']['name'][$k],
                'tmp_name' => $_FILES['images']['tmp_name'][$k]
            ];
            $fn = uploadFile($file, 'halls');
            if ($fn) {
                mysqli_query($conn, "INSERT INTO hall_gallery (hall_id, image) VALUES ($hid, '$fn')");
                $uploaded++;
            }
        }
    }
    redirect(BASE_URL.'/vendor/gallery.php', "$uploaded image(s) uploaded successfully.");
}

if (isset($_POST['upload_video']) && isset($_FILES['video']) && $_FILES['video']['error']===0) {
    $fn = uploadFile($_FILES['video'], 'videos');
    if ($fn) {
        mysqli_query($conn, "UPDATE halls SET video='$fn' WHERE id=$hid");
        redirect(BASE_URL.'/vendor/gallery.php', 'Video uploaded successfully.');
    }
}

if (isset($_GET['del_img'])) {
    $id = (int)$_GET['del_img'];
    mysqli_query($conn, "DELETE FROM hall_gallery WHERE id=$id AND hall_id=$hid");
    redirect(BASE_URL.'/vendor/gallery.php', 'Image deleted.');
}

if (isset($_GET['del_video'])) {
    mysqli_query($conn, "UPDATE halls SET video='' WHERE id=$hid");
    redirect(BASE_URL.'/vendor/gallery.php', 'Video deleted.');
}

$images = mysqli_query($conn, "SELECT * FROM hall_gallery WHERE hall_id=$hid");
$hallRow = mysqli_fetch_assoc(mysqli_query($conn, "SELECT video FROM halls WHERE id=$hid"));
?>

<div class="page-header">
    <h1>Gallery & Video</h1>
</div>

<?php showFlash(); ?>

<div class="split-layout">
    <div class="dashboard-card">
        <h2 class="full">📸 Upload Gallery Images</h2>
        <form method="POST" enctype="multipart/form-data">
            <div class="form-group">
                <label>Select multiple images</label>
                <input type="file" name="images[]" accept="image/*" multiple required>
                <small style="color:#888; font-size:12px;">You can select multiple images at once (JPG, PNG, WEBP)</small>
            </div>
            <button type="submit" name="upload_gallery" class="btn">Upload Images</button>
        </form>
    </div>
    
    <div class="dashboard-card">
        <h2 class="full">🎥 Upload Hall Video</h2>
        <form method="POST" enctype="multipart/form-data">
            <div class="form-group">
                <label>Video Tour (MP4 recommended)</label>
                <input type="file" name="video" accept="video/*" required>
                <small style="color:#888; font-size:12px;">Max 100MB recommended for faster loading</small>
            </div>
            <button type="submit" name="upload_video" class="btn btn-gold">Upload Video</button>
        </form>
    </div>
</div>

<?php if (!empty($hallRow['video'])): ?>
<div class="dashboard-card">
    <h2 class="full">Current Video</h2>
    <div class="video-wrapper">
        <video src="<?php echo BASE_URL.'/assets/uploads/videos/'.$hallRow['video']; ?>" controls></video>
    </div>
    <div style="margin-top:15px;">
        <a href="?del_video=1" class="btn btn-sm btn-danger" onclick="return confirm('Delete this video?')">Delete Video</a>
    </div>
</div>
<?php endif; ?>

<div class="dashboard-card">
    <h2 class="full">Current Gallery (<?php echo mysqli_num_rows($images); ?> photos)</h2>
    <?php if (mysqli_num_rows($images) > 0): ?>
        <div class="gallery">
            <?php while ($img = mysqli_fetch_assoc($images)): ?>
                <div class="gallery-item" style="position:relative;">
                    <img src="<?php echo BASE_URL.'/assets/uploads/halls/'.$img['image']; ?>">
                    <a href="?del_img=<?php echo $img['id']; ?>" 
                       onclick="return confirm('Delete this image?')"
                       style="position:absolute; top:8px; right:8px; background:rgba(139,38,53,0.95); color:white; width:28px; height:28px; border-radius:50%; text-decoration:none; font-weight:bold; display:flex; align-items:center; justify-content:center; font-size:16px;">×</a>
                </div>
            <?php endwhile; ?>
        </div>
    <?php else: ?>
        <div style="text-align:center; padding:40px; color:#888;">
            <div style="font-size:40px; color:var(--bronze); margin-bottom:10px;">❦</div>
            No images uploaded yet. Add some beautiful photos of your hall!
        </div>
    <?php endif; ?>
</div>

</main></div></body></html>
