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

{% block details %}
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-md-8">
            <h2>{{ fleet.name }}</h2>
            <p class="text-muted">
                <i class="fas fa-user"></i> {{ fleet.fleet_commander.username }}
                | <i class="fas fa-calendar"></i> {{ fleet.fleet_time|date:"Y-m-d H:i" }}
                | <i class="fas fa-map-marker-alt"></i> {{ fleet.location }}
            </p>
        </div>
        <div class="col-md-4 text-end">
            {% if can_edit %}
                <a href="{% url 'aapayout:fleet_edit' fleet.pk %}" class="btn btn-warning">
                    <i class="fas fa-edit"></i> {% translate "Edit Fleet" %}
                </a>
                <form method="post" action="{% url 'aapayout:fleet_delete' fleet.pk %}" style="display: inline;">
                    {% csrf_token %}
                    <button type="submit" class="btn btn-danger"
                            onclick="return confirm('{% translate "Are you sure you want to delete this fleet?" %}')">
                        <i class="fas fa-trash"></i> {% translate "Delete" %}
                    </button>
                </form>
            {% endif %}
        </div>
    </div>

    <!-- Fleet Info Card -->
    <div class="row mb-4">
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h5>{% translate "Fleet Information" %}</h5>
                </div>
                <div class="card-body">
                    <table class="table table-sm">
                        <tr>
                            <th>{% translate "Status" %}:</th>
                            <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>
                        </tr>
                        <tr>
                            <th>{% translate "Doctrine" %}:</th>
                            <td>{{ fleet.doctrine|default:"—" }}</td>
                        </tr>
                        <tr>
                            <th>{% translate "Participants" %}:</th>
                            <td>{{ participants.count }}</td>
                        </tr>
                        <tr>
                            <th>{% translate "Total Loot Value" %}:</th>
                            <td><strong>{{ fleet.get_total_loot_value|floatformat:2|intcomma }} ISK</strong></td>
                        </tr>
                        <tr>
                            <th>{% translate "Created" %}:</th>
                            <td>{{ fleet.created_at|date:"Y-m-d H:i" }}</td>
                        </tr>
                    </table>
                    {% if fleet.notes %}
                    <div class="mt-3">
                        <strong>{% translate "Notes" %}:</strong>
                        <p>{{ fleet.notes|linebreaks }}</p>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>

        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h5>{% translate "Loot Summary" %}</h5>
                </div>
                <div class="card-body">
                    {% if loot_pools %}
                        <ul class="list-group list-group-flush">
                            {% for pool in loot_pools %}
                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                <div>
                                    <a href="{% url 'aapayout:loot_detail' pool.pk %}">{{ pool.name }}</a>
                                    <br>
                                    <small class="text-muted">
                                        {% if pool.status == 'draft' %}
                                            <span class="badge bg-secondary">{{ pool.get_status_display }}</span>
                                        {% elif pool.status == 'valuing' %}
                                            <span class="badge bg-warning">{{ pool.get_status_display }}</span>
                                        {% elif pool.status == 'valued' %}
                                            <span class="badge bg-info">{{ pool.get_status_display }}</span>
                                        {% elif pool.status == 'approved' %}
                                            <span class="badge bg-success">{{ pool.get_status_display }}</span>
                                        {% elif pool.status == 'paid' %}
                                            <span class="badge bg-primary">{{ pool.get_status_display }}</span>
                                        {% endif %}
                                    </small>
                                </div>
                                <span class="badge bg-primary rounded-pill">
                                    {{ pool.total_value|floatformat:2|intcomma }} ISK
                                </span>
                            </li>
                            {% endfor %}
                        </ul>
                    {% else %}
                        <p class="text-muted">{% translate "No loot pools yet" %}</p>
                    {% endif %}
                    {% if can_edit %}
                    <div class="mt-3">
                        <a href="{% url 'aapayout:loot_create' fleet.pk %}" class="btn btn-sm btn-primary">
                            <i class="fas fa-plus"></i> {% translate "Add Loot Pool" %}
                        </a>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- Participants Section -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5>{% translate "Fleet Participants" %}</h5>
                    {% if can_edit %}
                        <div>
                            <a href="{% url 'aapayout:fleet_import' fleet.pk %}" class="btn btn-sm btn-success me-2">
                                <i class="fas fa-download"></i> {% translate "Import from ESI" %}
                            </a>
                            <a href="{% url 'aapayout:participant_add' fleet.pk %}" class="btn btn-sm btn-primary">
                                <i class="fas fa-plus"></i> {% translate "Add Manual" %}
                            </a>
                        </div>
                    {% endif %}
                </div>
                <div class="card-body">
                    {% if participants %}
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>{% translate "Character" %}</th>
                                    <th>{% translate "Main Character" %}</th>
                                    <th>{% translate "Role" %}</th>
                                    <th>{% translate "Joined At" %}</th>
                                    <th>{% translate "Status" %}</th>
                                    {% if can_edit %}
                                        <th class="text-center">
                                            <i class="fas fa-binoculars" title="{% translate 'Scout' %}"></i>
                                            {% translate "Scout" %}
                                        </th>
                                        <th class="text-center">
                                            <i class="fas fa-ban" title="{% translate 'Exclude from Payout' %}"></i>
                                            {% translate "Exclude" %}
                                        </th>
                                        <th>{% translate "Actions" %}</th>
                                    {% endif %}
                                </tr>
                            </thead>
                            <tbody>
                                {% for participant in participants %}
                                <tr class="{% if participant.excluded_from_payout %}table-secondary text-muted{% endif %}"
                                    id="participant-row-{{ participant.pk }}">
                                    <td>{{ participant.character.character_name }}</td>
                                    <td>
                                        {% if participant.main_character %}
                                            {{ participant.main_character.name }}
                                            {% if participant.main_character.id != participant.character.id %}
                                                <i class="fas fa-link text-muted" title="{% translate 'Alt character' %}"></i>
                                            {% endif %}
                                        {% else %}
                                            <span class="text-muted">—</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <span class="badge bg-info">{{ participant.get_role_display }}</span>
                                    </td>
                                    <td>{{ participant.joined_at|date:"Y-m-d H:i" }}</td>
                                    <td>
                                        {% if participant.is_active %}
                                            <span class="badge bg-success">{% translate "Active" %}</span>
                                        {% else %}
                                            <span class="badge bg-secondary">{% translate "Left" %}</span>
                                        {% endif %}
                                    </td>
                                    {% if can_edit %}
                                    <td class="text-center">
                                        <input type="checkbox" class="form-check-input scout-checkbox"
                                               data-participant-id="{{ participant.pk }}"
                                               {% if participant.is_scout %}checked{% endif %}>
                                    </td>
                                    <td class="text-center">
                                        <input type="checkbox" class="form-check-input exclude-checkbox"
                                               data-participant-id="{{ participant.pk }}"
                                               {% if participant.excluded_from_payout %}checked{% endif %}>
                                    </td>
                                    <td>
                                        <div class="btn-group btn-group-sm">
                                            <a href="{% url 'aapayout:participant_edit' participant.pk %}" class="btn btn-warning" title="{% translate 'Edit' %}">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <form method="post" action="{% url 'aapayout:participant_remove' participant.pk %}" style="display: inline;">
                                                {% csrf_token %}
                                                <button type="submit" class="btn btn-danger btn-sm" title="{% translate 'Remove' %}"
                                                        onclick="return confirm('{% translate "Remove this participant?" %}')">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </form>
                                        </div>
                                    </td>
                                    {% endif %}
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <p class="text-muted">{% translate "No participants added yet" %}</p>
                    {% if can_edit %}
                    <a href="{% url 'aapayout:participant_add' fleet.pk %}" class="btn btn-primary">
                        <i class="fas fa-plus"></i> {% translate "Add First Participant" %}
                    </a>
                    {% endif %}
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
