{% extends "wagtailadmin/base.html" %}
{% load wagtailadmin_tags i18n staticfiles %}

{% block extra_css %}
    {% include "wagtailadmin/pages/_editor_css.html" %}
    {{ block.super }}
    <script>
        (function(w,d,s,g,js,fs){
          g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(f){this.q.push(f);}};
          js=d.createElement(s);fs=d.getElementsByTagName(s)[0];
          js.src='https://apis.google.com/js/platform.js';
          fs.parentNode.insertBefore(js,fs);js.onload=function(){g.load('analytics');};
        }(window,document,'script'));
    </script>
    <style>
      .wagalytics .chart {
          height: 150px;
          margin-bottom: 50px;
      }

      .wagalytics .icon-spinner {
          margin-left: 50%;
      }

      .wagalytics .button-group {
        float: right;
      }

      .wagalytics .pagination {
        margin-bottom: 50px;
      }

      .wagalytics .pagination ul {
        margin-top: -1.265em;
      }

      .wagalytics .disabled a {
        cursor: default;
        pointer-events: none;
        color: #666;
        opacity: 0.5;
      }

      .wagalytics table.listing td.title:first-child {
        width: 100%;
      }

      footer .actions {
        width: auto;
      }

      footer .meta {
        float: none;
        text-align: left;
      }
    </style>
{% endblock %}

{% block titletag %}{% trans 'Analytics' %}{% endblock %}

{% block content %}
    <header class="col12">
        <div class="row nice-padding">
            <div class="left">
                <div class="col header-title">
                    <h1 class="icon icon-fa-bar-chart">
                        {% trans "Analytics" %}
                    </h1>
                </div>
            </div>
            <form class="left">
                <ul class="fields">
                    <li class="date_field col6">
                        <div class="field date_field admin_date_input field-small">
                            <label for="id_date_from">Start date:</label>
                            <div class="field-content">
                                <div class="input">
                                    <input id="id_date_from" type="date" value="{{ initial_start_date }}">
                                </div>
                            </div>
                        </div>
                    </li>
                    <li class="date_field col6">
                        <div class="field date_field admin_date_input field-small">
                            <label for="id_date_from">End date:</label>
                            <div class="field-content">
                                <div class="input">
                                    <input id="id_date_to" type="date" value="{% now "Y-m-d" %}">
                                </div>
                            </div>
                        </div>
                    </li>
                </ul>
            </form>
        </div>
    </header>

    <div class="wagalytics nice-padding">
        <div class="col12 clearfix">
            <h2>Sessions</h2>
            <div id="sessions-line-chart-container" class="chart"><i class="icon icon-spinner"></i></div>
        </div>
        <div class="col6 clearfix">
            <h2>Popular content</h2>
            <div id="popular-pages-table-container"><i class="icon icon-spinner"></i></div>
        </div>
        <div class="col6 clearfix">
            <h2>Top referrers</h2>
            <div id="top-referrers-table-container" ><i class="icon icon-spinner"></i></div>
        </div>
    </div>

    <footer>
        <ul>
            <li class="actions">
                <form id="export" action="{% url 'wagalytics_export' %}" method="post">
                    {% csrf_token %}
                    <input id="export-data" type="hidden" name="data">
                    <button id="export-button" class="button icon icon-download button-longrunning" data-clicked-text="Exporting…" type="submit" disabled>
                        <span class="icon icon-spinner"></span><em>Export Data</em>
                    </button>
                </form>
            </li>
            <li class="meta">
                <p>{% trans "The current view will be exported to a spreadsheet file (*.ods)" %}</p>
            </li>
        </ul>
    </footer>

    <div hidden id="wagalytics-data" data-token="{% url 'wagalytics_token' %}" data-view-id="{{ ga_view_id }}">
    </div>
{% endblock %}

{% block extra_js %}
    {{ block.super }}
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
    <script src="{% static 'wagalytics/vendors.bundle.js' %}"></script>
    <script src="{% static 'wagalytics/wagalytics.bundle.js' %}"></script>
{% endblock %}
