{% 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(controller='ckanext.datapusher.plugin:ResourceDataController', action='resource_data', id=pkg.name, resource_id=res.id) %}
  {% set show_table = true %}

  <form method="post" action="{{ action }}" class="datapusher-form">
    <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-error">
      <strong>{{ _('Upload error:') }}</strong> {{ status.error.message }}
    </div>
  {% elif status.task_info and status.task_info.error %}
    <div class="alert alert-error">
      {% 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.iteritems() %}
          {% 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="activity">
      {% for item in status.task_info.logs|sort(attribute='timestamp') %}
        {% set icon = 'ok' if item.level == 'INFO' else 'exclamation' %}
        {% set class = ' failure' if icon == 'exclamation' else ' success' %}
        {% set popover_content = 'test' %}
        <li class="item no-avatar{{ class }}">
          <i class="fa icon fa-{{ icon }}"></i>
          <p>
            {% 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-target="popover" data-content="<dl>{% for key, value in item.iteritems() %}<dt>{{ key }}</dt><dd>{{ h.clean_html(value|string) }}</dd>{% endfor %}</dl>" data-html="true">{{ _('Details') }}</a>
            </span>
          </p>
        </li>
      {% endfor %}
      <li class="item no-avatar">
        <i class="fa icon fa-info"></i>
        <p class="muted">{{ _('End of log') }}</p>
      </li>
    </ul>
  {% endif %}

{% endblock %}
