{% extends 'core/base.html' %}
{% load static %}

{% block title %}Credit Customers - SwiftPOS{% endblock %}
{% block topbar_title %}Credit Customers{% endblock %}

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

    <!-- Header -->
    <div class="d-flex justify-content-between align-items-center mb-3">
        <div>
            <h5 class="mb-0">Customers With Outstanding Balance</h5>
            <div class="small text-muted">View customers owing and clear credit payments.</div>
        </div>
    </div>

    <!-- Search bar -->
    <form method="get" class="mb-3">
        <div class="row g-2">
            <div class="col-sm-4 col-12">
                <input type="text" name="q" class="form-control form-control-sm"
                       placeholder="Search by name or phone..."
                       value="{{ request.GET.q }}">
            </div>
            <div class="col-sm-2 col-12">
                <button class="btn btn-primary btn-sm w-100">
                    <i class="fas fa-search me-1"></i> Search
                </button>
            </div>
        </div>
    </form>

    <!-- Credit table -->
    <div class="card shadow-sm">
        <div class="card-body p-0">

            <div class="sp-table-container">
                <table class="table table-sm align-middle">
                    <table>
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Customer</th>
                            <th>Phone</th>
                            <th>Total</th>
                            <th>Paid</th>
                            <th>Balance</th>
                            <th>Date</th>
                            <th class="text-end">Actions</th>
                        </tr>
                    </thead>

                    <tbody>
                        {% for order in credit_orders %}
                        <tr>
                            <td><a href="{% url 'sales:print_receipt' order.id %}"> {{ order.order_number }}</a></td>

                            <td>{{ order.customer_name|default:"-" }}</td>

                            <td>{{ order.customer_phone|default:"-" }}</td>

                            <td>{{ currency_symbol }}{{ order.final_amount|floatformat:2 }}</td>

                            <td>{{ currency_symbol }}{{ order.amount_paid|floatformat:2 }}</td>

                            <td class="fw-small text-danger fs-16">{{ currency_symbol }}{{ order.balance_amount|floatformat:2 }}</td>

                            <td>{{ order.created_at|date:"Y-m-d H:i" }}</td>

                            <td class="text-end">
                                <a href="{% url 'sales:clear_credit' order.id %}"
                                   class="btn btn-sm btn-primary">
                                    Clear Credit
                                </a>
                            </td>
                        </tr>

                        {% empty %}
                        <tr>
                            <td colspan="8" class="text-center text-muted py-3">
                                No customers with outstanding balance.
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>

                </table>
            </div>

        </div>
    </div>

</div>
{% endblock %}
