{% extends "base.html" %}

{% block title %}
	&raquo; Sites &raquo; {{site.code}} {{site.name}}
{% endblock %}

{% block style %}
	<style>
		label {
			width: 11em;
			float: left;
		}
	</style>
{% endblock %}

{% block nav %}
	<a href="/sites">Sites</a> &raquo;
	<a href="/sites/{{site.id}}">{{site.code}} {{site.name}}</a> &raquo; Edit
{% endblock %}

{% block content %}
	{% if request.method == "GET" and request.values.delete %}
		<form method="post" action="/sites/{{site.id}}/edit">
			<fieldset>
				<legend>
					Are you sure you want to delete this site and any associated snags?
				</legend>
				<input type="submit" name="delete" value="delete"> 
				<a href="/sites/{{site.id}}/edit">Cancel</a>
			</fieldset>
		</form>
	{% else %}
		<form action="/sites/{{site.id}}/edit" method="post">
			<fieldset>
				<legend>Update this site</legend>
				<label>Name</label> {{input_text('site_name', site.name, 40)}}
				<label>Code</label> {{input_text('code', site.code)}}
				<input name="update" type="submit" value="Update">
			</fieldset>
		</form>

		<form action="/sites/{{site.id}}/edit">
			<fieldset>
				<legend>Delete this site</legend>
				<input type="submit" name="delete" value="Delete">
			</fieldset>
		</form>

		<table>
			<caption>Eras</caption>
			<thead>
				<tr>
					<th>From</th>
					<th>To</th>
					<th>Source</th>
					<th>Supply</th>
					<th>Import MPAN core</th>
					<th>Export MPAN core</th>
				</tr>
			</thead>
			<tbody>
				{% for era in eras %}
					<tr>
						<td>{{era.start_date|hh_format}}</td>
						<td>{{era.finish_date|hh_format}}</td>
						<td>{{era.supply.source.code}}</td>
						<td>{{era.supply.name}}</td>
						<td>
							{% if era.imp_mpan_core %}
								{{era.imp_mpan_core}}
							{% endif %}
						</td>
						<td>
							{% if era.exp_mpan_core %}
								{{era.exp_mpan_core}}
							{% endif %}
						</td>
					</tr>
				{% endfor %}
			</tbody>
		</table>

		<form action="/sites/{{site.id}}/edit" method="post">
			<fieldset>
				<legend>Insert an electricity supply</legend>
				<label>Source</label>
				<select name="source_id">
					{% for source in sources %}
						{{input_option(
							'source_id', source.id, source.code + ' ' +
							source.name)}}
					{% endfor %}
				</select>
				<label>Generator Type (if source is 'gen' or 'gen-net')</label>
				<select name="generator_type_id">
					{% for generator_type in generator_types %}
						{{input_option(
							'generator_type_id', generator_type.id,
							generator_type.code + ' ' +
							generator_type.description)}}
					{% endfor %}
				</select>
				<label>Name</label> {{input_text('name', required=True)}}

				<label>Start Date</label> {{input_date('start')}}

				<label>Meter Serial Number</label> {{input_text('msn')}}
				<label>GSP Group</label>
				<select name="gsp_group_id">
					{% for gsp_group in gsp_groups %}
						{{input_option(
							'gsp_group_id', gsp_group.id,
							gsp_group.code + ' ' + gsp_group.description)}}
					{% endfor %}
				</select>
				<label>MOP Contract</label>
				<select name="mop_contract_id">
					{% for contract in mop_contracts %}
						{{input_option(
							'mop_contract_id', contract.id, contract.name)}}
					{% endfor %}
				</select>
				<label>MOP Account</label> {{input_text('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)}}
					{% endfor %}
				</select>
				<label>DC Account</label> {{input_text('dc_account')}}
				<label>Profile Class</label>
				<select name="pc_id">
					{% for pc in pcs %}
						{{input_option(
							'pc_id', pc.id, pc.code + ' ' + pc.name)}}
					{% endfor %}
				</select>
				<label>MTC Code</label>
				{{input_text('mtc_code', pattern='[0-9]*', maxlength='3')}}
				<label>CoP</label>
				<select name="cop_id">
					{% for cop in cops %}
						{{input_option('cop_id', cop.id, cop.description)}}
					{% endfor %}
				</select>										
				<label>Comm</label>
				<select name="comm_id">
					{% for comm in comms %}
						{{input_option(
							'comm_id', comm.id, comm.code + ' ' + comm.description)}}
					{% endfor %}
				</select>										
				<label>SSC</label> {{input_text('ssc_code')}}
				<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.code + ' - ' +
							energisation_status.description,
							initial=default_energisation_status.id)}}
					{% endfor %}
				</select>
				<label>Properties</label>
				{{input_textarea('properties', '{}', 20, 80)}}
				<fieldset>
					<legend>Import</legend>
					<label>Mpan Core</label>
							{{input_text('imp_mpan_core', '', 35, pattern='[0-9 ]*')}}
					<label>LLFC</label>
					{{input_text('imp_llfc_code', pattern='[a-zA-Z0-9]{3}', maxlength='3')}}
					<label>Supply Capacity</label>
					{{input_number('imp_sc', '0', required=True)}}
					<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)}}
						{% endfor %}
					</select>
					<label>Supplier Account</label>
					{{input_text('imp_supplier_account')}}
				</fieldset>	
				<fieldset>
					<legend>Export</legend>
					<label>Mpan Core</label>
					{{input_text('exp_mpan_core', '', 35, pattern='[0-9 ]*')}}
					<label>LLFC</label>
					{{input_text('exp_llfc_code', maxlength='3', pattern='[0-9]*')}}
					<label>Supply Capacity</label>
					{{input_number('exp_sc', '0', required=True)}}
					<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)}}
						{% endfor %}
					</select>
					<label>Supplier Account</label>
					{{input_text('exp_supplier_account')}}
				</fieldset>
				<input name="insert_electricity" type="submit" value="Insert">
			</fieldset>
		</form>

		<form action="/sites/{{site.id}}/edit" method="post">
			<fieldset>
				<legend>Insert a gas supply</legend>
				<label>Name</label> {{input_text('name')}}
				<fieldset>
						<legend>Start Date</legend>
						{{input_date('start')}}
				</fieldset>
				<label>Meter Serial Number</label> {{input_text('msn')}}
				<label>Correction Factor</label> {{input_text('correction_factor', '1.02264')}}
				<label>MPRN</label> {{input_text('mprn', '')}}
				<label>Exit Zone</label>
				<select name="g_exit_zone_id">
					{% for g_exit_zone in g_exit_zones %}
						{{input_option('g_exit_zone_id', g_exit_zone.id, g_exit_zone.code)}}
					{% endfor %}
				</select>
				<label>Contract</label>
				<select name="g_contract_id">
					{% for g_contract in g_contracts %}
						{{ input_option('g_contract_id', g_contract.id, g_contract.name)}}
					{% endfor %}
				</select>
				<label>Account</label> {{input_text('account')}}
				<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_unit.description)}}
					{% endfor %}
				</select>
				<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)}}
					{% endfor %}
				</select>
				<input name="insert_gas" type="submit" value="Insert">
			</fieldset>
		</form>
	{% endif %}
{% endblock %}
