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

{% load i18n %}
{% load static %}

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

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

        <h2>{% trans "All FAT Links" %}</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>
            <h4>
                {% if msg.0 == 'danger' %}
                    {% trans "Oh No!" %}
                {% elif msg.0 == 'success' %}
                    {% trans "Success!" %}
                {% endif %}
            </h4>
            <p>{{ msg.1 }}</p>
        </div>
        {% endif %}

        <table class="table" id="link-list">
            <thead>
                <th>{% trans "Fleet Name" %}</th>
                <th>{% trans "Fleet Type" %}</th>
                <th>{% trans "Creator" %}</th>
                <th>{% trans "EVE Time" %}</th>
                <th>{% trans "# of FATs" %}</th>
                <th>{% trans "Edit" %}</th>
                <th>{% trans "Delete" %}</th>
            </thead>

            <tbody>
                {% for link in links %}
                    <tr>
                        <td>
                            {% if link.fleet %}
                                {{ link.fleet }}
                            {% else %}
                                {{ link.hash }}
                            {% endif %}
                        </td>

                        <td>
                            {% if link.link_type %}
                                {{ link.link_type.name }}
                            {% endif %}
                        </td>

                        <td>{{ link.creator.profile.main_character.character_name }}</td>
                        <td data-sort='{{ link.afattime|date:"Y-m-d H:i" }}'>{{ link.afattime|date:"Y-M-d, H:i" }}</td>
                        <td>{{ link.number_of_fats }}</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>

    <!-- 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_javascript %}
    {% include 'bundles/datatables-js.html' %}
    {% include 'bundles/moment-js.html' %}
{% endblock %}

{% block extra_css %}
    {% include 'bundles/datatables-css.html' %}
{% endblock %}

{% block extra_script %}
    $.fn.dataTable.moment = function(format, locale) {
        var types = $.fn.dataTable.ext.type;

        // Add type detection
        types.detect.unshift(function(d) {
            return moment(d, format, locale, true).isValid() ?
                'moment-'+format :
                null;
        } );

        // Add sorting method - use an integer for the sorting
        types.order[ 'moment-'+format+'-pre' ] = function(d) {
            return moment(d, format, locale, true).unix();
        };
    };

    $(document).ready(function(){
        $.fn.dataTable.moment('YYYY-MMM-D, HH:mm');

        $('#link-list').DataTable( {
            "order": [[ 3, "desc" ]]
        } );
    });

    $('#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 %}
