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

{% block title %}{% trans "Submit Observation" %} - {{ 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-purple-500 to-purple-600 rounded-2xl shadow-lg shadow-purple-200">
                <i data-lucide="eye" class="w-8 h-8 text-white"></i>
            </div>
            <div>
                <h1 class="text-2xl font-bold text-navy">
                    {% trans "Submit Observation" %}
                </h1>
                <p class="text-slate text-sm">
                    {% trans "Report a safety observation from" %} <strong>{{ source.get_localized_name }}</strong>
                </p>
            </div>
        </div>
        <a href="{% url 'px_sources:source_user_observations' %}"
           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 Observations" %}
        </a>
    </div>

    <!-- Info Banner -->
    <div class="bg-purple-50 border border-purple-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-purple-600 mt-0.5 flex-shrink-0"></i>
            <div>
                <p class="text-sm font-bold text-purple-800 mb-1">{% trans "Source Information" %}</p>
                <p class="text-sm text-purple-700">
                    {% trans "This observation 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-purple-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">
                <!-- Reporter Information -->
                <h2 class="text-lg font-bold text-navy mb-4 flex items-center gap-2">
                    <i data-lucide="user" class="w-5 h-5 text-purple-500"></i>
                    {% trans "Reporter Information" %}
                </h2>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8">
                    <div>
                        <label for="{{ form.reporter_name.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.reporter_name.label }} <span class="text-red-500">*</span>
                        </label>
                        {{ form.reporter_name }}
                        {% if form.reporter_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.reporter_name.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>

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

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

                    <div>
                        <label for="{{ form.category.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.category.label }}
                        </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 -->
                <h2 class="text-lg font-bold text-navy mb-4 flex items-center gap-2">
                    <i data-lucide="map-pin" class="w-5 h-5 text-purple-500"></i>
                    {% trans "Location" %}
                </h2>
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
                    <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>

                <!-- Details -->
                <h2 class="text-lg font-bold text-navy mb-4 flex items-center gap-2">
                    <i data-lucide="file-text" class="w-5 h-5 text-purple-500"></i>
                    {% trans "Observation Details" %}
                </h2>
                <div class="space-y-6">
                    <div>
                        <label for="{{ form.title.id_for_label }}" class="block text-sm font-bold text-navy mb-2">
                            {{ form.title.label }}
                        </label>
                        {{ form.title }}
                        {% if form.title.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.title.errors %}{{ error }}{% endfor %}
                        </div>
                        {% endif %}
                    </div>

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

            <!-- Submit -->
            <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 Observation" %}
                </button>
                <a href="{% url 'px_sources:source_user_observations' %}"
                   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() {
    const deptSelect = document.getElementById('id_assigned_department');
    const sectionSelect = document.getElementById('id_section');

    if (deptSelect && sectionSelect) {
        deptSelect.addEventListener('change', function() {
            const deptId = this.value;
            sectionSelect.innerHTML = '<option value="">{% trans "Select Section" %}</option>';
            if (!deptId) return;
            fetch('/organizations/api/sections/?department=' + deptId)
                .then(r => r.json())
                .then(data => {
                    const sections = data.results || data;
                    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);
                    });
                })
                .catch(err => console.error('Error loading sections:', err));
        });
    }
});
</script>
{% endblock %}
