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

{% block title %}{% if mode == 'create' %}New Purchase Order{% else %}Edit Purchase Order{% endif %} - SwiftPOS{% endblock %}
{% block topbar_title %}Purchase Order{% endblock %}

{% block content %}
<div class="container-fluid px-0">
    <div class="card shadow-sm">
        <div class="card-body">
            <form method="post" novalidate>
                {% csrf_token %}
                <div class="row g-3 mb-3">
                    <div class="col-md-4">
                        <label class="form-label small fw-semibold">Supplier</label>
                        {{ form.supplier }}
                        {% for error in form.supplier.errors %}
                            <div class="text-danger small">{{ error }}</div>
                        {% endfor %}
                    </div>
                    <div class="col-md-4">
                        <label class="form-label small fw-semibold">Reference No.</label>
                        {{ form.reference_number }}
                        <div class="form-text small">Optional internal reference (e.g. PO-00012).</div>
                        {% for error in form.reference_number.errors %}
                            <div class="text-danger small">{{ error }}</div>
                        {% endfor %}
                    </div>
                    <div class="col-md-4">
                        <label class="form-label small fw-semibold">Status</label>
                        {{ form.status }}
                        {% for error in form.status.errors %}
                            <div class="text-danger small">{{ error }}</div>
                        {% endfor %}
                    </div>

                    <div class="col-md-4">
                        <label class="form-label small fw-semibold">Date Ordered</label>
                        {{ form.date_ordered }}
                        {% for error in form.date_ordered.errors %}
                            <div class="text-danger small">{{ error }}</div>
                        {% endfor %}
                    </div>
                    <div class="col-md-4">
                        <label class="form-label small fw-semibold">Date Received</label>
                        {{ form.date_received }}
                        <div class="form-text small">Fill when goods have arrived.</div>
                        {% for error in form.date_received.errors %}
                            <div class="text-danger small">{{ error }}</div>
                        {% endfor %}
                    </div>
                    <div class="col-md-12">
                        <label class="form-label small fw-semibold">Notes</label>
                        {{ form.notes }}
                        {% for error in form.notes.errors %}
                            <div class="text-danger small">{{ error }}</div>
                        {% endfor %}
                    </div>
                </div>

                <hr class="my-3">

                <h6 class="mb-2">Items</h6>
                <div class="small text-muted mb-2">
                    Add the products and quantities you are purchasing.
                </div>

                {{ formset.management_form }}
                <div class="sp-table-container">
                    <table class="table table-sm align-middle mb-0">
                        <thead class="table-light">
                            <tr>
                                <th style="width:40%;">Product</th>
                                <th style="width:20%;">Quantity</th>
                                <th style="width:20%;">Unit Cost ({{ currency_symbol }})</th>
                                <th style="width:20%;">Line Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for item_form in formset %}
                                <tr>
                                    <td>
                                        {{ item_form.product }}
                                        {% for error in item_form.product.errors %}
                                            <div class="text-danger small">{{ error }}</div>
                                        {% endfor %}
                                    </td>
                                    <td>
                                        {{ item_form.quantity }}
                                        {% for error in item_form.quantity.errors %}
                                            <div class="text-danger small">{{ error }}</div>
                                        {% endfor %}
                                    </td>
                                    <td>
                                        {{ item_form.unit_cost }}
                                        {% for error in item_form.unit_cost.errors %}
                                            <div class="text-danger small">{{ error }}</div>
                                        {% endfor %}
                                    </td>
                                    <td class="text-muted small">
                                        Auto-calculated after save
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>

                <div class="d-flex justify-content-between align-items-center">
                    <a href="{% url 'inventory:purchase_order_list' %}" class="btn btn-outline-secondary btn-sm">
                        Cancel
                    </a>
                    <button type="submit" class="btn btn-primary btn-sm">
                        <i class="fas fa-save me-1"></i> Save Purchase Order
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}
