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

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

{% block content %}{% spaceless %}
<header class="block">
    <h1 class="title">
        {% block page_title %}
        {% if job.retry %}
            {% trans "Retry Status" %}
        {% else %}
            {% trans "Import Status" %}
        {% endif %}
        {% endblock %}
    </h1>

    <nav class="breadcrumb subtitle" aria-label="breadcrumbs">
        <ul>
            <li><a href="{% url 'import' %}">{% trans "Imports" %}</a></li>
            {% url 'import-status' job.id as path %}
            <li{% if request.path in path %} class="is-active"{% endif %}>
                <a href="{{ path }}" {% if request.path in path %}aria-current="page"{% endif %}>
                    {% if job.retry %}
                    {% trans "Retry Status" %}
                    {% else %}
                    {% trans "Import Status" %}
                    {% endif %}
                </a>
            </li>
            {% block breadcrumbs %}{% endblock %}
        </ul>
    </nav>

    <div class="block">
        <dl>
            <dt class="is-pulled-left mr-5">{% trans "Import started:" %}</dt>
            <dd>{{ job.created_date | naturaltime }}</dd>
        </dl>
    </div>

    {% if job.status == "active" and show_progress %}
    <div class="box is-processing">
        <div class="block">
            <span class="icon icon-spinner is-pulled-left" aria-hidden="true"></span>
            <span>{% trans "In progress" %}</span>
            <span class="is-pulled-right">
                <a href="{% url 'import-status' job.id %}" class="button is-small">{% trans "Refresh" %}</a>
            </span>
        </div>
        <div class="is-flex">
            <progress
                class="progress is-success is-medium mr-2"
                role="progressbar"
                aria-min="0"
                value="{{ complete_count }}"
                aria-valuenow="{{ complete_count }}"
                max="{{ item_count }}"
                aria-valuemax="{{ item_count }}">
                {{ percent }} %
            </progress>
            <span>{{ percent }}%</span>
        </div>
    </div>
    {% endif %}

    {% if not job.complete %}
    <form name="stop-import" action="{% url 'import-stop' job.id %}" method="POST">
        {% csrf_token %}
        <button class="button is-danger" type="submit">{% trans "Stop import" %}</button>
    </form>
    {% endif %}

    {% if manual_review_count and not legacy %}
    <div class="notification">
        {% blocktrans trimmed count counter=manual_review_count with display_counter=manual_review_count|intcomma %}
            {{ display_counter }} item needs manual approval.
            {% plural %}
            {{ display_counter }} items need manual approval.
        {% endblocktrans %}
        <a href="{% url 'import-review' job.id %}">{% trans "Review items" %}</a>
    </div>
    {% endif %}

    {% if job.complete and fail_count and not job.retry and not legacy %}
    <div class="notification is-warning">
        {% blocktrans trimmed count counter=fail_count with display_counter=fail_count|intcomma %}
            {{ display_counter }} item failed to import.
            {% plural %}
            {{ display_counter }} items failed to import.
        {% endblocktrans %}
        <a href="{% url 'import-troubleshoot' job.id %}">
            {% trans "View and troubleshoot failed items" %}
        </a>
    </div>
    {% endif %}
</header>

<div class="block">
    {% block actions %}{% endblock %}
    <div class="table-container">
        <table class="table is-striped is-fullwidth">
            <tr>
                <th>
                    {% trans "Row" %}
                </th>
                <th>
                    {% trans "Title" %}
                </th>
                <th>
                    {% trans "ISBN" %}
                </th>
                {% if job.source == "OpenLibrary" %}
                <th>
                    {% trans "Openlibrary key" %}
                </th>
                {% endif %}
                <th>
                    {% trans "Author" %}
                </th>
                <th>
                    {% trans "Shelf" %}
                </th>
                <th>
                    {% trans "Review" %}
                </th>
                {% block import_cols_headers %}
                <th>
                    {% trans "Book" %}
                </th>
                <th>
                    {% trans "Status" %}
                </th>
                {% endblock %}
            </tr>
            {% if legacy %}
            <tr>
                <td colspan="8">
                    <p>
                        <em>{% trans "Import preview unavailable." %}</em>
                    </p>
                </td>
            </tr>
            {% else %}
            {% if not items %}
            <tr>
                <td colspan="6">
                    <em>{% trans "No items currently need review" %}</em>
                </td>
            </tr>
            {% endif %}
            {% for item in items %}
            <tr>
                {% block index_col %}
                <td>
                    {{ item.index }}
                </td>
                {% endblock %}
                <td>
                    {{ item.normalized_data.title }}
                </td>
                <td>
                    {{ item.isbn|default:'' }}
                </td>
                {% if job.source == "OpenLibrary" %}
                <td>
                    {{ item.openlibrary_key }}
                </td>
                {% endif %}
                <td>
                    {{ item.normalized_data.authors }}
                </td>
                <td>
                    {{ item.normalized_data.shelf }}
                </td>
                <td>
                    {% if item.rating %}
                    <p>{% include 'snippets/stars.html' with rating=item.rating %}</p>
                    {% endif %}
                    {% if item.review %}
                    <p>{{ item.review|truncatechars:100 }}</p>
                    {% endif %}
                    {% if item.linked_review %}
                    <a href="{{ item.linked_review.remote_id }}" target="_blank" rel="nofollow noopener noreferrer">{% trans "View imported review" %}</a>
                    {% endif %}
                </td>
                {% block import_cols %}
                <td>
                    {% if item.book %}
                    <a href="{{ item.book.local_path }}">
                    {% include 'snippets/book_cover.html' with book=item.book cover_class='is-h-s' size='small' %}
                    </a>
                    {% endif %}
                </td>
                <td>
                    {% if item.book %}
                    <span class="icon icon-check" aria-hidden="true"></span>
                    <span class="is-sr-only-mobile">{% trans "Imported" %}</span>

                    {% elif item.fail_reason %}
                    <span class="icon icon-x has-text-danger" aria-hidden="true"></span>
                    <span class="is-sr-only-mobile">
                        {% if item.book_guess %}
                        {% trans "Needs manual review" %}
                        {% else %}
                        {{ item.fail_reason }}
                        {% endif %}
                    </span>
                    {% else %}
                    <div class="is-flex">
                        <span class="icon icon-dots-three" aria-hidden="true"></span>
                        <span class="is-sr-only-mobile">{% trans "Pending" %}</span>
                        {# retry option if an item appears to be hanging #}
                        {% if job.created_date != job.updated_date and inactive_time > 24 %}
                        <form class="ml-2" method="POST" action="{% url 'import-item-retry' job.id item.id %}" name="retry-{{ item.id }}">
                            {% csrf_token %}
                            <button class="button is-danger is-outlined is-small">{% trans "Retry" %}</button>
                        </form>
                        {% endif %}
                    </div>
                    {% endif %}
                </td>
                {% endblock %}
            </tr>
            {% block action_row %}{% endblock %}
            {% endfor %}
            {% endif %}
        </table>
    </div>
    {% if legacy %}
    <div class="content">
        <form name="update-import" method="POST" action="{% url 'import-status' job.id %}">
            {% csrf_token %}
            <p>
                {% trans "This import is in an old format that is no longer supported. If you would like to troubleshoot missing items from this import, click the button below to update the import format." %}
            </p>
            <button class="button">{% trans "Update import" %}</button>
        </form>
    </div>
    {% endif %}
</div>

{% if not legacy %}
<div>
    {% include 'snippets/pagination.html' with page=items path=page_path %}
</div>
{% endif %}
{% endspaceless %}{% endblock %}
