{% extends "macros.html" %}

{% block html %}
	<table class="sticky" id="snags">
		<caption>
			Snags (truncated after 200)
			<a href="/reports/channel_snags?dc_contract_id={{contract.id}}&amp;days_hidden={{days_hidden}}&amp;is_ignored={{is_ignored}}&amp;only_ongoing={{only_ongoing}}&amp;show_settlement={{show_settlement}}&amp;as_csv=true&amp;days_long_hidden={{days_long_hidden}}
			{%- for comm_id in comm_ids -%}
				&amp;comm_id={{comm_id}}
			{%- endfor -%}
			">Download as CSV</a>
		</caption>
		<thead>
			<tr>
				<th>View</th>
				<th>Contract</th>
				<th>Import MPAN Core</th>
				<th>Export MPAN Core</th>
				<th>Site</th>
				<th>Description</th>
				<th>Channels</th>
				<th>Comm</th>
				<th>Start Date</th>
				<th>Finish Date</th>
				<th>
					<span title="Blank if the snag is ongoing (ie. if the finish date is 'ongoing' or if it finished less than 'Days Hidden' ago).">Days Since Finished</span>
				</th>
				<th>Duration</th>
				<th>Issues</th>
			</tr>
		</thead>
		<tbody>
			{% for snag_group in snag_groups %}
				<tr>
					<td>
						<ul>
							{% for snag in snag_group.snags %}
								<li>
									<a href="/e/channel_snags/{{snag.id}}">view</a>
									[<a href="/e/channel_snags/{{snag.id}}/edit">edit</a>]
									{% if snag.is_ignored %} ignored{% endif %}
								</li>
							{% endfor %}
						</ul>
					</td>
					<td>
						<a href="/e/dc_contracts/{{snag_group.contract.id}}">{{snag_group.contract.name}}</a>
					</td>
					<td>
						{% if snag_group.era.imp_mpan_core %}
							<a href="/e/supplies/{{snag_group.supply.id}}">{{snag_group.era.imp_mpan_core}}</a>
						{% endif %}
					</td>
					<td>
						{% if snag_group.era.exp_mpan_core %}
							<a href="/e/supplies/{{snag_group.supply.id}}">{{snag_group.era.exp_mpan_core}}</a>
						{% endif %}
					</td>
					<td>
						<a href="/sites/{{snag_group.site.id}}">{{snag_group.site.code}} {{snag_group.site.name}}</a>
					</td>
					<td>{{snag_group.description}}</td>
					<td>
						<ul>
							{% for snag in snag_group.snags %}
								<li>
									{% if snag.channel.imp_related %}
										Import
									{% else %}
										Export
									{% endif %}
									{{snag.channel.channel_type}}
								</li>
							{% endfor %}
						</ul>
					</td>
					<td>
						<a href="/e/comms/{{snag_group.era.comm.id}}" title="{{snag_group.era.comm.description}}">{{snag_group.era.comm.code}}</a>
					</td>
					<td>
						{{snag_group.start_date|hh_format}}
					</td>
					<td>
						{{snag_group.finish_date|hh_format}}
					</td>
					<td>
						{% if snag_group.days_since_finished %}
							{{snag_group.days_since_finished}}
						{% endif %}
					</td>
					<td>{{snag_group.duration}}</td>
					<td>
						{% set issue_bundles = snag_group.issue_bundles %}
						{% if issue_bundles|length > 0 %}
							<ul>
								{% for issue_bundle in snag_group.issue_bundles %}
									{% set issue = issue_bundle.issue %}
									<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>
			{% endfor %}
		</tbody>
	</table>
{% endblock %}
