{% load custom_tags_and_filters %}
{% with status_pending=0 status_approved=1 status_denied=2 %}
    <div class="panel panel-default {% if request_obj.status == status_approved %}panel-success{% elif request_obj.status == status_denied %}panel-danger{% else %}panel-info{% endif %}"
         style="margin-bottom: 0;
                border-right: none;
                border-left: none;
                {% if not forloop.last %}border-bottom: none;{% endif %}">
        <!-- Header / Summary -->
        <div class="panel-heading clearfix"
             onclick="toggle_details($('#adjustment_right_{{ request_obj.id }}'), 'up')"
             id="adjustment_{{ request_obj.id }}"
             data-toggle="collapse"
             href="#adjustment_details_{{ request_obj.id }}"
             aria-expanded="false"
             aria-controls="adjustment_details_{{ request_obj.id }}">
            <div class="col-xs-6 text-left" style="padding-left: 0">
                <strong>
                    {% if request_obj.id in request_notifications %}
                        <span class="label label-success" style="margin-left: -7px; font-size: 85%">New</span>
                    {% endif %}
                Adjustment Request #{{ request_obj.id }}</strong>
            </div>
            <div id="adjustment_right_{{ request_obj.id }}" class="col-xs-6 text-right" style="padding-right: 0">
                {% if request_obj.status != status_pending %}
                    {{ request_obj.get_status_display }} by {{ request_obj.reviewer.get_name }}
                    {% if request_obj.status == status_denied and request_obj.manager_note %}
                        <i class="glyphicon glyphicon-info-sign" title="{{ request_obj.manager_note }}"></i>
                    {% endif %}
                {% endif %}
                <i class="glyphicon glyphicon-chevron-down chevron" style="color: inherit; padding-right: inherit"></i>
            </div>
        </div>
        <div class="panel-body clearfix">
            <div class="pull-left">
                <p style="margin-bottom:4px;">
                    {% if user_is_reviewer or user.is_any_part_of_staff %}
                        Requested by <strong>{{ request_obj.creator.get_name }}</strong>
                    {% endif %}
                    on {{ request_obj.creation_time }}
                </p>
                {% if request_obj.item %}<p style="margin-bottom:0;">Charge: {{ request_obj.item.get_display }}</p>{% endif %}
            </div>
        </div>
        <!-- Requested Changes -->
        <div id="adjustment_details_{{ request_obj.id }}"
             class="panel-collapse collapse"
             role="tabpanel"
             aria-labelledby="adjustment_{{ request_obj.id }}">
            <div class="panel-body" style="padding-top: 0">
                <table class="table table-bordered" style="margin-bottom: 0;">
                    <thead>
                        <tr>
                            <th class="active">{{ request_obj.changes_status|capfirst }}:</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% if request_obj.has_item_changes %}
                            <tr>
                                <td>
                                    <ul style="margin-bottom: 0; padding-left: 20px">
                                        {% if request_obj.waive %}
                                            <li>
                                                <strong>
                                                    {% if request_obj.applied %}
                                                        This charge was waived
                                                    {% else %}
                                                        {{ request_obj.creator.get_name }} has requested that this charge be waived
                                                    {% endif %}
                                                </strong>
                                            </li>
                                        {% else %}
                                            {% if request_obj.new_start or request_obj.new_end %}
                                                <li>
                                                    <strong style="width: 90px; display: inline-block;"><u>Start Time:</u></strong>
                                                    {% if not request_obj.new_start %}
                                                        {{ request_obj.get_original_start|date:"SHORT_DATETIME_FORMAT" }} (unchanged)
                                                    {% else %}
                                                        {% if request_obj.get_original_start %}
                                                            <span class="danger-highlight">
                                                                <del>{{ request_obj.get_original_start|date:"SHORT_DATETIME_FORMAT" }}</del>
                                                            </span>
                                                        {% endif %}
                                                        ->
                                                        <span class="success-highlight">{{ request_obj.new_start|date:"SHORT_DATETIME_FORMAT" }}</span>
                                                    {% endif %}
                                                </li>
                                                <li>
                                                    <strong style="width: 90px; display: inline-block;"><u>End Time:</u></strong>
                                                    {% if not request_obj.new_end %}
                                                        {{ request_obj.get_original_end|date:"SHORT_DATETIME_FORMAT" }} (unchanged)
                                                    {% else %}
                                                        {% if request_obj.get_original_end %}
                                                            <span class="danger-highlight">
                                                                <del>{{ request_obj.get_original_end|date:"SHORT_DATETIME_FORMAT" }}</del>
                                                            </span>
                                                        {% endif %}
                                                        ->
                                                        <span class="success-highlight">{{ request_obj.new_end|date:"SHORT_DATETIME_FORMAT" }}</span>
                                                    {% endif %}
                                                </li>
                                                {% if request_obj.get_time_difference %}
                                                    <li>
                                                        <strong style="width: 90px; display: inline-block;"><u>Difference:</u></strong>
                                                        <span>{{ request_obj.get_time_difference }}</span>
                                                    </li>
                                                {% endif %}
                                            {% endif %}
                                            {% if request_obj.new_quantity is not None %}
                                                <li>
                                                    <strong style="width: 90px; display: inline-block;"><u>Quantity:</u></strong>
                                                    {% if request_obj.get_original_quantity is not None %}
                                                        <span class="danger-highlight">
                                                            <del>
                                                                {{ request_obj.get_original_quantity }}
                                                            </del>
                                                        </span>
                                                    {% endif %}
                                                    ->
                                                    <span class="success-highlight">{{ request_obj.new_quantity }}</span>
                                                    {% if request_obj.get_quantity_difference %}<span>({{ request_obj.get_quantity_difference }})</span>{% endif %}
                                                </li>
                                            {% endif %}
                                            {% if request_obj.new_project %}
                                                <li>
                                                    <strong style="width: 90px; display: inline-block;"><u>Project:</u></strong>
                                                    {% if request_obj.get_original_project %}
                                                        <span class="danger-highlight"><del>{{ request_obj.get_original_project }}</del></span>
                                                    {% endif %}
                                                    ->
                                                    <span class="success-highlight">{{ request_obj.new_project }}</span>
                                                </li>
                                            {% endif %}
                                        {% endif %}
                                    </ul>
                                </td>
                            </tr>
                        {% endif %}
                        <tr>
                            <td style="padding: 15px">
                                {% if request_obj.description %}
                                    <div>
                                        <strong>{{ request_obj.creator.get_name }}</strong>
                                        <span class="light-grey">· {{ request_obj.creation_time }}</span>
                                    </div>
                                    <div>{{ request_obj.description|linebreaks }}</div>
                                    {% if request_obj.replies.all %}<hr class="hr-xs">{% endif %}
                                {% endif %}
                                {% if request_obj.replies.all %}
                                    {% for message in request_obj.replies.all %}
                                        <div style="{% if not forloop.last %}margin-bottom: 5px{% endif %}">
                                            <div>
                                                <strong>{{ message.author.get_name }}</strong>
                                                <span class="light-grey">· {{ message.creation_date }}</span>
                                            </div>
                                            <div>{{ message.content|linebreaks }}</div>
                                        </div>
                                        {% if not forloop.last %}<hr class="hr-xs">{% endif %}
                                    {% endfor %}
                                {% endif %}
                                {% if request_obj.status == status_pending %}
                                    {% if user == request_obj.creator or user in request_obj.reviewers %}
                                        <div>
                                            <div class="adjustment-request-reply-textarea" style="display: none;">
                                                <form id="adjustment_request_reply_{{ request_obj.id }}"
                                                      action="{% url 'adjustment_request_reply' request_obj.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="{{ request_obj.user_reply_error|default_if_none:'' }}">
                                                    {% if request_obj.user_reply_error %}
                                                        {% button type="add" size="small" icon="glyphicon-send" value="Reply" disabled="disabled" %}
                                                    {% else %}
                                                        {% button type="add" size="small" icon="glyphicon-send" value="Reply" onclick="show_or_send_reply(this, 'adjustment_request_reply_"|concat:request_obj.id|concat:"')" %}
                                                    {% endif %}
                                                </span>
                                            </div>
                                        </div>
                                    {% endif %}
                                {% endif %}
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <!-- Actions -->
            <div class="text-center" style="padding-bottom: 15px">
                {% if request_obj.status == status_approved or request_obj.status == status_denied %}
                    {% if not request_obj.applied and request_obj.status == status_approved %}
                        {% if customizations|get_item:"adjustment_requests_edit_charge_button" == "enabled" %}
                            {% url "user_requests" "adjustment" as redirect_adjustment_url %}
                            {% admin_edit_url request_obj.item redirect_adjustment_url as edit_url %}
                            {% if edit_url %}
                                {% button id="edit_"|concat:request_obj.id type="edit" value="Edit charge" url=edit_url style="margin-bottom: 5px" %}
                            {% endif %}
                        {% endif %}
                        {% if user.is_user_office or user in request_obj.reviewers %}
                            {% if user in request_obj.reviewers and customizations|get_item:"adjustment_requests_apply_button" == "enabled" and request_obj.is_charge_adjustable %}
                                {% button id="apply_"|concat:request_obj.id icon="glyphicon-edit" type="save" value="Adjust charge" data_text=request_obj.apply_adjustment_text onclick="apply_adjustment(this,'"|concat:request_obj.id|concat:"')" style="margin-bottom: 5px" %}
                            {% endif %}
                            {% button id="applied_"|concat:request_obj.id icon="glyphicon-check" type="save" value="Mark as applied" onclick="mark_adjustment_as_applied("|concat:request_obj.id|concat:")" style="margin-bottom: 5px" %}
                        {% endif %}
                    {% endif %}
                {% else %}
                    {% if request_obj.status == status_pending and user in request_obj.reviewers %}
                        <a class="btn btn-default"
                           title="Approve/Deny"
                           href="{% url 'edit_adjustment_request' request_obj.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_obj.status == status_pending and request_obj.creator == user %}
                        {% url 'edit_adjustment_request' request_obj.id as edit_request_url %}
                        {% url 'delete_adjustment_request' request_obj.id as delete_request_url %}
                        {% button type="edit" value="Edit" title="Edit request" url=edit_request_url %}
                        {% button type="delete" value="Delete" title="Delete request" url=delete_request_url onclick="return confirm('Are you sure you want to delete this request?');" %}
                    {% endif %}
                {% endif %}
            </div>
        </div>
    </div>
{% endwith %}
