{% 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 %}
	
	<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 hx-get="/e/eras/{{era.id}}/edit/form" hx-params="*"
			 hx-trigger="load, change"
			 hx-include="[name='source_id'],[name='start_year'],[name='start_month'],[name='start_day'],[name='start_hour'],[name='start_minute'],[name='pc_id'],[name='name'],[name='imp_mpan_core'],[name='exp_mpan_core'],[name='mop_account'],[name='dc_account'],[name='dc_contract_id'],[name='mop_contract_id'],[name='imp_supplier_account'],[name='exp_supplier_account'],[name='imp_llfc_id'],[name='exp_llfc_id'],[name='mtc_participant_id'],[name='has_imp_mpan'],[name='has_exp_mpan'],[name='dno_id'],[name='gsp_group_id'],[name='ssc_id'],[name='cop_id'],[name='comm_id'],[name='energisation_status_id'],[name='is_ended'],[name='imp_supplier_contract_id'],[name='exp_supplier_contract_id'],[name='imp_sc'],[name='exp_sc'],[name='msn']">
			<input type="hidden" name="is_ended" value="
				{%- if era.finish_date == None -%}
					false		
				{%- else -%}
				  true
				{%- endif -%}">
			{{input_hidden('source_id', initial=era.supply.source.id)}}
			{{input_hidden('cop_id', initial=era.cop.id)}}
			{{input_hidden('comm_id', initial=era.comm.id)}}
			{{input_hidden('energisation_status_id', initial=era.energisation_status.id)}}
			{{input_hidden('pc_id', initial=era.pc.id)}}
			{{input_hidden('dno_id', initial=era.supply.dno.id)}}
			{{input_hidden('gsp_group_id', initial=era.supply.gsp_group.id)}}
			{{input_hidden('mtc_participant_id', initial=era.mtc_participant.id)}}
			{{input_hidden('ssc_id', initial=era.ssc.id)}}
			{{input_hidden('name', initial=era.name)}}
			{{input_hidden('msn', initial=era.msn)}}
			{{input_hidden('mop_contract_id', initial=era.mop_contract_id)}}
			{{input_hidden('mop_account', initial=era.mop_account)}}
			{{input_hidden('dc_contract_id', initial=era.dc_contract.id)}}
			{{input_hidden('dc_account', initial=era.dc_account)}}

			<input type="hidden" name="has_imp_mpan" value="
				{%- if 'has_imp_mpan' in request.values -%}
					{{request.values.has_imp_mpan}}
				{%- elif era.imp_mpan_core == None -%}
					false
				{%- else -%}
					true
				{%- endif -%}">

			{{input_hidden('imp_mpan_core', initial=era.imp_mpan_core)}}
			{{input_hidden('imp_supplier_contract_id', initial=era.imp_supplier_contract.id)}}
			{{input_hidden('imp_supplier_account', initial=era.imp_supplier_account)}}
			{{input_hidden('imp_llfc_id', initial=era.imp_llfc.id)}}
			{{input_hidden('imp_sc', initial=era.imp_sc)}}

			<input type="hidden" name="has_exp_mpan" value="
				{%- if 'has_exp_mpan' in request.values -%}
					{{request.values.has_exp_mpan}}
				{%- elif era.exp_mpan_core == None -%}
					false
				{%- else -%}
					true
				{%- endif -%}">

			{{input_hidden('exp_mpan_core', initial=era.exp_mpan_core)}}
			{{input_hidden('exp_supplier_contract_id', initial=era.exp_supplier_contract.id)}}
			{{input_hidden('exp_supplier_account', initial=era.exp_supplier_account)}}
			{{input_hidden('exp_llfc_id', initial=era.exp_llfc.id)}}
			{{input_hidden('exp_sc', initial=era.exp_sc)}}
		</fieldset>
	</form>
		
	{% if era.supply.eras|length > 1 %}
		<form hx-delete="/e/eras/{{era.id}}/edit"
				hx-confirm="Are you sure you want to delete this era?">
			<fieldset>
				<legend>Delete this era</legend>	
				<input type="submit" value="Delete">
			</fieldset>
		</form>
	{% endif %}

{% endblock %}
