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

{% block title %}{% trans "Create Inquiry" %} - {{ source.get_localized_name }}{% endblock %}

{% block extra_css %}
<style>
    @keyframes fadeIn {
        from { opacity: 0; transform: translateY(20px); }
        to { opacity: 1; transform: translateY(0); }
    }
    .animate-in {
        animation: fadeIn 0.5s ease-out forwards;
    }
</style>
{% endblock %}

{% block content %}
<div class="p-6 md:p-8 bg-gradient-to-br from-light to-blue-50 min-h-screen">
    <!-- Page Header -->
    <div class="flex flex-wrap justify-between items-center gap-4 mb-8 animate-in">
        <div class="flex items-center gap-3">
            <div class="flex items-center justify-center w-14 h-14 bg-gradient-to-br from-cyan-500 to-blue-500 rounded-2xl shadow-lg shadow-cyan-200">
                <i data-lucide="plus-circle" class="w-8 h-8 text-white"></i>
            </div>
            <div>
                <h1 class="text-2xl font-bold text-navy">
                    {% trans "Create Inquiry" %}
                </h1>
                <p class="text-slate text-sm">
                    {% trans "Submit a new inquiry from" %} <strong>{{ source.get_localized_name }}</strong>
                </p>
            </div>
        </div>
        <a href="{% url 'px_sources:source_user_inquiry_list' %}"
           class="inline-flex items-center gap-2 px-5 py-2.5 bg-white border-2 border-blue-200 text-navy rounded-xl font-semibold hover:bg-blue-50 transition">
            <i data-lucide="arrow-left" class="w-4 h-4"></i>
            {% trans "Back to Inquiries" %}
        </a>
    </div>

    <!-- Info Banner -->
    <div class="bg-cyan-50 border border-cyan-200 rounded-2xl p-4 mb-8 animate-in">
        <div class="flex items-start gap-3">
            <i data-lucide="info" class="w-5 h-5 text-cyan-600 mt-0.5 flex-shrink-0"></i>
            <div>
                <p class="text-sm font-bold text-cyan-800 mb-1">{% trans "Source Information" %}</p>
                <p class="text-sm text-cyan-700">
                    {% trans "This inquiry will be automatically linked to" %} <strong>{{ source.get_localized_name }}</strong>. 
                    {% trans "All required fields are marked with" %} <span class="text-red-500">*</span>
                </p>
            </div>
        </div>
    </div>

    <!-- Form -->
    <div class="bg-white rounded-2xl shadow-sm border border-blue-100 overflow-hidden animate-in">
        <form method="post" novalidate data-loading data-loading-text="{% trans 'Submitting...' %}">
            {% csrf_token %}

            <div class="p-6 md:p-8">
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <!-- Contact Name -->
                    <div>
                        <label for="{{ form.contact_name.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.contact_name.label }} <span class="text-red-500">*</span>
                        </label>
                        {{ form.contact_name }}
                        {% if form.contact_name.errors %}
                        <div class="flex items-center gap-2 mt-2 text-red-600 text-sm">
                            <i data-lucide="alert-circle" class="w-4 h-4"></i>
                            {% for error in form.contact_name.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>

                    <!-- Contact Phone -->
                    <div>
                        <label for="{{ form.contact_phone.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.contact_phone.label }} <span class="text-red-500">*</span>
                        </label>
                        {{ form.contact_phone }}
                        {% if form.contact_phone.errors %}
                        <div class="flex items-center gap-2 mt-2 text-red-600 text-sm">
                            <i data-lucide="alert-circle" class="w-4 h-4"></i>
                            {% for error in form.contact_phone.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>

                    <!-- Contact Email -->
                    <div>
                        <label for="{{ form.contact_email.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.contact_email.label }}
                        </label>
                        {{ form.contact_email }}
                        {% if form.contact_email.errors %}
                        <div class="flex items-center gap-2 mt-2 text-red-600 text-sm">
                            <i data-lucide="alert-circle" class="w-4 h-4"></i>
                            {% for error in form.contact_email.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>

                    <!-- Inquiry Type -->
                    <div>
                        <label for="{{ form.category.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.category.label }} <span class="text-red-500">*</span>
                        </label>
                        {{ form.category }}
                        {% if form.category.errors %}
                        <div class="flex items-center gap-2 mt-2 text-red-600 text-sm">
                            <i data-lucide="alert-circle" class="w-4 h-4"></i>
                            {% for error in form.category.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>
                </div>

                <!-- Location Hierarchy -->
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mt-6">
                    <div>
                        <label for="{{ form.location.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.location.label }}
                        </label>
                        {{ form.location }}
                    </div>

                    <div>
                        <label for="{{ form.main_section.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.main_section.label }}
                        </label>
                        {{ form.main_section }}
                    </div>

                    <div>
                        <label for="{{ form.subsection.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.subsection.label }}
                        </label>
                        {{ form.subsection }}
                    </div>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mt-6">

                    <!-- Subject -->
                    <div class="md:col-span-2">
                        <label for="{{ form.subject.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.subject.label }} <span class="text-red-500">*</span>
                        </label>
                        {{ form.subject }}
                        {% if form.subject.errors %}
                        <div class="flex items-center gap-2 mt-2 text-red-600 text-sm">
                            <i data-lucide="alert-circle" class="w-4 h-4"></i>
                            {% for error in form.subject.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>

                    <!-- Message -->
                    <div class="md:col-span-2">
                        <label for="{{ form.message.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.message.label }} <span class="text-red-500">*</span>
                        </label>
                        {{ form.message }}
                        {% if form.message.errors %}
                        <div class="flex items-center gap-2 mt-2 text-red-600 text-sm">
                            <i data-lucide="alert-circle" class="w-4 h-4"></i>
                            {% for error in form.message.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>

            <!-- Form Actions -->
            <div class="px-6 md:px-8 py-4 bg-slate-50 border-t border-slate-100 flex gap-3">
                <button type="submit"
                        class="inline-flex items-center gap-2 px-6 py-3 bg-blue-600 text-white rounded-xl font-semibold hover:bg-blue-700 transition">
                    <i data-lucide="check" class="w-5 h-5"></i>
                    {% trans "Submit Inquiry" %}
                </button>
                <a href="{% url 'px_sources:source_user_inquiry_list' %}"
                   class="inline-flex items-center gap-2 px-6 py-3 bg-white border-2 border-slate-200 text-slate-700 rounded-xl font-semibold hover:bg-slate-50 transition">
                    <i data-lucide="x" class="w-5 h-5"></i>
                    {% trans "Cancel" %}
                </a>
            </div>
        </form>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    lucide.createIcons();

    const locationSelect = document.getElementById('{{ form.location.id_for_label }}');
    const sectionSelect = document.getElementById('{{ form.main_section.id_for_label }}');
    const subsectionSelect = document.getElementById('{{ form.subsection.id_for_label }}');

    if (locationSelect) {
        locationSelect.addEventListener('change', function() {
            const locationId = this.value;
            sectionSelect.innerHTML = '<option value="">{% trans "Loading..." %}</option>';
            subsectionSelect.innerHTML = '<option value="">{% trans "Select Subsection" %}</option>';
            sectionSelect.disabled = true;
            subsectionSelect.disabled = true;

            if (!locationId) {
                sectionSelect.innerHTML = '<option value="">{% trans "Select Section" %}</option>';
                return;
            }

            fetch('{% url "organizations:ajax_main_sections" %}?location_id=' + locationId)
                .then(r => r.json())
                .then(data => {
                    const sections = data.sections || [];
                    sectionSelect.innerHTML = '<option value="">{% trans "Select Section" %}</option>';
                    sections.forEach(s => {
                        const opt = document.createElement('option');
                        opt.value = s.id;
                        opt.textContent = s.name;
                        sectionSelect.appendChild(opt);
                    });
                    if (sections.length > 0) sectionSelect.disabled = false;
                })
                .catch(err => {
                    console.error('Error loading sections:', err);
                    sectionSelect.innerHTML = '<option value="">{% trans "Error loading sections" %}</option>';
                });
        });

        if (locationSelect.value) {
            locationSelect.dispatchEvent(new Event('change'));
        }
    }

    if (sectionSelect) {
        sectionSelect.addEventListener('change', function() {
            const locationId = locationSelect ? locationSelect.value : '';
            const sectionId = this.value;
            subsectionSelect.innerHTML = '<option value="">{% trans "Loading..." %}</option>';
            subsectionSelect.disabled = true;

            if (!sectionId) {
                subsectionSelect.innerHTML = '<option value="">{% trans "Select Subsection" %}</option>';
                return;
            }

            fetch('{% url "organizations:ajax_subsections" %}?location_id=' + locationId + '&main_section_id=' + sectionId)
                .then(r => r.json())
                .then(data => {
                    const subsections = data.subsections || [];
                    subsectionSelect.innerHTML = '<option value="">{% trans "Select Subsection" %}</option>';
                    subsections.forEach(s => {
                        const opt = document.createElement('option');
                        opt.value = s.id;
                        opt.textContent = s.name;
                        subsectionSelect.appendChild(opt);
                    });
                    if (subsections.length > 0) subsectionSelect.disabled = false;
                })
                .catch(err => {
                    console.error('Error loading subsections:', err);
                    subsectionSelect.innerHTML = '<option value="">{% trans "Error loading subsections" %}</option>';
                });
        });

        if (sectionSelect.value) {
            sectionSelect.dispatchEvent(new Event('change'));
        }
    }
});
</script>
{% endblock %}
