{% extends "industry_reforged/base.html" %}
{% load i18n humanize %}

{% block details %}
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-12 d-flex justify-content-between align-items-center">
            <h3>{% trans "Industrialist Leaderboards & History" %}</h3>
            <a href="{% url 'industry_reforged:industrialist_dashboard' %}" class="btn btn-primary"><i class="fas fa-arrow-left"></i> {% trans "Back to Dashboard" %}</a>
        </div>
    </div>

    <!-- Leaderboards -->
    <div class="row mb-5">
        <div class="col-md-6">
            <div class="card border-0 mb-4 h-100">
                <div class="card-header">
                    <h3 class="card-title mb-0"><i class="fas fa-medal"></i> {% trans "Top Builders (by Tasks Completed)" %}</h3>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-striped m-0">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>{% trans "Industrialist" %}</th>
                                    <th class="text-end">{% trans "Tasks Completed" %}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for entry in leaderboard_vol %}
                                <tr>
                                    <td class="numeric"><strong>{{ forloop.counter }}</strong></td>
                                    <td>{{ entry.assigned_to__character_name }}</td>
                                    <td class="text-end fw-bold numeric text-warning">{{ entry.tasks }}</td>
                                </tr>
                            {% empty %}
                            <tr>
                                <td colspan="3" class="text-center text-muted">No data available.</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="col-md-6">
            <div class="card border-0 mb-4 h-100">
                <div class="card-header">
                    <h3 class="card-title mb-0"><i class="fas fa-coins"></i> {% trans "Top Builders (by ISK Value)" %}</h3>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-striped m-0">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>{% trans "Industrialist" %}</th>
                                    <th class="text-end">{% trans "Total ISK Value Produced" %}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for entry in leaderboard_isk %}
                                <tr>
                                    <td class="numeric"><strong>{{ forloop.counter }}</strong></td>
                                    <td>{{ entry.assigned_to__character_name }}</td>
                                    <td class="text-end fw-bold text-warning numeric">{{ entry.total_isk|floatformat:0|intcomma }}</td>
                                </tr>
                            {% empty %}
                            <tr>
                                <td colspan="3" class="text-center text-muted">No data available.</td>
                            </tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Personal History -->
    <div class="row">
        <div class="col-12">
            <div class="card border-0 mb-4">
                <div class="card-header">
                    <h3 class="card-title mb-0"><i class="fas fa-history"></i> {% trans "My Completed Tasks History" %}</h3>
                </div>
                <div class="card-body p-0" style="max-height: 400px; overflow-y: auto;">
                    <div class="table-responsive">
                        <table class="table table-striped table-hover m-0">
                            <thead>
                                <tr>
                                    <th>{% trans "Date Completed" %}</th>
                                    <th>{% trans "Item" %}</th>
                                    <th class="text-end">{% trans "Quantity" %}</th>
                                    <th>{% trans "Builder" %}</th>
                                    <th class="text-end">{% trans "Points (ISK Value)" %}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for task in personal_history %}
                                <tr>
                                    <td class="numeric">{{ task.completed_at|date:"Y-m-d H:i" }}</td>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <img onerror="this.onerror=null; this.src='https://images.evetech.net/types/3514/icon?size=32';" src="https://images.evetech.net/types/{{ task.item_type.id }}/icon?size=32" alt="{{ task.item_type.name }}" width="24" height="24" class="me-2 rounded">
                                            <span>{{ task.item_type.name }}</span>
                                        </div>
                                    </td>
                                    <td class="text-end numeric">{{ task.quantity }}</td>
                                    <td>{{ task.assigned_to.character_name }}</td>
                                    <td class="text-end numeric text-warning">{{ task.gamification_value|floatformat:0|intcomma }}</td>
                                </tr>
                            {% empty %}
                            <tr>
                                <td colspan="5" class="text-center text-muted">You haven't completed any tasks yet.</td>
                            </tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
