{% extends "base.html" %}

{% block title %}{{ h.dataset_display_name(package)}}&mdash;{{ h.resource_display_name(resource)}} - {{ super() }}{% endblock %}
{% block bodytag %}
  {{- super() -}}
  class="dt-view"
{%- endblock -%}

{%- block page -%}
{#- pass the datadictionary to javascript, so we can init columns there -#}
{%- set datadictionary = h.datastore_dictionary(resource.id, resource_view.get('show_fields')) -%}
{%- set nbspval = "&nbsp;"|safe -%}
{%- set current_language = h.lang() -%}

<script type=text/javascript>
    const gdataDict = {{ datadictionary|tojson }}
    const gresviewId = '{{ resource_view.id }}'
</script>

<div id="resize_wrapper">
  <table id="dtprv" width="100%"
      class="table table-striped table-bordered table-condensed table-hover"
      data-module="datatables_view"
      data-state-save-flag="{{ state_saving|lower }}"
      data-state-duration="{{ state_duration }}"
      data-ellipsis-length="{{ resource_view.ellipsis_length if resource_view.ellipsis_length is defined else ellipsis_length }}"
      data-date-format="{{ resource_view.date_format  if resource_view.date_format is defined else date_format }}"
      data-package-name="{{ package.name }}"
      data-resource-name="{{ h.resource_display_name(resource) }}"
      data-languagecode="{{ current_language }}"
      data-languagefile="{{ h.datatablesview_get_language_file_path(current_language) }}"
      data-ajaxurl="{{ h.url_for('datatablesview.ajax', resource_view_id=resource_view.id) }}"
      data-ckanfilters="{{ request.args.get('filters', '')|e }}"
      data-responsive-flag="{{ resource_view.get('responsive')|lower }}"
      data-page-length-choices="{{ page_length_choices }}"
      data-responsive-modal="{{ responsive_modal|lower }}"
      data-resource-url="{{ h.url_for(package.type ~ '_resource.read', id=package.name, resource_id=resource.id ) }}">
    <thead>
      <tr>
        <th class="all" data-name="_id">_id</th>
        {% for field in datadictionary -%}
          <th scope="col">
          {%- if data_dictionary_labels and field.info is defined and field.info.label|length -%}
            {{ field.info.label|replace(" ", nbspval) }}
          {%- else -%}
            {{ field.id|replace(" ", nbspval) }}
          {%- endif -%}
          &nbsp;
          {%- if data_dictionary_labels and field.info is defined and (field.info.label|length or field.info.notes|length)-%}
            <i class="fa fa-info-circle" title="{{field.id}} ({{field.type}})&#10;{{ h.markdown_extract(field.info.notes, 300) }}"></i>
          {%- endif -%}
          &nbsp;</th>
        {% endfor -%}
       <th id="_colspacer">colspacer</th>
      </tr>
      <tr>
        <th><button id="refit-button" class="btn btn-secondary" title="{{- _('Refit') -}}" onclick="fitColText()"><i class="fa fa-text-width"></i></button></th>
        {% for field in datadictionary -%}
          <th id="cdx{{ loop.index }}" class="fhead" data-type="{{ field.type }}">
              {{- field.id -}}
          </th>
        {% endfor -%}
       <th id="_colspacerfilter" class="none"></th>
      </tr>
    </thead>
    <tbody>
    </tbody>
  </table>
  <form id="filtered-datatables-download" method="POST" action="{{ h.url_for(
                                                                  'datatablesview.filtered_download',
                                                                  resource_view_id=resource_view.id) -}}">
    {{ h.csrf_input() }}
    <input type="hidden" name="filters" value="{{ request.args.get('filters', '')|e -}}" />
  </form>
</div>

{#- we do this macro instead of the snippet because the snippet pollutes the output with comments/whitespaces which #}
{# may be invisible for regular html, but not for tooltips -#}
{%- macro local_friendly_datetime(dt_obj) -%}
   <span class="automatic-local-datetime" data-datetime="{{ h.render_datetime(dt_obj, date_format='%Y-%m-%dT%H:%M:%S%z') }}">
    {{- h.render_datetime(dt_obj, with_hours=True) -}}
   </span>
{%- endmacro -%}

{#- we create tooltip here instead of javascript so we can leverage the automatic-local-datetime class date conversion CKAN does -#}
{%- set res = resource %}
<div id="dtv-resource-info" style="display: none;">
  {{- _('Data last updated') }}: {{ local_friendly_datetime(res.last_modified) }}&#10;
  {{- _('Metadata last updated') }}: {{ local_friendly_datetime(res.metadata_modified) }}&#10;
  {{- _('Created') }}: {{ local_friendly_datetime(res.created) }}&#10;
  {{- res.format or res.mimetype_inner or res.mimetype or _('unknown') -}}&nbsp;
  {%- if res.size and res.size|int != 0 -%}
   ( {{ h.localised_filesize(res.size)}} )
  {%- endif -%}
</div>
{%- endblock -%}
{%- block styles -%}
  {{- super() -}}
  {% asset 'ckanext-datatablesview/main-css' %}
  {% asset 'ckanext-datatablesview/main-js' %}
{% endblock %}
{% block custom_styles %}{% endblock %}
