{% extends "page.html" %}

{% block title %}
  {{ _('DOI') }}: {{ query_doi.doi }}
{% endblock %}

{%- block meta -%}
  {{ super() }}
    <meta name="citation_doi" content="doi:https://doi.org/{{ query_doi.doi }}"/>
    <meta name="citation_title"
          content="Data Portal Query on {{ resource_count }} resources, {{ query_doi.count }} records"/>
    <meta name="citation_author" content="{{ ', '.join(authors) }}"/>
    <meta name="citation_online_date" content="{{ query_doi.timestamp }}"/>
    <meta name="citation_publisher" content="{{ publisher }}"/>
{%- endblock -%}

{% block primary_content %}
  {% asset 'ckanext-query-dois/main' %}
    <article class="module">
        <div class="module-content">
            <h1 class="page-heading">{{ _('DOI') }}: {{ query_doi.doi }}</h1>

            {% block doi_warnings %}
            {% if warnings|length > 0 %}
            <div class="alert-warning qd_warning_banner">
                {% for warning in warnings %}
                <p>{{ warning }}</p>
                {% endfor %}
            </div>
            {% endif %}
            {% endblock %}

          {% block visual_query %}
            {% snippet "query_dois/snippets/visual_datastore_multisearch.html", query=query_doi.query %}
              <br/>
          {% endblock %}

          {% block citation %}
              <h3>{{ _('Cite this as') }}</h3>
              <div class="qd_block">
                  <p>
                    {{ h.create_multisearch_citation_text(query_doi, html=True) | safe }}
                  </p>
              </div>
          {% endblock %}

          {% block details %}
              <h3>{{ _('Details') }}</h3>
              <div class="qd_block qd_details">
                  <div>
                      <b>{{ _('Resource count:') }}</b> {{ details['resource_count'] }}
                  </div>
                  <div>
                      <b>{{ _('Dataset count:') }}</b> {{ details['package_count'] }}
                  </div>
                  <div>
                      <b>{{ _('Total records:') }}</b> {{ details['record_count'] }}
                  </div>
                  <div>
                      <b>{{ _('Retrieved:') }}</b> {{ query_doi.timestamp }}
                  </div>
                  {% if has_changed %}
                  <div>
                      <b>{{ _('Resource count at save time:') }}</b> {{ saved_details['resource_count'] }}
                  </div>
                  <div>
                      <b>{{ _('Total records at save time:') }}</b> {{ saved_details['record_count'] }}
                  </div>
                  <div>
                      <b>{{ _('Missing resources:') }}</b> {{ saved_details['missing_resources'] }}
                  </div>
                  <div>
                      <b>{{ _('Missing records:') }}</b> {{ saved_details['missing_records'] }}
                  </div>
                  {% endif %}
              </div>
          {% endblock %}

          {% block stats %}
              <h3>{{ _('Statistics') }}</h3>
              <div class=" qd_block qd_stats">
                  <div>
                      <b>{{ _('Total downloads:') }}</b> {{ usage_stats['downloads'] }}
                  </div>
                  <div>
                      <b>{{ _('Total saves:') }}</b> {{ usage_stats['saves'] }}
                  </div>
                  <div>
                      <b>{{ _('Last downloaded:') }}</b> {{ usage_stats['last_download_timestamp'] or _('Never') }}
                  </div>
              </div>
          {% endblock %}
          {% block resource_breakdown %}
            {% if not is_inaccessible %}
              <h3>{{ _('Resource breakdown') }}</h3>
              <div class="qd_block">
                  <table class="qd_breakdown">
                    {% for resource_id, count in details['sorted_resource_counts'] %}
                      {% set resource_info = resources[resource_id] %}
                      {% set package_info = packages[resource_info['package_id']] %}
                      {% set resource_url = h.url_for('resource.read', id=package_info['name'], resource_id=resource_id) %}
                      {% set package_url = h.url_for('dataset.read', id=package_info['name']) %}
                        <tr>
                            <td><a href="{{ package_url }}">{{ package_info['title'] }}</a> / <a
                                    href="{{ resource_url }}">{{ resource_info['name'] }}</a></td>
                            <td><b>{{ count }} {{ _('records') }}</b></td>
                        </tr>
                    {% endfor %}
                  </table>
              </div>
            {% endif %}
            {% if has_changed %}
            <h3>{{ _('Missing data') }}</h3>
              <div class="qd_block">
                  <table class="qd_breakdown">
                    {% for resource in inaccessible_resources %}
                      {% if not resource['id'] %}
                        <tr>
                            <td>{{ resource['package_title'] }} / {{ resource['name'] }}</td>
                            <td><b>{{ resource['record_count'] }} {{ _('records') }}</b></td>
                        </tr>
                      {% else %}
                        {% set resource_url = h.url_for('resource.read', id=resource['package_name'], resource_id=resource['id']) %}
                        {% set package_url = h.url_for('dataset.read', id=resource['package_name']) %}
                        <tr>
                            <td><a href="{{ package_url }}">{{ resource['package_title'] }}</a> / <a
                                    href="{{ resource_url }}">{{ resource['name'] }}</a></td>
                            <td><b>{{ resource['record_count'] }} {{ _('records') }}</b></td>
                        </tr>
                      {% endif %}
                    {% endfor %}
                  </table>
              </div>
            {% endif %}
          {% endblock %}
        </div>
    </article>
{% endblock %}


{% block secondary_content %}
{% if is_inaccessible %}
    <div class="module, module-narrow module-shallow">
        <h2 class="module-heading">
            <i class="fas fa-times fa-lg inline-icon-left"></i>{{ _('Data missing') }}
        </h2>
        <div class="module-content">
            <div class="qd_help_text">
              {% trans %}
                  All data associated with this DOI have been deleted, moved, or are no longer available.
              {% endtrans %}
            </div>
        </div>
    </div>
{% else %}
    <div class="module module-narrow module-shallow">
        <h2 class="module-heading">
            <i class="fas fa-eye fa-lg inline-icon-left"></i>{{ _('View data') }}
        </h2>
        <div class="module-content">
            <a target="_blank" href="/search/{{ original_slug }}">{{ _('Original') }} <i class="fas fa-arrow-circle-right"></i></a>
            <div class="qd_help_text">
              {% trans %}
                  View the data as it looked when the DOI was originally minted.
              {% endtrans %}
            </div>
        </div>
        <div class="module-content">
            <a target="_blank" href="/search/{{ current_slug }}">{{ _('Current') }} <i class="fas fa-arrow-circle-right"></i></a>
            <div class="qd_help_text">
              {% trans %}
                  View the data as it looks now. This may result in different results to the DOI's
                  original data if the data has been modified since it was minted.
              {% endtrans %}
            </div>
        </div>
    </div>

    {% if h.versioned_datastore_available %}
    <div class="module, module-narrow module-shallow">
        <h2 class="module-heading">
            <i class="far fa-arrow-alt-circle-down fa-lg inline-icon-left"></i>{{ _('Download') }}
        </h2>
        <div class="module-content">
            {% snippet 'versioned_datastore/snippets/download_button.html',
            slug_or_doi=query_doi.doi,
            icon_class="fas fa-download",
            label=_('Download') %}
        </div>
    </div>
    {% endif %}
{% endif %}
{% endblock %}
