{% extends "base.html" %}

{% block inside_head %}
	<style>
		section.elements {
			display: flex;
			flex-wrap: wrap;
			gap: 4em;
			align-items: flex-start;
		}
	</style>
{% endblock %}

{% block title %}
	&raquo; Report Runs &raquo; {{row.report_run.id}} &raquo; Row {{row.id}}
{% endblock %}

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


{% block content %}
	{% set values = row.data['values'] %}
	{% set properties = row.data.get('properties', {}) %}
	<table>
		<caption>Bill Check</caption>	
		<thead>
			<tr>
				<th>Batch</th>
				<th>Site</th>
				<th>MPRN</th>
				<th>Covered Bills</th>
				<th>Covered Start</th>
				<th>Covered Finish</th>
				<th>Problem</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td><a href="/g/batches/{{values.g_batch_id}}">{{values.batch}}</a></td>
				<td>
					<a href="/sites/{{values.site_id}}">{{values.site_code}} {{values.site_name}}</a>
				</td>
				<td>
					<a href="/g/supplies/{{values.g_supply_id}}">{{values.mprn}}</a>
				</td>
				<td>	
					{% if values['covered_bill_ids'] is not none %}
						{% for bill_id in values['covered_bill_ids'] %}
								<a href="/g/bills/{{bill_id}}">{{bill_id}}</a>
								{% if bill_id == values.g_bill_id %}
								{% endif %}
						{% endfor %}
					{% endif %}
				</td>
				<td><span title="{{values.covered_start}}">{{values.covered_start[:10]}}</span></td>
				<td><span title="{{values.covered_finish}}">{{values.covered_finish[:10]}}</span></td>
				{% for title in columns %}
					<td>	
						{% if title in values and values[title] is not none %}
							{{"%.2f"|format(values[title])}}
						{% endif %}
					</td>
				{% endfor %}
				<td>
					{{ values.covered_problem }} {{ values.virtual_problem }}
				</td>
			</tr>
		</tbody>
	</table>

	<section class="elements">
		<table>
			<caption>GBP</caption>
			<thead>
				<tr>
					<th>Part</th>
					<th>Covered</th>
					<th>Virtual</th>
					<th>Difference</th>
				</tr>
			</thead>
			<tbody>
				{% if 'difference_gross_gbp' in values %}
					<tr>
						<td>gross</td>
						<td>{{"{:0,.2f}".format(values.covered_gross_gbp)}}</td>
						<td>{{"{:0,.2f}".format(values.virtual_gross_gbp)}}</td>
						<td>{{"{:0,.2f}".format(values.difference_gross_gbp)}}</td>
					</tr>
				{% endif %}
					<tr>
						<td>net</td>
						<td>{{"{:0,.2f}".format(values.covered_net_gbp)}}</td>
						<td>{{"{:0,.2f}".format(values.virtual_net_gbp)}}</td>
						<td>{{"{:0,.2f}".format(values.difference_net_gbp)}}</td>
					</tr>
				{% if 'difference_vat_gbp' in values %}
					<tr>
						<td>vat</td>
						<td>{{"{:0,.2f}".format(values.covered_vat_gbp)}}</td>
						<td>{{"{:0,.2f}".format(values.virtual_vat_gbp)}}</td>
						<td>{{"{:0,.2f}".format(values.difference_vat_gbp)}}</td>
					</tr>
				{% endif %}
				{% if 'difference_vat_rate' in values and values.difference_vat_rate != 0 %}
					<tr>
						<td>vat_rate</td>
						<td>{{values.covered_vat_rate}}</td>
						<td>{{values.virtual_vat_rate}}</td>
						<td>{{"{:0,.2f}".format(values.difference_vat_rate)}}</td>
					</tr>
				{% endif %}
				{% if 'difference_is_commercial' in values %}
					<tr>
						<td>is_commercial</td>
						<td>{{values.covered_is_commercial}}</td>
						<td>{{values.virtual_is_commercial}}</td>
						<td>{{values.difference_is_commercial}}</td>
					</tr>
				{% endif %}

		</table>

		<table>
			<caption>kWh</caption>
			<thead>
				<tr>
					<th>Part</th>
					<th>Covered</th>
					<th>Virtual</th>
					<th>Difference</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>kwh</td>
					<td>{{"{:0,.2f}".format(values.covered_kwh)}}</td>
					<td>{{"{:0,.2f}".format(values.virtual_kwh)}}</td>
					<td>{{"{:0,.2f}".format(values.difference_kwh)}}</td>
				</tr>
				<tr>
					<td>units_consumed</td>
					<td>
						{% if 'covered_units_consumed' in values %}
							{{"{:0,.2f}".format(values.covered_units_consumed)}}
						{% endif %}
					</td>
					<td>{{"{:0,.2f}".format(values.virtual_units_consumed)}}</td>
					<td>{{"{:0,.2f}".format(values.difference_units_consumed)}}</td>
				</tr>
				<tr>
					<td>correction_factor</td>
					<td>
						{% if values.covered_correction_factor is number %}
							{{"{:0,.2f}".format(values.covered_correction_factor)}}
						{% else %}
							{{values.covered_correction_factor|join(' | ')}}
						{% endif %}
					</td>
					<td>
						{% if values.virtual_correction_factor is number %}
							{{"{:0,.2f}".format(values.virtual_correction_factor)}}
						{% else %}
							{{values.virtual_correction_factor|join(' | ')}}
						{% endif %}
					</td>
					<td>
						{% if values.difference_correction_factor is number %}
							{{"{:0,.2f}".format(values.difference_correction_factor)}}</td>
						{% else %}
							{{values.difference_correction_factor|join(' | ')}}
						{% endif %}
					</td>
				</tr>
				<tr>
					<td>unit_code</td>
					<td>{{values.covered_unit_code|join(' | ')}}</td>
					<td>{{values.virtual_unit_code|join(' | ')}}</td>
					<td>{{values.difference_unit_code}}</td>
				</tr>
				<tr>
					<td>calorific_value</td>
					<td>{{values.covered_calorific_value|join(' | ')}}</td>
					<td>{{values.virtual_calorific_value|join(' | ')}}</td>
					<td>{{values.difference_calorific_value}}</td>
				</tr>
		</table>

		{% for table in tables %}
			<table>
				<caption>{{table.name}}</caption>
				<thead>
					<tr>
						<th>Part</th>
						<th>Covered</th>
						<th>Virtual</th>
						<th>Difference</th>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td>gbp</td>
						{% for pref in ('covered_', 'virtual_', 'difference_') %}
							<td>
								{% set k = pref + table.name + "_gbp" %}
								{% if k in values %}
									{% set val = values[k] %}
								{% else %}
									{% set val = '' %}
								{% endif %}
								{% if val is number %}
									{{"{:0,.2f}".format(val)}}
								{% else %}
									{{val}}
								{% endif %}
							</td>
						{% endfor %}
					</tr>
					{% for part in table['parts']|sort %}
						<tr>
							<td>{{part}}</td>
							{% for pref in ('covered_', 'virtual_', 'difference_') %}
								<td>
									{% set k = pref + table.name + "_" + part %}
									{% if k in values %}
										{% set val = values[k] %}
									{% else %}
										{% set val = '' %}
									{% endif %}
									{% if val is number %}
										{{"{:0,.2f}".format(val)}}
									{% elif (val is string) or (val is boolean) %}
										{{val}}
									{% else %}
										{% for v in val %}
											{% if v is number %}
												{{"{:0,.2f}".format(v)}}
											{% else %}
												{{v}}
											{% endif %}
											{% if not loop.last %}
												|
											{% endif %}
										{% endfor %}
									{% endif %}
								</td>
							{% endfor %}
						</tr>
					{% endfor %}
				</tbody>
			</table>
		{% endfor %}
	</section>

	<h2>Raw</h2>	
	<pre>{{raw_data}}</pre>
{% endblock %}
