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

{% block content %}

<h3>{{ series.name }}</h3>

<dl>
	<dt>{% trans "Total Students" %}:</dt><dd>{{ series.numRegistered }}</dd>
	<dt>{% trans "Location" %}:</dt><dd>{{ series.location.name }}{% if series.room.name %} - {{ series.room.name }}{% endif %}</dd>
	<dt>{% trans "Time" %}:</dt><dd>{{ series.startTime|date:'l, h:i A' }}</dd>
	<dt>{% trans "Drop-Ins Permitted" %}:</dt><dd>{{ series.allowDropins|yesno }}</dd>
</dl>

{% if 'core.checkin_customers' in perms %}
<form id="checkin_form" action="{% url 'formhandler_checkin' %}">
{% endif %}

<table class="classregsummary table table-striped table-responsive">
<thead>
	<tr>
		{% if 'core.checkin_customers' in perms %}
		<th>{% trans "Check In" %}</th>
		{% endif %}
		<th>{% trans "Name" %}</th>
		<th>{% trans "This Series Price" %}</th>
		<th>{% trans "Total Payment" %}</th>
		{% if series.availableRoles %}<th>{% trans "Role" %}</th>{% endif %}
		{% if series.allowDropins %}<th>{% trans "Drop-In" %}</th>{% endif %}
		<th>{% trans "Student" %}</th>
		<th>{% trans "Customer Email" %}</th>
		<th>{% trans "All-Time Classes" %}</th>
		<th>{% trans "Links" %}</th>
	</tr>
</thead>
<tbody>
{% for reg in registrations %}
	<tr>
		{% if 'core.checkin_customers' in perms %}
		<td>
			<input type="checkbox" name="reg_id" value="{{ reg.registration.id }}" {% if reg.checkedIn %}checked{% endif %}/>
		</td>
		{% endif %}
		<td>
			{% if reg.registration.fullName != reg.registration.customer.fullName %}
				<a href="" data-toggle="popover" title="{% trans 'Customer Name' %}" data-container="body" data-placement="top" data-content="{% trans 'Most recent reg. from this email address' %}: {{ reg.registration.customer.fullName }}">{{ reg.registration.fullName }}</a>
			{% else %}
				{{ reg.registration.fullName }}
			{% endif %}
		</td>

		<td {% if reg.warningFlag %}class="warning"{% elif reg.refundFlag %}class="info"{% endif %}>
			{% if reg.discounted or reg.warningFlag %}
				<strong>{% trans "Gross" %}:</strong> {{ reg.price|floatformat:2 }} <br />
				<strong>{% trans "Net" %}:</strong> {{ reg.netPrice|floatformat:2 }}
				{% if reg.refundFlag %}<br />
				<strong>{% trans "Adjustments/Refunds" %}:</strong> {{ reg.invoiceitem.adjustments|floatformat:2 }}
				{% endif %}
				{% if reg.invoiceitem.revenueMismatch or reg.invoiceitem.revenueNotYetReceived %}<br />
				<strong>{% trans "Recorded Revenue" %}:</strong> {{ reg.invoiceitem.revenueReported|floatformat:2 }}<br />
				<strong>{% trans "Received Revenue" %}:</strong> {{ reg.invoiceitem.revenueReceived|floatformat:2 }}<br />
				{% endif %}
			{% else %}
				{{reg.price|floatformat:2 }}
			{% endif %}
		</td>
		
		<td 
		{% if reg.warningFlag %}
			class="warning"
		{% elif reg.refundFlag %}
			class="info"
		{% endif %}>
			{% if reg.registration.warningFlag or reg.registration.refundFlag %}
				<strong>{% trans "Gross Price" %}:</strong> {{ reg.registration.totalPrice|floatformat:2 }} <br />
				<strong>{% trans "Net Price" %}:</strong>
				{% if reg.registration.discounted %}
					<a href="" data-toggle="popover" title="{% trans 'Gross vs. Net Price' %}" data-container="body" data-placement="top" data-content="
					{% if reg.registration.discountCode %}{% trans 'Discount Code' %}: {{ reg.registration.discountCode.name }}, {% endif %}
					{% for v in reg.registration.voucheruse_set.all %}
						{% if v.amount > 0 %}
					 		{% trans 'Voucher' %}: {{ v.voucher.voucherId }} = -{{ v.amount }}, 
						{% endif %}
					{% endfor %}">
					{{ reg.registration.priceWithDiscount|floatformat:2 }}</a>
				{% else %}
				 	{{ reg.registration.priceWithDiscount|floatformat:2 }}
				{% endif %}
				{% if reg.registration.refundFlag %}<br />
				<strong>{% trans "Adjustments/Refunds" %}:</strong> {{ reg.registration.invoice.adjustments|floatformat:2 }}
				{% endif %}
				{% if reg.registration.invoice.outstandingBalance != 0 %}<br />
				<strong>{% trans "Invoice Status" %}:</strong> {{ reg.registration.invoice.statusLabel }}<br />
				<strong>{% trans "Outstanding Balance" %}:</strong> {{ reg.registration.invoice.outstandingBalance|floatformat:2 }}<br />
				{% endif %}
			{% else %}
				{% if reg.registration.discounted %}
					<strong>{% trans "Gross Price" %}:</strong> {{ reg.registration.totalPrice|floatformat:2 }} <br />
					<strong>{% trans "Net Price" %}:</strong>
					<a href="" data-toggle="popover" title="{% trans 'Gross vs. Net Price' %}" data-container="body" data-placement="top" data-content="
					{% if reg.registration.discountCode %}{% trans 'Discount Code' %}: {{ reg.registration.discountCode.name }}, {% endif %}
					{% for v in reg.registration.voucheruse_set.all %}
						{% if v.amount > 0 %}
					 		{% trans 'Voucher' %}: {{ v.voucher.voucherId }} = -{{ v.amount }}, 
						{% endif %}
					{% endfor %}">
					{{ reg.registration.priceWithDiscount|floatformat:2 }}</a>
				{% else %}
					{{ reg.registration.totalPrice|floatformat:2 }}
				{% endif %}
			{% endif %}
		</td>
		
		{% if series.availableRoles %}<td>{{ reg.role }}</td>{% endif %}
		{% if series.allowDropins %}<td>{{ reg.dropIn }}</td>{% endif %}
		<td>{{ reg.registration.student }}</td>
		<td>{{ reg.registration.customer.email }}</td>
		<td>{{ reg.registration.customer.numClassSeries }}</td>
		<td>
		<div class="btn-group">
		{% if 'core.view_all_invoices' in perms and reg.registration.invoice %}
			<a class="btn btn-outline-secondary btn-sm" href="{% url 'viewInvoice' reg.registration.invoice.id %}">{% trans "Invoice" %}</a>
		{% endif %}
		{% if 'core.change_registration' in perms %}
			<a class="btn btn-outline-secondary btn-sm" href="{% url 'admin:core_registration_change' reg.registration.id %}">{% trans "Reg." context "registration" %}</a>
		{% endif %}
		{% if 'core.process_refunds' in perms and reg.registration.invoice %}
			<a class="btn btn-outline-secondary btn-sm" href="{% url 'refundProcessing' reg.registration.invoice.id %}">{% trans "Refund" %}</a>
		{% endif %}
		{% if 'financial.change_revenueitem' in perms and reg.invoiceitem.revenueitem %}
			<a class="btn btn-outline-secondary btn-sm" href="{% url 'admin:financial_revenueitem_change' reg.invoiceitem.revenueitem.id %}">{% trans "Rev. Item" %}</a>
		{% endif %}
		</div>
		</td>
	</tr>
{% endfor %}
</tbody>
</table>

{% if 'core.checkin_customers' in perms %}
	<input type="hidden" name="event_id" value="{{series.id}}" />
	<input class="btn btn-primary" type="submit" value="{% trans 'Submit Checkins' %}" />
	</form>

	<div id="update_ok" style="display:none; font-size: 1.5em; color: #0a0; background-color: #ddd; padding: 0.5em; width: 95%;">{% trans "Check-ins submitted successfully!" %}</div>
	<div id="update_error" style="display:none; font-size: 1.5em; color: #a00; background-color: #ddd; padding: 0.5em; width: 95%;">{% trans "Error submitting class check-in data." %}</div>
{% endif %}

<hr />

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


{% addtoblock "js" %}
	<script type="text/javascript">
	$(function () {
	  $('[data-toggle="popover"]').popover();
	});

	{% if 'core.checkin_customers' in perms %}
	$('input').click(function(){
		$('#update_ok').slideUp();
		$('#update_error').slideUp();
	});

	// Use Jquery to get the cookie value of the CSRF token
	function getCookie(name) {
	    var cookieValue = null;
	    if (document.cookie && document.cookie !== '') {
	        var cookies = document.cookie.split(';');
	        for (var i = 0; i < cookies.length; i++) {
	            var cookie = jQuery.trim(cookies[i]);
	            // Does this cookie string begin with the name we want?
	            if (cookie.substring(0, name.length + 1) === (name + '=')) {
	                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
	                break;
	            }
	        }
	    }
	    return cookieValue;
	}
	var csrftoken = getCookie('csrftoken');

	function csrfSafeMethod(method) {
	    // these HTTP methods do not require CSRF protection
	    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
	}

	// Ensure that CSRF token is passed
	$.ajaxSetup({
	    beforeSend: function(xhr, settings) {
	        if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
	            xhr.setRequestHeader("X-CSRFToken", csrftoken);
	        }
	    }
	});

	$('#checkin_form').submit(function(e) {
		e.preventDefault(); //STOP default action

		var postData = $(this).serializeArray();
		var formURL = $(this).attr("action");

	    $.ajax(
	    {
	        url : formURL,
	        type: "POST",
	        data : postData,
	        success:function(data, textStatus, jqXHR)
	        {
	            $('#update_ok').slideDown();
	        },
	        error: function(jqXHR, textStatus, errorThrown)
	        {
	              $('#update_error').slideDown();
	        }
	    });
	});
	{% endif %}
	</script>
{% endaddtoblock %}

{% endblock %}
