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

{% block title %}{{ inquiry.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_inquiries_list' department.pk %}" class="hover:text-navy transition">{% trans "Inquiries" %}</a>
        <i data-lucide="chevron-right" class="w-4 h-4"></i>
        <span class="text-navy font-semibold">{{ inquiry.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-amber-50 rounded-xl flex items-center justify-center">
                <i data-lucide="help-circle" class="w-5 h-5 text-amber-600"></i>
            </div>
            <div>
                <div class="flex items-center gap-2">
                    <h1 class="text-xl font-bold text-navy">{{ inquiry.reference_number }}</h1>
                    <span class="px-2.5 py-0.5 rounded-full text-[10px] font-bold uppercase
                        {% if inquiry.status == 'open' %}bg-blue-50 text-blue-700
                        {% elif inquiry.status == 'in_progress' %}bg-amber-50 text-amber-700
                        {% elif inquiry.status == 'resolved' %}bg-green-50 text-green-700
                        {% elif inquiry.status == 'closed' %}bg-slate-100 text-slate-600
                        {% else %}bg-slate-50 text-slate-500{% endif %}">
                        {{ inquiry.get_status_display }}
                    </span>
                </div>
                <p class="text-sm text-slate-500">{% trans "Inquiry" %}</p>
            </div>
        </div>
        <div class="flex items-center gap-3">
            {% if can_respond and inquiry.status == 'open' or can_respond and inquiry.status == 'in_progress' %}
            <button onclick="openDeptResponseModal('inquiry', '{{ inquiry.pk }}', '{% url 'inquiries:inquiry_department_response' pk=inquiry.pk %}', '{{ inquiry.reference_number }}', '{{ inquiry.subject|escapejs }}', '{{ inquiry.department_response_en|default:'' }}', '{{ inquiry.department_response_ar|default:'' }}')"
                    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 %}
            <a href="{% url 'organizations:department_inquiries_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 inquiries" %}
            </a>
        </div>
    </div>

    {% if inquiry.subject %}
    <h2 class="text-lg font-semibold text-navy mt-4 mb-6">{{ inquiry.subject }}</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="message-circle" class="w-4 h-4"></i> {% trans "Message" %}
            </h3>
            <p class="text-sm text-slate-700 whitespace-pre-wrap leading-relaxed">{{ inquiry.message|default:"—" }}</p>
        </div>

        <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">{{ inquiry.department.get_localized_name|default:"—" }}</p>
                </div>
                {% if inquiry.outgoing_department %}
                <div>
                    <p class="text-[10px] text-slate-400 uppercase font-bold mb-0.5">{% trans "Outgoing Department" %}</p>
                    <p class="text-sm font-medium text-navy">{{ inquiry.outgoing_department.get_localized_name }}</p>
                </div>
                {% endif %}
                <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">{{ inquiry.assigned_to.get_full_name|default:"—" }}</p>
                </div>
                <div>
                    <p class="text-[10px] text-slate-400 uppercase font-bold mb-0.5">{% trans "Contact" %}</p>
                    <p class="text-sm font-medium text-navy">{{ inquiry.contact_name|default:"—" }}</p>
                </div>
                {% if inquiry.contact_phone %}
                <div>
                    <p class="text-[10px] text-slate-400 uppercase font-bold mb-0.5">{% trans "Phone" %}</p>
                    <p class="text-sm font-medium text-navy">{{ inquiry.contact_phone }}</p>
                </div>
                {% endif %}
                {% if inquiry.contact_email %}
                <div>
                    <p class="text-[10px] text-slate-400 uppercase font-bold mb-0.5">{% trans "Email" %}</p>
                    <p class="text-sm font-medium text-navy">{{ inquiry.contact_email }}</p>
                </div>
                {% endif %}
                {% if inquiry.category %}
                <div>
                    <p class="text-[10px] text-slate-400 uppercase font-bold mb-0.5">{% trans "Category" %}</p>
                    <p class="text-sm font-medium text-navy">{{ inquiry.get_category_display }}</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 "Created" %}</p>
                    <p class="text-sm font-medium text-navy">{{ inquiry.created_at|date:"Y-m-d H:i" }}</p>
                </div>
            </div>
        </div>

        {% if inquiry.department_response_en or inquiry.department_response_ar %}
        <div class="bg-green-50 rounded-2xl border border-green-100 p-6">
            <h3 class="text-sm font-bold text-green-700 mb-3 flex items-center gap-2">
                <i data-lucide="check-circle" class="w-4 h-4"></i> {% trans "Department Response" %}
                {% if inquiry.department_responded_at %}
                <span class="text-[10px] text-green-600 font-normal ml-2">{{ inquiry.department_responded_at|date:"Y-m-d H:i" }}</span>
                {% endif %}
            </h3>
            {% if inquiry.department_response_en %}
            <p class="text-sm text-slate-700 whitespace-pre-wrap mb-2">{{ inquiry.department_response_en }}</p>
            {% endif %}
            {% if inquiry.department_response_ar %}
            <p class="text-sm text-slate-700 whitespace-pre-wrap" dir="rtl">{{ inquiry.department_response_ar }}</p>
            {% endif %}
        </div>
        {% endif %}

        {% include "partials/notes_panel.html" %}
    </div>
</div>

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