{% extends "package/resource_edit_base.html" %}

{% block subtitle %}{{ h.dataset_display_name(pkg) }} - {{ h.resource_display_name(res) }}{% endblock %}

{% block primary_content_inner %}

  {% set action = h.url_for('datapusher.resource_data', id=pkg.name, resource_id=res.id) %}
  {% set delete_action = h.url_for('datapusher.delete_datastore_table', id=pkg.id, resource_id=res.id) %}
  {% set show_table = true %}

  <form method="post" action="{{ delete_action }}" class="mb-3 d-inline-block">
    {{ h.csrf_input() }}
    <button class="btn btn-danger" name="delete" type="submit"
      data-module="confirm-action"
      data-module-with-data=true
      data-module-content="{{ _('Are you sure you want to delete the DataStore and Data Dictionary?') }}">
      <i class="fa fa-remove"></i> {{ _('Delete from DataStore') }}
    </button>
  </form>

  <form method="post" action="{{ action }}" class="datapusher-form mb-3 d-inline-block">
    {{ h.csrf_input() }}
    <button class="btn btn-primary" name="save" type="submit">
      <i class="fa fa-cloud-upload"></i> {{ _('Upload to DataStore') }}
    </button>
  </form>

  {% if status.error and status.error.message %}
    {% set show_table = false %}
    <div class="alert alert-danger">
      <strong>{{ _('Upload error:') }}</strong> {{ status.error.message }}
    </div>
  {% elif status.task_info and status.task_info.error %}
    <div class="alert alert-danger">
      {% if status.task_info.error is string %}
        {# DataPusher < 0.0.3 #}
        <strong>{{ _('Error:') }}</strong> {{ status.task_info.error }}
      {% elif status.task_info.error is mapping %}
        <strong>{{ _('Error:') }}</strong> {{ status.task_info.error.message }}
        {% for error_key, error_value in status.task_info.error.items() %}
          {% if error_key != "message" and error_value %}
            <br>
            <strong>{{ error_key }}</strong>:
            {{ error_value }}
          {% endif %}
        {% endfor %}
      {% elif status.task_info.error is iterable %}
        <strong>{{ _('Error traceback:') }}</strong>
        <pre>{{ ''.join(status.task_info.error) }}</pre>
      {% endif %}
    </div>
  {% endif %}

  <table class="table table-bordered">
    <colgroup>
      <col width="150">
      <col>
    </colgroup>
    <tr>
      <th>{{ _('Status') }}</th>
      <td>{{ h.datapusher_status_description(status) }}</td>
    </tr>
    <tr>
      <th>{{ _('Last updated') }}</th>
      {% if status.status %}
        <td><span class="date" title="{{ h.render_datetime(status.last_updated, with_hours=True) }}">{{ h.time_ago_from_timestamp(status.last_updated) }}</span></td>
      {% else %}
        <td>{{ _('Never') }}</td>
      {% endif %}
    </tr>
  </table>

  {% if status.status and status.task_info and show_table %}
    <h3>{{ _('Upload Log') }}</h3>
    <ul class="datapusher-activity">
      {% for item in status.task_info.logs|sort(attribute='timestamp') %}
        {% set icon = 'check' if item.level == 'INFO' else 'exclamation' %}
        {% set class = ' failure' if icon == 'exclamation' else ' success' %}
        {% set popover_content = 'test' %}
        <li class="item no-avatar{{ class }}">
          <span class="fa-stack fa-lg">
            <i class="fa fa-circle fa-stack-2x icon"></i>
            <i class="fa fa-{{ icon }} fa-stack-1x fa-inverse"></i>
          </span>
          {% for line in item.message.strip().split('\n') %}
            {{ line | urlize }}<br />
          {% endfor %}
          <span class="date" title="{{ h.render_datetime(item.timestamp, with_hours=True) }}">
            {{ h.time_ago_from_timestamp(item.timestamp) }}
            <a href="#" data-module="datapusher_popover" data-module-title="Details <a href='#' class='popover-close'>X</a>" data-module-content="<ul class='list-unstyled'>{% for key, value in item.items() %}<li><b>{{ key }}</b>: {{ h.clean_html(value|string) }}</li>{% endfor %}</ul>">
              {{ _('Details') }}
            </a>
          </span>
        </li>
      {% endfor %}
      <li class="item no-avatar">
        <span class="fa-stack fa-lg">
          <i class="fa fa-circle fa-stack-2x icon"></i>
          <i class="fa fa-info fa-stack-1x fa-inverse"></i>
        </span>
        {{ _('End of log') }}
      </li>
    </ul>
  {% endif %}

{% endblock %}
