{% extends 'base.html' %}
{% load i18n %}

{% block title %}{% trans "Customer Receipts" %} | {{ site_settings.business_name }}{% endblock %}

{% block content %}
<div class="container-fluid px-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="fw-bold mb-0">{% trans "Customer Receipts" %}</h2>
            <p class="text-muted small mb-0">{% trans "History of payments received from customers" %}</p>
        </div>
        <div class="d-flex gap-2">
            <a href="{% url 'invoices' %}" class="btn btn-outline-primary rounded-3 px-4 shadow-sm">
                <i class="bi bi-file-earmark-text me-2"></i>{% trans "Sales Invoices" %}
            </a>
            <a href="{% url 'pos' %}" class="btn btn-primary rounded-3 px-4 shadow-sm">
                <i class="bi bi-shop me-2"></i>{% trans "POS System" %}
            </a>
        </div>
    </div>

    <!-- Filters -->
    <div class="card border-0 shadow-sm rounded-4 mb-4">
        <div class="card-body p-3">
            <form method="get" class="row g-3">
                <div class="col-md-3">
                    <label class="form-label small fw-bold text-muted">{% trans "Customer" %}</label>
                    <select name="customer" class="form-select border-0 bg-light">
                        <option value="">{% trans "All Customers" %}</option>
                        {% for c in customers %}
                        <option value="{{ c.id }}" {% if customer_id == c.id|stringformat:"s" %}selected{% endif %}>{{ c.name }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label small fw-bold text-muted">{% trans "From" %}</label>
                    <input type="date" name="start_date" class="form-control border-0 bg-light" value="{{ start_date }}">
                </div>
                <div class="col-md-3">
                    <label class="form-label small fw-bold text-muted">{% trans "To" %}</label>
                    <input type="date" name="end_date" class="form-control border-0 bg-light" value="{{ end_date }}">
                </div>
                <div class="col-md-3 d-flex align-items-end gap-2">
                    <button type="submit" class="btn btn-dark w-100 rounded-3">{% trans "Filter" %}</button>
                    <a href="{% url 'customer_payments' %}" class="btn btn-light w-100 rounded-3 border">{% trans "Clear" %}</a>
                </div>
            </form>
        </div>
    </div>

    <div class="card border-0 shadow-sm rounded-4">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4">{% trans "Date" %}</th>
                            <th>{% trans "Invoice #" %}</th>
                            <th>{% trans "Customer" %}</th>
                            <th>{% trans "Amount" %}</th>
                            <th>{% trans "Method" %}</th>
                            <th>{% trans "User" %}</th>
                            <th>{% trans "Receipt" %}</th>
                            <th class="pe-4">{% trans "Notes" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for payment in payments %}
                        <tr>
                            <td class="ps-4">{{ payment.payment_date|date:"Y-m-d" }}</td>
                            <td>
                                <a href="{% url 'invoice_detail' payment.sale.id %}" class="fw-bold text-decoration-none">
                                    {{ payment.sale.invoice_number|default:payment.sale.id }}
                                </a>
                            </td>
                            <td>{{ payment.sale.customer.name|default:"Guest" }}</td>
                            <td class="fw-bold text-dark">{{ site_settings.currency_symbol }}{{ payment.amount|floatformat:3 }}</td>
                            <td>
                                <span class="badge bg-light text-dark border rounded-pill px-3">
                                    {{ payment.payment_method_name }}
                                </span>
                            </td>
                            <td>
                                <span class="text-muted small">
                                    <i class="bi bi-person me-1"></i>{{ payment.created_by.username|default:"System" }}
                                </span>
                            </td>
                            <td>
                                <a href="{% url 'customer_payment_receipt' payment.id %}" target="_blank" class="btn btn-sm btn-outline-info rounded-pill">
                                    <i class="bi bi-printer me-1"></i>{% trans "Print" %}
                                </a>
                            </td>
                            <td class="pe-4">
                                <span class="small text-muted">{{ payment.notes|default:"-" }}</span>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="8" class="text-center py-5">
                                <img src="https://illustrations.popsy.co/gray/payments.svg" alt="Empty" style="width: 200px;" class="mb-3">
                                <p class="text-muted">{% trans "No customer receipts found." %}</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% include "core/pagination.html" with page_obj=payments %}
        </div>
    </div>
</div>
{% endblock %}
