{% extends "admin/import_export/base.html" %}
{% load i18n admin_kubi_tags %}
{% load admin_urls %}
{% load import_export_tags %}
{% load static %}

{% block extrahead %}{{ block.super }}
  <script type="text/javascript" src="{% url 'admin:jsi18n' %}"></script>
  {% if confirm_form %}
    {{ confirm_form.media }}
  {% else %}
    {{ form.media }}
  {% endif %}
{% endblock %}

{% block breadcrumbs_last %}
{% trans "Import" %}
{% endblock %}

{% block content %}

<div class="container-fluid">
    <div class="row align-items-stretch">
      <div class="col">

  {% if confirm_form %}
    {% block confirm_import_form %}
    <form action="{% url opts|admin_urlname:"process_import" %}" method="POST" class="mt-3">
      {% csrf_token %}
      {{ confirm_form.as_p }}
      <div class="alert alert-warning">
        <div class="row">
            <div class="col">
                {% trans "Below is a preview of data to be imported. If you are satisfied with the results, click 'Confirm import'" %}
            </div>
            <div class="col col-sm">
                <input type="submit" class="btn btn-success float-end ml-4 mt-1" name="confirm" value="{% trans "Confirm import" %}">
            </div>
        </div>
    </div>
    </form>
    {% endblock %}
  {% else %}
    {% block import_form %}
    <div class="row justify-content-center">
    <form action="" method="post" enctype="multipart/form-data" class="col col-md-6 col-lg-5 mt-5">
      {% csrf_token %}

      <p class="alert alert-info">
        {% trans "This importer will import the following fields: " %}
        {% if fields_list|length <= 1 %}
           <code>{{ fields_list.0.1|join:", " }}</code>
        {% else %}
           <dl>
              {% for resource, fields in fields_list %}
                  <dt>{{ resource }}</dt>
                  <dd><code>{{ fields|join:", " }}</code></dd>
              {% endfor %}
           </dl>
        {% endif %}
      </p>

      <fieldset class="module aligned">
        {% for field in form %}
          <div class="mb-3">
            {{ field|label_tag }}
            {% if field|widget_type == 'select' %}
                {{ field|add_class:"form-select" }}
            {% else %}
                {{ field|add_class:"form-control" }}
            {% endif %}
            {% for error in field.field.errors %}
                <div class="form-error">{{ error }}</div>
            {% endfor %}

            {% if field.field.help_text %}
                <div class="help form-text text-muted">
                {{ field.field.help_text|safe }}
                </div>
            {% endif %}
          </div>
        {% endfor %}
      </fieldset>

      <div class="mt-3">
        <input type="submit" class="btn btn-lg btn-primary w-100" value="{% trans "Submit" %}">
      </div>
    </form>
    </div>
    {% endblock %}
  {% endif %}

  {% if result %}

    {% if result.has_errors %}
    {% block errors %}
      <h4>{% trans "Errors" %}</h2>
      <ol class="list-group list-group-numbered">
        {% for error in result.base_errors %}
        <li class="list-group-item d-flex justify-content-between align-items-start list-group-item-danger">
          {{ error.error }}
          <div class="traceback">{{ error.traceback|linebreaks }}</div>
        </li>
        {% endfor %}
        {% for line, errors in result.row_errors %}
          {% for error in errors %}
            <li class="list-group-item d-flex justify-content-between align-items-start list-group-item-warning">
                <div class="ms-2 me-auto">
                    <div class="fw-bold">{% trans "Line number" %}: {{ line }} - {{ error.error }}</div>
                    <code>{{ error.row.values|join:", " }}</code>
                </div>
                <div class="collapse" id="col{{ line }}">
                    <code class="card card-body border-danger text-danger traceback">{{ error.traceback|linebreaks }}</code>
                </div>
                <button class="btn btn-secondary btn-sm ms-2" type="button" data-bs-toggle="collapse" data-bs-target="#col{{ line }}" aria-expanded="false">
                    traceback
                </button>
            </li>
          {% endfor %}
        {% endfor %}
      </ol>
    {% endblock %}

    {% elif result.has_validation_errors %}

    {% block validation_errors %}
      <h4>{% trans "Some rows failed to validate" %}</h2>

      <p>{% trans "Please correct these errors in your data where possible, then reupload it using the form above." %}</p>

      <table class="import-preview">
        <thead>
          <tr>
            <th>{% trans "Row" %}</th>
            <th>{% trans "Errors" %}</th>
            {% for field in result.diff_headers %}
              <th>{{ field }}</th>
            {% endfor %}
          </tr>
        </thead>
        <tbody>
        {% for row in result.invalid_rows %}
          <tr>
            <td>{{ row.number }} </td>
            <td class="errors">
              <span class="validation-error-count">{{ row.error_count }}</span>
              <div class="validation-error-container">
                <ul class="validation-error-list">
                  {% for field_name, error_list in row.field_specific_errors.items %}
                    <li>
                        <span class="validation-error-field-label">{{ field_name }}</span>
                        <ul>
                          {% for error in error_list %}
                            <li>{{ error }}</li>
                          {% endfor %}
                        </ul>
                    </li>
                  {% endfor %}
                  {% if row.non_field_specific_errors %}
                    <li>
                      <span class="validation-error-field-label">{% trans "Non field specific" %}</span>
                      <ul>
                        {% for error in row.non_field_specific_errors %}
                          <li>{{ error }}</li>
                        {% endfor %}
                      </ul>
                    </li>
                  {% endif %}
                </ul>
              </div>
            </td>
            {% for field in row.values %}
              <td>{{ field }}</td>
            {% endfor %}
          </tr>
        {% endfor %}
        </tbody>
      </table>
      {% endblock %}

    {% else %}

      {% block preview %}
      <h4>{% trans "Preview" %}</h4>

      <table class="table table-striped table-bordered import-preview">
        <thead>
          <tr>
            <th></th>
            {% for field in result.diff_headers %}
              <th>{{ field }}</th>
            {% endfor %}
          </tr>
        </thead>
        {% for row in result.valid_rows %}
          <tr class="{% if row.import_type == 'new' %}table-success{% elif row.import_type == 'skip' %}table-warning{% elif row.import_type == 'delete' %}table-danger{% elif row.import_type == 'update' %}table-info{% endif %}">
            <td class="import-type">
              {% if row.import_type == 'new' %}
                {% trans "New" %}
              {% elif row.import_type == 'skip' %}
                {% trans "Skipped" %}
              {% elif row.import_type == 'delete' %}
                {% trans "Delete" %}
              {% elif row.import_type == 'update' %}
                {% trans "Update" %}
              {% endif %}
            </td>
            {% for field in row.diff %}
              <td>{{ field }}</td>
            {% endfor %}
          </tr>
        {% endfor %}
      </table>
      {% endblock %}

    {% endif %}

  {% endif %}

      <div class="mt-5"></div>
    </div>
  </div>
</div>
{% endblock %}