{% 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">

    <!-- Page Header -->
    <div class="sp-page-header">
        <div>
            <h1 class="sp-page-title"><i class="fas fa-truck-loading"></i>Vendor Management</h1>
            <p class="sp-page-subtitle">Manage suppliers, purchase orders, and track balances.</p>
        </div>
        <a href="{% url 'inventory:supplier_create' %}" class="btn btn-primary btn-sm 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="sp-stat-card">
                <div class="card-body d-flex align-items-center gap-3">
                    <div class="sp-stat-icon" style="background:rgba(59,130,246,.1);color:#3b82f6;">
                        <i class="fas fa-building"></i>
                    </div>
                    <div>
                        <div class="sp-stat-label">Active Suppliers</div>
                        <div class="sp-stat-value">{{ suppliers|length }}</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="sp-stat-card h-100 sp-hover-lift">
                <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>

{% endblock %}
