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

{% block title %}{% trans "Create Complaint" %} - {{ 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-orange-400 to-orange-500 rounded-2xl shadow-lg shadow-orange-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 Complaint" %}
                </h1>
                <p class="text-slate text-sm">
                    {% trans "Submit a new complaint from" %} <strong>{{ source.get_localized_name }}</strong>
                </p>
            </div>
        </div>
        <a href="{% url 'px_sources:source_user_complaint_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 Complaints" %}
        </a>
    </div>

    <!-- Info Banner -->
    <div class="bg-blue-50 border border-blue-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-blue-600 mt-0.5 flex-shrink-0"></i>
            <div>
                <p class="text-sm font-bold text-blue-800 mb-1">{% trans "Source Information" %}</p>
                <p class="text-sm text-blue-700">
                    {% trans "This complaint 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">
                    <!-- Complainant Name -->
                    <div>
                        <label for="{{ form.complainant_name.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.complainant_name.label }} <span class="text-red-500">*</span>
                        </label>
                        {{ form.complainant_name }}
                        {% if form.complainant_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.complainant_name.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>

                    <!-- Mobile Number -->
                    <div>
                        <label for="{{ form.mobile_number.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.mobile_number.label }} <span class="text-red-500">*</span>
                        </label>
                        {{ form.mobile_number }}
                        {% if form.mobile_number.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.mobile_number.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>

                    <!-- Email -->
                    <div>
                        <label for="{{ form.email.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.email.label }}
                        </label>
                        {{ form.email }}
                        {% if form.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.email.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>

                    <!-- Relation to Patient -->
                    <div>
                        <label for="{{ form.relation_to_patient.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.relation_to_patient.label }} <span class="text-red-500">*</span>
                        </label>
                        {{ form.relation_to_patient }}
                        {% if form.relation_to_patient.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.relation_to_patient.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>

                    <!-- Patient Name -->
                    <div>
                        <label for="{{ form.patient_name.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.patient_name.label }} <span class="text-red-500">*</span>
                        </label>
                        {{ form.patient_name }}
                        {% if form.patient_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.patient_name.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>

                    <!-- National ID -->
                    <div>
                        <label for="{{ form.national_id.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.national_id.label }} <span class="text-red-500">*</span>
                        </label>
                        {{ form.national_id }}
                        {% if form.national_id.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.national_id.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>

                    <!-- Incident Date -->
                    <div>
                        <label for="{{ form.incident_date.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.incident_date.label }} <span class="text-red-500">*</span>
                        </label>
                        {{ form.incident_date }}
                        {% if form.incident_date.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.incident_date.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>

                    <!-- Staff Name -->
                    <div>
                        <label for="{{ form.staff_name.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.staff_name.label }}
                        </label>
                        {{ form.staff_name }}
                        <p class="text-xs text-slate mt-1">{% trans "Optional - Name of staff involved" %}</p>
                        {% if form.staff_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.staff_name.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>

                    <!-- Hospital -->
                    <div>
                        <label for="{{ form.hospital.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.hospital.label }} <span class="text-red-500">*</span>
                        </label>
                        {{ form.hospital }}
                        {% if form.hospital.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.hospital.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>

                    <!-- Department -->
                    <div class="md:col-span-2">
                        <label for="{{ form.department.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.department.label }} <span class="text-red-500">*</span>
                        </label>
                        {{ form.department }}
                        {% if form.department.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.department.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>
                </div>

                <!-- Section (optional, cascaded from Department) -->
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mt-6">
                    <div>
                        <label for="{{ form.section.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.section.label }}
                        </label>
                        {{ form.section }}
                        {% if form.section.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.section.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>
                </div>

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

                    <!-- Complaint Details (Description) -->
                    <div class="md:col-span-2">
                        <label for="{{ form.complaint_details.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.complaint_details.label }} <span class="text-red-500">*</span>
                        </label>
                        {{ form.complaint_details }}
                        {% if form.complaint_details.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.complaint_details.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>

                    <!-- Expected Result -->
                    <div class="md:col-span-2">
                        <label for="{{ form.expected_result.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.expected_result.label }}
                        </label>
                        {{ form.expected_result }}
                        <p class="text-xs text-slate mt-1">{% trans "What do you expect as a resolution? (Optional)" %}</p>
                        {% if form.expected_result.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.expected_result.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 Complaint" %}
                </button>
                <a href="{% url 'px_sources:source_user_complaint_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();
    
    // Set max date for incident date to today
    const incidentDateInput = document.getElementById('{{ form.incident_date.id_for_label }}');
    if (incidentDateInput) {
        const today = new Date().toISOString().split('T')[0];
        incidentDateInput.setAttribute('max', today);
    }
    
    // Cascading dropdowns for Department → Section
    const departmentSelect = document.getElementById('{{ form.department.id_for_label }}');
    const sectionSelect = document.getElementById('{{ form.section.id_for_label }}');
    
    if (departmentSelect && sectionSelect) {
        departmentSelect.addEventListener('change', function() {
            const deptId = this.value;
            
            sectionSelect.innerHTML = '<option value="">{% trans "Select Section (optional)" %}</option>';
            sectionSelect.disabled = true;
            
            if (deptId) {
                sectionSelect.innerHTML = '<option value="">{% trans "Loading..." %}</option>';
                fetch('/organizations/api/sections/?department=' + deptId)
                    .then(r => r.json())
                    .then(data => {
                        const sections = data.results || data;
                        sectionSelect.innerHTML = '<option value="">{% trans "Select Section (optional)" %}</option>';
                        sections.forEach(sec => {
                            const opt = document.createElement('option');
                            opt.value = sec.id;
                            opt.textContent = sec.name_en || sec.name || sec.display_name;
                            sectionSelect.appendChild(opt);
                        });
                        if (sections.length > 0) {
                            sectionSelect.disabled = false;
                        }
                    })
                    .catch(error => {
                        console.error('Error loading sections:', error);
                        sectionSelect.innerHTML = '<option value="">{% trans "Error loading sections" %}</option>';
                    });
            }
        });
        
        if (departmentSelect.value) {
            departmentSelect.dispatchEvent(new Event('change'));
        }
    }
});
</script>
{% endblock %}
