{% extends "macros.html" %}

{% block html %}
	<table class="sticky" id="issues">
		<caption>
			Issues (truncated after 200)
			<a href="/reports/g_issues?as_csv=true
			{%- for contract_id in contract_ids -%}
				&amp;contract_id={{contract_id}}
			{%- endfor -%}
			{%- for owner_id in owner_ids -%}
				&amp;owner_id={{owner_id}}
			{%- endfor -%}
			{%- for is_open in is_opens -%}
				{%- if is_open -%}
					&amp;is_open=true
				{%- else -%}
					&amp;is_open=false
				{%- endif -%}
			{%- endfor -%}
			">Download as CSV</a>
		</caption>
		<thead>
			<tr>
				<th rowspan="2">View</th>
				<th rowspan="2">Issue Id</th>
				<th rowspan="2">Contract</th>
				<th rowspan="2">Date Created</th>
				<th rowspan="2">Owner</th>
				<th rowspan="2">Status</th>
				<th rowspan="2">Subject</th>
				<th rowspan="2">Supplies</th>
				<th colspan="2">Latest Entry</th>
			</tr>
			<tr>
				<th>Timestamp</th>
				<th>Text</th>
			</tr>
		</thead>
		<tbody>
			{% for bundle in issue_bundles %}
				{% set entry = bundle.latest_entry %}
				<tr>
					<td>
						<a href="/g/issues/{{bundle.issue.id}}">View</a>
					</td>
					<td>{{bundle.issue.id}}</td>
					<td>
						<a href="/g/supplier_contracts/{{bundle.issue.g_contract.id}}">{{bundle.issue.g_contract.name}}</a>
					</td>
					<td>{{bundle.issue.date_created|date_format}}</td>
					<td>
						{% if bundle.owner %}
							<a href="/users/{{bundle.owner.id}}">{{bundle.owner.email_address}}</a>
						{% endif %}	
					</td>
					<td>
						{% if bundle.issue.is_open %}
							Open
						{% else %}
							Closed
						{% endif %}
					</td>
					<td>{{bundle.issue.properties['subject']}}</td>
					<td>
						<ul>
							{% for supply_bundle in bundle.supplies %}
								{% set supply = supply_bundle.supply %}
								{% set site = supply_bundle.site %}
								<li>
									<a href="/g/supplies/{{supply.id}}">{{supply.mprn}}</a>
								 	at <a href="/sites/{{site.id}}">{{site.code}} {{site.name}}</a>
								</li>
							{% endfor %}
						</ul>
					</td>
					<td>
						{% if entry %}
							{{entry.timestamp|date_format}}
						{% endif %}
					</td>
					<td>
						{% if entry %}
							{% set lines = entry.markdown.splitlines() %}
							{% if lines|length < 2 %}
								{{lines[0]}}
							{% else %}
								<details>
									<summary>{{lines[0]}}</summary>
									{{''.join(lines[1:])}}
								</details>
							{% endif %}
						{% endif %}
					</td>
				</tr>
			{% endfor %}
		</tbody>
	</table>
{% endblock %}
