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

{% block title %}Supplier - {{ supplier.name }}{% endblock %}
{% block topbar_title %}Supplier Details{% endblock %}

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

    <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 d-flex align-items-center gap-2">
                {{ supplier.name }}
                <a href="{% url 'inventory:supplier_edit' supplier.id %}" class="btn btn-sm btn-light rounded-pill border-0 shadow-sm" title="Edit Supplier">
                    <i class="fas fa-pencil-alt text-muted"></i>
                </a>
            </h4>
            <div class="text-muted small">
                {% if supplier.phone %}<i class="fas fa-phone me-1"></i> {{ supplier.phone }} &nbsp;|&nbsp; {% endif %}
                {% if supplier.email %}<i class="fas fa-envelope me-1"></i> {{ supplier.email }}{% endif %}
            </div>
        </div>

        <div class="d-flex flex-wrap gap-2">
            <a href="{% url 'inventory:supplier_record_payment' supplier.id %}" class="btn btn-success btn-sm rounded-pill px-3 shadow-sm">
                <i class="fas fa-money-bill-wave me-1"></i> Record Payment
            </a>
            <a href="{% url 'inventory:purchase_order_create' supplier.id %}" class="btn btn-primary btn-sm rounded-pill px-3 shadow-sm">
                <i class="fas fa-plus me-1"></i> Add Purchase Order
            </a>
            <a href="{% url 'inventory:supplier_list' %}" class="btn btn-outline-secondary btn-sm rounded-pill px-3">
                <i class="fas fa-arrow-left me-1"></i> Back
            </a>
        </div>
    </div>

    <div class="row g-4 mb-4">
        <!-- Balance Overview -->
        <div class="col-12 col-xl-4">
            <div class="card border-0 shadow-sm h-100" style="border-radius: 16px; background: linear-gradient(145deg, #ffffff, #f9fafb);">
                <div class="card-body p-4">
                    <h6 class="text-muted fw-bold mb-4 text-uppercase" style="font-size:0.75rem;"><i class="fas fa-wallet me-2"></i>Financial Overview</h6>
                    
                    <div class="d-flex justify-content-between align-items-center mb-3 pb-3 border-bottom border-light">
                        <div class="text-muted small">Total Purchased</div>
                        <div class="fw-bold fs-5 text-dark">{% money supplier.total_purchased %}</div>
                    </div>
                    <div class="d-flex justify-content-between align-items-center mb-3 pb-3 border-bottom border-light">
                        <div class="text-muted small">Total Paid</div>
                        <div class="fw-bold fs-5 text-success">{% money supplier.total_paid %}</div>
                    </div>
                    <div class="d-flex justify-content-between align-items-center bg-light p-3 rounded-3 mt-4">
                        <div class="text-muted fw-bold small">Outstanding Balance</div>
                        <div class="fw-bold fs-4 {% if supplier.balance > 0 %}text-danger{% else %}text-success{% endif %}">
                            {% money supplier.balance %}
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Supplier Info -->
        <div class="col-12 col-xl-8">
            <div class="card border-0 shadow-sm h-100" style="border-radius: 16px;">
                <div class="card-body p-4">
                    <h6 class="text-muted fw-bold mb-4 text-uppercase" style="font-size:0.75rem;"><i class="fas fa-store me-2"></i>Vendor Details</h6>
                    <div class="row g-4">
                        <div class="col-md-6">
                            <div class="small text-muted mb-1">Address</div>
                            <div class="fw-medium">{{ supplier.address|default:"<span class='text-muted fst-italic'>No address provided</span>"|safe|linebreaksbr }}</div>
                        </div>
                        <div class="col-md-6">
                            <div class="small text-muted mb-1">Internal Notes</div>
                            <div class="fw-medium">{{ supplier.notes|default:"<span class='text-muted fst-italic'>No notes available</span>"|safe|linebreaksbr }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Tables Row -->
    <div class="row g-4">
        <!-- Purchase Orders -->
        <div class="col-12 col-lg-8">
            <div class="card border-0 shadow-sm" style="border-radius: 16px;">
                <div class="card-header bg-white border-0 pt-4 px-4 pb-0 d-flex justify-content-between align-items-center">
                    <h6 class="mb-0 fw-bold"><i class="fas fa-boxes me-2 text-primary"></i>Purchase Orders</h6>
                </div>
                <div class="card-body p-0">
                    <div class="sp-table-container">
                        <table class="table table-hover align-middle mb-0 mt-3">
                            <thead class="table-light text-muted small">
                                <tr>
                                    <th class="ps-4">PO #</th>
                                    <th>Status</th>
                                    <th>Total Cost</th>
                                    <th>Date Ordered</th>
                                    <th class="text-end pe-4">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                            {% for po in purchase_orders %}
                                <tr>
                                    <td class="ps-4 fw-medium text-primary">
                                        <a href="{% url 'inventory:purchase_order_detail' po.id %}" class="text-decoration-none">
                                            {{ po.reference_number|default:po.id }}
                                        </a>
                                    </td>
                                    <td>
                                        <span class="badge {% if po.status == 'received' %}bg-success{% elif po.status == 'ordered' %}bg-primary{% else %}bg-secondary{% endif %} rounded-pill">
                                            {{ po.get_status_display }}
                                        </span>
                                    </td>
                                    <td class="fw-bold">{% money po.total_cost %}</td>
                                    <td class="text-muted small">{{ po.date_ordered|date:"M j, Y" }}</td>
                                    <td class="text-end pe-4">
                                        <a href="{% url 'inventory:purchase_order_detail' po.id %}" class="btn btn-light btn-sm rounded-pill text-primary fw-medium px-3">
                                            View
                                        </a>
                                    </td>
                                </tr>
                            {% empty %}
                                <tr><td colspan="5" class="text-center py-5 text-muted">No purchase orders found for this supplier.</td></tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- Payment History -->
        <div class="col-12 col-lg-4">
            <div class="card border-0 shadow-sm h-100" style="border-radius: 16px;">
                <div class="card-header bg-white border-0 pt-4 px-4 pb-0">
                    <h6 class="mb-0 fw-bold"><i class="fas fa-history me-2 text-success"></i>Payment Log</h6>
                </div>
                <div class="card-body p-0">
                    <div class="sp-table-container">
                        <table class="table align-middle mb-0">
                            <thead class="table-light text-muted small">
                                <tr>
                                    <th class="ps-4">Date</th>
                                    <th class="text-end pe-4">Amount</th>
                                </tr>
                            </thead>
                            <tbody>
                            {% for p in supplier.payments.all %}
                                <tr>
                                    <td class="ps-4">
                                        <div class="small fw-medium">{{ p.date|date:"M j, Y" }}</div>
                                        {% if p.notes %}<div class="text-muted" style="font-size:0.7rem;">{{ p.notes|truncatechars:20 }}</div>{% endif %}
                                    </td>
                                    <td class="text-end pe-4 fw-bold text-success">{% money p.amount %}</td>
                                </tr>
                            {% empty %}
                                <tr><td colspan="2" class="text-center py-5 text-muted small">No payment history available.</td></tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>
{% endblock %}
