{% load custom_tags_and_filters %}
<table class="table table-condensed table-double-stripped thead-light" style="border: 1px solid #ddd">
    <thead>
        <tr>
            <th>Created</th>
            {% if user_is_reviewer or user.is_any_part_of_staff %}<th>User</th>{% endif %}
            <th class="text-center">Charge</th>
            <th class="text-center text-nowrap">Change</th>
            <th class="text-center">Difference</th>
            <th class="text-right text-nowrap">
                {% if request_status == 'pending' and user_is_reviewer %}
                    Action
                {% elif request_status == 'approved' %}
                    Approved by
                {% elif request_status == 'denied' %}
                    Denied by
                {% endif %}
            </th>
        </tr>
    </thead>
    <tbody>
        {% for a_request in adjustment_request_list %}
            <tr>
                <td>
                    {% if a_request.id in request_notifications %}<span class="label label-success">New</span>{% endif %}
                    {{ a_request.creation_time|date:'SHORT_DATETIME_FORMAT' }}
                </td>
                {% if user_is_reviewer or user.is_any_part_of_staff %}<td>{{ a_request.creator.get_name }}</td>{% endif %}
                <td class="text-center">{{ a_request.item.get_display }}</td>
                <td class="text-center text-nowrap">
                    {% if a_request.get_new_start %}
                        start -> {{ a_request.get_new_start|date:'SHORT_DATETIME_FORMAT' }}
                        {% if a_request.get_new_end %}<br>{% endif %}
                    {% endif %}
                    {% if a_request.get_new_end %}end -> {{ a_request.get_new_end|date:'SHORT_DATETIME_FORMAT' }}{% endif %}
                    {% if a_request.new_quantity is not None %}quantity -> {{ a_request.new_quantity }}{% endif %}
                </td>
                <td class="text-center">{{ a_request.get_difference|default_if_none:"" }}</td>
                <td class="text-right text-nowrap" rowspan="2">
                    {% if request_status == 'approved' or request_status == 'denied' %}
                        {{ a_request.reviewer.get_name }}
                        {% if not a_request.applied and request_status == 'approved' %}
                            {% if "adjustment_requests"|customization:"adjustment_requests_edit_charge_button" == "enabled" %}
                                {% url "user_requests" "adjustment" as redirect_adjustment_url %}
                                {% admin_edit_url a_request.item redirect_adjustment_url as edit_url %}
                                {% if edit_url %}
                                    <br>
                                    {% button id="edit_"|concat:a_request.id type="edit" size="small" value="Edit charge" url=edit_url style="margin-bottom: 5px" %}
                                {% endif %}
                            {% endif %}
                            {% if user.is_user_office or user in a_request.reviewers %}
                                {% if user in a_request.reviewers and "adjustment_requests"|customization:"adjustment_requests_apply_button" == "enabled" and a_request.adjustable_charge %}
                                    <br>
                                    {% button id="apply_"|concat:a_request.id icon="glyphicon-edit" type="save" size="small" value="Adjust charge" onclick="apply_adjustment("|concat:a_request.id|concat:")" style="margin-bottom: 5px" %}
                                {% endif %}
                                <br>
                                {% button id="applied_"|concat:a_request.id icon="glyphicon-check" type="save" size="small" value="Mark as applied" onclick="mark_adjustment_as_applied("|concat:a_request.id|concat:")" style="margin-bottom: 5px" %}
                            {% endif %}
                        {% endif %}
                    {% else %}
                        {% if request_status == 'pending' and user in a_request.reviewers %}
                            <a class="btn btn-sm btn-default"
                               title="Approve/Deny"
                               href="{% url 'edit_adjustment_request' a_request.id %}"><i class="glyphicon glyphicon-ok-circle success-highlight"></i>&nbsp;&nbsp;<i class="glyphicon glyphicon-ban-circle danger-highlight"></i> Review</a>
                        {% endif %}
                        {% if request_status == 'pending' and a_request.creator == user %}
                            {% url 'edit_adjustment_request' a_request.id as edit_request_url %}
                            {% url 'delete_adjustment_request' a_request.id as delete_request_url %}
                            {% button type="edit" size="small" value="Edit" title="Edit request" url=edit_request_url %}
                            {% button type="delete" size="small" value="Delete" title="Delete request" url=delete_request_url onclick="return confirm('Are you sure you want to delete this request?');" %}
                        {% endif %}
                    {% endif %}
                </td>
            </tr>
            <tr>
                <td colspan="{% if user_is_reviewer or user.is_any_part_of_staff %}3{% else %}2{% endif %}"
                    style="border: none">
                    <div style="color: rgb(128,128,128)">
                        {% if a_request.description %}
                            <div>
                                <div class="adjustment-request-message">
                                    {{ a_request.description|linebreaksbr }}
                                    <div class="request-item-message-user">{{ a_request.creator.get_name }}, {{ a_request.creation_time }}</div>
                                </div>
                            </div>
                        {% endif %}
                        {% if a_request.replies %}
                            {% for message in a_request.replies %}
                                <div>
                                    <div class="adjustment-request-message">
                                        {% if message.id in reply_notifications %}
                                            <span class="label label-success request-item-message-new-reply">New reply</span>
                                        {% endif %}
                                        {{ message.content|linebreaksbr }}
                                        <div class="request-item-message-user">{{ message.author.get_name }}, {{ message.creation_date }}</div>
                                    </div>
                                </div>
                            {% endfor %}
                        {% endif %}
                        {% if a_request.get_status_display == "Pending" %}
                            {% if user == a_request.creator or user in a_request.reviewers %}
                                <div>
                                    <div class="adjustment-request-reply-textarea" style="display: none;">
                                        <form id="adjustment_request_reply_{{ a_request.id }}"
                                              action="{% url 'adjustment_request_reply' a_request.id %}"
                                              method="post">
                                            {% csrf_token %}
                                            <textarea required name="reply_content" aria-label="Reply content" style="width: 100%"></textarea>
                                        </form>
                                    </div>
                                    <div class="text-right" style="clear: both;">
                                        <span data-toggle="tooltip" title="{{ a_request.user_reply_error|default_if_none:'' }}">
                                            {% if a_request.user_reply_error %}
                                                {% button type="add" size="xsmall" icon="glyphicon-send" value="Reply" disabled="disabled" %}
                                            {% else %}
                                                {% button type="add" size="xsmall" icon="glyphicon-send" value="Reply" onclick="show_or_send_reply(this, 'adjustment_request_reply_"|concat:a_request.id|concat:"')" %}
                                            {% endif %}
                                        </span>
                                    </div>
                                </div>
                            {% endif %}
                        {% endif %}
                    </div>
                </td>
                <td colspan="4" style="border: none"></td>
            </tr>
        {% endfor %}
    </tbody>
</table>
