{% extends "base.html" %}

{% block title %}
	&raquo; Supplies &raquo; {{era.supply.id}} &raquo;
 	Era {{era.start_date|hh_format}} Edit
{% endblock %}

{% block nav %}
	<a href="/e/supplies">Supplies</a> &raquo; 
	<a href="/e/supplies/{{era.supply.id}}">{{era.supply.id}}</a> &raquo;
 	Era {{era.start_date|hh_format}} Edit
{% endblock %}

{% block content %}

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

		<form method="post" action="/e/eras/{{era.id}}/edit">
			<fieldset>
				<legend>Are you sure you want to delete this era?</legend>	
				<input type="submit" name="delete" value="Delete">
				<a href="/e/eras/{{era.supply.id}}/edit">Cancel</a>
			</fieldset>
		</form>
			
	{% else %}
	
		<table>
			<caption>Sites</caption>	
			<thead>
				<tr>
					<th>Code</th>
					<th>Name</th>
					{% if site_eras|length > 1 %}
						<th></th>
						<th></th>
						<th></th>
					{% endif %}
				</tr>
			</thead>
			<tbody>
				{% for site_era in site_eras %}
					<tr>
						<td>{{site_era.site.code}}</td>
						<td>{{site_era.site.name}}</td>
						{% if site_eras|length > 1 %}
							<td>
								{% if site_era.is_physical %}
									Located here
								{% else %}
									<form method="post" action="/e/eras/{{era.id}}/edit">
										<fieldset>
											<input type="hidden" name="site_id" value="{{site_era.site.id}}">
											<input type="submit" name="locate" value="Locate Here">
										</fieldset>
									</form>
								{% endif %}
							</td>
							<td>
								<form action="/e/eras/{{era.id}}/edit" method="post">
									<fieldset>
										<label>Detach from site</label>
										<input type="hidden" name="site_id" value="{{site_era.site.id}}">
										<input type="submit" name="detach" value="Detach">
									</fieldset>
								</form>
							</td>
						{% endif %}
					</tr>
				{% endfor %}
			</tbody>
		</table>

		<form action="/e/eras/{{era.id}}/edit" method="post">
			<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="/e/eras/{{era.id}}/edit" method="post">	
			<fieldset>
				<legend>Update this era</legend>	
				<label>Start Date</label> {{input_date('start', era.start_date)}}
				<label>Finish Date</label> {{input_date('finish', era.finish_date)}}
				<label>Finished?</label>
				{{input_checkbox('is_ended', era.finish_date is not none)}}
				<label>MOP Contract</label>
				<select name="mop_contract_id">
					{% for contract in mop_contracts %}
						{{input_option('mop_contract_id', contract.id, contract.name,
						era.mop_contract.id)}}
					{% endfor %}
				</select>
				<label>MOP Account</label> {{input_text('mop_account', era.mop_account)}}
				<label>DC Contract</label>
				<select name="dc_contract_id">
					{% for contract in dc_contracts %}
						{{input_option('dc_contract_id', contract.id, contract.name,
						era.dc_contract.id)}}
					{% endfor %}
				</select>
				<label>DC Account</label>
				{{input_text('dc_account', era.dc_account)}}
				<label>Meter Serial Number</label>
				{{input_text('msn', era.msn)}}
				<label>Profile Class</label>
				<select name="pc_id">
					{% for pc in pcs %}
						{{input_option('pc_id', pc.id, pc.code + ' - ' + pc.name, era.pc.id)}}
					{% endfor %}
				</select>
				<label>Meter Timeswitch Class</label>
				{{input_text('mtc_code', era.mtc_participant.mtc.code, 3, 3)}}
				<label>CoP</label>
				<select name="cop_id">
					{% for cop in cops %}
						{{input_option('cop_id', cop.id, cop.code + ' ' + cop.description,
						era.cop.id)}}
					{% endfor %}
				</select>
				<label>Comm</label>
				<select name="comm_id">
					{% for comm in comms %}
						{{input_option(
							'comm_id', comm.id, comm.code + ' ' + comm.description,
							era.comm.id)}}
					{% endfor %}
				</select>
				<label>SSC</label> {{input_text('ssc_code', era.ssc.code, 4, 4)}}
				<label>Energisation Status</label>
				<select name="energisation_status_id">
					{% for energisation_status in energisation_statuses %}
						{{input_option(
							'energisation_status_id', energisation_status.id,
							energisation_status.description, era.energisation_status.id)}}
					{% endfor %}
				</select>
				<label>Properties</label>
				<div>
					{{input_textarea('properties', era.properties, 20, 80)}}
					<p>
						The <code>meter_type</code> property contains a
						<a href="/e/dtc_meter_types">DTC Meter Type Code</a>.
					</p>
				</div>

				<fieldset>
					<legend>Import MPAN</legend>
					<label>Line Loss Factor Class</label>
					{{input_text('imp_llfc_code', era.imp_llfc.code, 3,3)}}
					<label>MPAN Core</label>
					{{input_text('imp_mpan_core', era.imp_mpan_core|default(''), 16)}}
					<label>Agreed Supply Capacity (kVA)</label>
					{{input_text('imp_sc', era.imp_sc, 9, 9)}}
					<label>Supplier Contract</label>
					<select name="imp_supplier_contract_id">
						{% for contract in supplier_contracts %}
							{{input_option('imp_supplier_contract_id', contract.id, contract.name, era.imp_supplier_contract.id)}}
						{% endfor %}
					</select>
					<label>Supplier Account</label>
					{{input_text('imp_supplier_account', era.imp_supplier_account)}}
				</fieldset>

				<fieldset>
					<legend>Export MPAN</legend>
					<label>Line Loss Factor Class</label>
					{{input_text('exp_llfc_code', era.exp_llfc.code, 3, 3)}}
					<label>MPAN Core</label>
					{{input_text('exp_mpan_core', era.exp_mpan_core, 16)}}
					<label>Agreed Supply Capacity (kVA)</label>
					{{input_text('exp_sc', era.exp_sc, 9, 9)}}
					<label>Supplier Contract</label>
					<select name="exp_supplier_contract_id">
						{% for contract in supplier_contracts %}
							{{input_option('exp_supplier_contract_id', contract.id, contract.name, era.exp_supplier_contract.id)}}
						{% endfor %}
					</select>
					<label class="aligned">Supplier Account</label>
					{{input_text('exp_supplier_account', era.exp_supplier_account)}}
				</fieldset>
				<input type="submit" value="Update">
			</fieldset>
		</form>
			
		{% if era.supply.eras|length > 1 %}
			<form action="/e/eras/{{era.id}}/edit">
				<fieldset>
					<legend>Delete this era</legend>	
					<input type="submit" name="delete" value="Delete">
				</fieldset>
			</form>
		{% endif %}
	 
	{% endif %}

{% endblock %}
