{% extends "base.html" %}

{% block title %}
	&raquo; Supplies &raquo; {{era.supply.id}} &raquo;
 	Era {{era.start_date|date_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|date_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="this">
			{{input_hidden_checkbox('is_ended', initial=era.finish_date != None)}}
			{{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('dtc_meter_type_id', initial=era.dtc_meter_type.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_hidden_checkbox('has_imp_mpan', initial=era.imp_mpan_core != None)}}
			{{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_hidden_checkbox('has_exp_mpan', initial=era.exp_mpan_core != None)}}
			{{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>

	<table>
		<caption>Connection Agreements</caption>
		<thead>
			<tr>
				<th>Direction</th>
				<th>File Name</th>
				<th>Start Date</th>
				<th>Finish Date</th>
				<th>View</th>
				<th>Action</th>
			</tr>
		</thead>
		<tbody>
			{% for direction, ca, dir_bool in
					[('Import', era.imp_ca, 'true'), ('Export', era.exp_ca, 'false')] %}

				<tr>
					<td>{{direction}}</td>
					{% if ca %}
						<td>{{ca.properties.get('filename')}}</td>
						<td>{{ca.start_date|date_format}}</td>
						<td>{{ca.finish_date|date_format}}</td>
						<td><a href="/e/cas/{{ca.id}}">View</a></td>
						<td>
							<form method="post" action="/e/eras/{{era.id}}/edit">
								<fieldset>
									<input type="hidden" name="is_import" value="{{dir_bool}}">
									<input type="submit" name="detach_ca" value="Detach">
								</fieldset>
							</form>
						</td>
					{% else %}
						<td colspan="5" style="text-align: center;">
							<a href="/e/eras/{{era.id}}/attach_ca?is_import={{dir_bool}}">Attach</a>
						</td>
					{% endif %}
				</tr>
			{% endfor %}
		</tbody>
	</table>

	{% 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 %}
