{% extends "base.html" %}

{% macro display_malcom(key, value) -%}
	{% if key == 'tags'%}
		{% if value | length == 0 %}
			<span class="label label-default">N/A</span>
		{%else%}
			{% for tag in value %}
				<span class="label label-tag-{{tag}}">{{tag}}</span>
			{%endfor%}
		{%endif%}
	{% else %}
		{{ value | display_other }}
	{% endif %}
{%- endmacro %}

{% block custom_head %}
<script src="{{ url_for('static', filename='custom_js/results.js') }}"></script>
<link href="{{ url_for('static', filename='custom_css/results.css') }}" rel='stylesheet'>

<link href="{{ url_for('static', filename='tagsinput/bootstrap-tagsinput.css') }}" rel='stylesheet'>
<script src="{{ url_for('static', filename='tagsinput/bootstrap-tagsinput.min.js') }}"></script>
{% endblock %}

{% block main %}
<div class='col-md-12'>

<div class="panel panel-default main-details">
	<div class="panel-body">

{% if value is iterable and value is not string %}
	<h2 class='page-header'>Details for bulk search</h2>
{%else%}
	<h2 class='page-header'><a class='glyphicon glyphicon-map-marker' href="{{ url_for('nodes', field=field, value=value) }}"></a> {{field}}: {{value}} </h2>
{%endif%}
	{% if base_elts|length == 1 %}

		<table class='table table-condensed main-table'>
		<tr>
			{% for key, label in base_elts[0].default_fields %}<th>{{label}}</th>{% endfor %}
		</tr>
		<tr>
			{% for key, label in base_elts[0].default_fields %}
					<td>{{ display_malcom(key, base_elts[0].get(key)) }}</td>
			{% endfor %}
		</tr>
		</table>

		<table class='table table-condensed details-table'>
			{% for label in base_elts[0].element_fields %}
				<tr><th>{{label[1]}}</th><td>{{ base_elts[0].get(label[0], "N/A") }}</td></tr>
			{% endfor %}
		</table>

	{% else %}
		<div>
			<div class="toggle-control" data-toggle-target='results'>
					<a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordionResults" href="#collapseResults">
					{{ base_elts|length }} matching elements
					</a>
			</div>
			<div id="results" class="toggle-hidden">
				<table class='table table-condensed'>
				{% for h in base_elts[0].fields %}
					<th>{{h[1]}}</th>
				{% endfor %}
				{% for e in base_elts %}
					<tr>
						{% for f in base_elts[0].fields %}
							{% if f[0] == 'value' %}
								<td>
								<a href="{{ url_for('nodes', field=f[0], value=e[f[0]]) }}" class='glyphicon glyphicon-map-marker'></a>&nbsp;
								<a href="{{ url_for('search', field=f[0], query=e[f[0]]) }}">{{ e[f[0]] }}</a>
								</td>
							{% else %}
								<td>{{ display_malcom(f[0], e[f[0]]) }}</td>
							{% endif %}
						{% endfor %}
						</tr>
					{% endfor %}
				</table>
			</div>
		</div>
	{% endif %}
	</div>
</div>

	{% if evil_elts|length > 0%}
		<div class="panel panel-danger">
			<div class="panel-heading">
				<h3 class="panel-title">Evilness</h3>
			</div>
			<div class='panel-body'>
				{% if evil_elts|length < 10 %}
					{% for elt in evil_elts.values() %}
						{% set outer_loop = loop %}
						{% for evil in elt.evil %}
							<div class='alert alert-danger evil toggle-control' data-toggle-target='details-evil-{{outer_loop.index0}}-{{loop.index0}}'>{{elt.value}} was listed in <strong>{{evil.source}}</strong> on {{evil.date_added|datetimeformat}}</div>
							<table class='table table-condensed toggle-hidden' id='details-evil-{{outer_loop.index0}}-{{loop.index0}}'>
								{% for key, value in evil.iteritems() %}
									<tr><th>{{key}}</th><td>{{value}}</td></tr>
								{% endfor %}
							</table>
						{% endfor %}
					{% endfor %}

				{% else %}
				<div>
					<div class="toggle-control" data-toggle-target='all-evil'>{{ evil_elts|length }} evil elements</div>
					<div id="all-evil" class='toggle-hidden'>
					{% for elt in evil_elts.values() %}
						{% set outer_loop = loop %}
						{% for evil in elt.evil %}
							<div class='alert alert-danger evil toggle-control' data-toggle-target='details-evil-{{evil.id}}{{outer_loop.index0}}'>{{elt.value}} was listed in <strong>{{evil.source}}</strong> on {{evil.date_added|datetimeformat}}</div>
							<table class='table table-condensed toggle-hidden' id='details-evil-{{evil.id}}{{outer_loop.index0}}'>
								{% for key, value in evil.iteritems() %}
									<tr><th>{{key}}</th><td>{{value}}</td></tr>
								{% endfor %}
							</table>
						{% endfor %}
					{% endfor %}
					</div>
				</div>
				{%endif%}
			</div>
		</div>
	{%endif%}

		<h3 class='page-header'>Related elements</h3>

		<ul id='relatedTabs' class='nav nav-tabs'>
			{% for r in related_elements %}
			<li {% if loop.index0 == 0%}class='active'{%endif%}><a href='#{{r}}' data-toggle='tab'>{{r}}</a></li>
			{% endfor %}
		</ul>

		<div id='relatedTabsContent' class='tab-content'>
			{% for r in related_elements %}
			<div class='tab-pane {% if loop.index0 == 0%}active{%endif%}' id='{{r}}'>
				<table class='table table-condensed'>
					<tr>{% for label in related_elements[r][0].display_fields if label[0] not in ['type'] %}<th>{{ label[1] }}</th>{% endfor %}</tr>
					{% for n in related_elements[r] %}
					<tr>
						{% for label in n.display_fields if label[0] not in ['type'] %}
							{% if label[0] == 'value' %}
								<td>
								<a href="{{ url_for('nodes', field=label[0], value=n[label[0]]) }}" class='glyphicon glyphicon-map-marker'></a>&nbsp;
								<a href="{{ url_for('search', field=label[0], query=n[label[0]]) }}">{{ n[label[0]] }}</a>
								</td>
							{%else%}
								<td>{{ display_malcom(label[0], n[label[0]]) }}</td>
							{%endif%}
						{% endfor %}
					</tr>
					{%endfor%}
				</table>
			</div>
			{% endfor %}
		</div>

		<h3 class='page-header'>Links</h3>


		<div role="tabpanel">

		<ul id='linkedTabs' class='nav nav-tabs'>
			{% for r in linked %}
			<li {% if loop.index0 == 0%}class='active'{%endif%}><a href='#{{r}}' data-toggle='tab'>{{r}}</a></li>
			{% endfor %}
		</ul>

		<div id='linkedTabsContent' class='tab-content'>
			{% for r in linked %}
				<div class='tab-pane {% if loop.first %}active{%endif%}' id='{{r}}'>
					{% for dest in linked[r]%}
						<div><a href="#" class="toggle-control" data-toggle-target='{{r}}{{loop.index0}}'>{{dest}}</a></div>
						<table id='{{r}}{{loop.index0}}' class='table table-condensed toggle-hidden'>
							<tr>
							{% for label in linked[r][dest][0].display_fields %}
								<th>{{label[1]}}</th>
							{%endfor%}
							</tr>

							{% for e in linked[r][dest] %}
							<tr>
								{% for label in e.display_fields %}

									{% if label[0] == 'value' %}
										<td>
										<a href="{{ url_for('nodes', field=label[0], value=e[label[0]]) }}" class='glyphicon glyphicon-map-marker'></a>&nbsp;
										<a href="{{ url_for('search', field=label[0], query=e[label[0]]) }}">{{ e[label[0]] }}</a></td>
									{%else%}
										<td>{{ display_malcom(label[0], e[label[0]]) }}</td>
									{%endif%}
								{% endfor %}
							</tr>
							{% endfor %}
						</table>
					{% endfor %}
				</div>
			{% endfor %}
		</div>
	</div>
</div>




{% endblock %}
