{% extends 'base.html' %}

{% block title %}Purchase Orders{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h1 class="h3 mb-0">
        <i class="bi bi-cart-plus me-2"></i>
        Purchase Orders
    </h1>
    <a href="#" class="btn btn-primary">
        <i class="bi bi-plus-circle me-2"></i>
        New Order
    </a>
</div>

<div class="card">
    <div class="card-body">
        {% if purchase_orders %}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead class="table-light">
                        <tr>
                            <th>Order Number</th>
                            <th>Date</th>
                            <th>Supplier</th>
                            <th>Status</th>
                            <th>Subtotal</th>
                            <th>Total Amount</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for order in purchase_orders %}
                        <tr>
                            <td>
                                <strong>{{ order.order_number }}</strong>
                            </td>
                            <td>{{ order.date|date:"d/m/Y" }}</td>
                            <td>{{ order.supplier.name }}</td>
                            <td>
                                <span class="badge bg-{% if order.status == 'received' %}success{% elif order.status == 'confirmed' %}info{% elif order.status == 'sent' %}warning{% else %}secondary{% endif %}">
                                    {{ order.get_status_display }}
                                </span>
                            </td>
                            <td>Rp {{ order.subtotal|floatformat:0 }}</td>
                            <td>Rp {{ order.total_amount|floatformat:0 }}</td>
                            <td>
                                <a href="{% url 'purchase:purchase_detail' order.pk %}" class="btn btn-sm btn-outline-primary">
                                    <i class="bi bi-eye"></i>
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            
            {% if is_paginated %}
            <nav aria-label="Page navigation" class="mt-4">
                <ul class="pagination justify-content-center">
                    {% if page_obj.has_previous %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a>
                        </li>
                    {% endif %}
                    
                    <li class="page-item active">
                        <span class="page-link">Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}</span>
                    </li>
                    
                    {% if page_obj.has_next %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a>
                        </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}
        {% else %}
            <div class="text-center py-5">
                <i class="bi bi-cart-plus display-1 text-muted"></i>
                <h4 class="mt-3 text-muted">No Purchase Orders</h4>
                <p class="text-muted">Start by creating your first purchase order.</p>
                <a href="#" class="btn btn-primary">
                    <i class="bi bi-plus-circle me-2"></i>
                    Create Order
                </a>
            </div>
        {% endif %}
    </div>
</div>
{% endblock %}
