{% extends "base.html" %}

{% block title %}
	&raquo; Report Runs &raquo; {{run.id}}
{% endblock %}

{% block nav %}
	<a href="/report_runs">Report Runs</a> &raquo; {{run.id}}
{% endblock %}

{% block content %}
	{% if request.method == "GET" and request.values.delete %}
		<form method="post" action="/report_runs/{{run.id}}">
			<fieldset>
				<legend>Are you sure you want to delete this report run?</legend>
				<input type="submit" name="delete" value="delete"> 
				<a href="/report_runs/{{run.id}}">Cancel</a>
			</fieldset>
		</form>
	{% elif run.name == 'bill_check' %}
		<table class="etheling">
			<caption>Bill Check</caption>
			<thead>
				<tr>
					<th>Date Created</th>
					<th>Created By</th>
					<th>Title</th>
					<th>State</th>
					<th>Number Of Rows</th>
					<th>Sum Of Difference GBP</th>
					<th>Delete</th>
					<th>Download Spreadsheet</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>{{run.date_created|date_format}}</td>
					<td>{{run.creator}}</td>
					<td>{{run.title}}</td>
					<td>{{run.state}}</td>
					<td>{{rows|length}}</td>
					<td>{{summary.sum_difference}}</td>
					<td>
						<form action="/report_runs/{{run.id}}">
							<fieldset style="border: none;">
								<input type="submit" name="delete" value="Delete">
							</fieldset>
						</form>
					</td>
					<td>
						<a href="/report_runs/{{run.id}}/spreadsheet">Download</a>
					</td>
				</tr>
			</tbody>
		</table>

		{% if summary.missing_bills|length > 0 %}
			<table class="sticky">
				<caption>Some Missing Bills</caption>
				<thead>
					<tr>
						<th>View</th>
						<th>Import MPAN Core</th>
						<th>Export MPAN Core</th>
						<th>Start</th>
						<th>Finish</th>
						<th>Problem</th>
						<th>Estimated Difference GBP</th>
					</tr>
				</thead>
				<tbody>
					{% for row in rows %}
						<tr>
							<td><a href="/report_run_rows/{{row.id}}">View</a></td>
							{% for title in row.data.titles %}
								<td>{{row.data['values'][title]}}</td>
							{% endfor %}
						</tr>
					{% endfor %}
				</tbody>
			</table>
		{% endif %}
		{% if rows|length > 0 %}
			<table class="etheling etheling_sticky">
				<caption>Rows - Ordered By {{order_by}}</caption>
				<thead>
					<tr>
						<th>View</th>
						{% for title in rows[0].data.titles %}
							<th>{{title}}</th>
						{% endfor %}
					</tr>
				</thead>
				<tbody>
					{% for row in rows %}
						<tr>
							<td><a href="/report_run_rows/{{row.id}}">View</a></td>
							{% for title in row.data.titles %}
								<td>{{row.data['values'][title]}}</td>
							{% endfor %}
						</tr>
					{% endfor %}
				</tbody>
			</table>
		{% endif %}
	{% else %}
		<table class="etheling">
			<caption>Report Run</caption>	
			<thead>
				<tr>
					<th>Date Created</th>
					<th>Created By</th>
					<th>Name</th>
					<th>Title</th>
					<th>State</th>
					<th>Number Of Rows</th>
					<th>Sum Of Difference GBP</th>
					<th>Delete</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>{{run.date_created|date_format}}</td>
					<td>{{run.creator}}</td>
					<td>{{run.name}}</td>
					<td>{{run.title}}</td>
					<td>{{run.state}}</td>
					<th>{{rows|length}}</th>
					<td>{{summary.sum_difference}}</td>
					<td>
						<form action="/report_runs/{{run.id}}">
							<fieldset style="border: none;">
								<input type="submit" name="delete" value="Delete">
							</fieldset>
						</form>
					</td>
				</tr>
			</tbody>
		</table>

		{% if rows|length > 0 %}
			<table class="etheling etheling_sticky">
				<caption>Rows - Ordered By {{order_by}}</caption>
				<thead>
					<tr>
						<th>View</th>
						{% for title in rows[0].data.titles %}
							<th>{{title}}</th>
						{% endfor %}
					</tr>
				</thead>
				<tbody>
					{% for row in rows %}
						<tr>
							<td><a href="/report_run_rows/{{row.id}}">View</a></td>
							{% for title in row.data.titles %}
								<td>{{row.data['values'][title]}}</td>
							{% endfor %}
						</tr>
					{% endfor %}
				</tbody>
			</table>
		{% endif %}
	{% endif %}
{% endblock %}
