{% extends 'base.html' %}
{% block title %}Asked questions{% endblock %}
{# set inboxLink = 'active' #}
{% block content %}
{% if questions != [] %}
<h3 class="fs-4">{{ len(questions) }} <span class="fw-light">question(s)</span></h3>
<div class="row">
	{% for question in questions %}
			<div class="card mt-3 mb-3" id="question-{{ question.id }}">
				<div class="position-relative">
					<div class="card-header">
						<div class="d-flex justify-content-between align-items-center flex-wrap text-break">
							<h3 class="h5 card-title mt-1 mb-1">
								{% if question.from_who %}
								{{ question.from_who }}
								{% else %}
								<i class="bi bi-incognito" data-bs-toggle="tooltip" data-bs-title="This question was asked anonymously" data-bs-placement="top"></i> {{ cfg.anonName }}
								{% endif %}
							</h3>
							<h3 class="h6 card-subtitle fw-light text-body-secondary" data-bs-toggle="tooltip" data-bs-title="{{ question.creation_date.strftime("%B %d, %Y %H:%M") }}" data-bs-placement="top">{{ formatRelativeTime(str(question.creation_date)) }}</h3>
						</div>
						<div class="card-text markdown-content">{{ question.content | render_markdown }}</div>
					</div>
				</div>
				<div class="card-body">
					{% for answer in item.answers %}
					<div class="markdown-content">{{ answer.content | render_markdown }}</div>
				</div>
				<div class="card-footer pt-0 pb-0 ps-3 pe-1 text-body-secondary d-flex justify-content-between align-items-center">
					<div>
						<span class="fs-6" data-bs-toggle="tooltip" data-bs-title="{{ answer.creation_date.strftime("%B %d, %Y %H:%M") }}">{{ formatRelativeTime(str(answer.creation_date)) }}</span>
						{% if question.private %}
						<span class="ms-1"><i class="bi bi-lock"></i> <span class="fw-medium">Private</span></span>
						{% endif %}
					</div>
					<div class="d-flex align-items-center">
						<a href="{{ url_for('viewQuestion', question_id=question.id) }}" class="btn pt-2 pb-2 text-body-secondary" data-bs-toggle="tooltip" data-bs-title="View question" aria-label="View question"><i class="bi bi-box-arrow-up-right"></i></a>
					</div>
					{% endfor %}
				</div>
			</div>
			{% endfor %}
</div>
{% else %}
<h2 class="text-center mt-5">Inbox is currently empty.</h2>
{% endif %}
{% endblock %}
{% block scripts %}
<script>
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
	const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl));
	const appendAlert = (elementId, message, type) => {
		const alertPlaceholder = document.getElementById(elementId);
		const alertHtml = `
		<div class="alert alert-${type} alert-dismissible" role="alert">
		<div>${message}</div>
		<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
		</div>
		`;

		alertPlaceholder.outerHTML = alertHtml;
	}

	document.addEventListener('htmx:afterRequest', function(event) {
		const jsonResponse = event.detail.xhr.response;
		if (jsonResponse) {
			const parsed = JSON.parse(jsonResponse);
			const msgType = event.detail.successful ? 'success' : 'error';
			const targetElementId = event.detail.target.id;
			appendAlert(targetElementId, parsed.message, msgType);
		}
	})
</script>
{% endblock %}
