{% extends 'base.html' %}

{% block content %}
<div class="animate-fade-in">
    <div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center mb-5 gap-3">
        <div>
            <h1 class="section-title mb-1">Menu Management</h1>
            <p class="text-secondary mb-0">Create and edit your restaurant's menu items.</p>
        </div>
        <div>
            <a href="{% url 'dashboard' %}" class="btn btn-outline-dark">Back to Dashboard</a>
        </div>
    </div>

    <div class="row g-4">
        <!-- Add Menu Item Form -->
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm sticky-top" style="top: 20px;">
                <div class="card-body p-4">
                    <h4 class="fw-bold mb-4">Add New Item</h4>
                    <form method="POST">
                        {% csrf_token %}
                        <div class="mb-3">
                            <label class="form-label small fw-bold text-secondary">Item Name</label>
                            <input type="text" name="name" class="form-control" placeholder="e.g. Classic Burger" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label small fw-bold text-secondary">Price (EGP)</label>
                            <input type="number" step="0.01" name="price" class="form-control" placeholder="0.00" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label small fw-bold text-secondary">Description</label>
                            <textarea name="description" class="form-control" rows="2" placeholder="Brief description..."></textarea>
                        </div>
                        <div class="mb-3">
                            <label class="form-label small fw-bold text-secondary">Image URL (Optional)</label>
                            <input type="url" name="image_url" class="form-control" placeholder="https://...">
                        </div>
                        <div class="mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" name="is_active" id="isActive" checked>
                                <label class="form-check-label small fw-bold text-secondary" for="isActive">Active & Visible</label>
                            </div>
                        </div>

                        <hr class="my-4">
                        <h6 class="fw-bold mb-3">Recipe / Ingredients</h6>
                        <div id="ingredient-rows">
                            <div class="ingredient-row row g-2 mb-2">
                                <div class="col-7">
                                    <select name="ingredients" class="form-select form-select-sm">
                                        <option value="">Select Ingredient</option>
                                        {% for ing in ingredients %}
                                        <option value="{{ ing.id }}">{{ ing.name }} ({{ ing.unit }})</option>
                                        {% endfor %}
                                    </select>
                                </div>
                                <div class="col-5">
                                    <input type="number" step="0.01" name="quantities" class="form-control form-control-sm" placeholder="Qty">
                                </div>
                            </div>
                        </div>
                        <button type="button" class="btn btn-sm btn-light w-100 mt-2" onclick="addIngredientRow()">
                            <i class="bi bi-plus-circle me-1"></i> Add Another Ingredient
                        </button>

                        <button type="submit" class="btn btn-vibrant w-100 mt-4 py-3">Create Menu Item</button>
                    </form>
                </div>
            </div>
        </div>

        <!-- Menu Item List -->
        <div class="col-lg-8">
            <div class="card border-0 shadow-sm">
                <div class="card-body p-4">
                    <h4 class="fw-bold mb-4">Current Menu</h4>
                    <div class="table-responsive">
                        <table class="table table-hover align-middle">
                            <thead class="bg-light">
                                <tr class="text-secondary small">
                                    <th class="py-3 border-0 rounded-start">Item</th>
                                    <th class="py-3 border-0 text-center">Price</th>
                                    <th class="py-3 border-0 text-center">Status</th>
                                    <th class="py-3 border-0 text-end rounded-end">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for item in menu_items %}
                                <tr>
                                    <td class="py-3">
                                        <div class="d-flex align-items-center">
                                            {% if item.image_url %}
                                            <img src="{{ item.image_url }}" alt="{{ item.name }}" class="rounded-3 me-3" style="width: 45px; height: 45px; object-fit: cover;">
                                            {% else %}
                                            <div class="bg-light rounded-3 me-3 d-flex align-items-center justify-content-center" style="width: 45px; height: 45px;">
                                                <i class="bi bi-egg-fried text-secondary"></i>
                                            </div>
                                            {% endif %}
                                            <div>
                                                <div class="fw-bold text-dark">{{ item.name }}</div>
                                                <div class="small text-secondary">{{ item.ingredients.count }} ingredients</div>
                                            </div>
                                        </div>
                                    </td>
                                    <td class="text-center fw-bold">{{ item.price|floatformat:2 }} EGP</td>
                                    <td class="text-center">
                                        {% if item.is_active %}
                                        <span class="badge bg-success bg-opacity-10 text-success rounded-pill px-3">Active</span>
                                        {% else %}
                                        <span class="badge bg-secondary bg-opacity-10 text-secondary rounded-pill px-3">Inactive</span>
                                        {% endif %}
                                    </td>
                                    <td class="text-end">
                                        <div class="dropdown">
                                            <button class="btn btn-sm btn-light rounded-pill px-3" data-bs-toggle="dropdown">
                                                <i class="bi bi-three-dots-vertical"></i>
                                            </button>
                                            <ul class="dropdown-menu dropdown-menu-end border-0 shadow-sm">
                                                <li><a class="dropdown-item py-2" href="{% url 'edit_menu_item' item.pk %}"><i class="bi bi-pencil me-2"></i> Edit Item</a></li>
                                                <li><a class="dropdown-item py-2" href="{% url 'toggle_menu_item_status' item.pk %}">
                                                    {% if item.is_active %}
                                                    <i class="bi bi-eye-slash me-2"></i> Deactivate
                                                    {% else %}
                                                    <i class="bi bi-eye me-2"></i> Activate
                                                    {% endif %}
                                                </a></li>
                                            </ul>
                                        </div>
                                    </td>
                                </tr>
                                {% empty %}
                                <tr>
                                    <td colspan="4" class="text-center py-5">
                                        <p class="text-secondary mb-0">No menu items found. Add your first one!</p>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function addIngredientRow() {
    const container = document.getElementById('ingredient-rows');
    const firstRow = container.querySelector('.ingredient-row');
    const newRow = firstRow.cloneNode(true);
    
    // Clear the selected option and quantity
    newRow.querySelector('select').selectedIndex = 0;
    newRow.querySelector('input').value = '';
    
    container.appendChild(newRow);
}
</script>
{% endblock %}
