{% extends "base.html" %}

{% block inside_head %}
	<script type="text/javascript" language="javascript">

		function collapseBills(collapseId) {
			var collapsibles = document.getElementsByClassName('collapsible_' + collapseId);
			for (var i = 0; i < collapsibles.length; i++) {
				collapsibles[i].style.display = 'none';
			}
			var expanders = document.getElementsByClassName('expander_' + collapseId);
			for (var i = 0; i < expanders.length; i++) {
				expanders[i].style.display = 'table-cell';
			}
			var collapsers = document.getElementsByClassName('collapser_' + collapseId);
			for (var i = 0; i < collapsers.length; i++) {
				collapsers[i].style.display = 'none';
			}
		}

		function expandBills(collapseId) {
			var collapsibles = document.getElementsByClassName('collapsible_' + collapseId);
			for (var i = 0; i < collapsibles.length; i++) {
				collapsibles[i].style.display = 'table-row';
			}
			var expanders = document.getElementsByClassName('expander_' + collapseId);
			for (var i = 0; i < expanders.length; i++) {
				expanders[i].style.display = 'none';
			}
			var collapsers = document.getElementsByClassName('collapser_' + collapseId);
			for (var i = 0; i < collapsers.length; i++) {
				collapsers[i].style.display = 'table-cell';
			}
		}
	</script>
	<style>
		details[open] summary span {
			display: none;
		}

		section.reports {
			display: flex;
			flex-wrap: wrap;
			flex-basis: 100%;
			gap: 4em;
		}
	</style>

{% endblock %}

{% block title %}
    &raquo; Supplies &raquo; {{supply.id}}
{% endblock %}

{% block nav %}
	<a href="/supplies">Supplies</a> &raquo; {{supply.id}}
 	[<a href="/e/supplies/{{supply.id}}/edit">edit</a>]
{% endblock %}

{% block content %}
	<table>
		<tr>
			<th>Name</th>
			<th>Source</th>
			<th><span title="Distribution Network Operator">DNO</span></th>
			<th>GSP Group</th>
			{% if system_properties['supply_links'] %}
				<th>Links</th>
			{% endif %}
			<th>
				<a href="/e/supplies/{{supply.id}}/notes">Notes</a>
			</th>
			<th>Open Issues</th>
		</tr>
		<tr>
			<td>{{ supply.name }}</td>
			<td>
				{{ supply.source.code }} - {{ supply.source.name }}
				{% if supply.source.code == 'gen' %}
				( {{ supply.generator_type.code }} {{ supply.generator_type.name }})
				{% endif %}
			</td>
			<td>
				<a href="/e/dnos/{{supply.dno.id}}" title="{{supply.dno.name}}">{{ supply.dno.dno_code }}</a>
			</td>
			<td>
				{{ supply.gsp_group.code }} {{ supply.gsp_group.description }}
			</td>
			{% if system_properties['supply_links'] %}
				<td>
					<ul>
						{% for link in system_properties['supply_links'] %}
							<li>
								<a href="{{link.url}}supply_id={{site.id}}">{{link.name}}</a>
							</li>
						{% endfor %}
					</ul>
				</td>
			{% endif %}
			<td>
				{% if note %}
					<pre style="white-space pre-wrap;">
						{%- if note.timestamp -%}<em>{{note.timestamp|hh_format}}</em>{%- endif %}
						{%- if truncated_line -%}
							<details><summary><span>{{truncated_line}}</span></summary>{{note.body}}</details>
						{%- else %} {{note.body}}
						{%- endif -%}
					</pre>
				{% endif %}
			</td>
			<td>
				{% if issues|length > 0 %}
					<ul>
						{% for issue in issues %}
							<li>
								{% if issue.contract.market_role.code == 'X' %}
									<a href="/e/supplier_issues/{{issue.id}}">Supplier:
										{{issue.id}} {{issue.properties['subject']}}</a>
								{% elif issue.contract.market_role.code == 'C' %}
									<a href="/e/dc_issues/{{issue.id}}">DC:
										{{issue.id}} {{issue.properties['subject']}}</a>
								{% else %}
									<a href="/e/mop_issues/{{issue.id}}">MOP:
										{{issue.id}} {{issue.properties['subject']}}</a>
								{% endif %}
							</li>
						{% endfor %}
					</ul>
				{% endif %}
			</td>
		</tr>
	</table>

	{% include 'e/supply_eras.html' %}

	<hr>

	<ul>
		<li>
			kWh, kVA, MD etc. by month:
			<a href="/e/supplies/{{supply.id}}/months?is_import=true&amp;year={{last_month_start.year}}&amp;years=1">Import</a>
			<a href="/e/supplies/{{supply.id}}/months?is_import=false&amp;year={{last_month_start.year}}&amp;years=1">Export</a>
		</li>
		<li>
			<a href="/e/supplies/{{ supply.id }}/hh_data?months=1&amp;finish_year={{ now.year }}&amp;finish_month={{ now.month }}">Raw HH data</a>
		</li>
		<li>
			<a href="/e/supplies/{{supply.id}}/virtual_bill?start_year={{last_month_start.year}}&amp;start_month={{last_month_start.month}}&amp;start_day={{last_month_start.day}}&amp;start_hour={{last_month_start.hour}}&amp;start_minute={{last_month_start.minute}}&amp;finish_year={{last_month_finish.year}}&amp;finish_month={{last_month_finish.month}}&amp;finish_day={{last_month_finish.day}}&amp;finish_hour={{last_month_finish.hour}}&amp;finish_minute={{last_month_finish.minute}}">Supplier Virtual Bill</a>
		</li>
		<li>
			<a href="/e/supplies/{{supply.id}}/issues">Issues</a>
		</li>
	</ul>

	<h2>Reports</h2>

	<section class="reports">

		<form action="/reports/291">
			<fieldset>
				<input type="hidden" name="supply_id" value="{{supply.id}}">
				<legend>Virtual Bills</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/241">
			<fieldset>
				<legend>Daily Supplier Virtual Bills</legend>
				<input type="hidden" name="supply_id" value="{{supply.id}}">
				<label>Import / Export</label>
				<select name="is_import">
					<option value="true">Import</option>
					<option value="false">Export</option>
				</select>
				<label>From</label> {{input_date('start', last_month_start, 'day')}}
				<label>To</label> {{input_date('finish', last_month_finish, 'day')}}
				<input type="submit" value="Download">
			</fieldset>
			<p style="width: min-content; min-width: 100%;">
				(Note that unless a month is entirely in the past, all the days for that month
				will be estimates)
			</p>
		</form>


		<form action="/reports/387">
			<fieldset>
				<input type="hidden" name="supply_id" value="{{supply.id}}">
				<legend>Virtual Bills By Half-Hour</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/169">
			<fieldset>
				<legend>HH Data: Day Per Row</legend>
				<input type="hidden" name="supply_id" value="{{supply.id}}">
				<label>Import / Export</label>
				<select name="imp_related">
					<option value="true">Import</option>
					<option value="false">Export</option>
				</select>
				<label>Channel Type</label>
				<select name="channel_type">
					<option value="ACTIVE">ACTIVE</option>
					<option value="REACTIVE_IMP">REACTIVE_IMP</option>
					<option value="REACTIVE_EXP">REACTIVE_EXP</option>
				</select>
				<label>From</label> {{input_date('start', last_month_start, resolution='day') }}
				<label>To</label> {{ input_date('finish', last_month_finish, resolution='day') }}
				<input type="submit" value="Download">
			</fieldset>
		</form>

		<form action="/reports/187" method="post">
			<fieldset>
				<legend>HH Data: HH Per Row</legend>
				<input type="hidden" name="supply_id" value="{{supply.id}}">
				<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/59" method=post>
			<fieldset>
				<input type="hidden" name="supply_id" value="{{supply.id}}">
				<input type="hidden" name="compression" value="true">
				<legend>Duration</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="supply_id" value="{{supply.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, 'month') }}
				<input type="submit" value="Download">
			</fieldset>
		</form>

		<form action="/reports/41?supply_id">
			<fieldset>
				<legend>TRIAD</legend>
				<input type="hidden" name="supply_id" value="{{ supply.id }}">
				<label>For financial year ending March</label>
				<input name="year" value="{{triad_year}}" size="4" maxlength="4">
				<input type="submit" value="Download">
			</fieldset>
		</form>

		<form action="/reports/33">
			<input type="hidden" name="supply_id" value="{{supply.id}}">
			<fieldset>
				<legend>Snapshot</legend>
				<label>At</label> {{ input_date('date', last_month_finish) }}
				<input type="submit" value="Download">
			</fieldset>
		</form>

		<form action="/reports/219">
			<input type="hidden" name="supply_id" value="{{ supply.id }}">
			<fieldset>
				<legend>Register Reads</legend>
				<label>Months Long</label> <input name="months" maxlength="2" size="2" value="1">
				<label>Final Month</label> {{ input_date('end', last_month_finish, 'month') }}
				<input type="submit" value="Download">
			</fieldset>
		</form>
	</section>
{% endblock %}
