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

{% block title %}Purchase Order #{{ order.id }} - SwiftPOS{% endblock %}
{% block topbar_title %}Purchase Order #{{ order.id }}{% endblock %}

{% block content %}
<div class="container-fluid px-0">
    <div class="row g-3 mb-3">
        <div class="col-lg-4">
            <div class="card h-100 shadow-sm">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-start mb-2">
                        <div>
                            <div class="small text-muted text-uppercase mb-1">Supplier</div>
                            <div class="fw-semibold">{{ order.supplier.name }}</div>
                            {% if order.supplier.phone %}
                                <div class="small text-muted">{{ order.supplier.phone }}</div>
                            {% endif %}
                            {% if order.supplier.address %}
                                <div class="small text-muted">{{ order.supplier.address }}</div>
                            {% endif %}
                        </div>
                        <span class="badge
                            {% if order.status == 'received' %} bg-success-subtle text-success
                            {% elif order.status == 'ordered' %} bg-primary-subtle text-primary
                            {% elif order.status == 'draft' %} bg-secondary-subtle text-secondary
                            {% else %} bg-danger-subtle text-danger
                            {% endif %}">
                            {{ order.get_status_display }}
                        </span>
                    </div>

                    <hr class="my-2">

                    <div class="small text-muted mb-1">Reference</div>
                    <div class="mb-2">{{ order.reference_number|default:"-" }}</div>

                    <div class="row g-2 small">
                        <div class="col-6">
                            <div class="text-muted">Date Ordered</div>
                            <div>{{ order.date_ordered|date:"Y-m-d"|default:"-" }}</div>
                        </div>
                        <div class="col-6">
                            <div class="text-muted">Date Received</div>
                            <div>{{ order.date_received|date:"Y-m-d"|default:"-" }}</div>
                        </div>
                    </div>

                    <hr class="my-2">

                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="small text-muted mb-1">Total Cost</div>
                            <div class="h5 mb-0">{% money order.total_cost %}</div>
                        </div>
                        {% if not order.is_received %}
                            <a href="{% url 'inventory:purchase_order_mark_received' order.id %}"
                               class="btn btn-success btn-sm">
                                <i class="fas fa-boxes-packing me-1"></i> Mark as Received
                            </a>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>

        <div class="col-lg-8">
            <div class="card h-100 shadow-sm">
                <div class="card-header">
                    <h6 class="card-title mb-0">Order Items</h6>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-sm align-middle mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>Product</th>
                                    <th class="text-end">Qty</th>
                                    <th class="text-end">Unit Cost ({{ currency_symbol }})</th>
                                    <th class="text-end">Line Total ({{ currency_symbol }})</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for item in order.items.all %}
                                    <tr>
                                        <td>
                                            <div class="fw-semibold small">{{ item.product.name }}</div>
                                            {% if item.product.sku %}
                                                <div class="text-muted small">{{ item.product.sku }}</div>
                                            {% endif %}
                                        </td>
                                        <td class="text-end">{{ item.quantity }}</td>
                                        <td class="text-end">{% money item.unit_cost %}</td>
                                        <td class="text-end">{% money item.line_total %}</td>
                                    </tr>
                                {% empty %}
                                    <tr>
                                        <td colspan="4" class="text-center text-muted py-3">
                                            No items on this purchase order.
                                        </td>
                                    </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
