{% import 'macros/form.html' as form %}

<div id="view-{{ resource_view['id'] }}" class="resource-view" data-id="{{ resource_view['id'] }}" data-title="{{ resource_view['title'] }}" data-description="{{ resource_view['descripion'] }}">
  <a class="btn pull-right"
     href="#embed-{{ resource_view['id'] }}"
     data-module="resource-view-embed"
     data-module-id="{{ resource_view['id'] }}"
     data-module-url="{{ h.url('resource_view', id=package['name'], resource_id=resource['id'], view_id=resource_view['id'], qualified=True) }}">
    <i class="icon-code"></i>
    {{ _("Embed") }}
  </a>
  <p class="desc">{{ resource_view['description'] }}</p>
  <div class="m-top ckanext-datapreview">
    {% if not to_preview and h.resource_view_is_filterable(resource_view) %}
      {% snippet 'package/snippets/resource_view_filters.html', resource=resource %}
    {% endif %}
    {% if not h.resource_view_is_iframed(resource_view) %}
      {{ h.rendered_resource_view(resource_view, resource, package) }}
    {% else %}
      <div class="data-viewer-error js-hide">
        <p class="text-error">
          <i class="icon-info-sign"></i>
          {{ _('This resource view is not available at the moment.') }}
          <a href="#" data-toggle="collapse" data-target="#data-view-error">
            {{ _('Click here for more information.') }}
          </a>
        </p>
        <p id="data-view-error" class="collapse"></p>
        <p>
          <a href="{{ resource.url }}" class="btn btn-large resource-url-analytics" target="_blank">
            <i class="icon-large icon-download"></i>
            {{ _('Download resource') }}
          </a>
        </p>
      </div>
      {% if not to_preview %}
        {% set current_filters = request.str_GET.get('filters') %}
        {% if current_filters %}
          {% set src = h.url(qualified=true, controller='package',
                             action='resource_view', id=package['name'],
                             resource_id=resource['id'],
                             view_id=resource_view['id'],
                             filters=current_filters)  %}
        {% else %}
          {% set src = h.url(qualified=true, controller='package',
                             action='resource_view', id=package['name'],
                             resource_id=resource['id'],
                             view_id=resource_view['id'])  %}
        {% endif %}
      {% else %}
        {# When previewing we need to stick the whole resource_view as a param as there is no other way to pass to information on to the iframe #}
        {% set src = h.url(qualified=true, controller='package', action='resource_view', id=package['name'], resource_id=resource['id']) + '?' + h.urlencode({'resource_view': h.dump_json(resource_view)}) %}
      {% endif %}
      <iframe src="{{ src }}" frameborder="0" width="100%" data-module="data-viewer">
        <p>{{ _('Your browser does not support iframes.') }}</p>
      </iframe>
    {% endif %}
  </div>
  <div id="embed-{{ resource_view['id'] }}" class="modal resource-view-embed hide">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h3>{{ _("Embed resource view") }}</h3>
    </div>
    <div class="modal-body">
      <p class="embed-content">{{ _("You can copy and paste the embed code into a CMS or blog software that supports raw HTML") }}</p>
      <div class="row-fluid">
        <div class="span6">
          {{ form.input("width", label=_("Width"), value=700, classes=["control-full"]) }}
        </div>
        <div class="span6">
          {{ form.input("height", label=_("Height"), value=400, classes=["control-full"]) }}
        </div>
      </div>
      {{ form.textarea("code", label=_("Code"), value="", classes=["pre"], rows=3) }}
    </div>
  </div>
</div>
