{% load bootstrap3 %}
{% load i18n %}

{% include 'editor/fragment_modal_close.html' %}
<h3>
    {% if changeset.title %}
        {{ changeset.title }}
    {% else %}
        {% with id=changeset.pk %}
            {% blocktrans %}Change set #{{ id }}{% endblocktrans %}
        {% endwith %}
    {% endif %}
</h3>
<form method="post" action="{{ request.path }}">
{% csrf_token %}
<p class="text-muted">
    {% with datetime=changeset.created|date:"DATETIME_FORMAT" author=changeset.author %}
        {% if author %}
            {% url 'editor.users.detail' pk=changeset.author.pk as author_url %}
            {% blocktrans %}created at {{ datetime }} by <a href="{{ author_url }}">{{ author }}</a>{% endblocktrans %}
        {% else %}
            {% blocktrans %}created at {{ datetime }}{% endblocktrans %}
        {% endif %}
    {% endwith %}
    <br>
    {% with datetime=changeset.last_change.datetime|date:"DATETIME_FORMAT" %}
        {% blocktrans %}last change at {{ datetime }}{% endblocktrans %}
    {% endwith %}
</p>
{% bootstrap_messages %}
{% if changeset.last_state_update and changeset.state != 'unproposed' %}
    <div class="panel panel-{{ changeset.style }}">
        <div class="panel-heading">
            <h3 class="panel-title">
                <i class="glyphicon glyphicon-{{ changeset.icon }} pull-right"></i>
                {{ changeset.get_state_display }}
            </h3>
        </div>
        <div class="panel-body">
            {% if changeset.state == 'proposed' or changeset.state == 'reproposed' %}
                <p>{% blocktrans %}These changes are currently pending review.{% endblocktrans %}</p>
                {% if changeset.author == request.user %}
                    <p>{% blocktrans %}As long as the review is pending you can unpropose them to edit them again.{% endblocktrans %}</p>
                {% endif %}
            {% elif changeset.state == 'review' %}
                {% with user=changeset.assigned_to %}
                    {% url 'editor.users.detail' pk=changeset.assigned_to.pk as user_url %}
                    <p>{% blocktrans %}These changes are currently being reviewed by <a href="{{ user_url }}">{{ user }}</a>.{% endblocktrans %}</p>
                {% endwith %}
            {% elif changeset.state == 'rejected' or changeset.state == 'finallyrejected' %}
                {% with user=changeset.last_state_update.user comment=changeset.last_state_update.comment %}
                    {% url 'editor.users.detail' pk=changeset.last_state_update.user.pk as user_url %}
                    {% if changeset.state == 'rejected' %}
                        <p>{% blocktrans %}These changes were rejected by <a href="{{ user_url }}">{{ user }}</a> with the followed comment:{% endblocktrans %}</p>
                    {% else %}
                        <p>{% blocktrans %}These changes were <strong>finally</strong> rejected by <a href="{{ user_url }}">{{ user }}</a> with the followed comment:{% endblocktrans %}</p>
                    {% endif %}
                    <p>{% blocktrans %}“{{ comment }}”{% endblocktrans %}</p>
                    {% if changeset.author == request.user and changeset.state == 'rejected' %}
                        <p>{% blocktrans %}You can now edit your changes and propose them again.{% endblocktrans %}</p>
                    {% endif %}
                {% endwith %}
            {% elif changeset.state == 'applied' %}
                {% with user=changeset.last_state_update.user %}
                    {% url 'editor.users.detail' pk=changeset.last_state_update.user.pk as user_url %}
                    <p>{% blocktrans %}These changes were applied by <a href="{{ user_url }}">{{ user }}</a>.{% endblocktrans %}</p>
                {% endwith %}
            {% endif %}
            {% if changeset.author == request.user and active %}
                {% url 'editor.users.detail' pk=request.user.pk as user_url %}
                {% if not changeset.closed %}
                    <p>{% blocktrans %}In the meantime, if you want to create a new change set, you can do so <a href="{{ user_url }}">in your profile</a>.{% endblocktrans %}</p>
                {% else %}
                    <p>{% blocktrans %}If you want to create a new change set, you can do so <a href="{{ user_url }}">in your profile</a>.{% endblocktrans %}</p>
                {% endif %}
            {% endif %}
            <span class="text-muted">{{ changeset.last_state_update.datetime|date:"DATETIME_FORMAT" }}</span>
        </div>
    </div>
{% endif %}
{% if changeset.description %}
    <p>{{ changeset.description }}</p>
{% endif %}
{% if changed_objects and operations|length %}
    <p><em>{% blocktranslate count counter=operations|length %}({{ counter }} operation in total){% plural %}({{ counter }} operations in total){% endblocktranslate %}</em></p>
{% endif %}
{% for obj in changed_objects %}
    <table class="table table-condensed table-h-bordered change-group">
        <thead>
            <tr>
                <th colspan="2">
                    {% if obj.edit_url %}
                        <a class="btn btn-default btn-xs pull-right" data-force-next-zoom href="{{ obj.edit_url }}">
                            {% trans 'Edit' %}
                        </a>
                    {% elif obj.deleted and can_edit and not obj.missing_dependencies and not obj.unique_collisions %}
                        <button type="submit" name="restore" value="{{ obj.model_name }}-{{ obj.pk }}" class="btn btn-warning btn-xs pull-right">
                            {% trans 'Restore' %}
                        </button>
                    {% endif %}
                    {% if obj.title %}
                        {{ obj.title }} <small>({{ obj.desc }})</small>
                    {% else %}
                        {{ obj.desc }}
                    {% endif %}
                </th>
            </tr>
        </thead>
        <tbody>
            {% for change in obj.changes %}
                <tr>
                    <td class="text-{{ change.class }}"><i class="glyphicon glyphicon-{{ change.icon }}"></i></td>
                    <td{% if obj.deleted and change.icon != 'minus' %} class="text-muted"{% endif %}>
                        {% if change.missing_dependency or change.unique_collision %}<i class="glyphicon glyphicon-alert pull-right text-warning"></i>{% endif %}
                        {% if change.empty %}<em>{% else %}<u>{% endif %}{{ change.title }}{% if not change.empty %}</u>:{% else %}</em>{% endif %}
                        {% if change.url %}
                            <a href="{{ change.url }}">{{ change.value }}</a>
                        {% else %}
                            {{ change.value }}
                        {% endif %}
                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
{% empty %}
    <p><em>{% trans 'This changeset contains no changes.' %}</em></p>
{% endfor %}

{% buttons %}
    {% if active %}
        {% if can_delete %}
            <button type="submit" class="btn btn-danger" name="delete" value="1">{% trans 'Delete' %}</button>
        {% endif %}
    {% endif %}
    {% if can_unpropose %}
        <button type="submit" class="btn btn-warning" name="unpropose" value="1">{% trans 'Unpropose' %}</button>
    {% endif %}
    {% if can_unreject %}
        <button type="submit" class="btn btn-warning" name="unreject" value="1">{% trans 'Unreject' %}</button>
    {% endif %}
    <div class="pull-right">
        {% if active %}
            {% if can_edit %}
                <a href="{% url 'editor.changesets.edit' pk=changeset.pk %}" class="btn btn-default">{% trans 'Edit' %}</a>
            {% endif %}
            {% if can_propose %}
                <button type="submit" class="btn btn-primary" name="propose" value="1">{% trans 'Propose' %}</button>
            {% endif %}
            {% if can_commit %}
                <button type="submit" class="btn btn-primary" name="commit" value="1">{% trans 'Apply' %}</button>
            {% endif %}
            {% if can_end_review %}
                <button type="submit" class="btn btn-danger" name="reject" value="1">{% trans 'Reject' %}</button>
                <button type="submit" {% if not can_apply %}disabled="disabled" {% endif %}class="btn btn-success" name="apply" value="1">{% trans 'Accept' %}</button>
            {% endif %}
        {% endif %}
        {% if can_start_review %}
            <button type="submit" class="btn btn-primary" name="review" value="1">{% trans 'Review' %}</button>
        {% endif %}
        {% if not active and not changeset.closed and can_edit %}
            <button type="submit" class="btn btn-info" name="activate" value="1">{% trans 'Activate' %}</button>
        {% endif %}
    </div>
{% endbuttons %}
</form>
