{% extends "base.html" %}

{% block title %}
	&raquo; Report Runs &raquo; {{run.id}}
{% endblock %}

{% block nav %}
	<a href="/report_runs">Report Runs</a> &raquo; {{run.id}}
{% endblock %}

{% block content %}
	<table>
		<caption>ECOES Comparison</caption>
		<thead>
			<tr>
				<th>Date Created</th>
				<th>Created By</th>
				<th>State</th>
				<th>Number Of Rows</th>
				<th>Delete</th>
				<th>Download Spreadsheet</th>
				<th>Re-run Report</th>
				<th>Configuration</th>
				<th>Update Report Run</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>{{run.date_created|hh_format}}</td>
				<td>{{run.creator}}</td>
				<td>{{run.state}}</td>
				<td>{{rows|length}}</td>
				<td>
					<form action="/report_runs/{{run.id}}">
						<fieldset>
							<input type="submit" name="delete" value="Delete">
						</fieldset>
					</form>
				</td>
				<td><a href="/report_runs/{{run.id}}/spreadsheet">Download</a></td>
				<td><a href="/reports/ecoes_comparison">Re-run</a></td>
				<td>
					The <a href="/configuration">configuration</a> is held under the
					<code>ecoes</code> section and has keys <code>user_name</code>,
					<code>password</code>, <code>prefix</code>,
					<code>exclude_mpan_cores</code> and
					<code>ignore_mpan_cores_msn</code>.
				</td>
				<td>
					<form action="/report_runs/{{run.id}}" method="post">
						<fieldset>
							<label>Keep</label> {{input_checkbox('keep', run.data['keep'])}}
							<input type="submit" name="update" value="Update">
						</fieldset>
					</form>
				</td>
			</tr>
		</tbody>
	</table>

	{% for row in rows %}
		{% set vals = row.data['values'] %}
		<table>
			<caption>{{vals['mpan_core_no_spaces']}}</caption>
			<thead>
				<tr>
					<th>Source</th>
					<th>Mpan Core</th>
					<th>Supplier Contract</th>
					<th>Edit Era</th>
					<th>Edit Supply</th>
					{% for diff in vals['diffs'] %}
						<th>{{diff}}</th>
					{% endfor %}
					<th>Problem</th>
					<th>Site</th>
					<th>Open Issues</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>Chellow</td>
					<td>
						{% if vals['chellow_supply_id'] is none %}
							{{vals['mpan_core']}}
						{% else %}
							<a href="/e/supplies/{{vals['chellow_supply_id']}}">{{vals['mpan_core']}}</a>
						{% endif %}
					</td>
					<td>
						{% if vals['chellow_supplier_contract_id'] is not none %}
							<a href="/e/supplier_contracts/{{vals['chellow_supplier_contract_id']}}"
								>{{vals['chellow_supplier_contract_name']}}</a>
						{% endif %}
					</td>
					<td>
						{% if vals['chellow_era_id'] is not none %}
							[<a href="/e/eras/{{vals['chellow_era_id']}}/edit">edit</a>]
						{% endif %}
					</td>
					<td>
						{% if vals['chellow_supply_id'] is not none %}
							[<a href="/e/supplies/{{vals['chellow_supply_id']}}/edit">edit</a>]
						{% endif %}
					</td>
					{% for diff in vals['diffs'] %}
						<td>
							{{vals['chellow_' + diff]}}
							{% if diff == 'llfc' %}
								- {{vals['chellow_' + diff + '_desc']}}
								<form hx-post="/e/supplies/{{vals['chellow_supply_id']}}">
									<fieldset>
										<input type="hidden" name="llfc_code" value="{{vals['ecoes_llfc']}}">
										<input type="hidden" name="mpan_core" value="{{vals['mpan_core']}}">
										<input type="hidden" name="start_date"
											value="{{vals['ecoes_llfc_from_date']}}">
										<input type="submit" name="new_llfc" value="Fix">
									</fieldset>
								</form>
							{% elif diff == 'msn' %}
								<form hx-post="/e/supplies/{{vals['chellow_supply_id']}}">
									<fieldset>
										<input type="hidden" name="msn" value="{{vals['ecoes_msn']}}">
										<input type="hidden" name="start_date"
											value="{{vals['ecoes_msn_install_date']}}">
										<input type="submit" name="new_msn" value="Fix MSN">
									</fieldset>
								</form>
							{% endif %}
						</td>
					{% endfor %}
					<td rowspan="2">{{vals['problem']}}</td>
					<td>{{vals['chellow_site_code']}} {{vals['chellow_site_name']}}</td>
					<td rowspan="2">
						{% set issues = vals['issues'] %}
						{% if issues|length > 0 %}
							<ul>
								{% for issue in issues %}
									<li>
										{% if issue.contract.market_role.code == 'X' %}
											<a href="/e/supplier_issues/{{issue.id}}">Supplier:
												{{issue.id}} {{issue.properties['subject']}}</a>
										{% elif issue.contract.market_role.code == 'C' %}
											<a href="/e/dc_issues/{{issue.id}}">DC:
												{{issue.id}} {{issue.properties['subject']}}</a>
										{% else %}
											<a href="/e/mop_issues/{{issue.id}}">MOP:
												{{issue.id}} {{issue.properties['subject']}}</a>
										{% endif %}
									</li>
								{% endfor %}
							</ul>
						{% endif %}
					</td>
				</tr>
				<tr>
					<td>
						ECOES
					</td>
					<td>
					</td>
					<td>
					</td>
					<td>
					<td>
					</td>
					{% for diff in vals['diffs'] %}
						<td>{{vals['ecoes_' + diff]}}
							{% if diff == 'msn' %}
								- {{vals['ecoes_msn_install_date']}}
							{% elif diff == 'supplier' %}
								- {{vals['ecoes_supplier_registration_from']}}
							{% elif diff == 'mtc' %}
								- {{vals['ecoes_mtc_date']}}
							{% elif diff == 'llfc' %}
								{{vals['ecoes_llfc_desc']}} - From {{vals['ecoes_llfc_from']}}
							{% elif diff == 'mop' %}
								- {{vals['ecoes_mop_appoint_date']}}
							{% elif diff == 'gsp_group' %}
								- {{vals['ecoes_gsp_effective_from']}}
							{% endif %}
						</td>
					{% endfor %}
					<td>{{vals['address']}}</td>
				</tr>
			</tbody>
		</table>
	{% endfor %}
{% endblock %}
