{% extends 'layout.html' %}
{% load i18n %}
{% load humanize %}

{% block title %}{% trans "Import Books" %}{% endblock %}

{% block content %}
<div class="block">
    <h1 class="title">{% trans "Import Books" %}</h1>

    {% if invalid %}
    <div class="notification is-danger">
        {% trans "Not a valid CSV file" %}
    </div>
    {% endif %}

    {% if site.imports_enabled %}
        {% if import_size_limit and import_limit_reset %}
            <div class="notification">
                <p>{% blocktrans %}Currently you are allowed to import {{ import_size_limit }} books every {{ import_limit_reset }} days.{% endblocktrans %}</p>
                <p>{% blocktrans %}You have {{ allowed_imports }} left.{% endblocktrans %}</p>
            </div>
        {% endif %}
        {% if recent_avg_hours or recent_avg_minutes %}
        <div class="notification">
            <p>
            {% if recent_avg_hours %}
                {% blocktrans trimmed with hours=recent_avg_hours|floatformat:0|intcomma %}
                    On average, recent imports have taken {{ hours }} hours.
                {% endblocktrans %}
            {% else %}
                {% blocktrans trimmed with minutes=recent_avg_minutes|floatformat:0|intcomma %}
                    On average, recent imports have taken {{ minutes }} minutes.
                {% endblocktrans %}
            {% endif %}
            </p>
        </div>
        {% endif %}

        <form class="box" name="import" action="/import" method="post" enctype="multipart/form-data">
            {% csrf_token %}

            <div class="columns">
            <div class="column is-half">
                <div class="field">
                    <label class="label" for="source">
                        {% trans "Data source:" %}
                    </label>

                    <div class="select">
                        <select name="source" id="source" aria-describedby="desc_source">
                            <option value="Goodreads" {% if current == 'Goodreads' %}selected{% endif %}>
                                {% trans "Goodreads (CSV)" %}
                            </option>
                            <option value="Storygraph" {% if current == 'Storygraph' %}selected{% endif %}>
                                {% trans "Storygraph (CSV)" %}
                            </option>
                            <option value="LibraryThing" {% if current == 'LibraryThing' %}selected{% endif %}>
                            {% trans "LibraryThing (TSV)" %}
                            </option>
                            <option value="OpenLibrary" {% if current == 'OpenLibrary' %}selected{% endif %}>
                                {% trans "OpenLibrary (CSV)" %}
                            </option>
                            <option value="Calibre" {% if current == 'Calibre' %}selected{% endif %}>
                                {% trans "Calibre (CSV)" %}
                            </option>
                        </select>
                    </div>

                    <p class="help" id="desc_source">
                    {% blocktrans trimmed %}
                        You can download your Goodreads data from the
                        <a href="https://www.goodreads.com/review/import" target="_blank" rel="nofollow noopener noreferrer">Import/Export page</a>
                        of your Goodreads account.
                    {% endblocktrans %}
                    </p>
                </div>

                <div class="field">
                    <label class="label" for="id_csv_file">{% trans "Data file:" %}</label>
                    {{ import_form.csv_file }}
                </div>
            </div>

            <div class="column is-half">
                <div class="field">
                    <label class="label">
                        <input type="checkbox" name="include_reviews" checked> {% trans "Include reviews" %}
                    </label>
                </div>
                <div class="field">
                    <label class="label" for="privacy_import">
                        {% trans "Privacy setting for imported reviews:" %}
                    </label>
                    {% include 'snippets/privacy_select.html' with no_label=True privacy_uuid="import" %}
                </div>
            </div>
            </div>
            {% if not import_limit_reset and not import_size_limit or allowed_imports > 0 %}
                <button class="button is-primary" type="submit">{% trans "Import" %}</button>
            {% else %}
                <button class="button is-primary is-disabled" type="submit">{% trans "Import" %}</button>
                <p>{% trans "You've reached the import limit." %}</p>
            {% endif%}
        </form>
    {% else %}
    <div class="box notification has-text-centered is-warning m-6 content">
        <p class="mt-5">
            <span class="icon icon-warning is-size-2" aria-hidden="true"></span>
        </p>
        <p class="mb-5">
            {% trans "Imports are temporarily disabled; thank you for your patience." %}
        </p>
    </div>
    {% endif %}
</div>

<div class="content block">
    <h2 class="title">{% trans "Recent Imports" %}</h2>
    <div class="table-container">
        <table class="table is-striped is-fullwidth">
            <tr>
                <th>
                    {% trans "Date Created" %}
                </th>
                <th>
                    {% trans "Last Updated" %}
                </th>
                <th>
                    {% trans "Items" %}
                </th>
                <th>
                    {% trans "Status" %}
                </th>
            </tr>
            {% if not jobs %}
            <tr>
                <td colspan="4">
                    <em>{% trans "No recent imports" %}</em>
                </td>
            </tr>
            {% endif %}
            {% for job in jobs %}
            <tr>
                <td>
                    <a href="{% url 'import-status' job.id %}">{{ job.created_date }}</a>
                </td>
                <td>{{ job.updated_date }}</td>
                <td>{{ job.item_count|intcomma }}</td>
                <td>
                    <span
                        {% if job.status == "stopped" %}
                        class="tag is-danger"
                        {% elif job.status == "pending" %}
                        class="tag is-warning"
                        {% elif job.complete %}
                        class="tag"
                        {% else %}
                        class="tag is-success"
                        {% endif %}
                    >
                        {% if job.status %}
                        {{ job.status }}
                            {{ job.status_display }}
                        {% elif job.complete %}
                            {% trans "Complete" %}
                        {% else %}
                            {% trans "Active" %}
                        {% endif %}
                    </span>
                </td>
            </tr>
            {% endfor %}
        </table>
    </div>

    {% include 'snippets/pagination.html' with page=jobs path=request.path %}
</div>
{% endblock %}
