{% extends "base.html" %}

{% block title %}
	&raquo; Sites &raquo; {{site.name}}
{% endblock %}

{% block nav %}
	<a href="/sites">Sites</a> &raquo;
 	{{site.name}} [<a href="/sites/{{site.id}}/edit">edit</a>]
{% endblock %}

{% block content %}
	<table class="table">
		<tr>
			<th>Code</th>
			<td>{{site.code}}</td>
		</tr>
		<tr>
			<th>Name</th>
			<td>{{site.name}}</td>
		</tr>
		{% if properties.site_reports %}
			<tr>
				<th>Custom Reports</th>
				<td>
					<ul>
						{% for report in properties.site_reports %}
							<li>
								<a href="/local_reports/{{report.id}}/output/?site_id={{site.id}}">{{report.name}}</a>
							</li>
						{% endfor %}
					</ul>
				</td>
			</tr>
		{% endif %}
		{% if properties.site_links %}
			<tr>
				<th>External Links</th>
				<td>
					<ul>
						{% for link in properties.site_links %}
							<li>
								<a href="{{link.href}}{{site.code}}">{{link.name}}</a>
							</li>
						{% endfor %}
					</ul>
				</td>
			</tr>
		{% endif %}
	</table>

	{% if other_sites|length > 0 %}
		<table>
			<caption>Other sites in this group</caption>
			<tr>
				<th>Code</th>
				<th>Name</th>
			</tr>

			{% for osite in other_sites %}
			<tr>
				<td>
					<a href="/sites/{{osite.id}}">{{osite.code}}</a>
				</td>
				<td>{{osite.name}}</td>
			</tr>
			{% endfor %}
		</table>
	{% endif %}

	<table>
		<caption>
			Electricity supplies that power this site
			[<a href="/e/sites/{{site.id}}/add_e_supply">add</a>]
		</caption>
		<thead>
			<tr>
				<th>View</th>
				<th>Name</th>
				<th>From</th>
				<th>To</th>
				<th>Source</th>
				<th>Generator Type</th>
				<th>Import MPAN core</th>
				<th>Export MPAN core</th>
				<th>Physical Location</th>
				<th>Also Supplies</th>
				<th>Meter Category</th>
				<th>Meter Serial Number</th>
			</tr>
		</thead>
		<tbody>
			{% for group in groups %}
				{% set last_era = group['last_era'] %}
				<tr
						{% if not group['is_ongoing'] %}
							style="color: silver;"
						{% endif %}>
					<td>
						<a href="/e/supplies/{{last_era.supply_id}}">view</a>
					</td>
					<td>{{last_era.supply.name}}</td>
					<td>{{group['first_era'].start_date|hh_format}}</td>
					<td>{{last_era.finish_date|hh_format}}</td>
					<td>{{last_era.supply.source.code}}</td>
					<td>
						{% if last_era.supply.generator_type %}
							{{last_era.supply.generator_type.code}}
						{% endif %}
					</td>
					<td>
						{% if last_era.imp_mpan_core %}
							<a
									href="/e/dnos/{{last_era.supply.dno.id}}"
									>{{last_era.supply.dno.dno_code}}</a>
							{{last_era.imp_mpan_core[2:]}}
						{% endif %}
					</td>
					<td>
						{% if last_era.exp_mpan_core %}
							<a
									href="/e/dnos/{{last_era.supply.dno.id}}"
									>{{last_era.supply.dno.dno_code}}</a>
							{{last_era.exp_mpan_core[2:]}}
						{% endif %}
					</td>
					<td>
						{% for site_era in last_era.site_eras %}
							{% if site_era.is_physical %}
								{% if site_era.site.id == site.id %}
									this site
								{% else %}
									<a href="/sites/{{site_era.site.id}}" title="{{site_era.site.name}}">{{site_era.site.code}}</a>
								{% endif %}
							{% endif %}
						{% endfor %}
					</td>
					<td>
						{% for site_era in last_era.site_eras %}
							{% if site_era.site.id != site.id %}
								<a href="/sites/{{site_era.site.id}}" title="{{site_era.site.name}}">{{site_era.site.code}}</a>
							{% endif %}
						{% endfor %}
					</td>
					<td>{{group.meter_category}}</td>
					<td>{{last_era.msn}}</td>
				</tr>
			{% endfor %}
		</tbody>
	</table>

	<ul>
		<li>
			<a href="/sites/{{site.id}}/used_graph?months=1&amp;finish_year={{month_finish.year}}&amp;finish_month={{month_finish.month}}">Graph of site usage</a>
		</li>
		<li>
			<a href="/sites/{{site.id}}/gen_graph?months=1&amp;finish_year={{month_finish.year}}&amp;finish_month={{month_finish.month}}">Generation Graph</a>
		</li>
		<li>
			<a href="/sites/{{site.id}}/months?finish_year={{last_month_finish.year}}&amp;finish_month={{last_month_finish.month}}">
Table of site level monthly kWh, MD kWh etc.</a>
		</li>
		<li>
			<a href="/sites/{{site.id}}/hh_data?year={{month_finish.year}}&amp;month={{month_finish.month}}">Table of hh data</a>
		</li>
		<li>
			<a href="/e/sites/{{site.id}}/site_snags">Site Snags</a>
		</li>
		<li>
			<a href="/e/sites/{{site.id}}/energy_management">Energy Managment Summary</a>
		</li>
	</ul>

	<h2>Electricity Reports</h2>

	<form action="/reports/247" method="post">
		<fieldset>
			<input type="hidden" name="site_id" value="{{site.id}}">
			<legend>Monthly Duration</legend>
			<label>Months Long</label>
			{{ input_number('months', initial='1', size='2', maxlength='2', required=True) }}
			<label>Final Month</label>
			{{ input_date('finish', last_month_finish, resolution='month') }}
			<label>Include bill check tabs?</label>
			{{input_checkbox('is_bill_check')}}
			<input type="submit" value="Download">
		</fieldset>
	</form>			

	<form action="/reports/247" method="post">
		<fieldset>
			<input type="hidden" name="site_id" value="{{site.id}}">
			<input type="hidden" name="months" value="1">
			<input type="hidden" name="by_hh" value="true">
			<legend>Monthly Duration By Half-Hour</legend>
			<label>For</label>
			{{input_date('finish', last_month_finish, resolution='month')}}
			<input type="submit" value="Download">
		</fieldset>
	</form>			

	<form action="/reports/59" method="post">
		<fieldset>
			<input type="hidden" name="site_id" value="{{site.id}}">
			<input type="hidden" name="compression" value="true">
			<legend>Duration</legend>
			<label>Start Date</label>
			{{ input_date('start', last_month_start) }}
			<label>Finish Date</label>
			{{ input_date('finish', last_month_finish) }}
			<input type="submit" value="Download">
		</fieldset>
	</form>			

	<form action="/reports/181">
		<input type="hidden" name="site_id" value="{{site.id}}">
		<fieldset>
			<legend>Download Displaced TRIAD CSV</legend>
			<label>For financial year ending March</label>
			{{input_date(initial=last_month_finish, resolution='year')}}
			<input type="submit" value="Download">
		</fieldset>
	</form>			

	<form action="/reports/29">
		<fieldset>
			<input type="hidden" name="site_id" value="{{site.id}}">
			<legend>HH Data: 48 Column Format</legend>
			<label>Type</label>
			<select name="type">
				<option value="used">Used</option>
				<option value="imp_net">Imported</option>
				<option value="exp_net">Exported</option>
				<option value="imp_gen">Generated</option>
				<option value="displaced">Displaced</option>
				<option value="exp_gen">Parasitic</option>
			</select>
			<label>Months</label> {{input_text('months', '1', 2, 2)}}
			<label>Last Month</label>
			{{input_date('finish', last_month_finish, resolution='month')}}
			<input type="submit" value="Download">
		</fieldset>
	</form>

	<form action="/reports/csv_site_hh_data">
		<fieldset>
			<input type="hidden" name="site_id" value="{{site.id}}">
			<legend>HH Data: HH Per Row Format</legend>
			<label>From</label> {{input_date('start', last_month_start)}}
			<label>To</label> {{input_date('finish', last_month_finish)}}
			<input type="submit" value="Download">
		</fieldset>
	</form>

	<form action="/reports/247" method="post">
		<fieldset>
			<input type="hidden" name="site_id" value="{{site.id}}">
			<legend>Run Scenario With Monthly Duration</legend>
			<select name="scenario_id">
				{% for scenario in scenarios %}
					{{input_option('scenario', scenario.id, scenario.name)}}
				{% endfor %}
			</select>
			<input type="submit" value="Run">
		</fieldset>
	</form>

	<form action="/reports/149" method="post">
		<fieldset>
			<input type="hidden" name="site_id" value="{{site.id}}">
			<legend>Run Scenario With Duration</legend>
			<select name="scenario_id">
				{% for scenario in scenarios %}
					{{input_option('scenario', scenario.id, scenario.name)}}
				{% endfor %}
			</select>
			<input type="submit" value="Run">
		</fieldset>
	</form>

	<h2>Gas</h2>

	<table>
		<caption>Gas supplies that power this site</caption>
		<thead>
			<tr>
				<th>View</th>
				<th>Name</th>
				<th>From</th>
				<th>To</th>
				<th>MPRN</th>
				<th>Physical Location</th>
				<th>Also Supplies</th>
				<th>Meter Serial Number</th>
				<th>Reading Frequency</th>
			</tr>
		</thead>
		<tbody>
			{% for g_group in g_groups %}
				{% set last_g_era = g_group['last_g_era'] %}

				<tr
					{% if not g_group['is_ongoing'] %}
						style="color: silver;"
					{% endif %}
				>

					<td>
						<a href="/g/supplies/{{last_g_era.g_supply_id}}">view</a>
					</td>
					<td>{{last_g_era.g_supply.name}}</td>
					<td>{{g_group['first_g_era'].start_date|hh_format}}</td>
					<td>{{last_g_era.finish_date|hh_format}}</td>
					<td>{{last_g_era.g_supply.mprn}}</td>
					<td>
						{% for site_era in last_g_era.site_g_eras %}
							{% if site_era.is_physical %}
								{% if site_era.site.id == site.id %}
									this site
								{% else %}
									<a
											href="/sites/{{site_era.site.id}}"
											title="{{site_era.site.name}}"
											>{{site_era.site.code}}</a>
								{% endif %}
							{% endif %}
						{% endfor %}
					</td>
					<td>
						{% for site_g_era in last_g_era.site_g_eras %}
							{% if site_g_era.site.id != site.id %}
								<a
										href="/sites/{{site_g_era.site.id}}"
										title="{{site_g_era.site.name}}"
										>{{site_g_era.site.code}}</a>
							{% endif %}
						{% endfor %}
					</td>
					<td>{{last_g_era.msn}}</td>
					<td>
						<a title="{{last_g_era.g_reading_frequency.description}}">
							{{last_g_era.g_reading_frequency.code}}
						</a>
					</td>
				</tr>
			{% endfor %}
		</tbody>
	</table>

	<form action="/reports/g_monthly_duration">
		<fieldset>
			<input type="hidden" name="site_id" value="{{site.id}}">
			<legend>Monthly Duration</legend>
			<br>
			For {{input_text('months', '1', 2, 2)}} month(s) until the end of
			{{input_date('finish', last_month_finish, resolution='month')}}
			<input type="submit" value="Download">
		</fieldset>
	</form>			
{% endblock %}
