{% extends "cms/admin_home.html" %}
{% load danceschool_tags sekizai_tags i18n %}

{% block content %}

<h3>{% trans "Invoice #" %}{{ invoice.id }}
{% if registration %}
	{% blocktrans with name=registration.fullName %}
	for {{ name }}
	{% endblocktrans %}
{% endif %}
</h3>

<dl>
	<dt>{% trans "Date &amp; Time" %}:</dt><dd>{{ invoice.creationDate|date:'DATETIME_FORMAT' }}</dd>
	<dt>{% trans "Net Amount Paid" %}:</dt><dd>{{ invoice.amountPaid }}</dd>
	<dt>{% trans "Price with Discounts" %}:</dt><dd>{{ invoice.total }}</dd>
	<dt>{% trans "Paid Online" %}:</dt><dd>{{ invoice.paidOnline|yesno }}</dd>
	{% if registration %}<dt>{% trans "Student" %}:</dt><dd>{{ registration.student|yesno }}</dd>{% endif %}
</dl>

{% if payments %}
	<div class="panel panel-default">
		<div class="panel-heading"><h4 class="panel-title">{% trans "Payment Details" %}</h4></div>
		<div class="panel-body">
			{% for payment in payments %}
				{{ payment.methodName }}: {{ payment.recordId }}<br />
				{% if not payment.refundable %}
					<strong>{% trans "Note" context "alert" %}:</strong> {% trans "This payment does not appear to be automatically refundable.  Please manually refund the money to this student after filling out this form." %}<br />
				{% endif %}
			{% endfor %}
		</div>
	</div>
{% endif %}

<hr />

<form id="refund_form" method="post" action="">{% csrf_token %}

<h2>{% trans "Total Refund Amount" %}: {{ currencySymbol }}<span id="total_refund_amount">{{ invoice.refunds|floatformat:2 }}</span></h2>

{% if invoice.revenueNotYetReceived %}
	<div class="alert alert-danger" role="alert">{% trans "Revenue has been recorded for this registration that has not yet been marked as received.  In determining any refund amount, please account for revenue that has not yet been received.  Items with revenue not yet marked as received are highlighted below." %}</div>
{% endif %}
{% if invoice.unpaid %}
	<div class="alert alert-danger" role="alert">{% trans "This invoice is not yet marked as paid.  In determining any refund amount, please account for revenue that has not yet been received." %}</div>
{% endif %}

{% if form.non_field_errors %}
	<div class="alert alert-danger" role="alert">
		{{ form.non_field_errors }}
	</div>
{% endif %}
{% if form.id.errors %}
	<div class="alert alert-danger" role="alert">
		<strong>{% trans "Error with ID" %}:</strong>
		{{ form.id.errors }}
	</div>
{% endif %}

{% if form.total_refund_amount.errors %}
	<div class="alert alert-danger" role="alert">
		<strong>{% trans "Error with Total Refund Amount" %}:</strong>
		{{ form.total_refund_amount.errors }}
	</div>
{% endif %}

{% if form.initial_refund_amount.errors %}
	<div class="alert alert-danger" role="alert">
		<strong>{% trans "Error with Initial Refund Amount" %}:</strong>
		{{ form.initial_refund_amount.errors }}
	</div>
{% endif %}

{{ form.id }}
{{ form.total_refund_amount }}
{{ form.initial_refund_amount }}

<input type="submit" class="btn btn-primary" value="{% trans 'Process Refund' %}" />

	<h4>{% trans "Item Details" %}</h4>
	<table class="regrefundtable table" border=1>
	<thead>
		<tr>
			<td>{% trans "Item ID" %}</td>
			<td>{% trans "Item Name" %}</td>
			<td>{% trans "Price (Gross)" %}</td>
			<td>{% trans "Price (Net)" %}</td>
			<td>{% trans "Cancelled" %}</td>
			<td>{% trans "Refunds/Adjustments" %}</td>
		</tr>
	</thead>
	<tbody>
	{% for item in invoice.invoiceitem_set.all %}
		<tr {%if item.revenueNotYetReceived %}class="warning"{% endif %}>
			<td>{{ item.id }}</td>
			<td>{{ item.name }}</td>
			<td id="item_price_{{ item.id }}">{{ currencySymbol }}{{ item.grossTotal|floatformat:2 }}</td>
			<td id="item_netPrice_{{ item.id }}">{{ currencySymbol }}{{ item.total|floatformat:2 }}</td>
			<td>{% get_field_for_object 'item_cancelled' item.id form as field_cancelled %}{{ field_cancelled }}</td>
			<td>{% get_field_for_object 'item_refundamount' item.id form as field_refundAmount %}{{ field_refundAmount }} {{ field_cancelled.errors }}{{ field_refundAmount.errors }}</td>
		</tr>
	{% endfor %}
	</tbody>
	</table>

<div class="form-group">
    {{ form.comments.errors }}
    {{ form.comments.label_tag }}<br />
    {{ form.comments }}
    <p class="help-block">{{ form.comments.help_text }}</p>
</div>
</form>


<hr />

<p>
	<a class="btn btn-default btn-sm" href="{% url 'registration' %}">{% trans "Class Registration Page" %}</a>
	<a class="btn btn-default btn-sm" href="{% url 'viewregistrations_selectevent' %}">{% trans "Select Another Event" %}</a>
</p>

{% addtoblock "js" %}
<script type="text/javascript">
	$(document).ready(function(){
		$('input[id*=_refundamount_]').change(function(){
			var totalRefund = 0;

			$('input[id*=_refundamount_]').each(function(i,n){
				totalRefund += parseFloat($(n).val());
			});

			$('#total_refund_amount').html(totalRefund.toFixed(2));
			$('#id_total_refund_amount').val(totalRefund);
		});

		$('input[id*=_cancelled_]').change(function(){
			if (this.checked) {
				var thisType = this.id.split("_")[1];
				var thisId = this.id.split("_")[3];
				var netPrice = parseFloat($("#" + thisType + "_netPrice_" + thisId).html().replace("{{ currencySymbol }}",""))

				$("#id_" + thisType + "_refundamount_" + thisId).val(netPrice.toFixed(2))
			}


			var totalRefund = 0;

			$('input[id*=_refundamount_]').each(function(i,n){
				totalRefund += parseFloat($(n).val());
			});

			$('#total_refund_amount').html(totalRefund.toFixed(2));
			$('#id_total_refund_amount').val(totalRefund);
		});

		$('input[type=submit]').click(function( event ){
			var alertFlag = false;

			$('input[id*=_cancelled_]:checked').each(function(i,n){
				var thisType = n.id.split("_")[1];
				var thisId = n.id.split("_")[3];
				var thisRefund = parseFloat($("#id_" + thisType + "_refundamount_" + thisId).val());

				if (thisRefund != parseFloat($("#" + thisType + "_netPrice_" + thisId).html().replace("$",""))) {
					alertFlag = true;
				}
			});

			if (alertFlag == true) {
				if (confirm("{% trans 'Cancelled classes are not fully refunded. Confirm that this is desired?' %}")) {
					$('#refund_form').submit();
				}
				else {
					event.preventDefault();
				}
			}
			else {
				$('#refund_form').submit();
			}
		});

	});
</script>
{% endaddtoblock %}

{% endblock %}
