{% extends 'core/base.html' %}
{% load static %}

{% block title %}Edit Product - SwiftPOS{% endblock %}

{% block topbar_title %}
Edit Product
{% endblock %}

{% block content %}
<div class="container-fluid px-0">

    <!-- Toast Messages -->
    <div class="swiftpos-toast-container">
        {% if messages %}
            {% for message in messages %}
                <div class="swiftpos-toast swiftpos-toast-{{ message.tags }}">
                    <div class="toast-icon">
                        {% if message.tags == 'success' %}
                            <i class="fas fa-check-circle"></i>
                        {% elif message.tags == 'error' or message.tags == 'danger' %}
                            <i class="fas fa-exclamation-circle"></i>
                        {% elif message.tags == 'warning' %}
                            <i class="fas fa-exclamation-triangle"></i>
                        {% else %}
                            <i class="fas fa-info-circle"></i>
                        {% endif %}
                    </div>
                    <div class="toast-text">
                        {{ message }}
                    </div>
                </div>
            {% endfor %}
        {% endif %}
    </div>

    {# Non-field form errors #}
    {% if form.non_field_errors %}
        <div class="alert alert-danger mb-3">
            {{ form.non_field_errors }}
        </div>
    {% endif %}

    {# DEBUG: show raw errors so we can see *anything* that goes wrong #}
    {% if form.errors %}
        <div class="alert alert-danger mb-3">
            <strong>Form errors:</strong>
            {{ form.errors }}
        </div>
    {% endif %}

    <div class="d-flex flex-wrap justify-content-between align-items-center mb-3 gap-2">
        <div>
            <h5 class="mb-0">Edit Product</h5>
            <div class="text-muted small">
                Updating: {{ product.name }}
            </div>
        </div>
        <div class="d-flex flex-wrap gap-2">
            <a href="{% url 'inventory:product_detail' product.id %}"
               class="btn btn-outline-secondary btn-sm d-flex align-items-center gap-2">
                <i class="fas fa-arrow-left"></i> Back to product
            </a>
            <a href="{% url 'inventory:product_list' %}"
               class="btn btn-outline-secondary btn-sm">
                Products
            </a>
        </div>
    </div>

    <div class="card">
        <div class="card-body">
            <form method="post" enctype="multipart/form-data">
                {% csrf_token %}

                <div class="row g-3">
                    {# Render every field from ProductForm so all required fields are visible #}
                    {% for field in form %}
                        <div class="col-md-6">
                            <label for="{{ field.id_for_label }}"
                                   class="form-label small fw-semibold">
                                {{ field.label }}
                                {% if field.field.required %}
                                    <span class="text-danger">*</span>
                                {% endif %}
                            </label>
                            {{ field }}
                            {% if field.help_text %}
                                <div class="form-text small text-muted">
                                    {{ field.help_text }}
                                </div>
                            {% endif %}
                            {% for error in field.errors %}
                                <div class="text-danger small">
                                    {{ error }}
                                </div>
                            {% endfor %}
                        </div>
                    {% endfor %}
                </div>

                {# Optional current image preview #}
                {% if product.image %}
                    <div class="mt-3 row">
                        <div class="col-md-6">
                            <div class="small text-muted mb-1">Current image:</div>
                            <div class="ratio ratio-4x3">
                                <img src="{{ product.image.url }}"
                                     class="img-fluid rounded"
                                     style="object-fit: cover;"
                                     alt="{{ product.name }}">
                            </div>
                        </div>
                    </div>
                {% endif %}

                <div class="mt-4 d-flex justify-content-between align-items-center flex-wrap gap-2">
                    <span class="small text-muted">
                        Check prices and stock before saving changes.
                    </span>
                    <div class="d-flex gap-2">
                        <a href="{% url 'inventory:product_detail' product.id %}"
                           class="btn btn-outline-secondary btn-sm">
                            Cancel
                        </a>
                        <button type="submit"
                                class="btn btn-primary btn-sm d-flex align-items-center gap-2">
                            <i class="fas fa-save"></i> Save changes
                        </button>
                    </div>
                </div>

            </form>
        </div>
    </div>
</div>
{% endblock %}