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

{% block title %}Suppliers - SwiftPOS{% endblock %}
{% block topbar_title %}Suppliers{% endblock %}

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

    <!-- Header Action Bar -->
    <div class="d-flex flex-column flex-md-row justify-content-between align-items-start align-items-md-center mb-4 gap-3">
        <div>
            <h4 class="mb-1 fw-bold text-dark"><i class="fas fa-truck-loading me-2 text-primary"></i>Vendor Management</h4>
            <div class="text-muted small">Manage suppliers, purchase orders, and track balances</div>
        </div>
        <a href="{% url 'inventory:supplier_create' %}" class="btn btn-primary rounded-pill px-4 shadow-sm fw-medium d-flex align-items-center gap-2">
            <i class="fas fa-plus"></i> Add New Supplier
        </a>
    </div>

    <!-- Summary Strip -->
    {% if suppliers %}
    <div class="row g-3 mb-4">
        <div class="col-12 col-md-4 col-xl-3">
            <div class="card border-0 shadow-sm" style="border-radius: 16px; background: linear-gradient(135deg, #ffffff, #f8f9fa);">
                <div class="card-body p-4 d-flex align-items-center gap-3">
                    <div class="bg-primary bg-opacity-10 text-primary rounded-circle d-flex align-items-center justify-content-center" style="width: 48px; height: 48px;">
                        <i class="fas fa-building fs-5"></i>
                    </div>
                    <div>
                        <div class="fs-4 fw-bold text-dark line-height-1">{{ suppliers|length }}</div>
                        <div class="small fw-semibold text-muted text-uppercase" style="letter-spacing: 0.5px; font-size: 0.7rem;">Active Suppliers</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    {% endif %}

    <!-- Suppliers Grid -->
    <div class="row g-4">
        {% for s in suppliers %}
        <div class="col-12 col-md-6 col-xl-4 sp-animate-card">
            <div class="card h-100 border-0 shadow-sm hover-lift" style="border-radius: 16px; transition: all 0.2s ease;">
                <div class="card-body p-4">
                    <div class="d-flex justify-content-between align-items-start mb-4">
                        <div>
                            <h5 class="fw-bold mb-1 text-dark">{{ s.name }}</h5>
                            <div class="d-flex flex-column gap-1 mt-2">
                                {% if s.contact_name %}
                                <div class="small text-muted"><i class="fas fa-user text-primary opacity-75 me-2" style="width:14px;"></i>{{ s.contact_name }}</div>
                                {% endif %}
                                {% if s.phone %}
                                <div class="small text-muted"><i class="fas fa-phone text-success opacity-75 me-2" style="width:14px;"></i>{{ s.phone }}</div>
                                {% endif %}
                                {% if not s.contact_name and not s.phone %}
                                <div class="small text-muted fst-italic">No contact info</div>
                                {% endif %}
                            </div>
                        </div>
                        <a href="{% url 'inventory:supplier_detail' s.id %}" class="btn btn-light rounded-circle shadow-sm d-flex align-items-center justify-content-center text-primary" style="width: 40px; height: 40px;">
                            <i class="fas fa-arrow-right"></i>
                        </a>
                    </div>
                    
                    <div class="bg-light rounded-3 p-3 mb-2">
                        <div class="row g-2">
                            <div class="col-6">
                                <div class="small text-muted fw-bold text-uppercase" style="font-size:0.65rem;">Purchased</div>
                                <div class="fw-bold text-dark mt-1">{% money s.total_purchased %}</div>
                            </div>
                            <div class="col-6 border-start border-light ps-3">
                                <div class="small text-muted fw-bold text-uppercase" style="font-size:0.65rem;">Paid</div>
                                <div class="fw-bold text-success mt-1">{% money s.total_paid %}</div>
                            </div>
                        </div>
                    </div>

                    <div class="mt-3">
                        <div class="d-flex justify-content-between mb-1">
                            <div class="small text-muted fw-bold">Outstanding Balance</div>
                            <div class="small fw-bold {% if s.balance > 0 %}text-danger{% else %}text-success{% endif %}">
                                {% money s.balance %}
                            </div>
                        </div>
                        {% if s.balance > 0 %}
                        <div class="progress rounded-pill bg-danger bg-opacity-10" style="height: 6px;">
                            {% with pct=s.total_paid|default:0 %}
                            <div class="progress-bar bg-success rounded-pill" style="width:{% if s.total_purchased > 0 %}{{ s.total_paid|floatformat:0 }}{% else %}0{% endif %}%;"></div>
                            {% endwith %}
                        </div>
                        {% else %}
                        <div class="progress rounded-pill bg-light" style="height: 6px;">
                            <div class="progress-bar bg-success rounded-pill" style="width:100%;"></div>
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
        {% empty %}
        <div class="col-12">
            <div class="card border-0 shadow-sm" style="border-radius: 16px;">
                <div class="card-body text-center py-5 text-muted">
                    <div class="bg-light rounded-circle d-inline-flex align-items-center justify-content-center mb-3" style="width: 80px; height: 80px;">
                        <i class="fas fa-store-slash fa-2x text-muted opacity-50"></i>
                    </div>
                    <h5 class="fw-bold text-dark mb-2">No Suppliers Found</h5>
                    <p class="small text-muted mx-auto" style="max-width: 300px;">Add your first supplier to begin creating purchase orders and tracking inventory restocks.</p>
                    <a href="{% url 'inventory:supplier_create' %}" class="btn btn-primary rounded-pill px-4 mt-2">
                        <i class="fas fa-plus me-1"></i> Add Supplier
                    </a>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>

</div>

<style>
.hover-lift { transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); }
.hover-lift:hover { 
    transform: translateY(-4px); 
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1) !important;
}
.line-height-1 { line-height: 1; }
</style>
{% endblock %}
