{% 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) -%}
{%- set nbspval = "&nbsp;"|safe -%}
<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="{{ h.lang() }}"
      data-languagefile="{{ h.url_for_static('vendor/DataTables/i18n/' + h.lang() + '.json') }}"
      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-resource-url="{{ h.url_for('resource.read', id=package.name, resource_id=resource.id ) }}">
    <thead>
      <tr>
        <th class="all" data-name="_id">_id</th>
        {% for field in datadictionary -%}
          {% if 'show_fields' not in resource_view or field.id in resource_view.show_fields -%}
            <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>
          {%- endif %}
        {% endfor -%}
       <th id="_colspacer">colspacer</th>
      </tr>
      <tr>
        <th><button id="refit-button" class="btn btn-default" title="{{- _('Refit') -}}" onclick="fitColText()"><i class="fa fa-text-width"></i></button></th>
        {% for field in datadictionary -%}
          {% if 'show_fields' not in resource_view or field.id in resource_view.show_fields -%}
            <th id="cdx{{ loop.index }}" class="fhead" data-type="{{ field.type }}">
                {{- field.id -}}
            </th>
          {% endif -%}
        {% 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|int != 0 -%}
   ( {{ h.SI_number_span(res.size)|striptags }} )
  {%- endif -%}
</div>
{%- endblock -%}
{%- block styles -%}
  {{- super() -}}
  {% asset 'ckanext-datatablesview/main-css' %}
  {% asset 'ckanext-datatablesview/main-js' %}
{% endblock %}
{% block custom_styles %}{% endblock %}
