{% extends "allianceauth/base.html" %}
{% load static %}
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}

{% block page_title %}{% trans "Fleet Operation Management" %}{% endblock page_title %}
{% block extra_css %}{% endblock extra_css %}

{% block content %}
    <div>
        <h1 class="page-header text-center">{% trans "Fleet Operation Timers" %}
            <div class="text-right">
                {% if perms.auth.optimer_management %}
                    <a href="{% url 'optimer:add' %}" class="btn btn-success">{% trans "Create Operation" %}</a>
                {% endif %}
            </div>
        </h1>

        <div class="col-lg-12 text-center row">
            <div class="label label-info text-left">
                <b>{% trans "Current Eve Time:" %} </b>
            </div>
            <strong class="label label-info text-left" id="current-time"></strong>
            <br  />
        </div>

        <h4><b>{% trans "Next Timers" %}</b></h4>
        {% if future_timers %}
            {% include "optimer/fleetoptable.html" with timers=future_timers %}
        {% else %}
            <div class="alert alert-warning text-center">{% trans "No upcoming timers." %}</div>
        {% endif %}

        <h4><b>{% trans "Past Timers" %}</b></h4>
        {% if past_timers %}
            {% include "optimer/fleetoptable.html" with timers=past_timers %}
        {% else %}
            <div class="alert alert-warning text-center">{% trans "No past timers." %}</div>
        {% endif %}
    </div>

    {% include 'bundles/moment-js.html' with locale=True %}
    <script src="{% static 'js/timers.js' %}"></script>
    <script type="application/javascript">
        // Data
        var timers = [
        {% for op in optimer %}
            {
                'id': {{ op.id }},
                'start': moment("{{ op.start | date:"c" }}"),
                'expired': false
            },
        {% endfor %}
        ];
    </script>
    <script type="application/javascript">

        timedUpdate();
        setAllLocalTimes();

        // Start timed updates
        setInterval(timedUpdate, 1000);

        function timedUpdate() {
            updateClock();
            updateAllTimers();
        }

        function updateAllTimers () {
            var l = timers.length;
            for (var i=0; i < l; ++i) {
                if (timers[i].expired) continue;
                updateTimer(timers[i]);
            }
        }

        /**
         * Update a timer
         * @param timer Timer information
         * @param timer.start Date of the timer
         * @param timer.id Id number of the timer
         * @param timer.expired
         */
        function updateTimer(timer) {
            if (timer.start.isAfter(Date.now())) {
                var duration = moment.duration(timer.start - moment(), 'milliseconds');
                document.getElementById("countdown" + timer.id).innerHTML = getDurationString(duration);
            } else {
                timer.expired = true;
                document.getElementById("countdown" + timer.id).innerHTML = "";
            }
        }

        /**
         * Set all local time fields
         */
        function setAllLocalTimes() {
            var l = timers.length;
            for (var i=0; i < l; ++i) {
                setLocalTime(timers[i]);
            }
        }

        /**
         * Set the local time info for the timer
         * @param timer Timer information
         * @param timer.start Date of the timer
         * @param timer.id Id number of the timer
         */
        function setLocalTime(timer) {
            document.getElementById("localtime" + timer.id).innerHTML = timer.start.format("ddd @ LT");
        }

        function updateClock() {
            document.getElementById("current-time").innerHTML = getCurrentEveTimeString();
        }
    </script>
{% endblock content %}
