{% extends 'base.html' %}
{% load humanize %}
{% block title %}Reservation abuse{% endblock %}
{% block extrahead %}
	{% load static %}
	<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 %}
	<h1>Reservation abuse</h1>
	<p>
		This page ranks users that abuse reservation privileges. Abuse is defined as cancelling, moving, or extending
		a reservation shortly before the reservation would have started. A point penalty is calculated per abuse.
		Point penalties increase the shorter the notice of a reservation change. (That is, cancelling a reservation right
		before it would have started will incur a high amount of points, while cancelling it 6 hours before would incur
		fewer points).
	</p>

	<p>The weight & details of what constitutes abuse can be configured (though reasonable defaults have been chosen).</p>

	<form class="form-horizontal" method="get" action="{% url 'abuse' %}">
		<div class="form-group">
			<label for="cancellation_horizon" class="col-sm-3 control-label">Cancellation horizon</label>
			<div class="col-sm-2">
				<div class="input-group">
					<input id="cancellation_horizon" name="cancellation_horizon" type="text" class="form-control" value="{{ form.cancellation_horizon.value }}">
					<span class="input-group-addon">hours</span>
				</div>
			</div>
		</div>
		<div class="form-group">
			<label for="cancellation_penalty" class="col-sm-3 control-label">Cancellation penalty</label>
			<div class="col-sm-2">
				<div class="input-group">
					<span class="input-group-addon">Up to</span>
					<input id="cancellation_penalty" name="cancellation_penalty" type="text" class="form-control" value="{{ form.cancellation_penalty.value }}">
					<span class="input-group-addon">points</span>
				</div>
			</div>
		</div>
		<div class="form-group">
			<label for="target" class="col-sm-3 control-label">Evaluate for the</label>
			<div class="col-sm-3">
				<select id="target" name="target" class="form-control">
					<option value="">Entire NanoFab</option>
					{% for tool in tools %}
						<option value="{{ tool.id }}">{{ tool.name }}</option>
					{% endfor %}
				</select>
			</div>
		</div>
		<div class="form-group">
			<label for="start" class="col-sm-3 control-label">Starting</label>
			<div class="col-sm-3">
				<input id="start" type="text" name="start" class="form-control" value="{{ form.start.value }}">
			</div>
			<label for="end" class="col-sm-1 control-label">Ending</label>
			<div class="col-sm-3">
				<input id="end" type="text" name="end" class="form-control" value="{{ form.end.value }}">
			</div>
		</div>
		<div class="form-group">
			<div class="col-sm-3">
				<input type="submit" id="generate_report" class="btn btn-warning pull-right" data-loading-text="Generating report ..." onclick="$('#generate_report').button('loading')" value="Generate report">
			</div>
		</div>
	</form>

	{% if results %}
		<div>
			<table class="table">
				<tr>
					<th>User</th>
					<th>Points</th>
				</tr>
				{% for user, score in results %}
					<tr>
						<td><a href="{% url 'user_drill_down' %}?user={{ user.id }}&cancellation_horizon={{ form.cancellation_horizon.value }}&cancellation_penalty={{ form.cancellation_penalty.value }}&target={{ form.target.value }}&start={{ form.start.value }}&end={{ form.end.value }}">{{ user }}</a></td>
						<td>{{ score|intcomma }}</td>
					</tr>
				{% endfor %}
			</table>
		</div>
	{% endif %}

	<script>
		var timepicker_properties = {format: 'MM/DD/YYYY'};
		$('#start').datetimepicker(timepicker_properties);
		$('#end').datetimepicker(timepicker_properties);
	</script>

{% endblock %}