{% extends "documents/base.html" %}
{% load book_list i18n %}
{% load bootstrap4 %}
{% load depot %}
{% load isbn %}


{% block titleextra %}{{ book.title }}{% endblock %}


{% block content %}

  <div class="card mt-4">
    <div class="card-header">
      <h1>{{ book.title }}</h1>
    </div>
    <div class="card-body">
      <div class="row">
	<div class="col-md-6">


          {% if editable %}<form method='POST'>{% csrf_token %}{% endif %}
            {% bootstrap_form form %}
            {% if editable %}
              {% buttons %}
              <button type="submit" class="btn btn-primary">{% trans "Save" %}</button>
        {% endbuttons %}
            {% endif %}
            {% if editable %}</form>{% endif %}

            {% if editable %}
              {% if book.gallery %}
                <p><a href="{% url 'documents_book_gallery' book.slug %}">{% trans "Edit gallery" %}</a></p>
              {% endif %}

              <p style="text-align:right"><a class="btn btn-sm btn-danger" href="{% url 'documents_book_append' book.slug %}">{% trans "Append to other book" %}</a></p>
            {% endif %}


	</div>
	<div class="col-md-6">
          {% with cbook=book.catalogue_book %}
            {% if cbook %}
              <p>
	        W katalogu:
	        <a href="{% url 'catalogue_book' cbook.slug %}">{{ cbook }}</a>
              </p>

              {% if cbook.slug != book.dc_slug %}
                <p class="alert alert-warning">
                  Slug książki w metadanych tekstu nie zgadza się ze slugiem książki w katalogu.</p>
                <p>
                Upewnij się że tekst jest przypisany do właściwego rekordu w katalogu:<br>
                <tt>{{ book.dc_slug }}</tt> (tekst)<br>
                <tt>{{ cbook.slug }}</tt> (<a href="/admin/catalogue/book/{{ cbook.pk }}/change/">katalog</a>)<br>
                </p>
              {% endif %}

            {% else %}
              <p class="alert alert-warning">Tekst nie przypisany do rekordu książki w katalogu.</p>

              {% if book.dc_slug %}
                {% if cbook_by_slug %}
                  W katalogue znajduje się
                  <a href="/admin/catalogue/book/{{ cbook_by_slug.id }}/change/">rekord książki</a> o slugu:<br>
                  {{ book.dc_slug }}<br><br>
                  {% if request.user.is_staff %}
                    <form method="POST" action="{% url 'documents_book_attach_to_catalogue' book.id %}">
                      {% csrf_token %}
                      <button class="btn btn-primary">Przypisz tekst do tego rekordu w katalogu</button>
                    </form>
                  {% endif %}
                {% else %}
                  <p>
                    W katalogu brak rekordu o slugu:<br><tt>{{ book.dc_slug }}</tt>
                  </p>
                {% endif %}
              {% else %}
                <p>
                  Ten tekst nie ma slugu określonego w metadanych.
                </p>
              {% endif %}
            {% endif %}
          {% endwith %}
	</div>
      </div>
    </div>
  </div>

  <div class="card mt-4">
    <div class="card-header">
      <h2>{% trans "Chunks" %}</h2>
    </div>
    <div class="card-body">

      <table class='single-book-list table'><tbody>
        {% for chunk in book %}
          {% include 'documents/book_list/chunk.html' %}
        {% endfor %}
      </tbody></table>
    </div>
  </div>




  <div class='card mt-4'>

    <div class="card-header">
      <h2>
        {% trans "Publication" %}
        |
        {% if book.project %}
          {% if book.project.can_sell %}
            <span title="Książka do sprzedaży">💶</span>
          {% else %}
            <span title="Książka nie do sprzedaży">🙅</span>
          {% endif %}
        {% else %}
          <span title="Brak informacji o możliwości sprzedaży">❓</span>
        {% endif %}
        {% if has_audio %}
          | 🎧
          {% if can_sell_audio %}
            <span title="Audiobook do sprzedaży">💶</span>
          {% else %}
            <span title="Audiobook nie do sprzedaży">🙅</span>
          {% endif %}
        {% endif %}
      </h2>

    </div>
    <div class="card-body">
      <div class="row">
        <div class="col-lg-3">
          <img class="cover-preview" src="{% url 'cover_preview' book.slug %}" />
          {% if book.dc_cover_image %}
            <a href="{{ book.dc_cover_image.get_absolute_url }}">{{ book.dc_cover_image }}</a>
          {% endif %}
          <br><br>

          <form action="{% url 'cover_quick_import' book.pk %}" method="post">
            {% csrf_token %}
            <div class="input-group">
              <input type="url" name="url" class="form-control" placeholder="URL okładki">
              <button type="submit" class="btn btn-sm btn-info">Ustaw</button>
            </div>
          </form>


          <br><br>
          <form action="{% url 'cover_preview' book.slug %}">
            <input type="hidden" name="download" value="1">
            Pobierz okładkę:
            <div class="input-group">
              <select class="form-control" name='cover_class'>
	        <option value="default">nowa</option>
	        <option value="m-label">nowa + label</option>
                <option value="legacy">dawna</option>
              </select><br>
              <input class="form-control" name="width" type="number" required value="600" size="3" placeholder="szer.">
              <input class="form-control" name="height" type="number" size="3" placeholder="wys.">
              <button type="submit" class="btn btn-sm btn-primary">🡇</button>
            </div>
          </form>
        </div>
        <div class="col-lg-9">
          <p>{% trans "Last published" %}:
            {% if book.last_published %}
              {{ book.last_published }}
            {% else %}
              &mdash;
            {% endif %}
          </p>

          {% if publishable %}
            <p>
              <a href="{% url 'documents_book_xml' book.slug %}" rel="nofollow">{% trans "Full XML" %}</a><br/>
              <a target="_blank" href="{% url 'documents_book_html' book.slug %}" rel="nofollow">{% trans "HTML version" %}</a><br/>
              <a href="{% url 'documents_book_txt' book.slug %}" rel="nofollow">{% trans "TXT version" %}</a><br/>
              <a href="{% url 'documents_book_pdf' book.slug %}" rel="nofollow">{% trans "PDF version" %}</a><br/>
              <a href="{% url 'documents_book_pdf_mobile' book.slug %}" rel="nofollow">{% trans "PDF version for mobiles" %}</a><br/>
              <a href="{% url 'documents_book_epub' book.slug %}" rel="nofollow">{% trans "EPUB version" %}</a>
              <a href="{% url 'documents_book_epubcheck' book.slug %}" rel="nofollow">sprawdź</a><br/>
              <a href="{% url 'documents_book_mobi' book.slug %}" rel="nofollow">{% trans "MOBI version" %}</a><br/>
            </p>

            <p><a href="./synchro">Sprawdź synchronizację</a></p>

            {% isbn_status book %}

            {% if user.is_authenticated %}
              <!--
                   Angel photos:
                   Angels in Ely Cathedral (http://www.flickr.com/photos/21804434@N02/4483220595/) /
                   mira66 (http://www.flickr.com/photos/21804434@N02/) /
                   CC BY 2.0 (http://creativecommons.org/licenses/by/2.0/)
              -->
              <form method="POST" action="{% url 'documents_publish' book.slug %}">{% csrf_token %}
                {{ publish_options_form.as_p }}
                <img src="{{ STATIC_URL }}img/angel-left.png" style="vertical-align: middle" />
                <button id="publish-button" type="submit">
                  <span>{% trans "Publish" %}</span></button>
                <img src="{{ STATIC_URL }}img/angel-right.png" style="vertical-align: middle" />
              </form>


              {% if perms.depot.add_sitebookpublish %}
                {% depot_sites book as sites %}
                {% for site in sites %}
                  <hr>
                  <h3 class="mb-3">{{ site.name }}</h3>
                  {% if not site.errors %}
                    <form method="post" action="{% url 'depot_site_publish' site.site_id book.pk %}">
                      {% csrf_token %}
                      <button class="btn btn-primary mb-3" type="submit">
                        Opublikuj na {{ site.name }}
                      </button>
                    </form>
                    {% for info in site.info %}
                      <div class="alert alert-info">
                        {{ info }}
                      </div>
                    {% endfor %}
                  {% else %}
                    {% for error in site.errors %}
                      <div class="alert alert-danger">
                        {{ error }}
                      </div>
                    {% endfor %}
                    {% for info in site.info %}
                      <div class="alert alert-info">
                        {{ info }}
                      </div>
                    {% endfor %}
                  {% endif %}
                  {% for warning in site.warnings %}
                    <div class="alert alert-warning">
                      {{ warning }}
                    </div>
                  {% endfor %}
                  {% if site.id %}
                    id:{{ site.id }}
                  {% endif %}
                  {% with last=site.last %}
                    {% if last %}
                      {{ last.created_at }} &rarr;
                      {{ last.started_at }} &rarr;
                      {{ last.finished_at }}
                      <span title="{{ last.error }}">
                        ({{ last.get_status_display }})
                      </span>
                      <!-- {{ site.last.id }} -->
                    {% endif %}
                  {% endwith %}
                {% endfor %}
              {% endif %}

            {% else %}
              <a href="{% url 'cas_ng_login' %}">{% trans "Log in to publish." %}</a>
            {% endif %}
          {% else %}
            <p>{% trans "This book can't be published yet, because:" %}</p>
            <ul><li>{{ publishable_error }}</li></ul>
          {% endif %}
        </div>
      </div>
    </div>
  </div>


  {% if stats %}
    <div class="card mt-4">
      <div class="card-header">
        <h2>{% trans "Statistics" %}</h2>
      </div>
      <div class="card-body">
        <table class="table">
          <thead>
            <tr>
              <th>
                {% trans "book" %}
              </th>
              <th>{% trans "characters" %}</th>
              <th>{% trans "characters (with footnotes)" %}</th>
              <th>{% trans "words" %}</th>
              <th>{% trans "words (with footnotes)" %}</th>
              <th>{% trans "wiersze (+ inne znaki)" %}</th>
              <th>{% trans "wiersze (+ inne znaki, z przypisami)" %}</th>
            </tr>
          </thead>
          <tbody>
            {% include 'documents/book_stats.html' with book=doc stats=stats depth=0 %}
        </tbody>
      </table>
    </div>
  </div>
{% endif %}
{% endblock content %}
