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

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

{% block page_title %}{{ corporation }} {% trans "Statistics" %}{% endblock %}

{% block content %}
    <script type="text/javascript" src="{% static 'afat/javascript/chart-js/Chart.min.js' %}"></script>

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

        <h2>{{ corporation }} {% trans "Statistics" %}</h2>
        <h4>{{ month|month }} {{ year }}</h4>

        <ul class="nav nav-tabs">
            <li class="active"><a data-toggle="tab" href="#graphs">{% trans "Graphs" %}</a></li>
            <li><a data-toggle="tab" href="#members">{% trans "Members" %}</a></li>
        </ul>

        <div class="tab-content">
            <div id="graphs" class="tab-pane fade in active panel-default">
                <div class="panel-body">
                    <ul class="nav nav-pills">
                        <li class="active"><a data-toggle="tab" href="#fatsbyship">{% trans "FATs By Ship Type" %}</a></li>
                        <li><a data-toggle="tab" href="#fatsbytime">{% trans "FATs By Time" %}</a></li>
                        <li><a data-toggle="tab" href="#fatsbyweek">{% trans "FATs By Weekday" %}</a></li>
                    </ul>

                    <div class="tab-content">
                        <div class="tab-pane fade in active panel-default" id="fatsbyship">
                            <div class="panel-body">
                                <h3>{% trans "FATs By Ship Type" %}</h3>

                                <canvas id="ship-bar-stacked" width="800" height="500"></canvas>
                                <script>
                                var corpShipTypeBarChart = new Chart(document.getElementById('ship-bar-stacked'), {
                                    type: "bar",
                                    data: {
                                        datasets: [{% for set in data_stacked.1 %}
                                                {
                                                  label: '{{ set.0|escapejs }}',
                                                  backgroundColor: '{{ set.1|safe }}',
                                                  data: {{ set.2 }}
                                                },
                                            {% endfor %}],
                                        labels: {{ data_stacked.0|safe }}
                                    },
                                    options: {
                                        responsive: false,
                                        scales: {
                                            xAxes: [{
                                                stacked: true,
                                                ticks: {
                                                    autoSkip: false
                                                }
                                            }],
                                            yAxes: [{
                                                stacked: true
                                            }]
                                        }
                                    }
                                })
                                </script>
                            </div>
                        </div>

                        <div id="fatsbytime" class="tab-pane fade in panel-default">
                            <div class="panel-body">
                                <h3>{% trans "FATs By Time" %}</h3>

                                <canvas id="time-line" width="800" height="500"></canvas>
                                <script>
                                    var timeLineChart = new Chart(document.getElementById("time-line"), {
                                        type: 'line',
                                        data: {
                                            datasets: [{
                                                label: '# of FATs',
                                                backgroundColor: {{ data_time.2|safe }},
                                                data: {{ data_time.1 }}
                                            }],
                                            labels: {{ data_time.0|safe }}
                                        },
                                        options: {
                                            responsive: false
                                        }
                                    })
                                </script>
                            </div>
                        </div>

                        <div class="tab-pane fade in panel-default" id="fatsbyweek">
                            <div class="panel-body">
                                <h3>{% trans "FATs By Weekday" %}</h3>

                                <canvas id="weekday-line" width="800" height="500"></canvas>
                                <script>
                                    var weekdayLineChart = new Chart(document.getElementById('weekday-line'), {
                                        type: 'line',
                                        data:{
                                            datasets: [{
                                                label: '# of FATs',
                                                backgroundColor: {{ data_weekday.2|safe }},
                                                data: {{ data_weekday.1 }}
                                            }],
                                            labels: {{ data_weekday.0|safe }}
                                        },
                                        options: {
                                            responsive: false
                                        }
                                    })
                                </script>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="tab-pane fade in panel-default" id="members">
                <div class="panel-body">
                    <h3>{{ corporation }} {% trans "Members" %}</h3>

                    <table class="table" id="corp-members">
                        <thead>
                            <th>{% trans "Character" %}</th>
                            <th>{% trans "FATs" %}</th>
                            <th>{% trans "Character Stats" %}</th>
                        </thead>

                        <tbody>
                            {% for key, value in chars.items %}
                                <tr>
                                    <td>{{ key }}</td>
                                    <td>{{ value.0 }}</td>
                                    <td>
                                        <a class="btn btn-primary" href="/afat/stats/char/{{ value.1 }}/{{ month }}/{{ year }}/">
                                            <span class="glyphicon glyphicon-eye-open"></span>
                                        </a>
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </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');

        $('#corp-members').DataTable();
    });
{% endblock %}
