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

$message = '';

// ==================== Get Menu ID ====================
if(!isset($_GET['id'])){
    echo "<script>alert('Menu ID not specified!'); window.location='add_menu.php';</script>";
    exit;
}

$menu_id = intval($_GET['id']);

// Fetch menu item
$menu = $conn->query("SELECT * FROM restaurant_menu WHERE id=$menu_id")->fetch_assoc();
if(!$menu){
    echo "<script>alert('Menu item not found!'); window.location='add_menu.php';</script>";
    exit;
}

// Fetch products for optional linking
$products = $conn->query("SELECT * FROM products ORDER BY name ASC");

// ==================== Handle Form Submission ====================
if(isset($_POST['update_menu'])){
    $name = $conn->real_escape_string($_POST['name']);
    $category = $conn->real_escape_string($_POST['category']);
    $price = floatval($_POST['price']);
    $product_id = !empty($_POST['product_id']) ? intval($_POST['product_id']) : NULL;
    $is_active = isset($_POST['is_active']) ? 1 : 0;

    $sql = "UPDATE restaurant_menu SET 
                name='$name',
                category='$category',
                price=$price,
                product_id=".($product_id ?? 'NULL').",
                is_active=$is_active
            WHERE id=$menu_id";

    if($conn->query($sql)){
        $message = "<div class='alert alert-success'>Menu item '$name' updated successfully!</div>";
        // Refresh menu data
        $menu = $conn->query("SELECT * FROM restaurant_menu WHERE id=$menu_id")->fetch_assoc();
    } else {
        $message = "<div class='alert alert-danger'>Error: ".$conn->error."</div>";
    }
}
?>

<div class="container mt-4">
      <!-- ================= WELCOME MESSAGE ================= -->
    
<h3>Edit Menu Item</h3>
<hr>

<?= $message ?>

<form method="POST" class="row g-3">
    <div class="col-md-6">
        <label>Menu Item Name</label>
        <input type="text" name="name" class="form-control" value="<?= htmlspecialchars($menu['name']) ?>" required>
    </div>
    <div class="col-md-3">
        <label>Category</label>
        <select name="category" class="form-control" required>
            <?php 
            $categories = ['Appetizer','Main','Dessert','Drink','Other'];
            foreach($categories as $cat):
            ?>
            <option value="<?= $cat ?>" <?= $menu['category']==$cat ? 'selected' : '' ?>><?= $cat ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="col-md-3">
        <label>Price (MK)</label>
        <input type="number" step="0.01" name="price" class="form-control" value="<?= $menu['price'] ?>" required>
    </div>
    <div class="col-md-6">
        <label>Link to Stock Product (optional)</label>
        <select name="product_id" class="form-control">
            <option value="">-- None --</option>
            <?php while($p = $products->fetch_assoc()): ?>
                <option value="<?= $p['id'] ?>" <?= $menu['product_id']==$p['id'] ? 'selected' : '' ?>>
                    <?= $p['name'] ?> (SKU: <?= $p['sku'] ?>)
                </option>
            <?php endwhile; ?>
        </select>
    </div>
    <div class="col-md-3">
        <label>Active?</label>
        <div class="form-check mt-2">
            <input type="checkbox" name="is_active" class="form-check-input" <?= $menu['is_active'] ? 'checked' : '' ?>>
            <label class="form-check-label">Yes</label>
        </div>
    </div>
    <div class="col-md-12 mt-3">
        <button type="submit" name="update_menu" class="btn btn-success w-100">
            <i class="fa fa-save"></i> Update Menu Item
        </button>
    </div>
</form>
<div class="mt-3">
    <a href="restaurant_menu.php" class="btn btn-secondary"><i class="fa fa-arrow-left"></i> Back to Menu List</a>
</div>
</div>

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