{% extends 'allianceauth/base.html' %}

{% load evelinks %}
{% load i18n %}

{% block page_title %}{% trans 'Fleet Activity' %}{% endblock %}

{% block content %}
    <div class="col-lg-12">
        <br>
        {% include "afat/menu.html" %}

        <h2>{% trans "Recent Activity" %}</h2>

        {% if msg %}
        <div class="alert alert-{{ msg.0 }} alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="close">
                <span aria-hidden="true">&times</span>
            </button>
            {% if msg.0 != 'warning' %}
            <h4>
                {% if msg.0 == 'danger' %}
                    {% trans "Oh No!" %}
                {% elif msg.0 == 'success' %}
                    {% trans "Success!" %}
                {% endif %}
            </h4>
            {% endif %}
            <p>{{ msg.1 }}</p>
        </div>
        {% endif %}

        <ul class="nav nav-tabs">
            <li class="active"><a data-toggle="tab" href="#fats">{% trans "FATs" %}</a></li>
            <li><a data-toggle="tab" href="#fatlinks">{% trans "FAT Links" %}</a></li>
        </ul>

        <div class="tab-content">
            <div id="fats" class="tab-pane fade in active panel-default">
                <div class="panel-body">
                    <h4>{% trans "Your Most Recent FATs" %}</h4>

                    <ul class="nav nav-pills">
                        {% for char in fats %}
                            {% if forloop.first %}
                                <li class="active"><a data-toggle="tab" href="#{{ char|last }}"><img class="img-circle" src="{{ char|last|character_portrait_url:32 }}"/>  {{ char.0 }}</a></li>
                            {% else %}
                                <li><a data-toggle="tab" href="#{{ char|last }}"><img class="img-circle" src="{{ char|last|character_portrait_url:32 }}"/>  {{ char.0 }}</a></li>
                            {% endif %}
                        {% endfor %}
                    </ul>

                    <div class="tab-content">
                        {% for char in fats %}
                            {% if forloop.first %}
                            <div id="{{ char|last }}" class="tab-pane fade in active panel-default">
                            {% else %}
                            <div id="{{ char|last }}" class="tab-pane fade panel-default">
                            {% endif %}

                                <div class="panel-body">
                                    {% if char|slice:"1:-1"|length > 0 %}
                                        <table class="table">
                                            <thead>
                                                <th>Fleet Name</th>
                                                <th>Fleet Type</th>
                                                <th>Location</th>
                                                <th>Ship</th>
                                                <th>EVE Time</th>
                                            </thead>
                                            <tbody>
                                                {% for afat in char|slice:"1:" %}
                                                    <tr>
                                                        <td>
                                                            {% if afat.afatlink.fleet %}
                                                                {{ afat.afatlink.fleet }}
                                                            {% else %}
                                                                {{ afat.afatlink.hash }}
                                                            {% endif %}
                                                        </td>
                                                        <td>
                                                            {% if afat.afatlink.link_type %}
                                                                {{ afat.afatlink.link_type.name }}
                                                            {% endif %}
                                                        </td>
                                                        <td>{{ afat.system }}</td>
                                                        <td>{{ afat.shiptype }}</td>
                                                        <td>{{ afat.afatlink.afattime|date:"Y-M-d, H:i" }}</td>
                                                    </tr>
                                                {% endfor %}
                                            </tbody>
                                        </table>
                                    {% else %}
                                        <div class="alert alert-warning" role="alert">
                                            <p>{% trans "No FATs for" %} {{ char.0 }}</p>
                                        </div>
                                    {% endif %}
                                </div>
                            </div>
                        {% endfor %}
                    </div>
                </div>
            </div>

            <div id="fatlinks" class="tab-pane fade panel-default">
                <div class="panel-body">
                    <h4>{% trans "Most Recent FAT Links" %}</h4>

                    <table class="table">
                        <thead>
                            <th>{% trans "Fleet Name" %}</th>
                            <th>{% trans "Creator" %}</th>
                            <th>{% trans "EVE Time" %}</th>
                            <th>{% trans "Utils" %}</th>
                        </thead>

                        <tbody>
                            {% for link in links %}
                                <tr>
                                    <td>{% if link.fleet %}{{ link.fleet }}{% else %}{{ link.hash }}{% endif %}</td>
                                    <td>{{ link.creator.profile.main_character.character_name }}</td>
                                    <td>{{ link.afattime|date:"Y-M-d, H:i" }}</td>
                                    <td>
                                        {% if perms.afat.change_afatlink %}
                                        <a class="btn btn-info" href="{% url 'afat:link_edit' link.hash %}">
                                            <span class="glyphicon glyphicon-pencil"></span>
                                        </a>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if perms.afat.delete_afatlink %}
                                        <a class="btn btn-danger"
                                            data-toggle="modal"
                                            data-target="#deleteModal"
                                            data-url="{{ request.scheme }}://{{ request.get_host }}{% url 'afat:link_delete' link.hash %}"
                                            data-name="{% if link.fleet %}{{ link.fleet }}{% else %}{{ link.hash }}{% endif %}">
                                            <span class="glyphicon glyphicon-trash"></span>
                                        </a>
                                        {% endif %}
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- Delete FAT link Modal -->
    <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalTitle"
        aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="modalLongTitle">{% trans "Delete FAT Link" %}</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">{% trans "Cancel" %}</button>
                    <a id="fat-link" class="btn btn-danger" role="button" href="">{% trans "Delete" %}</a>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block extra_script %}
    $('#deleteModal').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget) // Button that triggered the modal
        var url = button.data('url') // Extract info from data-* attributes
        var name = button.data('name');

        var modal = $(this)
        modal.find('#fat-link').attr("href", url)
        modal.find('.modal-body').text('{% trans "Are you sure you want to delete" %} ' + name + '?');
    });
{% endblock %}
