{% extends 'projectroles/base.html' %}

{% load rules %}
{% load static %}
{% load crispy_forms_filters %}
{% load projectroles_tags %}
{% load projectroles_common_tags %}

{% block title %}
  {% get_django_setting 'PROJECTROLES_DISABLE_CATEGORIES' as disable_categories %}
  {% get_display_name 'PROJECT' title=True as project_display %}
  {% get_display_name 'CATEGORY' title=True as category_display %}

  {% if object.pk %}
    Update {% get_display_name object.type title=True %} {{ object.title }}
  {% elif parent.pk %}
    Create {{ project_display }} or {{ category_display }} Under {{ parent.title }}
  {% elif disable_categories %}
    Create {{ project_display }}
  {% else %}
    Create Top Level {{ category_display }}
  {% endif %}
{% endblock title %}

{% block head_extend %}
  {{ form.media }}
{% endblock head_extend %}

{% block projectroles %}
  {% get_django_setting 'PROJECTROLES_DISABLE_CATEGORIES' as disable_categories %}
  {% get_display_name 'PROJECT' title=True as project_display %}
  {% get_display_name 'CATEGORY' title=True as category_display %}

  {% if object.pk %}
    {% include 'projectroles/_project_header.html' %}
  {% endif %}

  <div class="row sodar-subtitle-container">
    {% if object.pk %}
      {# NOTE: h3 is correct here, as we also have the project header #}
      <h3 id="sodar-pr-project-form-title" data-project-type={{ object.type }}>
        Update {% get_display_name object.type title=True %}
      </h3>

      {# TODO: Convert to btn-group, how to enable tooltip for disabled button? #}
      <div class="ml-auto">
        {% if object.is_project %}
          <a role="button" class="btn btn-warning"
             href="{% url 'projectroles:archive' project=object.sodar_uuid %}"
             id="sodar-pr-btn-archive">
            {% if not object.archive %}
              <i class="iconify" data-icon="mdi:archive-arrow-up"></i> Archive
            {% else %}
              <i class="iconify" data-icon="mdi:archive-arrow-down"></i> Unarchive
            {% endif %}
          </a>
         {% endif %}
         <span class="d-inline-block" tabindex="0" data-toggle="tooltip"
               id="sodar-pr-btn-delete-tooltip"
               title="{{ project_delete_msg }}">
           <a role="button" class="btn btn-danger"
              href="{% url 'projectroles:delete' project=object.sodar_uuid %}"
              {% if not project_delete_access %}disabled="disabled"{% endif %}
              id="sodar-pr-btn-delete">
            <i class="iconify" data-icon="mdi:close-thick"></i> Delete
          </a>
        </span>
      </div>
    {% elif parent.pk %}
      <h2>Create {{ project_display }} or {{ category_display }} Under {{ parent.title }}</h2>
    {% elif disable_categories %}
      <h2>Create {{ project_display }}</h2>
    {% else %}
      <h2 id="sodar-pr-project-form-title" data-project-type="CATEGORY">
        Create Top Level {{ category_display }}
      </h2>
    {% endif %}
  </div>

  <div class="container-fluid sodar-page-container">
    <form method="post">
      {% csrf_token %}
      {{ form | crispy }}
      <div class="row">
        <div class="btn-group ml-auto" role="group">
          <a role="button" class="btn btn-secondary"
             href="{{ request.session.real_referer }}">
            <i class="iconify" data-icon="mdi:arrow-left-circle"></i> Cancel
          </a>
          <button type="submit" class="btn btn-primary sodar-btn-submit-once">
            <i class="iconify" data-icon="mdi:check-bold"></i>
            {% if object.pk %}Update{% else %}Create{% endif %}
          </button>
        </div>
      </div>
    </form>
  </div>
{% endblock projectroles %}

{% block javascript %}
  {{ block.super }}
  <!-- Project starring -->
  <script type="text/javascript" src="{% static 'projectroles/js/project_star.js' %}"></script>
  <!-- Settings field toggling -->
  <script type="text/javascript" src="{% static 'projectroles/js/project_form.js' %}"></script>
{% endblock javascript %}
