{% extends 'aapayout/base.html' %}
{% load i18n %}
{% load humanize %}
{% load aapayout_filters %}

{% block details %}
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-md-9">
            <h2>{% translate "Payout List" %}</h2>
        </div>
        <div class="col-md-3 text-end">
            {% if perms.aapayout.create_fleet %}
                <a href="{% url 'aapayout:fleet_create' %}" class="btn btn-primary">
                    <i class="fas fa-plus"></i> {% translate "Create Payout" %}
                </a>
            {% endif %}
        </div>
    </div>

    {% if page_obj %}
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>{% translate "Payout Name" %}</th>
                                    <th>{% translate "Organizer" %}</th>
                                    <th>{% translate "Date" %}</th>
                                    <th>{% translate "Status" %}</th>
                                    <th>{% translate "Participants" %}</th>
                                    <th class="text-end">{% translate "Total Value" %}</th>
                                    <th>{% translate "Actions" %}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for fleet in page_obj %}
                                <tr>
                                    <td>
                                        <a href="{% url 'aapayout:fleet_detail' fleet.pk %}">
                                            <strong>{{ fleet.name }}</strong>
                                        </a>
                                    </td>
                                    <td>{{ fleet.fleet_commander.username }}</td>
                                    <td>{{ fleet.fleet_time|date:"Y-m-d H:i" }}</td>
                                    <td>
                                        {% if fleet.status == 'draft' %}
                                            <span class="badge bg-secondary">{{ fleet.get_status_display }}</span>
                                        {% elif fleet.status == 'active' %}
                                            <span class="badge bg-primary">{{ fleet.get_status_display }}</span>
                                        {% elif fleet.status == 'completed' %}
                                            <span class="badge bg-success">{{ fleet.get_status_display }}</span>
                                        {% elif fleet.status == 'paid' %}
                                            <span class="badge bg-info">{{ fleet.get_status_display }}</span>
                                        {% endif %}
                                    </td>
                                    <td>{{ fleet.get_participant_count }}</td>
                                    <td class="text-end">{{ fleet.get_total_loot_value|isk_format_full }}</td>
                                    <td>
                                        <div class="btn-group btn-group-sm">
                                            <a href="{% url 'aapayout:fleet_detail' fleet.pk %}" class="btn btn-info" title="{% translate 'View' %}">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            {% if fleet.fleet_commander == request.user or perms.aapayout.manage_all_fleets %}
                                                <a href="{% url 'aapayout:fleet_edit' fleet.pk %}" class="btn btn-warning" title="{% translate 'Edit' %}">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                            {% endif %}
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>

                    <!-- Pagination (if needed) -->
                    {% if page_obj.paginator.num_pages > 1 %}
                    <nav aria-label="Payout list pagination">
                        <ul class="pagination justify-content-center">
                            {% if page_obj.has_previous %}
                                <li class="page-item">
                                    <a class="page-link" href="?page=1">{% translate "First" %}</a>
                                </li>
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.previous_page_number }}">{% translate "Previous" %}</a>
                                </li>
                            {% endif %}

                            <li class="page-item active">
                                <span class="page-link">
                                    {% translate "Page" %} {{ page_obj.number }} {% translate "of" %} {{ page_obj.paginator.num_pages }}
                                </span>
                            </li>

                            {% if page_obj.has_next %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.next_page_number }}">{% translate "Next" %}</a>
                                </li>
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">{% translate "Last" %}</a>
                                </li>
                            {% endif %}
                        </ul>
                    </nav>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
    {% else %}
    <div class="row">
        <div class="col-12">
            <div class="alert alert-info">
                <h4>{% translate "No Payouts Found" %}</h4>
                <p>{% translate "There are no payouts in the system yet." %}</p>
                {% if perms.aapayout.create_fleet %}
                <p>
                    <a href="{% url 'aapayout:fleet_create' %}" class="btn btn-primary">
                        <i class="fas fa-plus"></i> {% translate "Create the First Payout" %}
                    </a>
                </p>
                {% endif %}
            </div>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}
