{% extends "panoramix/basic.html" %}

{% block body %}
  {% set datasource = viz.datasource %}
  {% set form = viz.form %}

  {% macro panofield(fieldname)%}
    <div>
      {% set field = form.get_field(fieldname)%}
      <div>
        {{ viz.get_form_override(fieldname, 'label') or field.label }}
        {% if field.description %}
        <i class="fa fa-info-circle" data-toggle="tooltip" data-placement="right"
          title="{{ viz.get_form_override(fieldname, 'description') or field.description }}"></i>
        {% endif %}
        {{ field(class_=form.field_css_classes(field.name)) }}
      </div>
    </div>
  {% endmacro %}

  <div class="datasource container-fluid">
    <form id="query" method="GET" style="display: none;">
      <div class="header">
          <span class="btn btn-default notbtn">
            <strong>{{ datasource.full_name }}</strong>
            {% if datasource.description %}
            <a data-toggle="modal" data-target="#sourceinfo_modal">
              <i class="fa fa-info-circle" data-toggle="tooltip" data-placement="bottom" title="{{ datasource.description }}"></i>
            </a>
            {% endif %}
              <a class="" href="/{{ datasource.baselink }}/edit/{{ datasource.id }}" data-toggle="tooltip" title="Edit datasource">
                <i class="fa fa-edit"></i>
              </a>
          </span>
          <span>{{ form.get_field("viz_type")(class_="select2") }}</span>
          {% if slice %}
            <span class="alert alert-info" title="Slice" data-toggle="tooltip">{{ slice.slice_name }}
                <a class="" href="/slicemodelview/edit/{{ slice.id }}" data-toggle="tooltip" title="Edit Slice metadata">
                  {% if slice.description %}
                    <i class="fa fa-info-circle" data-toggle="tooltip" data-placement="bottom" title="{{ slice.description }}"></i>
                  {% endif %}
                  <i class="fa fa-edit"></i>
                </a>
            </span>
          {% endif %}
          <div class="btn-group results pull-right" role="group">
            <a role="button" tabindex="0" class="btn btn-default" id="shortner" title="Short URL" data-toggle="popover" data-trigger="focus">
              <i class="fa fa-link"></i>&nbsp;
            </a>
            <span class="btn btn-default" id="standalone" title="Standalone version, use to embed anywhere" data-toggle="tooltip">
              <i class="fa fa-code"></i>&nbsp;
            </span>
            <span class="btn btn-default " id="json" title="Export to .json" data-toggle="tooltip">
              <i class="fa fa-file-code-o"></i>
              .json
            </span>
            <span class="btn btn-default " id="csv" title="Export to .csv format" data-toggle="tooltip">
              <i class="fa fa-file-text-o"></i>.csv
            </span>
            <span class="btn btn-warning notbtn" id="timer">0 sec</span>
            <span class="btn btn-info disabled query"
              data-toggle="modal" data-target="#query_modal">query</span>
          </div>
          <hr/>
      </div>
        <div id="form_container" class="col-left-fixed">
          <div class="row center-block">
              <button type="button" class="btn btn-primary druidify">
                <i class="fa fa-bolt"></i>
                Query
              </button>
            {% if viz.form_data.slice_id %}
                <button type="button" class="btn btn-default" id="btn_overwrite">
                  <i class="fa fa-save"></i>
                  Overwrite
                </button>
            {% endif %}
              <button type="button" class="btn btn-default" id="btn_save">
                <i class="fa fa-plus-circle"></i>
                Save as
              </button>
          </div>
          <br/>
          {% for fieldset in form.fieldsets %}
              <fieldset class="fs-style">
                {% if fieldset.label %}
                  <legend class="legend-style">
                    <span class="legend_label">{{ fieldset.label }}</span>
                    {% if fieldset.description %}
                      <i    class="fa fa-info-circle" data-toggle="tooltip"
                            data-placement="bottom"
                            title="{{ fieldset.description }}"></i>
                    {% endif %}
                    <span class="collapser"> [-]</span>
                  </legend>
                {% endif %}
                <div class="fieldset_content">
                {% for fieldname in fieldset.fields %}
                  {% if not fieldname %}
                    <hr/>
                  {% elif not fieldname.__iter__ %}
                    {{ panofield(fieldname) }}
                  {% else %}
                  <div class="row">
                    <div class="form-group">
                      {% for name in fieldname %}
                      <div class="col-xs-{{ (12 / fieldname|length) | int }}">
                        {% if name %}
                          {{ panofield(name) }}
                        {% endif %}
                      </div>
                      {% endfor %}
                    </div>
                  </div>
                  {% endif %}
                {% endfor %}
                </div>
              </fieldset>
            {% endfor %}
            <fieldset class="fs-style">
              <legend class="legend-style">
                <span class="legend_label">Filters</span>
                <i  class="fa fa-info-circle" data-toggle="tooltip"
                    data-placement="bottom"
                    title="Filters are defined using comma delimited strings as in 'US,FR,Other'"></i>
                <span class="collapser"> [-]</span>
              </legend>
              <div class="fieldset_content">
                <div id="flt0" style="display: none;">
                  <span class="">{{ form.flt_col_0(class_="form-control inc") }}</span>
                  <div class="row">
                    <span class="col col-sm-4">{{ form.flt_op_0(class_="form-control inc") }}</span>
                    <span class="col col-sm-6">{{ form.flt_eq_0(class_="form-control inc") }}</span>
                    <button type="button" class="btn btn-default btn-sm remove" aria-label="Delete filter">
                      <span class="fa fa-minus"  aria-hidden="true"></span>

                    </button>
                  </div>
                </div>
                <div id="filters"></div>
                <button type="button" id="plus" class="btn btn-default btn-sm" aria-label="Add a filter">
                  <span class="fa fa-plus" aria-hidden="true"></span>
                  <span>Add filter</span>
                </button>
              </div>
            </fieldset>
            {{ form.slice_id() }}
            {{ form.slice_name() }}
            {{ form.collapsed_fieldsets() }}
            <input type="hidden" name="action" id="action" value="">
            <input type="hidden" name="datasource_name" value="{{ datasource.name }}">
            <input type="hidden" name="datasource_id" value="{{ datasource.id }}">
            <input type="hidden" name="datasource_type" value="{{ datasource.type }}">
            <input type="hidden" name="previous_viz_type" value="{{ viz.viz_type or "table" }}">
        </div>

        <div class="col-offset">
          {% block messages %}{% endblock %}
          {% include 'appbuilder/flash.html' %}
          <div
            id="{{ viz.token }}"
            class="widget viz slice {{ viz.viz_type }}"
            data-slice="{{ viz.json_data }}"
            style="height: 700px;">
            <img src="{{ url_for("static", filename="img/loading.gif") }}" class="loading" alt="loading">
            <div id="{{ viz.token }}_con" class="slice_container" style="height: 100%; width: 100%"></div>
          </div>
        </div>
      <div class="modal fade" id="query_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
              <h4 class="modal-title">Query</h4>
            </div>
            <div class="modal-body">
              <pre id="query_container"></pre>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
          </div>
        </div>
      </div>
      <div class="modal fade" id="sourceinfo_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog modal-lg" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
              <h4 class="modal-title">Datasource Description</h4>
            </div>
            <div class="modal-body">
              {{ datasource.description_markeddown | safe }}
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
          </div>
        </div>
      </div>
    </form>
  </div>
{% endblock %}

{% block tail_js %}
  {{ super() }}
  <script src="/static/assets/javascripts/dist/explore.entry.js"></script>
{% endblock %}
