{% extends 'base.html' %}
{% load custom_tags_and_filters %}
{% load static %}
{% load tz %}
{% block title %}
    {% if form.instance.id %}
        Modify adjustment request
    {% else %}
        New adjustment request
    {% endif %}
{% endblock %}
{% block extrahead %}
    <script type="text/javascript" src="{% static "datetimepicker/bootstrap-datetimepicker.js" %}"></script>
    <link rel="stylesheet" type="text/css" href="{% static "datetimepicker/bootstrap-datetimepicker.css" %}" />
{% endblock %}
{% block content %}
    {% with instance_id=form.instance.id %}
        <h1 class="form-group">
            {% if instance_id %}
                Modify adjustment request
            {% else %}
                New adjustment request
            {% endif %}
        </h1>
        <form class="well"
              action="{% if instance_id %}{% url 'edit_adjustment_request' instance_id %}{% else %}{% url 'create_adjustment_request' %}{% endif %}"
              method="post">
            {% csrf_token %}
            {% if form.non_field_errors %}<div class="alert alert-danger">{{ form.non_field_errors }}</div>{% endif %}
            {% if instance_id and user in form.instance.reviewers %}
                <div class="form-group">
                    <div class="row">
                        <div class="col-sm-6">
                            <label for="creator_readonly" class="control-label">Creator</label>
                            <input id="creator_readonly"
                                   class="form-control"
                                   style="min-width: 250px"
                                   disabled
                                   value="{{ form.instance.creator }}">
                        </div>
                    </div>
                </div>
            {% endif %}
            <div class="form-group">
                <div class="row">
                    <div class="col-sm-12">
                        <input type="hidden" name="item_type" value="{{ form.item_type.value|default_if_none:"" }}">
                        <input type="hidden" name="item_id" value="{{ form.item_id.value|default_if_none:"" }}">
                        <label for="billable_item" class="control-label">Charge</label>
                        <select class="form-control" id="billable_item" name="item">
                            <option url="{% if instance_id %}{% url 'edit_adjustment_request' instance_id %}{% else %}{% url 'create_adjustment_request' %}{% endif %}"
                                    selected
                                    value="">General adjustment request</option>
                            {% if form.instance.item %}
                                <option url="{% if instance_id %}{% url 'edit_adjustment_request' instance_id form.item_type.value form.item_id.value %}{% else %}{% url 'create_adjustment_request' form.item_type.value form.item_id.value %}{% endif %}"
                                        value="{{ form.item_type.value }}|{{ form.item_id.value }}"
                                        selected>{{ form.instance.item|billable_display:user }}</option>
                            {% endif %}
                            {% for charge in eligible_items %}
                                {% with charge_type=charge|content_type %}
                                    <option url="{% if instance_id %}{% url 'edit_adjustment_request' instance_id charge_type.id charge.id %}{% else %}{% url 'create_adjustment_request' charge_type.id charge.id %}{% endif %}"
                                            value="{{ charge_type.id }}|{{ charge.id }}"
                                            {% if form.item_type.value|to_int == charge_type.id and form.item_id.value|to_int == charge.id %}selected{% endif %}>
                                        {{ charge|billable_display:user }}
                                    </option>
                                {% endwith %}
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-sm-12 help-block">
                        Only the {{ "adjustment_requests"|customization:"adjustment_requests_charges_display_number" }} most recent charges are displayed. To find earlier charges, go to your <a href='{% url "user_usage" %}'>Usage</a> page and create an adjustment request from there.
                    </div>
                </div>
            </div>
            {% if item.can_times_be_changed and form.item_id.value and form.item_type.value %}
                <div class="form-group" id="new_times">
                    <div class="row">
                        <div class="col-md-3 col-sm-6">
                            <label for="new_start">New start</label>
                            <input type="text"
                                   class="form-control text-center"
                                   name="new_start"
                                   id="new_start"
                                   value="{{ form.new_start.value|input_date_format }}">
                        </div>
                        <div class="col-md-3 col-sm-6">
                            <label for="new_end">New end</label>
                            <input type="text"
                                   class="form-control text-center"
                                   name="new_end"
                                   id="new_end"
                                   value="{{ form.new_end.value|input_date_format }}">
                        </div>
                    </div>
                    {% if form.new_start.errors or form.new_end.errors %}
                        <div class="row">
                            <div class="col-md-3 col-sm-6">
                                {% if form.new_start.errors %}<span style="color:red">{{ form.new_start.errors|striptags }}</span>{% endif %}
                            </div>
                            <div class="col-md-3 col-sm-6">
                                {% if form.new_end.errors %}<span style="color:red">{{ form.new_end.errors|striptags }}</span>{% endif %}
                            </div>
                        </div>
                    {% endif %}
                </div>
            {% endif %}
            {% if item.can_quantity_be_changed and form.item_id.value and form.item_type.value %}
                <div class="form-group" id="new_quantity">
                    <div class="row">
                        <div class="col-md-3 col-sm-6">
                            <label for="new_quantity">New quantity</label>
                            <input type="text"
                                   class="form-control text-center"
                                   name="new_quantity"
                                   id="new_quantity"
                                   value="{{ form.new_quantity.value|default_if_none:'' }}">
                        </div>
                    </div>
                    {% if form.new_quantity.errors %}
                        <div class="row">
                            <div class="col-md-3 col-sm-6">
                                {% if form.new_quantity.errors %}<span style="color:red">{{ form.new_quantity.errors|striptags }}</span>{% endif %}
                            </div>
                        </div>
                    {% endif %}
                </div>
            {% endif %}
            {% if item.can_be_waived %}
                <div class="form-group">
                    <label>
                        <input type="checkbox"
                               id="waive_charge"
                               name="waive"
                               {% if form.waive.value %}checked{% endif %}
                               value="waive"
                               onchange="toggle_waive_fields();">
                        I request that this charge be waived
                    </label>
                </div>
            {% endif %}
            <div class="form-group">
                <label for="description" class="control-label">Description</label>
                {% if form.description.errors %}- <span style="color:red">{{ form.description.errors|striptags }}</span>{% endif %}
                <textarea class="form-control"
                          rows="3"
                          name="description"
                          id="description"
                          placeholder="Explain the reason for this request"
                          required>{{ form.description.value|default_if_none:"" }}</textarea>
            </div>
            {% if form.instance.get_status_display == "Pending" and instance_id and user in form.instance.reviewers %}
                <div class="form-group">
                    <label for="manager_note" class="control-label">Feedback/Instructions</label>
                    {% if form.description.errors %}
                        - <span style="color:red">{{ form.manager_note.errors|striptags }}</span>
                    {% endif %}
                    <textarea class="form-control"
                              rows="3"
                              name="manager_note"
                              id="manager_note"
                              placeholder="Feedback to send to the user if the request is denied or instructions to the user office if it's approved">{{ form.manager_note.value|default_if_none:"" }}</textarea>
                </div>
            {% endif %}
            <div class="text-right">
                {% if form.instance.get_status_display == "Pending" %}
                    {% if instance_id and user in form.instance.reviewers %}
                        {% if form.instance.adjustable_charge and "adjustment_requests"|customization:"adjustment_requests_apply_button" == "enabled" %}
                            {% button type="save" submit=False name="approve_apply_request" title="Approve and adjust the original charge" icon="glyphicon-ok-circle" value="Approve and adjust" onclick="confirm_review_dialog(this, 'approve and adjust the actual charge of');" %}
                            {% button type="save" submit=False name="approve_request" title="Approve without adjusting" icon="glyphicon-ok-circle" value="Approve only" onclick="confirm_review_dialog(this);" %}
                        {% else %}
                            {% button type="save" submit=False name="approve_request" title="Approve request" icon="glyphicon-ok-circle" value="Approve" onclick="confirm_review_dialog(this);" %}
                        {% endif %}
                        {% button type="delete" submit=False name="deny_request" title="Deny request" icon="glyphicon-ban-circle" value="Deny" onclick="confirm_review_dialog(this);" %}
                    {% endif %}
                    {% if not instance_id or form.instance.creator == user %}
                        {% button type="save" value=instance_id|yesno:"Save changes,Create adjustment request" %}
                    {% endif %}
                {% endif %}
            </div>
        </form>
    {% endwith %}
    <script>
        let timepicker_properties =
		{
			format: '{{ datetime_input_js_format }}',
            showClose: true,
            useCurrent: false
		};
        $("#new_end").datetimepicker(timepicker_properties);
        $("#new_start").datetimepicker(timepicker_properties);
        $('#billable_item').on('change', function() {
            window.location.href = this.options[this.selectedIndex].getAttribute("url");
        });
        function confirm_review_dialog(review_button, decision_text)
        {
            let decision = review_button.innerText.trim().toLowerCase();
            decision_text = decision_text || decision 
            let dialog_text = "Are you sure you want to "+decision_text+" this request?";
            let note = $("#manager_note").val();
            if (note && decision === "approve")
            {
                dialog_text += "\n\nThe following instructions will be sent to the *User Office*:\n\n" + note;
            }
            else if (note && decision === "deny")
            {
                dialog_text += "\n\nThe following feedback will be sent to *{{ form.instance.creator.get_name }}*:\n\n" + note;
            }
            if (confirm(dialog_text))
            {
                submit_and_disable(review_button)
            }
        }
	    function toggle_waive_fields()
        {
            let waive_checkbox = $("#waive_charge")[0];
            if (waive_checkbox)
            {
                $("#new_quantity, #new_times").toggle(!waive_checkbox.checked);
            }
        }
        
        toggle_waive_fields();
    </script>
{% endblock %}
