{% extends "base.html" %}

{% block title %}
	&raquo; Rate Server Importer
{% endblock %}

{% block nav %}
	Rate Server Importer
{% endblock %}

{% block content %}
	{% if importer %}
		<ul>
			<li><a href="#controls">Controls</a></li>
			<li><a href="#log">Log</a></li>
			<li><a href="#latest">Latest Imported Files</a></li>
			<li><a href="#config">Configuration</a></li>
		</ul>

		<h4 id="controls">Controls</h4>

		<table>
			<tr>
				<th>Is Locked?</th>
				<td>{{importer.is_locked()}}</td>
			</tr>
			<tr>
				<th>Import Now</th>
				<td>
					<form method="post">
						<fieldset {% if importer.is_locked() %}disabled{% endif %}>
							<input type="submit" value="Import" name="import">
						</fieldset>
					</form>
				</td>
			</tr>
			{% if importer.is_locked() %}
				<tr>
					<th>Progress</th>
					<td>{{importer.progress}}</td>
				</tr>
			{% endif %}
		</table>


		<h4 id="log">Log</h4>

		<ul>
			{% for message in importer.messages %}
				<li>{{message}}</li>
			{% endfor %}
		</ul>

		<h4 id="latest">Latest Imported Files</h4>

		<table>
			<caption>Electricity</caption>
			<tbody>
				<tr>
					<th>MDD Version</th>
					<td>{{ config_state.mdd_version }}</td>
				</tr>
				<tr>
					<th>LAF Files</th>
					<td>
						{% if 'laf_importer' in config_state %}
							<table>
								{% for code, dt in config_state.laf_importer.items() %}
									<tr>
										<td>{{code}}</td>
										<td>{{dt}}</td>
									</tr>
								{% endfor %}
							</table>
						{% endif %}
					</td>
				</tr>
				<tr>
					<th>DNO Spreadsheets</th>
					<td>
						<table>
							<thead>
								<tr>
									<th>DNO</th>
									<th>Start Date</th>
									<th>File Name</th>
								</tr>
							</thead>
							<tbody>
								{% for rs in dno_rs %}
									{% set script = rs.make_script() %}
									<tr>
										<td>{{rs.contract.name}}</td>
										<td>{{rs.start_date|hh_format}}</td>
										<td>{{script.a_file_name}}</td>
									</tr>
								{% endfor %}
							</tbody>
						</table>
					</td>
				</tr>
				<tr>
					<th>TNUoS Spreadsheet</th>
					<td>
						<table>
							<thead>
								<tr>
									<th>Start Date</th>
									<th>File Name</th>
								</tr>
							</thead>
							<tbody>
								{% for rs in tnuos_rs %}
									{% set script = rs.make_script() %}
									<tr>
										<td>{{rs.start_date|hh_format}}</td>
										<td>{{script.a_file_name}}</td>
									</tr>
								{% endfor %}
							</tbody>
						</table>
					</td>
				</tr>
				<tr>
					<th>BSUoS PDF</th>
					<td>
						<table>
							<thead>
								<tr>
									<th>Start Date</th>
									<th>File Name</th>
								</tr>
							</thead>
							<tbody>
								{% for rs in bsuos_rs %}
									{% set script = rs.make_script() %}
									<tr>
										<td>{{rs.start_date|hh_format}}</td>
										<td>{{script.a_file_name}}</td>
									</tr>
								{% endfor %}
							</tbody>
						</table>
					</td>
				</tr>
			</tbody>
		</table>

		<table>
			<caption>Gas</caption>
			<tbody>
				<tr>
					<th>NTS Spreadsheet</th>
					<td>
						<table>
							<thead>
								<tr>
									<th>Start Date</th>
									<th>File Name</th>
								</tr>
							</thead>
							<tbody>
								{% for rs in nts_rs %}
									{% set script = rs.make_script() %}
									<tr>
										<td>{{rs.start_date|hh_format}}</td>
										<td>{{script.a_file_name}}</td>
									</tr>
								{% endfor %}
							</tbody>
						</table>
					</td>
				</tr>
				<tr>
					<th>DN Spreadsheet</th>
					<td>
						<table>
							<thead>
								<tr>
									<th>Start Date</th>
									<th>File Name</th>
								</tr>
							</thead>
							<tbody>
								{% for rs in dn_rs %}
									{% set script = rs.make_script() %}
									<tr>
										<td>{{rs.start_date|hh_format}}</td>
										<td>{{script.a_file_name}}</td>
									</tr>
								{% endfor %}
							</tbody>
						</table>
					</td>
				</tr>
			</tbody>
		</table>

		<h4 id="config">Configuration</h4>

		<p>
			The rate server details can be set in the
			<a href="/configuration">configuration</a> with an entry like:
		</p>

		<pre>"rate_server": {
  "url": "https://api.github.com/repos/WessexWater/chellow-rates",
  "branch": "main",
},</pre>

		<p>
			If <code>url</code> or <code>branch</code> are omitted, then the default is as
			above.
		</p>

	{% else %}
		<p>Importer not present.</p>
	{% endif %}
{% endblock %}
