{% extends "documents/base.html" %}

{% load i18n %}
{% load pagination_tags %}


{% block titleextra %}{% trans "Catalogue" %}{% endblock %}


{% block content %}
  <div class="card">
    <div class="card-header">
      <h1>{{ book }}</h1>
    </div>
    <div class="card-body">
      <table class="table">
        {% with is_published=book.is_published %}
          {% if is_published %}
            <a href="https://wolnelektury.pl/katalog/lektura/{{ book.slug }}" target="_blank">
          {% endif %}
          {% if is_published %}
            </a>
          {% endif %}
        {% endwith %}

        {% for author in book.authors.all %}
          <tr>
            <th>
              <a href="{{ author.get_absolute_url }}">
                {{ author }}
              </a>
            </th>
            <td>
              {{ author.pd_year|default_if_none:"-" }}
            </td>
            <td>
              {{ author.wikidata_link }}
            </td>
            <td>
              {{ author.get_priority_display }}
            </td>
          </tr>
        {% endfor %}
        {% for author in book.translator_set.all %}
          <tr>
            <th>
              <a href="{{ author.get_absolute_url }}">
                {{ author }}
              </a>
            </th>
            <td>
              {{ author.pd_year|default_if_none:"-" }}
            </td>
            <td>
              {{ author.wikidata_link }}
            </td>
            <td>
              {{ author.get_priority_display }}
            </td>
          </tr>
        {% endfor %}
        <tr>
          <td>
            &nbsp;
            <a href="{{ book.get_absolute_url }}">
              {{ book.title }}
            </a>
          </td>
          <td>
            {{ book.pd_year|default_if_none:"-" }}
          </td>
          <td>
            {{ book.wikidata_link }}
          </td>
          <td>
            {{ book.get_priorty_display }}
          </td>
          <td>
          </td>
        </tr>
      </table>
    </div>
  </div>

  <div class="card mt-4">
    <div class="card-header">
      Tekst
    </div>
    <div class="card-body">
      {% for b in book.document_books.all %}
        <a href="{{ b.get_absolute_url }}">
          {{ b }}
        </a>
      {% empty %}
        Brak tekstu. Jeśli masz źródło ze skanami, utwórz z niego tekst niżej.
      {% endfor %}
    </div>
  </div>

  <div class="card mt-4">
    <div class="card-header">
      Audio
    </div>
    <div class="card-body">
      {% with audio=book.audio_status.items %}
        {% if audio %}
          <table class="table">
          {% for s in audio %}
            <tr><td>{{ s.part }}</td><td>{{ s.youtube_status }}</td></tr>
          {% endfor %}
          </table>
        {% endif %}
      {% endwith %}
      <!-- tbd -->
    </div>
  </div>

  <div class="card mt-4">
    <div class="card-header">
      Źródło
    </div>
    <div class="card-body">
      <table class="table">
        <tbody>
          {% for bs in book.booksource_set.all %}
            <tr>
              <td>
                <a href="{{ bs.source.get_absolute_url }}">
                  {{ bs.source }}
                </a>
              </td>
              <td>
                {% if bs.page_start or bs.page_end %}
                  (strony {{ bs.page_start }}—{{ bs.page_end }})
                {% else %}
                  <small>
                    (źródło w całości przypisane do utworu <em>{{ book }}</em>)
                  </small>
                {% endif %}
              </td>
              <td>
                {% if not bs.source.has_upload_files %}
                  <div class="alert alert-info">
                    Źródło nie ma jeszcze załadowanych skanów.
                    <a href="{% url 'source_upload' bs.source.id %}">
                      Załaduj skany</a>.
                  </div>
                {% elif not bs.source.has_view_files %}
                  <div class="alert alert-info">
                    Trwa generowanie podglądu.
                  </div>
                {% elif not bs.source.has_ocr_files %}
                  <div class="alert alert-info">
                    Trwa OCR.
                  </div>
                {% elif bs.source.modified_at > bs.source.processed_at %}
                  <div class="alert alert-info">
                    Zmodyfikowano skany, trwa aktualizacja.
                  </div>
                {% endif %}
              </td>
            </tr>
          {% empty %}
            Brak źródła.
            Możesz je dodać.
          {% endfor %}
        </tbody>
      </table>

      <form method="post" action="{% url 'source_book_prepare' book.pk %}">
        {% csrf_token %}
        <button class="btn btn-primary"
                {% if not book.are_sources_ready %}disabled="disabled"{% endif %}
        >Utwórz tekst książki</button>
      </form>
    </div>
  </div>



{% endblock content %}
