{% extends "base.html" %}

{% block title %}
	&raquo; Gas Supplies &raquo; {{g_era.g_supply.id}} &raquo;
 	Era {{g_era.start_date|date_format}} Edit
{% endblock %}

{% block nav %}
	<a href="/g/supplies">Gas Supplies</a> &raquo;
	<a href="/g/supplies/{{g_era.g_supply.id}}">{{g_era.g_supply.id}}</a> &raquo;
 	Era {{g_era.start_date|date_format}} Edit
{% endblock %}

{% block content %}
	{% if request.method == 'GET' and request.values.delete %}

		<form method="post" action="/g/eras/{{g_era.id}}/edit">
			<fieldset>
				<legend>Are you sure you want to delete this era?</legend>
				<input type="submit" name="delete" value="Delete">
			</fieldset>
		</form>
		<p>
			<a href="/g/eras/{{g_era.id}}/edit">Cancel</a>
		</p>

	{% else %}

		<table>
			<caption>Sites</caption>	
			<thead>
				<tr>
					<th>Code</th>
					<th>Name</th>
					{% if site_g_eras|length > 1 %}
						<th></th>
						<th></th>
						<th></th>
					{% endif %}
				</tr>
			</thead>
			<tbody>
				{% for site_g_era in site_g_eras %}
					<tr>
						<td>{{site_g_era.site.code}}</td>
						<td>{{site_g_era.site.name}}</td>
						{% if site_g_eras|length > 1 %}
							<td>
								{% if site_g_era.is_physical %}
									Located here
								{% else %}
									<form method="post" action="/g/eras/{{g_era.id}}/edit">
										<fieldset>
											<input
													type="hidden" name="site_id"
													value="{{site_g_era.site.id}}">
											<input
													type="submit" name="locate" value="Locate Here">
										</fieldset>
									</form>
								{% endif %}
							</td>
							<td>
								<form method="post" action="/g/eras/{{g_era.id}}/edit">
									<fieldset>
										<legend>Detach from site</legend>
										<input type="hidden" name="site_id" value="{{site_g_era.site.id}}">
										<input type="submit" name="detach" value="Detach">
									</fieldset>
								</form>
							</td>
						{% endif %}
					</tr>
				{% endfor %}
			</tbody>
		</table>
			 
		<form method="post" action="/g/eras/{{g_era.id}}/edit">
			<fieldset>
				<legend>Attach to another site</legend>
				<label>Site Code</label> {{input_text("site_code", '')}}
				<input type="submit" name="attach" value="Attach">
			</fieldset>
		</form>

		<form action="/g/eras/{{g_era.id}}/edit" method="post">
			<fieldset>
				<legend>Update this era</legend>
				<label>Start date</label> {{input_date('start', g_era.start_date)}}

				<fieldset>
					<legend>End Date</legend>
					<label>Ended?
					{{input_checkbox('is_ended', g_era.finish_date is not none)}}
					</label>
					{{input_date('finish', g_era.finish_date)}}
				</fieldset>

				<label>Meter Serial Number</label> {{input_text('msn', g_era.msn, 16)}}
				<label>Correction Factor</label>
				{{input_text('correction_factor', g_era.correction_factor)}}
				<label>Units</label>
				<select name="g_unit_id">
					{% for g_unit in g_units %}
						{{input_option('g_unit_id', g_unit.id, g_unit.code, g_era.g_unit.id)}}
					{% endfor %}
				</select>
				<label>Contract</label>
				<select name="g_contract_id">
					{% for g_contract in supplier_g_contracts %}
						{{
							input_option(
								'g_contract_id', g_contract.id, g_contract.name, g_era.g_contract.id)}}
					{% endfor %}
				</select>
				<label>Account</label> {{input_text('account', g_era.account)}}
				<label>Reading Frequency</label>
				<select name="g_reading_frequency_id">
					{% for g_reading_frequency in g_reading_frequencies %}
						{{input_option(
							'g_reading_frequency_id', g_reading_frequency.id,
							g_reading_frequency.code + ' - ' + g_reading_frequency.description,
							g_era.g_reading_frequency.id)}}
					{% endfor %}
				</select>

				<label>AQ</label> {{input_text('aq', g_era.aq)}}
				<label>SOQ</label> {{input_text('soq', g_era.soq)}}

				<input type="submit" value="Update">
			</fieldset>
		</form>

		{% if g_era.g_supply.g_eras|length > 1 %}
			<form action="/g/eras/{{g_era.id}}/edit">
				<fieldset>
					<legend>Delete this era</legend>
					<input type="submit" name="delete" value="Delete">
				</fieldset>
			</form>
		{% endif %}
	{% endif %}
{% endblock %}
