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

{% block title %}Purchase Orders - SwiftPOS{% endblock %}
{% block topbar_title %}Purchase Orders{% endblock %}

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

    <!-- Page Header -->
    <div class="sp-page-header">
        <div>
            <h1 class="sp-page-title"><i class="fas fa-file-invoice"></i>Purchase Orders</h1>
            <p class="sp-page-subtitle">Manage stock arriving from suppliers — track ordered and received quantities.</p>
        </div>
        <div class="sp-page-actions">
            <a href="{% url 'inventory:purchase_order_create' %}" class="btn btn-primary btn-sm d-flex align-items-center gap-2">
                <i class="fas fa-plus"></i> New Purchase Order
            </a>
        </div>
    </div>

    <!-- Orders Table -->
    <div class="card">
        <div class="card-body p-0">
            <div class="sp-table-container">
                <table class="table table-hover align-middle mb-0">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Supplier</th>
                            <th>Reference</th>
                            <th>Status</th>
                            <th class="d-none d-md-table-cell">Ordered</th>
                            <th class="d-none d-md-table-cell">Received</th>
                            <th class="text-end">Total Cost ({{ currency_symbol }})</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for order in orders %}
                            <tr>
                                <td>
                                    <a href="{% url 'inventory:purchase_order_detail' order.id %}" class="fw-semibold text-decoration-none">
                                        #{{ order.id }}
                                    </a>
                                </td>
                                <td>{{ order.supplier.name }}</td>
                                <td class="text-muted small">{{ order.reference_number|default:"—" }}</td>
                                <td>
                                    <span class="sp-badge
                                        {% if order.status == 'received' %}sp-badge-success
                                        {% elif order.status == 'ordered' %}sp-badge-info
                                        {% elif order.status == 'draft' %}sp-badge-neutral
                                        {% else %}sp-badge-danger{% endif %}">
                                        {{ order.get_status_display }}
                                    </span>
                                </td>
                                <td class="d-none d-md-table-cell text-muted small">{{ order.date_ordered|date:"M d, Y" }}</td>
                                <td class="d-none d-md-table-cell text-muted small">{{ order.date_received|date:"M d, Y"|default:"—" }}</td>
                                <td class="text-end fw-semibold">{% money order.total_cost %}</td>
                            </tr>
                        {% empty %}
                            <tr>
                                <td colspan="7">
                                    <div class="sp-empty-state">
                                        <div class="sp-empty-state-icon">
                                            <i class="fas fa-file-invoice"></i>
                                        </div>
                                        <div class="sp-empty-state-title">No purchase orders yet</div>
                                        <p class="sp-empty-state-text">Click <strong>New Purchase Order</strong> to get started.</p>
                                    </div>
                                </td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

</div>
{% endblock %}
