{% load custom_tags_and_filters %}
<div style="margin: 15px 0">
    <div style="display: table-cell; width: 100%">
        {% if adjustment_requests_description %}
            <div class="well" style="display: table; margin-right: 15px">
                <span class="requests-description-info glyphicon glyphicon-info-sign primary-highlight"></span>
                <div class="requests-description">{{ adjustment_requests_description|safe|linebreaksbr }}</div>
            </div>
        {% endif %}
    </div>
    <div class="text-right" style="display: table-cell; vertical-align: top">
        {% button type="add" url="create_adjustment_request" value="New request" %}
    </div>
</div>
{% if pending_adjustment_requests or approved_adjustment_requests or denied_adjustment_requests %}
    {% if user.is_superuser or user.is_facility_manager or user.is_user_office or user.is_accounting_officer %}
        <div class="text-right" style="margin-bottom: -20px">
            {% button type="export" value="Export" url="export_adjustment_requests" target="_blank" %}
        </div>
    {% endif %}
    <div>
        {% if pending_adjustment_requests %}
            <h3>
                <a onclick="toggle_details(this)"
                   data-toggle="collapse"
                   data-target="#pending_adjustment_requests"
                   class="pointer">
                    <span class="glyphicon glyphicon-chevron-down pull-left chevron"></span>Pending ({{ pending_adjustment_requests|length }})
                </a>
            </h3>
            <div id="pending_adjustment_requests" class="collapse in">
                {% include 'requests/adjustment_requests/adjustment_requests_table.html' with request_status='pending' adjustment_request_list=pending_adjustment_requests %}
            </div>
        {% endif %}
        {% if approved_adjustment_requests %}
            <h3>
                <a onclick="toggle_details(this)"
                   data-toggle="collapse"
                   data-target="#approved_adjustment_requests"
                   class="pointer">
                    <span class="glyphicon glyphicon-chevron-down pull-left chevron"></span>Approved
                </a>
            </h3>
            <div id="approved_adjustment_requests" class="collapse in">
                {% include 'requests/adjustment_requests/adjustment_requests_table.html' with request_status='approved' adjustment_request_list=approved_adjustment_requests %}
            </div>
        {% endif %}
        {% if denied_adjustment_requests %}
            <h3>
                <a onclick="toggle_details(this)"
                   data-toggle="collapse"
                   data-target="#denied_adjustment_requests"
                   class="pointer">
                    <span class="glyphicon glyphicon-chevron-down pull-left chevron"></span>Denied
                </a>
            </h3>
            <div id="denied_adjustment_requests" class="collapse in">
                {% include 'requests/adjustment_requests/adjustment_requests_table.html' with request_status='denied' adjustment_request_list=denied_adjustment_requests %}
            </div>
        {% endif %}
    </div>
{% else %}
    <div class="italic" style="margin-top: 20px">You do not have any current active adjustment requests</div>
{% endif %}
<script>
    function show_or_send_reply(button_element, form_selector)
    {
        let form_element = $('#'+form_selector)
        if (form_element.is(":visible"))
        {
            form_element.submit();
        }
        else
        {
            form_element.parents('div').fadeIn('50');
            form_element.children('textarea').focus();
            $(button_element).html('<i class="glyphicon glyphicon-send"></i> Send');
        }
    }

    function mark_adjustment_as_applied(adjustment_id)
    {
        if (confirm("Are you sure you want to mark this adjustment as applied?"))
        {
            let apply_url = "{% url "mark_adjustment_as_applied" 99999 %}".replace("99999", adjustment_id);
            ajax_get(apply_url, undefined, function () {$("#apply_"+adjustment_id+", #applied_"+adjustment_id+", #edit_"+adjustment_id).remove()}, ajax_failure_callback("Request update failed", "There was a problem marking this adjustment as applied."));
        }
    }

    function apply_adjustment(adjustment_id)
    {
        if (confirm("Are you sure you want to adjust this charge? This cannot be undone"))
        {
            let apply_url = "{% url "apply_adjustment" 99999 %}".replace("99999", adjustment_id);
            ajax_get(apply_url, undefined, load_adjustment_requests, ajax_failure_callback("Request update failed", "There was a problem applying this adjustment."));
        }
    }

    $(function ()
    {
        {# Scroll to msg id if available #}
        let msg_id = window.location.hash.substring(1);
        if (msg_id) document.getElementById(msg_id).scrollIntoView();

        $('[data-toggle="tooltip"]').tooltip();
    })
</script>
