{% extends "base.html" %}

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

{% block style %}
	<style>
		label.aligned {
			width: 18em;
			float: left;
		}
	</style>
{% 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|hh_format}} Edit
{% endblock %}

{% block content %}
	<!-- Layer Section -->
	<div class="layer-section">
		{% if request.method == 'GET' and request.values.delete %}
			<!-- Section Header -->
			<div class="section-header">
				<h2>
					Are you sure you want to delete this era?
				</h2>
			</div>
			<!-- End Section Header -->

			<!-- Section Content -->
			<div class="section-content">
				<div class="section-data">
					<div class="standardForm">
						<form method="post" action="/g_eras/{{g_era.id}}/edit">
							<fieldset>
								<input type="submit" name="delete" value="Delete">
							</fieldset>
						</form>
						<p>
							<a href="/g_eras/{{g_era.id}}/edit">Cancel</a>
						</p>
					</div>
				</div>
			</div>
			<!-- End Section Content -->
		{% else %}
			<!-- Section Header -->
			<div class="section-header">
				<h2>Sites</h2>	
			</div>
			<!-- End Section Header -->
			<!-- Section Content -->
			<div class="section-content">
				<div class="section-data">
					<table class="DataTable widthAuto noMarginBottom">
						<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>
				 
					<br>
					<form method="post" action="/g_eras/{{g_era.id}}/edit">
						<fieldset>
							<legend>Attach to another site</legend>
							<label>Site Code {{input_text("site_code", '')}}</label>
							<input type="submit" name="attach" value="Attach">
						</fieldset>
					</form>
					<br>
					<form action="/g_eras/{{g_era.id}}/edit" method="post">
						<fieldset>
							<legend>Update this era</legend>
							<fieldset>
								<legend>Start date</legend>
								{{input_date('start', g_era.start_date)}}
							</fieldset>
							<br>

							<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>

						 
							<br>
							<label class="aligned">Meter Serial Number</label>
							{{input_text('msn', g_era.msn, 16)}}
							<br>
							<label class="aligned">Correction Factor</label>
							{{input_text('correction_factor', g_era.correction_factor)}}
							<br>
							<label class="aligned">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>
							<br>
							<label class="aligned">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>
							<br>
							<label class="aligned">Account</label>
							{{input_text('account', g_era.account)}}
							<br>
							<label class="aligned">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>
							<br>
							<input type="submit" value="Update">
						</fieldset>
					</form>
					<br>
					{% 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 %}
				</div>
			</div>
		{% endif %}
	</div>
{% endblock %}
