<?php
$pageTitle = "My Hall";
require_once 'sidebar.php';

$vid = $_SESSION['user_id'];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = clean($conn, $_POST['hall_name']);
    $desc = clean($conn, $_POST['description']);
    $loc = clean($conn, $_POST['location']);
    $cap = (int)$_POST['capacity'];
    $price = (float)$_POST['price'];
    
    $mainImage = $myHall['main_image'] ?? '';
    if (isset($_FILES['main_image']) && $_FILES['main_image']['error'] === 0) {
        $uploaded = uploadFile($_FILES['main_image'], 'halls');
        if ($uploaded) $mainImage = $uploaded;
    }
    
    if ($myHall) {
        $hid = $myHall['id'];
        mysqli_query($conn, "UPDATE halls SET hall_name='$name', description='$desc', location='$loc', 
                             capacity=$cap, price_per_day=$price, main_image='$mainImage' WHERE id=$hid");
        redirect(BASE_URL.'/vendor/my-hall.php', 'Hall updated successfully.');
    } else {
        mysqli_query($conn, "INSERT INTO halls (vendor_id, hall_name, description, location, capacity, price_per_day, main_image) 
                             VALUES ($vid, '$name', '$desc', '$loc', $cap, $price, '$mainImage')");
        redirect(BASE_URL.'/vendor/my-hall.php', 'Hall added successfully! Now add gallery photos and menu.');
    }
}
?>

<div class="page-header">
    <h1><?php echo $myHall ? 'Edit My Hall' : 'Add Your Shadi Hall'; ?></h1>
    <?php if ($myHall): ?>
        <a href="<?php echo BASE_URL; ?>/hall-details.php?id=<?php echo $myHall['id']; ?>" target="_blank" class="btn btn-sm btn-gold">Preview Live</a>
    <?php endif; ?>
</div>

<?php showFlash(); ?>

<div class="dashboard-card" style="max-width:800px;">
    <form method="POST" enctype="multipart/form-data">
        <div class="form-group">
            <label>Hall Name *</label>
            <input type="text" name="hall_name" required value="<?php echo htmlspecialchars($myHall['hall_name'] ?? ''); ?>" placeholder="e.g. Royal Palace Wedding Hall">
        </div>
        <div class="form-group">
            <label>Description</label>
            <textarea name="description" rows="5" placeholder="Describe your hall, its unique features, ambiance, and facilities..."><?php echo htmlspecialchars($myHall['description'] ?? ''); ?></textarea>
        </div>
        <div class="form-group">
            <label>Location *</label>
            <input type="text" name="location" required placeholder="Full address with area, city" value="<?php echo htmlspecialchars($myHall['location'] ?? ''); ?>">
        </div>
        <div class="split-layout">
            <div class="form-group">
                <label>Capacity (guests) *</label>
                <input type="number" name="capacity" required min="1" value="<?php echo $myHall['capacity'] ?? ''; ?>" placeholder="e.g. 500">
            </div>
            <div class="form-group">
                <label>Price per Day (Rs) *</label>
                <input type="number" name="price" required min="0" step="0.01" value="<?php echo $myHall['price_per_day'] ?? ''; ?>" placeholder="e.g. 150000">
            </div>
        </div>
        <div class="form-group">
            <label>Main Image <?php echo $myHall ? '(leave empty to keep current)' : '*'; ?></label>
            <input type="file" name="main_image" accept="image/*" <?php echo $myHall ? '' : 'required'; ?>>
            <?php if (!empty($myHall['main_image'])): ?>
                <div style="margin-top:15px;">
                    <img src="<?php echo BASE_URL.'/assets/uploads/halls/'.$myHall['main_image']; ?>" 
                         style="width:200px; height:150px; object-fit:cover; border-radius:4px; border:1px solid var(--cream-dark);">
                </div>
            <?php endif; ?>
        </div>
        <button type="submit" class="btn"><?php echo $myHall ? 'Update Hall' : 'Add Hall'; ?></button>
    </form>
</div>

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