{% load static %}
{% load i18n %}
{% load mastodon %}
{% if item %}
  <div class="action">
    <span>
      <a title="{% trans "edit" %}"
         href="{% url 'catalog:edit' item.url_path item.uuid %}"><i class="fa-solid fa-pen-to-square"></i></a>
    </span>
    <span>
      <a title="{% trans "revision history" %}"
         href="{% url 'catalog:history' item.url_path item.uuid %}"><i class="fa-solid fa-clock-rotate-left"></i></a>
    </span>
    <span>
      <a title="{% trans "back to item" %}" href="{{ item.url }}"><i class="fa-solid fa-file-lines"></i></a>
    </span>
  </div>
  <h5>{% trans "Edit Options" %}</h5>
  {% if item.is_deleted %}
    <p>
      <i class="fa-solid fa-circle-xmark"></i> {% trans "Item has been deleted." %}
    </p>
  {% endif %}
  {% if item.child_items %}
    <p>
      <i class="fa-solid fa-circle-exclamation"></i> {% trans "Item contains sub-items." %}
    </p>
  {% endif %}
  {% if item.merged_to_item %}
    <p>
      <i class="fa-solid fa-circle-xmark"></i> {% trans "Item has been merged to another item." %}
    </p>
  {% endif %}
  {% if item.journal_exists %}
    <p>
      <i class="fa-solid fa-circle-exclamation"></i> {% trans "Item has been marked by users." %}
    </p>
  {% endif %}
  {% for i in item.merged_from_items.all %}
    {% if forloop.first %}
      <i class="fa-solid fa-circle-info"></i> {% trans "The following items are merged into this item" %}
      <ul>
      {% endif %}
      <li>
        <a href="{{ i.url }}?skipcheck=1">{{ i.display_title }}</a>
      </li>
      {% if forloop.last %}</ul>{% endif %}
  {% endfor %}
  {% if item.editable or request.user.is_staff %}
    {% for res in item.external_resources.all %}
      <details>
        <summary>
          {% trans 'External website' %}: <a href="{{ res.url }}">{{ res.site_label }}</a>
        </summary>
        <div class="grid">
          <form method="post"
                action="{% url 'catalog:refetch' %}"
                onsubmit="return confirm('{% trans "Existing metadata might get overwritten, sure to proceed?" %}');">
            {% csrf_token %}
            <input type="hidden" name="id" value="{{ res.id }}">
            <input type="hidden" name="url" value="{{ res.url }}">
            <input class="secondary" type="submit" value="{% trans 'Fetch again' %}">
          </form>
          {% if request.user.is_staff %}
            <form method="post"
                  action="{% url 'catalog:unlink' %}"
                  onsubmit="return confirm('{% trans "You may not be able to undo this operation, sure to remove?" %}');">
              {% csrf_token %}
              <input type="hidden" name="id" value="{{ res.id }}">
              <input class="contrast"
                     type="submit"
                     value="{% trans 'Remove link to site' %}">
            </form>
          {% endif %}
        </div>
      </details>
    {% endfor %}
    {% if item.child_class %}
      <details>
        <summary>
          {% trans 'Edit sub-items' %} <small>({{ item.child_items.count }})</small>
        </summary>
        <p class="tag-list" style="overflow: scroll; max-height: 20em;">
          {% for ep in item.child_items %}
            <span class="season-number" id="ci_{{ ep.uuid }}">
              <a href="{% url 'catalog:edit' ep.url_path ep.uuid %}">{{ ep.episode_number|default:ep.display_title|default:"#" }}</a>
            </span>
          {% endfor %}
        </p>
        <form method="get" action="{% url 'catalog:create' item.child_class %}">
          <input name="parent" type="hidden" value="{{ item.uuid }}">
          <input class="contrast"
                 type="submit"
                 value="{% trans "create" %} {{ item.child_class }}">
        </form>
      </details>
    {% endif %}
    {% if item.class_name == "tvseason" %}
      <details>
        <summary>{% trans 'Fetch all episodes' %}</summary>
        {% if item.imdb and item.season_number is not None %}
          <form method="post"
                action="{% url 'catalog:fetch_tvepisodes' item.url_path item.uuid %}">
            {% csrf_token %}
            <input class="contrast" type="submit" value="{% trans 'Fetch all' %}">
            <small>{% trans "Due to differences in how Douban, IMDB, and TMDB handle season data, a small number of TV shows and animations may not return correct results. Please manually verify and clean up after updating." %}</small>
          </form>
        {% else %}
          <i class="fa-solid fa-circle-exclamation"></i> <i>{% trans "To fetch all episodes, season numbers and IMDB information are required. If filling this out is inconvenient, you can also manually create sub-entries." %}</i>
        {% endif %}
      </details>
    {% endif %}
    {% if item.class_name == "movie" %}
      <details>
        <summary>{% trans 'switch category' %}</summary>
        <form method="post"
              action="{% url 'catalog:recast' item.url_path item.uuid %}"
              onsubmit="return confirm('{% trans "Switching may remove some metadata. Sure to proceed?" %}');">
          {% csrf_token %}
          <input type="hidden" value="tvshow" name="class">
          <input class="contrast" type="submit" value="➡ {% trans 'TV Show' %}">
        </form>
      </details>
    {% elif item.class_name == "tvshow" %}
      {% if not item.all_seasons or request.user.is_staff %}
        <details>
          <summary>{% trans 'switch category' %}</summary>
          <form method="post"
                action="{% url 'catalog:recast' item.url_path item.uuid %}"
                onsubmit="return confirm('Switching may remove some metadata. Sure to proceed?');">
            {% csrf_token %}
            <input type="hidden" value="movie" name="class">
            <input class="contrast" type="submit" value="➡ {% trans 'Movie' %}">
          </form>
          <form method="post"
                action="{% url 'catalog:recast' item.url_path item.uuid %}"
                onsubmit="return confirm('Switching may remove some metadata. Sure to proceed?');">
            {% csrf_token %}
            <input type="hidden" value="tvseason" name="class">
            <input class="contrast" type="submit" value="➡ {% trans 'TV Season' %}">
          </form>
        </details>
      {% endif %}
    {% elif item.class_name == "album" %}
      <details>
        <summary>{% trans 'switch category' %}</summary>
        <form method="post"
              action="{% url 'catalog:recast' item.url_path item.uuid %}"
              onsubmit="return confirm('Switching may remove some metadata. Sure to proceed?');">
          {% csrf_token %}
          <input type="hidden" value="podcast" name="class">
          <input class="contrast" type="submit" value="➡ {% trans 'Podcast' %}">
        </form>
      </details>
    {% else %}
      <!-- no switch possible -->
    {% endif %}
    {% if item.class_name == "tvseason" or item.class_name == "performanceproduction" %}
      <details>
        <summary>{% trans "Link to parent item" %}</summary>
        <form method="post"
              action="{% url 'catalog:assign_parent' item.url_path item.uuid %}"
              onsubmit="return confirm('{% trans "Sure to link?" %}');">
          {% csrf_token %}
          <input type="url"
                 name="parent_item_url"
                 placeholder=""
                 value="{{ item.show.absolute_url }}">
          <br>
          <input class="contrast" type="submit" value="{% trans 'Update link' %}">
        </form>
      </details>
    {% endif %}
    {% if item.class_name == "tvshow" %}
      <details>
        <summary>{% trans "Cleanup seasons" %}</summary>
        <form method="post"
              action="{% url 'catalog:remove_unused_seasons' item.url_path item.uuid %}"
              onsubmit="return confirm('{% trans "This operation cannot be undone. Sure to delete?" %}');">
          {% csrf_token %}
          <input class="contrast"
                 type="submit"
                 value="{% trans 'remove seasons not marked' %}">
        </form>
      </details>
    {% endif %}
    {% if not item.journal_exists or request.user.is_staff %}
      <details>
        <summary>{% trans 'Merge' %}</summary>
        <form method="post"
              action="{% url 'catalog:merge' item.url_path item.uuid %}">
          {% csrf_token %}
          <input type="url"
                 name="target_item_url"
                 placeholder="{% trans "URL of target item, or empty if undo merge" %}"
                 {% if item.is_deleted and not item.merged_to_item %}disabled{% endif %}
                 value="{{ item.merged_to_item.absolute_url }}">
          <br>
          <input class="contrast"
                 type="submit"
                 value="{% trans 'merge to another item' %}">
        </form>
      </details>
      {% if item.can_soft_delete %}
        <details>
          <summary>{% trans 'Delete' %}</summary>
          <form method="post"
                action="{% url 'catalog:delete' item.url_path item.uuid %}">
            {% csrf_token %}
            <input class="contrast" type="submit" value="{% trans 'Delete' %}">
          </form>
        </details>
      {% endif %}
    {% endif %}
    {% if item.class_name == 'edition' %}
      {% if item.has_works %}
        <details>
          <summary>{% trans 'Work' %}</summary>
          {% trans "This edition belongs to the following work" %}
          {% for i in item.works.all %}
            <li>
              <a href="{{ i.url }}?skipcheck=1">{{ i.display_title }}</a>
            </li>
          {% endfor %}
          <form method="post"
                onsubmit="return confirm('{% trans "Sure to unlink?" %}');"
                action="{% url 'catalog:unlink_works' item.url_path item.uuid %}">
            {% csrf_token %}
            <input class="contrast"
                   type="submit"
                   {% if not request.user.is_staff %}disabled{% endif %}
                   value="{% trans 'Unlink from work' %}">
          </form>
        </details>
      {% endif %}
      {% if not item.has_works %}
        <details>
          <summary>{% trans 'Link' %}</summary>
          <form method="post"
                action="{% url 'catalog:link_edition' item.url_path item.uuid %}">
            {% csrf_token %}
            <input type="url"
              name="target_item_url"
              placeholder="{{ site_url }}/book/1234/
              {% if item.is_deleted and not item.merged_to_item %}disabled{% endif %}
              value="">
              <input class="contrast"
                     type="submit"
                     value="{% trans 'Link to another edition from same work' %}">
            </form>
          </details>
        {% endif %}
      {% endif %}
    {% endif %}
    <details>
      <summary>{% trans 'Suggest Edits' %}</summary>
      <form method="post"
            action="{% url 'catalog:suggest' item.url_path item.uuid %}">
        {% csrf_token %}
        <select name="action" aria-label="Select action..." required>
          <option selected disabled value="">{% trans "proposed action" %}</option>
          <option value="merge">{% trans "merge to another item" %}</option>
          <option value="link">{% trans "link to another item" %}</option>
          <option value="type">{% trans "change item category" %}</option>
          <option value="metadata">{% trans "change item metadata" %}</option>
          <option value="delete">{% trans "remove item" %}</option>
          <option value="other">{% trans "other" %}</option>
        </select>
        <textarea name="detail"
                  required
                  placeholder="{% trans "To suggest merging or association, please include the URL of the target item" %}"></textarea>
        <input type="submit" value="{% trans 'submit' %}">
        <small>{% trans "As a user of this community, you may edit some metadata of items on this site. If you are unsure whether your edits are appropriate or are unable to make a certain change, you can make suggestions here. Moderators will consider each suggestion, although there is no guarantee that they will always be fully adopted; suggestions may also be viewed or discussed by other community users. If you have suggestions that are not related to a specific item, please contact us. Thank you for your support and contribution." %}</small>
      </form>
    </details>
  {% endif %}
