{% extends "base.html" %}
{% block comments %}
<!--
# Copyright (c) 2015 Riverbed Technology, Inc.
#
# This software is licensed under the terms and conditions of the MIT License
# accompanying the software ("License").  This software is distributed "AS IS"
# as set forth in the License.
-->
{% endblock %}

{% load tz %}
{% load url from future %}
{% load add_attrs %}

{% block header %}
    <link href="{{ STATIC_URL }}showLoading/css/showLoading.css" rel="stylesheet"/>
    <link href="{{ STATIC_URL }}css/report.css" rel="stylesheet"/>

    <script type="text/javascript" src="{{ STATIC_URL }}showLoading/js/jquery.showLoading.js"></script>
    <script type="text/javascript" src="{{ STATIC_URL }}jquery-timepicker/jquery.timepicker.min.js"></script>

    <script type="text/javascript" src="{{ STATIC_URL }}js/rvbd-widgets.js"></script>
    <script type="text/javascript" src="{{ STATIC_URL }}js/rvbd-report.js"></script>
    <script type="text/javascript" src="{{ STATIC_URL }}js/rvbd-yui3.js"></script>

    <!-- clean up these refs -->
    <script type="text/javascript" src="{{ STATIC_URL }}js/rvbd-c3.js"></script>
    <script type="text/javascript" src="{{ STATIC_URL }}js/rvbd-tables.js"></script>

    {% if maps_version == 'OPEN_STREET_MAPS' %}
        <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.2/leaflet.js"></script>
        <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.2/leaflet.css" />
        <script type="text/javascript" src="{{ STATIC_URL }}js/rvbd-openstreetmaps.js"></script>
    {% else %}
        {% if maps_version == 'DEVELOPER' %}
            <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
        {% elif maps_version == 'FREE' %}
            <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3%key={{ maps_api_key }}&sensor=false"></script>
        {% elif maps_version == 'BUSINESS' %}
            <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3&client={{ maps_api_key }}&sensor=false"></script>
        {% endif %}
        <script type="text/javascript" src="{{ STATIC_URL }}js/rvbd-google-maps.js"></script>
    {% endif %}

    {% for js in js_extensions %}
        <script type="text/javascript" src="{{ js }}"></script>
    {% endfor %}

    {% for css in css_extensions %}
        <link href="{{ css }}" rel="stylesheet"/>
    {% endfor %}

    <script type="text/javascript">
    {% block header_script %}
      rvbd.report.isEmbedded = false;
      rvbd.report.embeddable_widgets = {% if user.is_authenticated %} true {% else %} false {% endif %};
      rvbd.report.widgetsUrl = "{% url 'report-auto-view' report.namespace report.slug %}";
      rvbd.report.csrfToken = "{{ csrf_token }}";
      rvbd.report.weatherWidget = {
          enabled: {% if weather_enabled %}true{% else %}false{% endif %},
          url: {% if weather_url %}'{{ weather_url }}'{% else %} undefined {% endif %},
          timeout: 15 // The number of minutes until we force the image cache to be replaced
      }
      rvbd.report.autoLoad = {% if report.hide_criteria %} true {% else %} false {% endif %};
      rvbd.report.reloadMinutes = {{ report.reload_minutes }};
      rvbd.report.offsetSeconds = {{ report.reload_offset }};
      rvbd.report.autoRun = {% if report.auto_run and not form.errors %} true {% else %} false {% endif %};
      rvbd.report.static = {% if report.static %} true {% else %} false {% endif %};
      rvbd.report.live = {% if report.live %} true {% else %} false {% endif %};
      rvbd.report.expandTables = {% if expand_tables %} true {% else %} false {% endif %};

      // rvbd.report.debugUrl is used in rvbd-report.js to allow the user to download a zip
      // of the debug information if rvbd_debug is set to true.
      rvbd.report.debugUrl = "{% url 'steelscript.appfwk.apps.report.views.download_debug' %}";

      // turn on different features if developer mode enabled
      rvbd.report.developer = {% if developer %}true{% else %}false{% endif %};

      $(rvbd.report.start);

      function appendCurrentTimeUrlParam(uri, timeoutValue) {
          // Calculate the current time rounding to the last 15 minutes
          var coeff = 1000 * 60 * timeoutValue;
          var timeout = new Date();  //or use any other date
          var roundedTimeout = new Date(Math.round(timeout.getTime() / coeff) * coeff)

          // Set our key and value to be appended to the url
          var key = 'time';
          var value = roundedTimeout;
          // Append "time = <roundedTimeout>" to the url
          // If we have a url parameter, add it with an &, otherwise add ?
          var separator = uri.indexOf('?') !== -1 ? "&" : "?";
          return uri + separator + key + "=" + value;
      }
    {% endblock %}
    </script>
{% endblock %}

{% block header_row %}<h2 class="report-title">{{ report.title }}</h2>{% endblock %}

{% block content %}
    {% if report.description %}
    <div class="report-description">
      {{ report.description | safe }}
    </div>
    {% endif %}
        {% if missing_devices %}
            <div class="block-centered" style="text-align: center">
                <h4 class="text-danger">
                    {% if is_superuser %}
                        This report requires the following devices to be added or enabled: '{{ missing_devices }}'.
                        Please visit <a href="{%  url 'device-list' %}">Devices</a>.
                    {% else %}
                        This report requires the following devices to be added or enabled: '{{ missing_devices  }}'.
                        Please contact a system administrator.
                    {% endif %}
                </h4>
            </div>
        {% endif %}
    {% if not report.hide_criteria %}
    {% if report.reload_minutes == 0 or report.live %}
    <div class="container-fluid">
      <div id="criteria-row" class="row">
        <div class="col-xs-12">
          <div class="section-title">
            <a data-toggle="collapse" data-target="#criteria" href="#">
              Criteria
            </a>
          </div>

          <div id="criteria" class="criteria collapse in">
            <div class="widget-content">
                <div class="col-xs-8">
                <form class="form-horizontal" action="{% url 'report-view' report.namespace report.slug %}" method="post" id="criteria-form"
                        {% if form.is_multipart %}enctype="multipart/form-data"{% endif %} target="_blank">
                    <fieldset>
                    {% csrf_token %}
                    <input type="hidden" id="id_expand_tables" name="expand_tables">
                        <div id="form-error-info" class="text-danger"></div>

                        {% if form.errors %}
                            <p class="text-error">Please correct the errors below.</p>
                        {% endif %}

                        {% for s in section_map %}
                            <div class="criteria-group">
                                {% if show_sections %}
                                    <div class="criteria-group-title"> {{ s.title }} </div>
                                {% endif %}
                                <div class="criteria-group-contents">
                                    {% for field in form.visible_fields %}
                                        {% if field.name in s.parameters %}
                                            <div class="form-group">
                                                <label for="{{ field.auto_id }}" class="col-xs-3 control-label"> {{ field.label_tag }} </label>
                                                <div class="col-xs-8">
                                                {{ field|addcss:"form-control" }}
                                                </div>
                                                {% if field.errors %}
                                                    {% for err in field.errors %}
                                                        <p class="text-error">{{ err }}</p>
                                                    {% endfor %}
                                                {% endif %}
                                                {% if field.help_text %}
                                                    <span class="glyphicon glyphicon-info-sign" title="{{ field.help_text }}"></span>
                                                {% endif %}
                                            </div>
                                        {% endif %}
                                    {% endfor %}
                                </div>
                            </div>
                        {% endfor %}
                        {% for hidden in form.hidden_fields %}
                            {{ hidden }}
                        {% endfor %}
                    </fieldset>
              </form>
              </div>
              <div class="criteria col-xs-offset-2 col-xs-8">
              {% if not missing_devices %}
                  <div class="btn-group dropup">
                      <button id="button-run" class="btn btn-primary">Run</button>
                      <button class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
                        <span class="caret"></span>
                      </button>
                      <ul class="dropdown-menu">
                          {% if developer %}
                              <li><a href="#" id="menu-debug">Debug This Report</a></li>
                          {% endif %}
                          <li><a href="#" id="menu-update-time-run">Update Time & Run</a></li>
                          <li class="divider"></li>
                          <li><a href="#" id="menu-run">Run Report</a></li>
                      </ul>
                  </div>
              {% endif  %}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    {% endif %}
    {% endif %}

    <div class="container-fluid">
      <div class="row">
        <div class="col-xs-12">
          <div class="section-title report-title">
              {% if report.hide_criteria %}
                <a data-toggle="collapse" data-target="#report-container" href="#">&nbsp;</a>
              {% else %}
                <a data-toggle="collapse" data-target="#report-container" href="#"> Report </a>
              {% endif %}
              <span id="print-report" class="glyphicon glyphicon-print disabled pull-right" title="Print this report (run report to enable)"></span>
              <span id="reload-report" class="glyphicon glyphicon-refresh disabled pull-right" title="Reload all widgets in this report"></span>
          </div>
          <div class="collapse in" id="report-container">
            <div class="widget-content collapse in" id="report">
            </div>
          </div>
        </div> <!-- span12 -->
      </div> <!-- row-fluid -->
    </div>
    {% endblock %}
