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

{% block title %}{{ complaint.reference_number }} - {{ department.get_localized_name }} - PX360{% endblock %}

{% block content %}
<div class="p-6 max-w-5xl mx-auto">
    <div class="flex items-center gap-2 text-sm text-slate-500 mb-4">
        <a href="{% url 'organizations:department_list' %}" class="hover:text-navy transition">{% trans "Departments" %}</a>
        <i data-lucide="chevron-right" class="w-4 h-4"></i>
        <a href="{% url 'organizations:department_detail' department.pk %}" class="hover:text-navy transition">{{ department.get_localized_name }}</a>
        <i data-lucide="chevron-right" class="w-4 h-4"></i>
        <a href="{% url 'organizations:department_complaints_list' department.pk %}" class="hover:text-navy transition">{% trans "Complaints" %}</a>
        <i data-lucide="chevron-right" class="w-4 h-4"></i>
        <span class="text-navy font-semibold">{{ complaint.reference_number }}</span>
    </div>

    <div class="flex items-center justify-between mb-2">
        <div class="flex items-center gap-3">
            <div class="w-10 h-10 bg-red-50 rounded-xl flex items-center justify-center">
                <i data-lucide="file-text" class="w-5 h-5 text-red-600"></i>
            </div>
            <div>
                <div class="flex items-center gap-2">
                    <h1 class="text-xl font-bold text-navy">{{ complaint.reference_number }}</h1>
                    <span class="px-2.5 py-0.5 rounded-full text-[10px] font-bold uppercase
                        {% if complaint.status == 'open' %}bg-blue-50 text-blue-700
                        {% elif complaint.status == 'in_progress' %}bg-amber-50 text-amber-700
                        {% elif complaint.status == 'resolved' %}bg-green-50 text-green-700
                        {% elif complaint.status == 'closed' %}bg-slate-100 text-slate-600
                        {% else %}bg-slate-50 text-slate-500{% endif %}">
                        {{ complaint.get_status_display }}
                    </span>
                    {% if complaint.severity %}
                    <span class="px-2.5 py-0.5 rounded-full text-[10px] font-bold uppercase
                        {% if complaint.severity == 'critical' %}bg-gray-800 text-white
                        {% elif complaint.severity == 'high' %}bg-red-50 text-red-700
                        {% elif complaint.severity == 'medium' %}bg-amber-50 text-amber-700
                        {% else %}bg-green-50 text-green-700{% endif %}">
                        {{ complaint.get_severity_display }}
                    </span>
                    {% endif %}
                    {% if complaint.is_overdue %}
                    <span class="px-2 py-0.5 rounded-full text-[10px] font-bold bg-red-100 text-red-700">
                        <i data-lucide="alert-triangle" class="w-3 h-3 inline"></i> {% trans "Overdue" %}
                    </span>
                    {% endif %}
                </div>
                <p class="text-sm text-slate-500">{% trans "Complaint" %}</p>
            </div>
        </div>
        <div class="flex items-center gap-3">
            {% if can_respond and involved_dept %}
            <button onclick="openDeptResponseModal('complaint', '{{ involved_dept.pk }}', '{% url 'complaints:involved_department_response' involved_dept.pk %}', '{{ complaint.reference_number }}', '{{ complaint.title|escapejs }}')"
                    class="px-4 py-2 bg-navy text-white rounded-xl text-sm font-semibold hover:bg-blue transition flex items-center gap-2">
                <i data-lucide="message-square" class="w-4 h-4"></i> {% trans "Respond" %}
            </button>
            {% endif %}
            {% if can_manager_review and involved_dept %}
            <a href="{% url 'organizations:department_manager_review' department.pk involved_dept.pk %}"
                    class="px-4 py-2 bg-amber-600 text-white rounded-xl text-sm font-semibold hover:bg-amber-700 transition flex items-center gap-2">
                <i data-lucide="shield-check" class="w-4 h-4"></i> {% trans "Review Response" %}
            </a>
            {% endif %}
            <a href="{% url 'organizations:department_complaints_list' department.pk %}" class="text-sm text-slate hover:text-navy transition flex items-center gap-1">
                <i data-lucide="arrow-left" class="w-4 h-4"></i> {% trans "Back to complaints" %}
            </a>
        </div>
    </div>

    {% if complaint.title %}
    <h2 class="text-lg font-semibold text-navy mt-4 mb-6">{{ complaint.title }}</h2>
    {% endif %}

    <div class="space-y-6 mt-6">
        <div class="bg-white rounded-2xl border border-slate-200 p-6">
            <h3 class="text-sm font-bold text-navy mb-3 flex items-center gap-2">
                <i data-lucide="file-text" class="w-4 h-4"></i> {% trans "Description" %}
            </h3>
            <p class="text-sm text-slate-700 whitespace-pre-wrap leading-relaxed">{{ complaint.description|default:"—" }}</p>
        </div>

        {% if complaint.ai_brief_en %}
        <div class="bg-indigo-50 rounded-2xl border border-indigo-100 p-6">
            <h3 class="text-sm font-bold text-indigo-700 mb-3 flex items-center gap-2">
                <i data-lucide="file-text" class="w-4 h-4"></i> {% trans "Short Description" %}
            </h3>
            <p class="text-sm text-slate-700 whitespace-pre-wrap">{{ complaint.ai_brief_en }}</p>
        </div>
        {% endif %}

        <div class="bg-white rounded-2xl border border-slate-200 p-6">
            <h3 class="text-sm font-bold text-navy mb-4 flex items-center gap-2">
                <i data-lucide="info" class="w-4 h-4"></i> {% trans "Details" %}
            </h3>
            <div class="grid grid-cols-2 md:grid-cols-3 gap-4">
                <div>
                    <p class="text-[10px] text-slate-400 uppercase font-bold mb-0.5">{% trans "Department" %}</p>
                    <p class="text-sm font-medium text-navy">{{ complaint.department.get_localized_name|default:"—" }}</p>
                </div>
                <div>
                    <p class="text-[10px] text-slate-400 uppercase font-bold mb-0.5">{% trans "Assigned To" %}</p>
                    <p class="text-sm font-medium text-navy">{{ complaint.assigned_to.get_full_name|default:"—" }}</p>
                </div>
                <div>
                    <p class="text-[10px] text-slate-400 uppercase font-bold mb-0.5">{% trans "Patient MRN" %}</p>
                    <p class="text-sm font-medium text-navy">{{ complaint.patient.mrn|default:"—" }}</p>
                </div>
                {% if staff_list %}
                <div>
                    <p class="text-[10px] text-slate-400 uppercase font-bold mb-0.5">{% trans "Staff Involved" %}</p>
                    {% for s in staff_list %}
                    <p class="text-sm font-medium text-navy">{{ s }}</p>
                    {% endfor %}
                </div>
                {% endif %}
                {% if taxonomy %}
                <div>
                    <p class="text-[10px] text-slate-400 uppercase font-bold mb-0.5">{% trans "Classification" %}</p>
                    <p class="text-sm font-medium text-navy">{{ taxonomy|join:" > " }}</p>
                </div>
                {% endif %}
                {% if location_str %}
                <div>
                    <p class="text-[10px] text-slate-400 uppercase font-bold mb-0.5">{% trans "Location" %}</p>
                    <p class="text-sm font-medium text-navy">{{ location_str }}</p>
                </div>
                {% endif %}
                <div>
                    <p class="text-[10px] text-slate-400 uppercase font-bold mb-0.5">{% trans "Source" %}</p>
                    <p class="text-sm font-medium text-navy">{{ complaint.source.name_en|default:"—" }}</p>
                </div>
                <div>
                    <p class="text-[10px] text-slate-400 uppercase font-bold mb-0.5">{% trans "Created" %}</p>
                    <p class="text-sm font-medium text-navy">{{ complaint.created_at|date:"Y-m-d H:i" }}</p>
                </div>
                {% if complaint.due_at %}
                <div>
                    <p class="text-[10px] text-slate-400 uppercase font-bold mb-0.5">{% trans "SLA Deadline" %}</p>
                    <p class="text-sm font-medium {% if complaint.is_overdue %}text-red-600{% else %}text-navy{% endif %}">
                        {{ complaint.due_at|date:"Y-m-d H:i" }}
                        {% if complaint.is_overdue %} <span class="text-[10px] font-bold">({% trans "Overdue" %})</span>{% endif %}
                    </p>
                </div>
                {% endif %}
                {% if complaint.escalated_at %}
                <div>
                    <p class="text-[10px] text-slate-400 uppercase font-bold mb-0.5">{% trans "Escalated" %}</p>
                    <p class="text-sm font-medium text-red-600">{{ complaint.escalated_at|date:"Y-m-d H:i" }}</p>
                </div>
                {% endif %}
            </div>
        </div>

    {% if complaint.expected_result %}
    <div class="bg-blue-50 rounded-2xl border border-blue-100 p-6">
        <h3 class="text-sm font-bold text-blue-700 mb-3 flex items-center gap-2">
            <i data-lucide="target" class="w-4 h-4"></i> {% trans "Expected Result" %}
        </h3>
        <p class="text-sm text-slate-700 whitespace-pre-wrap">{{ complaint.expected_result }}</p>
    </div>
    {% endif %}
    </div>
</div>

{% include "components/department_response_modal.html" %}
{% endblock %}
