<?php
$pageTitle = "Dishes & Menu";
require_once 'sidebar.php';

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

$hid = $myHall['id'];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = clean($conn, $_POST['dish_name']);
    $desc = clean($conn, $_POST['description']);
    $price = (float)$_POST['price'];
    $cat = clean($conn, $_POST['category']);
    
    if (isset($_POST['edit_id'])) {
        $eid = (int)$_POST['edit_id'];
        mysqli_query($conn, "UPDATE dishes SET dish_name='$name', description='$desc', price=$price, category='$cat' WHERE id=$eid AND hall_id=$hid");
        redirect(BASE_URL.'/vendor/dishes.php', 'Dish updated successfully.');
    } else {
        mysqli_query($conn, "INSERT INTO dishes (hall_id, dish_name, description, price, category) 
                             VALUES ($hid, '$name', '$desc', $price, '$cat')");
        redirect(BASE_URL.'/vendor/dishes.php', 'Dish added successfully.');
    }
}

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

$editDish = null;
if (isset($_GET['edit'])) {
    $eid = (int)$_GET['edit'];
    $r = mysqli_query($conn, "SELECT * FROM dishes WHERE id=$eid AND hall_id=$hid");
    $editDish = mysqli_fetch_assoc($r);
}

$dishes = mysqli_query($conn, "SELECT * FROM dishes WHERE hall_id=$hid ORDER BY category, dish_name");
?>

<div class="page-header">
    <h1>Dishes & Menu Management</h1>
</div>

<?php showFlash(); ?>

<div class="dashboard-card">
    <h2 class="full"><?php echo $editDish ? '✎ Edit Dish' : '+ Add New Dish'; ?></h2>
    <form method="POST">
        <?php if ($editDish): ?>
            <input type="hidden" name="edit_id" value="<?php echo $editDish['id']; ?>">
        <?php endif; ?>
        <div class="split-layout">
            <div class="form-group">
                <label>Dish Name *</label>
                <input type="text" name="dish_name" required value="<?php echo htmlspecialchars($editDish['dish_name'] ?? ''); ?>" placeholder="e.g. Chicken Biryani">
            </div>
            <div class="form-group">
                <label>Category *</label>
                <select name="category" required>
                    <?php $cats = ['Starter','Main Course','BBQ','Rice','Dessert','Drinks','Salad','Others']; 
                    foreach ($cats as $c): ?>
                        <option value="<?php echo $c; ?>" <?php echo (isset($editDish['category']) && $editDish['category']===$c)?'selected':''; ?>><?php echo $c; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label>Description</label>
            <textarea name="description" rows="2" placeholder="Brief description of the dish"><?php echo htmlspecialchars($editDish['description'] ?? ''); ?></textarea>
        </div>
        <div class="form-group">
            <label>Price per Plate (Rs) *</label>
            <input type="number" name="price" required min="0" step="0.01" value="<?php echo $editDish['price'] ?? ''; ?>" placeholder="e.g. 500">
        </div>
        <button type="submit" class="btn"><?php echo $editDish ? 'Update Dish' : 'Add Dish'; ?></button>
        <?php if ($editDish): ?>
            <a href="dishes.php" class="btn btn-gold">Cancel</a>
        <?php endif; ?>
    </form>
</div>

<div class="dashboard-card">
    <h2 class="full">Menu List (<?php echo mysqli_num_rows($dishes); ?> dishes)</h2>
    <?php if (mysqli_num_rows($dishes) > 0): ?>
        <div class="table-wrapper">
            <table>
                <thead>
                    <tr>
                        <th>Category</th>
                        <th>Dish Name</th>
                        <th>Description</th>
                        <th>Price</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($d = mysqli_fetch_assoc($dishes)): ?>
                        <tr>
                            <td><span class="badge badge-confirmed"><?php echo htmlspecialchars($d['category']); ?></span></td>
                            <td><strong style="color:var(--maroon-dark); font-family:var(--font-display);"><?php echo htmlspecialchars($d['dish_name']); ?></strong></td>
                            <td style="color:#666;"><?php echo htmlspecialchars($d['description']); ?></td>
                            <td style="color:var(--bronze); font-weight:700; font-size:16px; font-family:var(--font-display);">Rs. <?php echo number_format($d['price']); ?></td>
                            <td>
                                <a href="?edit=<?php echo $d['id']; ?>" class="btn btn-sm">Edit</a>
                                <a href="?delete=<?php echo $d['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete dish?')">Delete</a>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </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 dishes added yet. Start building your menu above!
        </div>
    <?php endif; ?>
</div>

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