{% extends base %}
{% from 'macro.html' import breadcrumbs with context %}

{% block extrahead %}
    {{- super() }}
    {% if page_obj.has_previous() -%}
        <link rel="prev" href="{{ url_for('story.view', pk=story.id, comments_page=page_obj.number - 1, _external=True) }}" />
    {%- endif %}
    {% if page_obj.has_next() -%}
        <link rel="next" href="{{ url_for('story.view', pk=story.id, comments_page=page_obj.number + 1, _external=True) }}" />
    {%- endif %}
    {% if page_current == -1 -%}
        <meta name="description" content="{{ story.summary|striptags }}" />
    {%- endif -%}
{% endblock %}

{% block content %}
<div class="row">
    <div class="span12 story-page" id="story_{{ story.id }}">
        {{- breadcrumbs() }}

        <div class="story-info" itemscope itemtype="http://schema.org/Book">
        {% include 'includes/story_control_buttons.html' %}
        <h1 id="story_title">
            <span itemprop="name"><a itemprop="url" class="story-title-link" href="{{ url_for('story.view', pk=story.id, _external=True) }}">{{ story.title }}</a></span>
            {% if story.bl.can_show_stars() %}
                <span itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
                    <meta itemprop="ratingValue" content="{{ '{:.1f}'.format(story.vote_average) }}" />
                    <meta itemprop="ratingCount" content="{{ story.vote_total }}" />
                </span>
            {% endif %}
            <span id="stars" style="display: inline;">{% include 'includes/story_stars.html' %}</span>
            {% include 'includes/story_marks.html' %}
        </h1>
        {% if current_user.is_authenticated and not story.bl.is_author(current_user) %}
        <div class="vote-area">
            Ваша оценка:
            <span class="starblock">
            {% if vote %}
                {%- for i in range(1, 6) -%}
                    <a href="{{ url_for('story.vote', pk=story.id, value=i) }}" class="starvote star-button star-{% if vote.vote_value >= i %}5{% else %}0{% endif %}" title="{{ i }}"></a>
                {%- endfor -%}
            {% else %}
                {%- for i in range(1, 6) -%}
                    <a href="{{ url_for('story.vote', pk=story.id, value=i) }}" class="starvote star-button star-0" title="{{ i }}"></a>
                {%- endfor -%}
            {% endif %}
            </span>
        </div>
        {% endif %}
            <p>
                {%- include 'includes/story_reader_buttons.html' -%}
            </p>
            <p class="story-genres">
                {%- for category in story.categories.order_by('c.id') -%}
                    <a class="gen" style="background-color: {{ category.color }}" href="{{ url_for('search.simple', search_type='category', search_id=category.id) }}">{{ category.name }}</a>
                {%- endfor -%}
            </p>
            <p class="story-thumbnails">
                {%- for character in story.characters.order_by('c.id') -%}
                    <a href="{{ url_for('search.simple', search_type='character', search_id=character.id) }}"><img src="{{ character.thumb }}" alt="{{ character.name }}" title="{{ character.name }}"/></a>
                {%- endfor -%}
            </p>

        <p>Написал: {% include 'includes/story_authors_list_microdata.html' %}</p>
        {% if contributors.editors -%}
            <p>Редакторы: {% for u in contributors.editors -%}
                <a class="authorlink" href="{{ url_for('author.info', user_id=u.id) }}">{{ u.username }}</a>{% if not loop.last %}, {% endif -%}
            {% endfor %}</p>
        {%- endif %}
        {% if contributors.betas -%}
            <p>Бета-читатели: {% for u in contributors.betas -%}
                <a class="authorlink" href="{{ url_for('author.info', user_id=u.id) }}">{{ u.username }}</a>{% if not loop.last %}, {% endif -%}
            {% endfor %}</p>
        {%- endif %}

        <div class="story-description" itemprop="description">{{ story.summary_as_html }}</div>

        {% if story.notes %}
            <div>Заметки к рассказу:</div>
            <div class="story-notes">{{ story.notes_as_html }}</div>
        {% endif %}

        <blockquote class="more-info">
            <p><strong>Подробности и статистика</strong></p>

            {% if story.source_link %}
                <p class="story-source">Источник: <a href="{{ story.source_link }}" target="_blank">{{ story.source_title or story.source_link }}</a></p>
            {% endif %}
            <p>
                Рейтинг — <a href="{{ url_for('search.simple', search_type='rating', search_id=story.rating.id) }}">{{ story.rating.name }}</a><br/>

                События: {% for classifier in story.classifications.order_by('c.id') %}<a href="{{ url_for('search.simple', search_type='classifier', search_id=classifier.id) }}">{{ classifier.name }}</a>{% if not loop.last %}, {% endif %}{% endfor %}<br/>
                {{ ngettext("%(num)d word", "%(num)d words", story.words) }}, {{ ngettext("%(num)d view", "%(num)d views", story.views) }}<br/>

                {% if not story.first_published_at or current_user.is_staff or story.bl.is_contributor(current_user) %}
                    Создан: <time datetime="{{ story.date.strftime('%Y-%m-%dT%H:%M:%SZ') }}">
                        {{- story.date|datetimeformat('d MMMM Y') -}}
                    </time>
                    {%- if not story.first_published_at %}, последнее изменение – <time datetime="{{ story.updated.strftime('%Y-%m-%dT%H:%M:%SZ') }}" title="{{ story.updated|datetimeformat('d MMMM Y, HH:mm') }}">
                        {{- story.updated|timedeltaformat }} назад
                    </time>{% endif %}<br/>
                {% endif %}
                {% if story.first_published_at %}
                    Опубликован: <time itemprop="datePublished" datetime="{{ story.first_published_at.strftime('%Y-%m-%dT%H:%M:%SZ') }}">
                        {{- story.first_published_at|datetimeformat('d MMMM Y') -}}
                    </time>, последнее изменение – <time itemprop="dateModified" datetime="{{ story.updated.strftime('%Y-%m-%dT%H:%M:%SZ') }}" title="{{ story.updated|datetimeformat('d MMMM Y, HH:mm') }}">
                        {{- story.updated|timedeltaformat }} назад
                    </time><br/>
                {%- endif -%}
            </p>

            {% if current_user.is_authenticated -%}
            <form method="POST" action="{{ url_for('story.subscribe', pk=story.id) }}" class="subscription-form js-subscription-form">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
                Подписаться на новые главы:
                <label><input type="checkbox" name="email" value="1"{% if sub.email %} checked{% endif %} /> почта</label>
                <label><input type="checkbox" name="tracker" value="1"{% if sub.tracker %} checked{% endif %} /> сайт</label>
                <button type="submit" class="btn btn-mini js-subscription-form-submit">Сохранить</button>
            </form>
            {%- endif %}
        </blockquote>
        </div><!-- /story-info -->

        <div class="story-chapters">
            {% if chapters|length > 1 %}
                <h2>Содержание</h2>
            {% include "includes/story_panel.html" %}
                <ul id="story_chapters">
                {% for chapter in chapters %}
                    <li>
                        <h4>
                            <a href="{{ url_for('chapter.view', story_id=story.id, chapter_order=chapter.order) }}">{{ chapter.autotitle }}</a>
                            {% if chapter.draft %}<sup class="draft">{{ _('Draft') }}</sup>{% endif %}
                            {% if story.bl.editable_by(current_user) %}<sup><a class="edit-link" href="{{ url_for('chapter.edit', pk=chapter.id) }}">Редактировать</a></sup>{% endif %}
                        </h4>
                      {{ ngettext("%(num)d word", "%(num)d words", chapter.words) }}, {{ ngettext("%(num)d view", "%(num)d views", chapter.views) }}<li>
                {% endfor %}
                </ul>
            {% elif chapters|length > 0 %}
              {% with chapter=chapters[0] %}
                {% include "includes/story_panel.html" %}
                {% include "includes/chapter_single.html" %}
              {% endwith %}
        {% endif %}
        </div>

        {% if current_user.is_staff or story.bl.is_contributor(current_user) %}
            <h3 style="margin-bottom: 24px;"><a href="{{ url_for('story_local_comment.view', story_id=story.id) }}">
                Комментарии бета-читателей и редакторов ({{ local_comments_count }})</a>
            {%- if new_local_comments_count > 0 %} <span class="green">(+{{ new_local_comments_count }})</span>{% endif %}</h3>
        {% endif %}

        <!-- Вывод комментариев: начало -->
        <div id="comments">
            <h3>Комментарии (<span id="comments-count">{{ comments_count }}</span>)</h3>
            {% if current_user.is_authenticated %}
                <form method="POST" action="{{ url_for('story.comments_subscribe', pk=story.id) }}" class="subscription-form js-subscription-form">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
                    Получать новые:
                    <label><input type="checkbox" name="email" value="1"{% if sub_comments.email %} checked{% endif %} /> в почте</label>
                    <label><input type="checkbox" name="tracker" value="1"{% if sub_comments.tracker %} checked{% endif %} /> на сайте</label>
                    <button type="submit" class="btn btn-mini js-subscription-form-submit">Сохранить</button>
                </form>
            {% endif %}
            <div id="comments-tree" class="comments-tree">
                {% if comments_count > 0 %}
                    {%- include 'includes/comments_tree.html' -%}
                {%- endif %}
            </div>
            <div id="comments-pagination" class="pagination center">
                {% if comments_count > 0 %}
                    {%- include 'includes/comments_pagination_story.html' -%}
                {%- endif %}
            </div>
        </div>

        {% if story.bl.can_comment_by(current_user) %}
            <a href="{{ url_for('story_comment.add', story_id=story.id) }}" id="comment-add-link" class="comment-add-link">Добавить новый комментарий</a>

            <form id="comment-form" class="comment-form" method="POST" enctype="multipart/form-data" action="{{ url_for('story_comment.add', story_id=story.id) }}">
            {% from 'macro.html' import comment_form_content with context %}
            {{ comment_form_content(comment_form, 'story', story.id, preview_html=preview_html) }}
            </form>
        {% elif not story.published %}
        <h3 id="banned">В неопубликованном рассказе нельзя добавлять комментарии</h3>
        {% elif current_user.is_authenticated %}
        <h3 id="banned">К сожалению, Вы не можете добавлять комментарии</h3>
        {% else %}
        <a href="{{ url_for('auth.login', next=url_for('story.view', pk=story.id)) }}">Авторизуйтесь</a> для отправки комментария.
        {% endif %}
        <!-- Вывод комментариев: конец -->
    </div>
</div>
{% endblock %}

{%- block modal -%}
    {%- if story.nsfw and not current_user.nsfw -%}
        {%- include 'includes/nsfw_modal.html' -%}
    {%- endif -%}
{%- endblock -%}
