{% extends "layout.html" %}

{% macro buttons(is_popup=False) %}
<div class="buttons form-controls init-block" data-block-name="compact-buttons" data-compact-name="form-controls">
  <a class="compact-toggle" title="Компактный вид"></a>
  {% if not is_popup %}
    {% for action in actions %}
      {%- set action_url = action.url(env, item=item) -%}
      <a href="{{ action_url }}"
         rel="{{ action.mode }}"
         {%- if action.hint %} title="{{ action.hint }}"{% endif %}
         {%- if action.item_lock %} data-item-lock="true"{% endif %}
         {%- if action.accepts_item_form %} data-item-form="true"{% endif %}
         {%- if action.js_block %} data-block-name="{{ action.js_block }}"{% endif %}
         class="button action-{{ action.action }}
                {%- if action.js_block %} init-block{% endif %}
                {%- if action.cls %} icon-{{ action.cls}}{% endif %}">
        {{- action.title -}}
      </a>
    {% endfor -%}
  {% endif %}

  {% if save_allowed %}
  <div class="buttons__save-actions">
    {% if 'save_and_continue' in item_buttons %}
    <a href="" class="button icon-apply" rel="save-and-continue"
       data-item-lock="true"
       data-item-form="true">
      Сохранить
    </a>
    {% endif %}

    {% macro buttons_in_tooltip() %}
      {%- if create_allowed and 'save_and_add_another' in item_buttons -%}
      <a href="{{ stream.url_for(env, 'item', item=None).qs_set(filter_form.get_data()) }}"
         data-item-lock="true"
         data-item-form="true"
         class="button icon-add" rel="save-and-add">
        … и создать новый
      </a>
      {%- endif -%}

      {%- if list_allowed and 'save' in item_buttons -%}
      <a href="{{ stream_url }}" class="button icon-back" rel="save"
         data-item-lock="true"
         data-item-form="true">
        … и закрыть
      </a>
      {%- endif -%}
    {% endmacro %}
    {% set buttons_in_tooltip_rendered = buttons_in_tooltip() %}

    {% if buttons_in_tooltip_rendered %}
    <div class="buttons__save-hidden">
      {{- buttons_in_tooltip_rendered -}}
    </div>
    {% endif %}
  </div>
  {% endif %}
</div>
{% endmacro %}


{% block content %}
<form id="{{ form.id }}"
      class="init-block item-form{% if is_popup %} popup-form{% endif %}"
      action="{{ submit_url }}"
      method="post"
      enctype="multipart/form-data"
      data-block-name="item-form"
      data-item-id="{{ item.id or '' }}"
      {%- if autosave_allowed %}
      data-autosave="{{ stream.url_for(env, 'item.autosave', item=item.id).qs_set(filter_form.get_data()) }}"
      {%- endif %}
      {%- if form.presavehooks is defined %} data-presavehooks="{{ ' '.join(form.presavehooks) }}"{% endif %}>
  <div class="content">
    <div class="header">
      <div style="float:left">
        {% block title %}
        <h1>
          {%- if is_popup or not list_allowed %}
            {{- stream_title }}
          {%- else -%}
            <a href="{{ stream_url }}">{{ stream_title }}</a>
          {%- endif -%}
        </h1>
        {% endblock %}

        {% block after_title %}{% endblock -%}
        {%- if log_enabled and item.id %}
            <a class="edit-log-link" href="{{ stream.url_for(env, 'edit_log', item=item.id) }}" title="История правок"></a>
        {%- endif %}

        {% if item.id and stream.referrers %} {# XXX #}
            <a class="referrers-link" href="{{ stream.url_for(env, 'referrers', item=item.id) }}" title="Связанные объекты" rel="popup"></a>
        {% endif %}

        {%- if context.item_trays is defined and item and item.id -%}
        <div class="trays init-block" data-block-name="item-trays"
             data-delete-url="{{ env.url_for('delete_from_tray') }}"
             data-stream-name="{{ stream.uid(env, version=False) }}"
             data-object-id="{{ item.id }}">
          <button type="button" class="trays__add" title="Отправить в рабочую папку пользователю"></button>
          <div class="trays__dropdown">
            <div class="trays__list">
            {%- for object_tray in context.item_trays(stream, item) -%}
            <div class="trays__tray tray{{ object_tray.tray.id % 24 }}" data-id="{{ object_tray.id }}">
              <a href="{{ url_for('tray', tray=object_tray.tray.id) }}">
                {{- object_tray.tray.title -}}
              </a>
              {%- if object_tray.comment -%}
                <div class="trays__comment-hellip">
                  <div class="trays__comment">{{ object_tray.comment }}</div>
                </div>
              {%- endif -%}
              {%- if object_tray.can_delete(env.user) -%}
                <button type="button" class="trays__tray__remove">&times;</button>
              {%- endif -%}
            </div>
            {%- endfor -%}
            </div>
          </div>
        </div>
        {%- endif -%}
      </div>

      {{ buttons(is_popup) }}

      {% from "macros/autosave_status.html" import autosave_status %}
      {{ autosave_status(autosave_allowed, draft) }}
    </div>
    <div class="text">
      <div class="line"></div>
      {% if has_draft and not draft %}
        <div class="has-draft">
          <p>Имеется несохранённый черновик объекта.</p>
          <p>Вы можете <a href="{{ submit_url.qs_set(force_draft=1) }}">редактировать черновик</a> или
             продолжить редактирование без черновика (он будет удалён при первом автосохранении).</p>
        </div>
      {% endif %}
      <div class="form">
        {{ form.render() }}
      </div>
      {% block after_form %}{% endblock %}
    </div>
  </div>

  {% if item_lock %}{{ item_lock.render() }}{% endif %}
</form>
{% endblock %}
