{% from "_forms.html" import quickform, parsefield %}
{% extends "layout.html" %}

{%- macro accordion(title, container_list) -%}
<div class="accordion" style="width: 100%;">
	{% for container in container_list %}
 		<div class="title active">
			<i class="dropdown icon"></i>{{ title }}: {{container_list|length}}
		</div>
		<div class="content active">
			<p class="transition visible" style="display: block !important;">
			<ul class="ui list">
				{% for item in container %}
					<li>{{ item['provider_info']['pushed'] }}: {{ incident['arguments'] }}</li>
				{% endfor %}
			</ul>	
			</p>
		</div>
	{% endfor %}
</div>


{%- endmacro -%}

{%- macro call_indicator(call_dict) -%}
	{% if call_dict and call_dict['incidents'] %}
		{{ call_dict['incidents']|length }}
		{%- if call_dict['status'] and call_dict['status']['name'] == 'unknown' %}
		{% elif call_dict['status'] and call_dict['status']['name'] == 'done' %}*
		{% elif call_dict['status'] and call_dict['status']['name'] == 'postponed' %}+
		{% elif call_dict['status'] and call_dict['status']['name'] == 'insufficient incidents' %}
		{% else %}?
		{% endif %}
	{% else %}
		0
    {% endif %}
{%- endmacro -%}

{%- macro call_table_head(event, call, use, allow_manufacture) -%}
	{% if event[call] and event[call]['incidents'] or allow_manufacture %}
	<td>
		<a href="{{ url_for('show_incidents_per_id', incident_id=event['id_string'], call=call, use=use) }}">
			{% if call == 'create' %}Create<br />DT#1
			{%- elif call == 'dynamic_bmgs' %}Dynamic BMGs<br />DT#2
			{%- elif call == 'in_progress' %}In Progress<br />DT#3
			{%- elif call == 'finish' %}Finish<br />DT#4
			{%- elif call == 'result' %}Result<br />DT#5
			{%- elif call == 'canceled' %}Canceled<br />DT#6
			{% endif %}
		</a>
		{% if event[call] and event[call]['status'] and not event[call]['status']['name'] == 'unknown' %}
			<br/>{{ event[call]['status']['name'] }}
		{% endif %}
	</td>
    {% endif %}
{%- endmacro -%}

{%- macro call_table_body(event, call, allow_manufacture) -%}
	{% if event[call] and event[call]['incidents'] or allow_manufacture %}
		<td>
			{% if event[call] and event[call]['incidents'] %}
				{% for provider,incidents_dict in event[call]['incidents_per_provider'].items() %}
					{{ provider }}
					<br />
					<ul class="ui list">
						{% for incident in incidents_dict["incidents"] %}
							<li>
								{{ incident['provider_info']['pushed'] }}: {{ incident['arguments'] }}
								<br />
								<a href="{{ url_for('replay', filter=incident['unique_string'], use=use) }}">replay</a>
							</li>
						{% endfor %}
					</ul>
				{% endfor %}
			{% endif %}
			{% if allow_manufacture and menuInfo['incidents'] and menuInfo['incidents']['dataproxy_link'] %}
				Manufacture
				<br />
				<ul class="ui list">
				    {% for provider_hash, provider in menuInfo['incidents']['dataproxy_link'].items() %}
						<li>
							<a target="_blank" href="{{ provider['replay'] + '&manufacture=' + event['reconstruct_string'] + '__' + call + '__None' }}">{{ provider_hash }}</a>
						 </li>
					{% endfor %}
				</ul>
			{% endif %}
		</td>
	{% endif %}
{%- endmacro -%}

{% block content %}
{% if menuInfo['incidents'] %}
	<h2>Linked dataproxies</h2>
	<ol>
	{% for provider_hash, provider in menuInfo['incidents']['dataproxy_link'].items() %}
		<li>
		<a href="{{provider['isalive']}}">{{provider["name"]}}</a>
		<br />
		{% if not provider["status"] == "ok"%}
			{% if provider["details"] %}
				{% for group, subscriber_list in provider["details"]["subscribers"].items() %}
					{% for subscriber in subscriber_list %}	
					 	{% if subscriber["status"] == "nok" %}
							<i class="small red circle icon"></i> Anonymous subscriber <br />
						{% elif subscriber["status"] == "unknown" %}
							<i class="small yellow circle icon"></i> Anonymous subscriber, nothing sent yet <br />
						{% endif %}
					{% endfor %}
				{% endfor %}		
				{% for incoming in provider["details"]["providers"] %}
					{% if incoming["status"] == "ok" %}
						<i class="small green circle icon"></i>
					{% else %}
						<i class="small red circle icon"></i>			
					{% endif %} {{incoming["name"]}}: {{incoming["last_incident"]}}				<br />
				{% endfor %}
			{% else %}
				<i class="small red circle icon"></i> Unknown reason <br />
			{% endif %}
		{% endif %}
		</li>
	{% endfor %}
	</ol>
{% endif %}

<h2>Received incidents ({{ use }})</h2>
Showing all received incidents for events that are scheduled between {{ from_date }} (change with from_date url argument) and {{ to_date }} (change with to_date url argument)
{% if matching %}
 that match {{ matching }} in their identifier
{% endif %}
<br /><br />
<p>
All events are listed as with their id and incidents count
<table class="ui very basic collapsing celled table">
<tr>
<td>Id</td>
<td>"Scheduled Time-Sport-Event Group-Home Team-Away Team"</td>
</tr>
<tr>
<td style="vertical-align: top;">Incident count</td>
<td>"Create - In progress - Finish - Result"<br/><br />
Markings according to the status:
<ul>
  <li>No special mark - Nothing done yet</li>
  <li>* - Incident successfully triggered an action on the blockchain</li>
  <li>+ - Action is deliberately postponed</li>
  <li>? - An error occured, manual checking necessary</li>
</ul></td>
</tr>
</table>

</p>

{% if events|length > 0 %}

<div class="ui equal width grid">
<div class="column">
<div class="ui grid">
<div class="six wide column">
</div></div>
<br />
<div class="ui styled accordion" style="width: 100%;">
	{% for event in events %}
  		<div class="title">
<i class="dropdown icon"></i>{{ event['reconstruct_string'] }}:
	{{ call_indicator(event['create']) }}
   	-
   	{{ call_indicator(event['dynamic_bmgs']) }}
   	-
   	{{ call_indicator(event['in_progress']) }}
    -
   	{{ call_indicator(event['finish']) }}
    -
   	{{ call_indicator(event['result']) }}
   	-
   	{{ call_indicator(event['canceled']) }}
</div>
<div class="content">
	<p class="transition visible" style="display: block !important;">
	Id string: {{ event['id_string'] }}:
	<table class="ui very basic collapsing celled table">
		<tbody>
			<thead>
				{{ call_table_head(event, 'create', use) }}
				{{ call_table_head(event, 'dynamic_bmgs', use) }}
			</thead>
			<tr>
				{{ call_table_body(event, 'create') }}
				{{ call_table_body(event, 'dynamic_bmgs') }}
			</tr>
			<thead>
				{{ call_table_head(event, 'in_progress', use) }}
				{{ call_table_head(event, 'finish', use) }}
			</thead>
			<tr>
				{{ call_table_body(event, 'in_progress') }}
				{{ call_table_body(event, 'finish') }}
			</tr>
			<thead>
				{{ call_table_head(event, 'result', use) }}
				{{ call_table_head(event, 'canceled', use, true) }}
			</thead>
			<tr>
				{{ call_table_body(event, 'result') }}
				{{ call_table_body(event, 'canceled', true) }}
			</tr>
		</tbody>
	</table>
	</p>
</div>
  	{% endfor %}
</div>
</div>
</div>

{% else %}

No incidents came in yet.

{% endif %}

{% endblock %}


