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

{% block title %}{% trans "Sales Returns" %} | {{ 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 "Sales Returns" %}</h2>
            <p class="text-muted small mb-0">{% trans "Manage customer product returns" %}</p>
        </div>
        <a href="{% url 'sale_return_create' %}" class="btn btn-primary rounded-3 px-4 shadow-sm">
            <i class="bi bi-plus-circle me-2"></i>{% trans "New Sales Return" %}
        </a>
    </div>

    {% if messages %}
    <div class="mb-4">
        {% for message in messages %}
        <div class="alert alert-{{ message.tags }} alert-dismissible fade show rounded-3 shadow-sm border-0" role="alert">
            <i class="bi bi-info-circle me-2"></i>{{ message }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        {% endfor %}
    </div>
    {% endif %}

    <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 "Return #" %}</th>
                            <th>{% trans "Date" %}</th>
                            <th>{% trans "Customer" %}</th>
                            <th>{% trans "Total Amount" %}</th>
                            <th>{% trans "User" %}</th>
                            <th class="text-end pe-4">{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for return in returns %}
                        <tr>
                            <td class="ps-4 fw-bold">
                                {{ return.return_number|default:return.id }}
                            </td>
                            <td>{{ return.created_at|date:"Y-m-d" }}</td>
                            <td>{{ return.customer.name|default:_("Guest") }}</td>
                            <td class="fw-bold text-dark">{{ site_settings.currency_symbol }}{{ return.total_amount|floatformat:3 }}</td>
                            <td>
                                <span class="text-muted small">
                                    <i class="bi bi-person me-1"></i>{{ return.created_by.username|default:"System" }}
                                </span>
                            </td>
                            <td class="text-end pe-4">
                                <div class="btn-group shadow-sm rounded-3">
                                    <a href="{% url 'sale_return_detail' return.id %}" class="btn btn-sm btn-white border" title="{% trans 'View & Print' %}">
                                        <i class="bi bi-printer"></i>
                                    </a>
                                    <button type="button" class="btn btn-sm btn-white border text-danger" data-bs-toggle="modal" data-bs-target="#deleteModal{{ return.id }}">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </div>

                                <!-- Delete Modal -->
                                <div class="modal fade text-start" id="deleteModal{{ return.id }}" tabindex="-1">
                                    <div class="modal-dialog">
                                        <div class="modal-content border-0 shadow rounded-4">
                                            <div class="modal-body p-4 text-center">
                                                <div class="text-danger mb-3">
                                                    <i class="bi bi-exclamation-octagon" style="font-size: 3rem;"></i>
                                                </div>
                                                <h4 class="fw-bold">{% trans "Delete Sales Return?" %}</h4>
                                                <p class="text-muted">{% trans "This will deduct the quantities from stock again. This action cannot be undone." %}</p>
                                                <div class="d-grid gap-2">
                                                    <a href="{% url 'delete_sale_return' return.id %}" class="btn btn-danger rounded-3 py-2">{% trans "Yes, Delete" %}</a>
                                                    <button type="button" class="btn btn-light rounded-3 py-2" data-bs-dismiss="modal">{% trans "Cancel" %}</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="6" class="text-center py-5">
                                <img src="https://illustrations.popsy.co/gray/success.svg" alt="Empty" style="width: 200px;" class="mb-3">
                                <p class="text-muted">{% trans "No sales returns found." %}</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
                {% include "core/pagination.html" with page_obj=returns %}
        </div>
    </div>
</div>
{% endblock %}