{% extends "layouts/base.html" %}
{% load i18n %}

{% block title %}{% trans "Preview Department Requests" %} - {{ complaint.reference_number }} - PX360{% endblock %}

{% block content %}
<div class="max-w-4xl mx-auto px-4 py-6">
    <div class="bg-gradient-to-r from-navy to-blue rounded-2xl p-6 text-white mb-6 shadow-lg">
        <h1 class="text-2xl font-bold">{% trans "Preview Department Requests" %}</h1>
        <p class="text-white/80 text-sm mt-1">{{ complaint.reference_number }} - {% trans "Review messages before sending to department champions" %}</p>
    </div>

    <div class="bg-amber-50 border border-amber-200 rounded-xl p-4 mb-6 flex items-start gap-3">
        <i data-lucide="info" class="w-5 h-5 text-amber-600 mt-0.5"></i>
        <div>
            <p class="text-sm font-semibold text-amber-800">{% trans "Review each message below" %}</p>
            <p class="text-xs text-amber-700 mt-1">{% trans "Department requests will be sent to department champions. Department managers will receive notification." %}</p>
        </div>
    </div>

    <form method="post">
        {% csrf_token %}
        {% for dept_id in selected_dept_ids %}
        <input type="hidden" name="selected_departments" value="{{ dept_id }}">
        {% endfor %}
        <input type="hidden" name="action" value="send">
        <input type="hidden" name="request_message" value="{{ request_message }}">
        {% for dept_id, contact_person_id in contact_person_map.items %}
        <input type="hidden" name="contact_person_{{ dept_id }}" value="{{ contact_person_id }}">
        {% endfor %}

        {% for dept_info in preview_depts %}
        <div class="bg-white rounded-2xl border border-slate-200 shadow-sm mb-4 overflow-hidden">
            <div class="bg-slate-50 px-5 py-3 border-b border-slate-200 flex items-center justify-between">
                <div class="flex items-center gap-3">
                    <div class="w-10 h-10 bg-navy rounded-full flex items-center justify-center text-white font-bold text-sm">
                        {{ dept_info.department_name|truncatechars:2 }}
                    </div>
                    <div>
                        <p class="font-bold text-navy text-sm">{{ dept_info.department_name }}</p>
                        <div class="flex items-center gap-2">
                            <i data-lucide="crown" class="w-3 h-3 text-amber-500"></i>
                            <span class="text-xs text-slate">{% trans "Send To:" %} {{ dept_info.contact_person_name }} ({{ dept_info.contact_person_role }})</span>
                        </div>
                    </div>
                </div>
                <span class="text-xs text-slate">{{ dept_info.champion_email|default:"No email" }}</span>
            </div>
            <div class="p-5">
                <div class="mb-3">
                    <p class="text-xs font-semibold text-slate uppercase tracking-wide mb-2">{% trans "Involved Staff" %}</p>
                    <div class="flex flex-wrap gap-2">
                        {% for s in dept_info.staff_list %}
                        <span class="px-2.5 py-1 bg-slate-100 rounded-lg text-xs font-bold text-navy">
                            {{ s.staff_name }} <span class="text-slate font-normal">({{ s.role }})</span>
                        </span>
                        {% endfor %}
                    </div>
                </div>
                {% if dept_info.dept_manager %}
                <p class="text-xs text-slate mt-2">
                    <i data-lucide="user-check" class="w-3 h-3 inline"></i>
                    {% trans "Manager notification:" %} {{ dept_info.dept_manager_name }} ({{ dept_info.dept_manager_email|default:"No email" }})
                </p>
                {% endif %}
            </div>
        </div>
        {% endfor %}

        <div class="flex gap-3 justify-end mt-6 mb-8">
            <a href="{% url 'complaints:send_to_department_form' pk=complaint.pk %}"
               class="px-6 py-3 border border-slate-200 rounded-xl font-semibold text-slate hover:bg-slate-50 transition">
                {% trans "Back to Selection" %}
            </a>
            <button type="submit" class="px-6 py-3 bg-navy text-white rounded-xl font-semibold hover:bg-blue transition flex items-center gap-2">
                <i data-lucide="send" class="w-5 h-5"></i>
                {% trans "Send All Requests" %} ({{ preview_depts|length }})
            </button>
        </div>
    </form>
</div>
{% endblock %}
