{% extends "admin/import_export/base.html" %}
{% load url from future %}
{% load i18n admin_urls import_export_tags %}

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

{% block content %}

    {% if not confirm_form %}
        <form action="{{ form_url }}" method="POST" id="{{ opts.module_name }}_form" enctype="multipart/form-data" class="form-horizontal">
            {% csrf_token %}


            <div class="alert alert-info">
                {% trans "This importer will import the following fields: " %}
                {% for f in fields %}
                    <code>{{ f }}</code>
                {% endfor %}
            </div>

            {% include 'suit/form_as_fieldset.html' %}
            <div class="submit-row submit-row-bottom">
                <input type="submit" value="{% trans "Import" %}" class="btn btn-info"/>
            </div>

        </form>
    {% endif %}

    {% if result %}
        {% if result.has_errors %}
            <div class="alert alert-error alert-block">
                <h4>{% trans "Errors" %}</h4>
                <ul>
                    {% for error in result.base_errors %}
                        <li>{{ error.error }}</li>
                    {% endfor %}
                    {% for line, errors in result.row_errors %}
                        {% for error in errors %}
                            <li>
                                {% trans "Line number" %}: {{ line }} - {{ error.error }}
                                <pre class="traceback">{{ error.traceback|linebreaks }}</pre>
                            </li>
                        {% endfor %}
                    {% endfor %}
                </ul>
            </div>
        {% else %}

            <h2>{% trans "Preview" %}</h2>
            <table class="table table-bordered table-hover table-condensed table-striped">
                <thead>
                <tr>
                    <th></th>
                    {% for field in fields %}
                        <th>{{ field }}</th>
                    {% endfor %}
                </tr>
                </thead>
                {% for row in result.rows %}
                    <tr>
                        <td>
                            {% 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>
        {% endif %}

    {% endif %}

    {% if confirm_form %}
        <form action="{% url opts|admin_urlname:"process_import" %}" method="POST">
            {% csrf_token %}
            {{ confirm_form.as_p }}
            <input class="btn btn-success" name="confirm" type="submit" value="{% trans "Confirm import" %}">
        </form>
    {% endif %}
{% endblock %}
