{% extends "base.html" %}
{% import "entity_macros.html" as entity_macros %}

{% block title %}Editgroup{% endblock %}

{% block body %}

{% macro edit_list(auth_to, editgroup, edits, entity_type, entity_name) -%}
<div class="{% if edits %}active{% endif %} title">
  <h3><i class="dropdown icon"></i>{{ entity_name }} Edits ({{ edits|count }})</h3>
</div><div class="{% if edits %}active{% endif %} content" style="padding-bottom: 0.5em;">
  <div class="ui divided list">
    {% for edit in edits %}
    <div class="item">
      <div class="content">
        <div class="header">
          <a href="/{{ entity_type }}/{{ edit.ident }}">{{ entity_type }}/{{ edit.ident }}</a>
          {% if edit.redirect_ident %}
            =&gt; redirect to <a href="/{{ entity_type }}/{{ edit.redirect_ident }}">{{ entity_type }}/{{ edit.redirect_ident }}</a>
          {% elif not edit.revision %}
            deleted
          {% elif not edit.prev_revision %}
            created
          {% else %}
            updated
          {% endif %}
          <a href="/editgroup/{{ editgroup.editgroup_id }}/{{ entity_type }}/{{ edit.ident }}">[view edit]</a>
          {% if auth_to.edit and not editgroup.changelog_index and not editgroup.submitted %}
            <a href="/editgroup/{{ editgroup.editgroup_id }}/{{ entity_type }}/{{ edit.ident }}/edit" style="color: green;">[re-edit]</a>
            <form id="submit_edit_delete" method="POST" action="/editgroup/{{ editgroup.editgroup_id }}/{{ entity_type }}/edit/{{ edit.edit_id }}/delete" style="display:inline;">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                <input type="submit" value="[delete-edit]" style="background:none; color: red; border: none; padding:9;font-weight:bold;cursor:pointer;"></input>
            </form>
          {% endif %}
        </div>
        {% if edit.revision %}
          Revision: <small><code><a href="/{{ entity_type }}/rev/{{ edit.revision }}">{{ edit.revision }}</a></code></small>
        {% endif %}
        {% if edit.extra %}
          {{ entity_macros.extra_metadata(edit.extra) }}
        {% endif %}
      </div>
    </div>
    {% endfor %}
  </div>
</div>
{%- endmacro %}

{# extended by changelog_entry #}
{% block editgroupheader %}
{% if not editgroup.changelog_index %}
  <div class="ui right floated center aligned segment">
    {% if auth_to.accept %}
      <form id="submit_editgroup_form" method="POST" action="/editgroup/{{ editgroup.editgroup_id }}/accept">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
        <button class="ui orange button">Accept Edits</button>
      </form><br>
    {% endif %}
    {% if auth_to.submit %}
      {% if editgroup.submitted %}
        <form id="submit_editgroup_form" method="POST" action="/editgroup/{{ editgroup.editgroup_id }}/unsubmit">
          <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
          <button class="ui button">Un-Submit</button>
        </form><br>
        <form id="submit_editgroup_form" method="POST" action="/editgroup/{{ editgroup.editgroup_id }}/submit">
          <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
          <button class="ui button">Re-Submit</button>
        </form>
      {% else %}
        <form id="submit_editgroup_form" method="POST" action="/editgroup/{{ editgroup.editgroup_id }}/submit">
          <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
          <button class="ui primary button">Submit</button>
        </form>
      {% endif %}
    {% endif %}
  </div>
{% endif %}

<h1 class="ui header">Editgroup
<span class="sub header"><code>editgroup {{ editgroup.editgroup_id }}</code></span></h1>
{% endblock %}

<p><b>What is an editgroup?</b>
An editgroup is a set of entity edits, bundled together into a coherent,
reviewable bundle.
<br>

<br><b>Status:</b>
{% if editgroup.changelog_index %}
  Merged (<a href="/changelog/{{ editgroup.changelog_index }}">Changelog #{{ editgroup.changelog_index }}</a>)
{% elif editgroup.submitted %}
  Submitted ({{ editgroup.submitted.strftime("%Y-%m-%d %H:%M:%S") }})
{% else %}
  Not Submitted
{% endif %}

<br><b>Editor:</b> <a href="/editor/{{editgroup.editor_id}}">{{ editgroup.editor.username }}</a>
<br><b>Description:</b>
{% if editgroup.description %}
  {{ editgroup.description }}
{% else %}
  <i>none</i>
{% endif %}
{% if editgroup.extra %}
  <h4>Extra Metadata (raw JSON)</h4>
  {{ entity_macros.extra_metadata(editgroup.extra) }}
{% endif %}
<br><br style="clear: both;">

<div class="ui styled fluid accordion">
  {{ edit_list(auth_to, editgroup, editgroup.edits.releases, "release", "Release") }}
  {{ edit_list(auth_to, editgroup, editgroup.edits.works, "work", "Work") }}
  {{ edit_list(auth_to, editgroup, editgroup.edits.containers, "container", "Container") }}
  {{ edit_list(auth_to, editgroup, editgroup.edits.creators, "creator", "Creator") }}
  {{ edit_list(auth_to, editgroup, editgroup.edits.files, "file", "File") }}
  {{ edit_list(auth_to, editgroup, editgroup.edits.filesets, "fileset", "File Set") }}
  {{ edit_list(auth_to, editgroup, editgroup.edits.webcaptures, "webcapture", "Web Capture") }}
</div>

<br>
<h2 class="ui header">Comments and Annotations</h2>
{% for annotation in editgroup.annotations|reverse %}
  <div class="ui segments">
    <div class="ui top attached secondary segment">
      {% if annotation.editor.is_bot %}
        <i class="icon bug"></i>
      {% else %}
        <i class="icon user"></i>
      {% endif %}
      <b><a href="/editor/{{ annotation.editor_id }}">{{ annotation.editor.username}}</a></b>
      {% if annotation.editor.is_admin %}
        <span class="ui tiny olive label">Admin</span>
      {% endif %}
      at {{ annotation.created.strftime("%Y-%m-%d %H:%M:%S") }}
      {# TODO: get individual editgroup annotation not supported yet
      (<a href="{% if config.FATCAT_DOMAIN == 'dev.fatcat.wiki' %}http://localhost:9411{% else %}https://api.{{ config.FATCAT_DOMAIN }}{% endif %}/v0/editgroup/{{ annotation.editgroup_id }}/annotation/{{ annotation.annotation_id }}">as JSON</a>)
      #}
      {% if annotation.extra and annotation.extra.disposition %}
        {% set disp = annotation.extra.disposition %}
        <span class="ui small {% if disp == "accept" %}green{% elif disp == "reject" %}red{% else %}orange{%endif %} label" style="float: right;">{{ annotation.extra.disposition|capitalize }}</span>
      {% endif %}
    </div>
    {% if annotation.extra %}
      <div class="ui attached tertiary segment accordion" style="padding-top: 0.2em; padding-bottom: 0.2em;">
        <div class="title">
          <i class="dropdown icon"></i>Review Metadata (raw JSON)
        </div>
        <div class="content" style="padding-bottom: 0.5em;">
          {{ entity_macros.extra_metadata(annotation.extra) }}
        </div>
      </div>
    {% endif %}
    <div class="ui bottom attached segment">
      {{ annotation.comment_markdown|markdown(escape=True) }}
    </div>
  </div>
{% else %}
  <i>None!</i>
{% endfor %}

{% if not editgroup.changelog_index and auth_to.annotate %}
  <div class="ui segment">
    <h3 class="ui header">Add Comment</h3>
    <form class="ui form" id="submit_editgroup_annotation_form" method="POST" action="/editgroup/{{ editgroup.editgroup_id }}/annotation">
      <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
      <div class="field">
        <textarea rows="2" name="comment_markdown" required type="text" value=""></textarea>
      </div>
      <i>Markdown is allowed</i>
      <button class="ui right floated primary button">
        <i class="icon edit"></i> Submit
      </button>
      <br>
    </form><br>
  </div>
{% endif %}

{% endblock %}

{% block postscript %}
<script>
$('.ui.accordion')
  .accordion({ exclusive: false });
</script>
{% endblock %}