{% extends "base.html" %}

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

{% block content %}
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>Purchase Orders</h2>
        <a href="{% url 'create_purchase_order' %}" class="btn btn-success">
            <i class="bi bi-plus-circle"></i> Add New PO
        </a>
    </div>

    <div class="card mb-4">
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <div class="input-group mb-3">
                        <input type="text" class="form-control table-search" placeholder="Search..." data-table="poTable">
                    </div>
                </div>
                <div class="col-md-3">
                    <select class="form-select" id="statusFilter">
                        <option value="">All Statuses</option>
                        {% for value, label in status_choices %}
                            <option value="{{ value }}">{{ label }}</option>
                        {% endfor %}
                    </select>
                </div>
            </div>
        </div>
    </div>

    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover" id="poTable">
                    <thead class="table-light">
                        <tr>
                            <th>PO Number</th>
                            <th>Supplier</th>
                            <th>Company</th>
                            <th>Project</th>
                            
                            <th>Status</th>
                            <th>Order Date</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for order in purchase_orders %}
                        <tr>
                            <td>
                                <a href="{% url 'purchase_order_detail' order.id %}" class="text-primary">
                                    {{ order.po_number }}
                                </a>
                            </td>
                            <td>{{ order.supplier.name }}</td>
                            <td>{{ order.company_id }}</td>
                            <td>
                                {% if order.project %}
                                    {{ order.project.project_title }}
                                {% else %}
                                    <span class="text-muted">No Project Assigned</span>
                                {% endif %}
                            </td>
                           
                            <td>
                                <span class="badge 
                                    {% if order.status == 'draft' %}bg-secondary
                                    {% elif order.status == 'completed' %}badge bg-success
                                    {% elif order.status == 'approved' %}badge bg-success
                                    {% elif order.status == 'cancelled' %}bg-danger
                                    {% else %}bg-primary{% endif %}">
                                    {{ order.get_status_display }}
                                </span>
                            </td>
                            <td>{{ order.order_date|date:"M d, Y" }}</td>
                            <td>
                                {% if order.status == 'completed' %}
                                <a href="{% url 'purchase_order_detail' order.id %}" class="btn btn-sm btn-primary">
                                    <i class="bi bi-eye"></i> View
                                </a>
                                <a href="{% url 'purchase_return_view' order.id %}" class="btn btn-sm btn-primary">
                                    <i class="bi bi-eye"></i> Return
                                </a>
                                {% else %}
                                <a href="{% url 'purchase_order_edit' order.id %}" class="btn btn-sm btn-primary">
                                    <i class="bi bi-pencil"></i> View / Edit
                                </a>
                            {% endif %}
                                
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="6" class="text-center py-4">
                                <div class="text-muted">No purchase orders found.</div>
                                <a href="{% url 'create_purchase_order' %}" class="btn btn-primary mt-2">
                                    Create Your First Purchase Order
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            
            {% if is_paginated %}
            <nav aria-label="Page navigation">
                <ul class="pagination justify-content-center">
                    {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page=1" aria-label="First">
                            <span aria-hidden="true">&laquo;&laquo;</span>
                        </a>
                    </li>
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    {% endif %}
                    
                    {% for num in page_obj.paginator.page_range %}
                    <li class="page-item {% if page_obj.number == num %}active{% endif %}">
                        <a class="page-link" href="?page={{ num }}">{{ num }}</a>
                    </li>
                    {% endfor %}
                    
                    {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.next_page_number }}" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}" aria-label="Last">
                            <span aria-hidden="true">&raquo;&raquo;</span>
                        </a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}
        </div>
    </div>
</div>

<!-- Bootstrap Icons -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.0/font/bootstrap-icons.css">

<!-- JavaScript for enhanced functionality -->


<style>
    .badge {
        font-size: 0.85em;
        padding: 0.35em 0.65em;
    }
    .table-hover tbody tr:hover {
        background-color: rgba(0, 0, 0, 0.02);
    }
</style>
{% endblock %}