{% 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">Edit Menu Item</h1>
            <p class="text-secondary mb-0">Updating: <span class="fw-bold text-dark">{{ menu_item.name }}</span></p>
        </div>
        <div>
            <a href="{% url 'manage_menu' %}" class="btn btn-outline-dark">Cancel & Go Back</a>
        </div>
    </div>

    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="card border-0 shadow-sm">
                <div class="card-body p-4 p-md-5">
                    <form method="POST">
                        {% csrf_token %}
                        <div class="row g-4">
                            <div class="col-md-6">
                                <label class="form-label small fw-bold text-secondary">Item Name</label>
                                <input type="text" name="name" class="form-control form-control-lg" value="{{ menu_item.name }}" required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label small fw-bold text-secondary">Price (EGP)</label>
                                <input type="number" step="0.01" name="price" class="form-control form-control-lg" value="{{ menu_item.price|stringformat:'.2f' }}" required>
                            </div>
                            <div class="col-12">
                                <label class="form-label small fw-bold text-secondary">Description</label>
                                <textarea name="description" class="form-control" rows="3">{{ menu_item.description }}</textarea>
                            </div>
                            <div class="col-12">
                                <label class="form-label small fw-bold text-secondary">Image URL</label>
                                <input type="url" name="image_url" class="form-control" value="{{ menu_item.image_url|default:'' }}">
                                {% if menu_item.image_url %}
                                <div class="mt-2">
                                    <img src="{{ menu_item.image_url }}" alt="Preview" class="rounded-3 shadow-sm" style="max-height: 100px;">
                                </div>
                                {% endif %}
                            </div>
                            <div class="col-12">
                                <div class="form-check form-switch">
                                    <input class="form-check-input" type="checkbox" name="is_active" id="isActive" {% if menu_item.is_active %}checked{% endif %}>
                                    <label class="form-check-label small fw-bold text-secondary" for="isActive">Item is Active & Visible in POS</label>
                                </div>
                            </div>

                            <div class="col-12">
                                <hr class="my-4">
                                <h5 class="fw-bold mb-4">Recipe / Ingredients</h5>
                                <div id="ingredient-rows">
                                    {% for current in current_ingredients %}
                                    <div class="ingredient-row row g-3 mb-3 align-items-end">
                                        <div class="col-7">
                                            <label class="form-label small text-secondary">Ingredient</label>
                                            <select name="ingredients" class="form-select">
                                                <option value="">Select Ingredient</option>
                                                {% for ing in ingredients %}
                                                <option value="{{ ing.id }}" {% if current.ingredient.id == ing.id %}selected{% endif %}>
                                                    {{ ing.name }} ({{ ing.unit }})
                                                </option>
                                                {% endfor %}
                                            </select>
                                        </div>
                                        <div class="col-3">
                                            <label class="form-label small text-secondary">Qty Required</label>
                                            <input type="number" step="0.01" name="quantities" class="form-control" value="{{ current.quantity_required|stringformat:'g' }}">
                                        </div>
                                        <div class="col-2">
                                            <button type="button" class="btn btn-outline-danger w-100" onclick="this.parentElement.parentElement.remove()">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </div>
                                    </div>
                                    {% empty %}
                                    <div class="ingredient-row row g-3 mb-3 align-items-end">
                                        <div class="col-7">
                                            <label class="form-label small text-secondary">Ingredient</label>
                                            <select name="ingredients" class="form-select">
                                                <option value="">Select Ingredient</option>
                                                {% for ing in ingredients %}
                                                <option value="{{ ing.id }}">{{ ing.name }} ({{ ing.unit }})</option>
                                                {% endfor %}
                                            </select>
                                        </div>
                                        <div class="col-3">
                                            <label class="form-label small text-secondary">Qty Required</label>
                                            <input type="number" step="0.01" name="quantities" class="form-control" placeholder="0.00">
                                        </div>
                                        <div class="col-2">
                                            <button type="button" class="btn btn-outline-danger w-100" onclick="this.parentElement.parentElement.remove()">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </div>
                                    </div>
                                    {% endfor %}
                                </div>
                                <button type="button" class="btn btn-light rounded-pill px-4 mt-3" onclick="addIngredientRow()">
                                    <i class="bi bi-plus-circle me-1"></i> Add Another Ingredient
                                </button>
                            </div>

                            <div class="col-12 mt-5">
                                <button type="submit" class="btn btn-vibrant btn-lg w-100 py-3">Update Menu Item</button>
                            </div>
                        </div>
                    </form>
                </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 values
    newRow.querySelectorAll('select').forEach(s => s.selectedIndex = 0);
    newRow.querySelectorAll('input').forEach(i => i.value = '');
    
    // Ensure the trash button works on the new row
    newRow.querySelector('.btn-outline-danger').onclick = function() {
        this.parentElement.parentElement.remove();
    };
    
    container.appendChild(newRow);
}
</script>
{% endblock %}
