{% extends 'base.html' %}
{% load i18n %}

{% block title %}{% trans "Local Purchase Orders (LPO)" %} | {{ site_settings.business_name }}{% endblock %}

{% block content %}
<div class="container-fluid px-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="fw-bold mb-0">{% trans "Purchase Orders (LPO)" %}</h2>
            <p class="text-muted small mb-0">{% trans "Manage draft orders to suppliers" %}</p>
        </div>
        <a href="{% url 'lpo_create' %}" class="btn btn-primary rounded-3 px-4 shadow-sm">
            <i class="bi bi-plus-circle me-2"></i>{% trans "Create LPO" %}
        </a>
    </div>

    {% if messages %}
    <div class="mb-4">
        {% for message in messages %}
        <div class="alert alert-{{ message.tags }} alert-dismissible fade show rounded-3 shadow-sm border-0" role="alert">
            <i class="bi bi-info-circle me-2"></i>{{ message }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        {% endfor %}
    </div>
    {% endif %}

    <div class="card border-0 shadow-sm rounded-4">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4">{% trans "LPO #" %}</th>
                            <th>{% trans "Date" %}</th>
                            <th>{% trans "Supplier" %}</th>
                            <th>{% trans "Total" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Created By" %}</th>
                            <th class="text-end pe-4">{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for order in orders %}
                        <tr>
                            <td class="ps-4 fw-bold">
                                {{ order.lpo_number|default:order.id }}
                            </td>
                            <td>{{ order.issue_date|date:"Y-m-d" }}</td>
                            <td>{{ order.supplier.name|default:"-" }}</td>
                            <td class="fw-bold text-dark">{{ site_settings.currency_symbol }}{{ order.total_amount|floatformat:3 }}</td>
                            <td>
                                {% if order.status == 'converted' %}
                                    <span class="badge bg-success-subtle text-success rounded-pill px-3">{% trans "Converted" %}</span>
                                {% elif order.status == 'sent' %}
                                    <span class="badge bg-info-subtle text-info rounded-pill px-3">{% trans "Sent" %}</span>
                                {% else %}
                                    <span class="badge bg-secondary-subtle text-secondary rounded-pill px-3">{% trans "Draft" %}</span>
                                {% endif %}
                            </td>
                            <td>
                                <span class="text-muted small">
                                    <i class="bi bi-person me-1"></i>{{ order.created_by.username|default:"System" }}
                                </span>
                            </td>
                            <td class="text-end pe-4">
                                <div class="btn-group shadow-sm rounded-3">
                                    <a href="{% url 'lpo_detail' order.id %}" class="btn btn-sm btn-white border" title="{% trans 'View & Print' %}">
                                        <i class="bi bi-printer"></i>
                                    </a>
                                    {% if order.status != 'converted' %}
                                    <form action="{% url 'convert_lpo_to_purchase' order.id %}" method="POST" style="display: inline;" onsubmit="return confirm('{% trans 'Convert this LPO to a Purchase Invoice? This will update stock.' %}');">
                                        {% csrf_token %}
                                        <button type="submit" class="btn btn-sm btn-white border text-success" title="{% trans 'Convert to Invoice' %}">
                                            <i class="bi bi-check2-circle"></i>
                                        </button>
                                    </form>
                                    <button type="button" class="btn btn-sm btn-white border text-danger" data-bs-toggle="modal" data-bs-target="#deleteModal{{ order.id }}" title="{% trans 'Delete' %}">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                    {% endif %}
                                </div>

                                <!-- Delete Modal -->
                                <div class="modal fade text-start" id="deleteModal{{ order.id }}" tabindex="-1">
                                    <div class="modal-dialog">
                                        <div class="modal-content border-0 shadow rounded-4">
                                            <div class="modal-body p-4 text-center">
                                                <div class="text-danger mb-3">
                                                    <i class="bi bi-exclamation-octagon" style="font-size: 3rem;"></i>
                                                </div>
                                                <h4 class="fw-bold">{% trans "Delete LPO?" %}</h4>
                                                <p class="text-muted">{% trans "This action cannot be undone." %}</p>
                                                <div class="d-grid gap-2">
                                                    <a href="{% url 'lpo_delete' order.id %}" class="btn btn-danger rounded-3 py-2">{% trans "Yes, Delete" %}</a>
                                                    <button type="button" class="btn btn-light rounded-3 py-2" data-bs-dismiss="modal">{% trans "Cancel" %}</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="7" class="text-center py-5">
                                <img src="https://illustrations.popsy.co/gray/document.svg" alt="Empty" style="width: 200px;" class="mb-3">
                                <p class="text-muted">{% trans "No Purchase Orders found." %}</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
             <!-- Pagination if needed -->
             {% if orders.has_other_pages %}
                 {% include "core/pagination.html" with page_obj=orders %}
             {% endif %}
        </div>
    </div>
</div>
{% endblock %}