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

{% block title %}{% trans "New Observation" %} - PX360{% endblock %}

{% block extra_css %}
<style>
    .page-header {
        background: linear-gradient(135deg, #005696 0%, #0069a8 50%, #007bbd 100%);
        color: white;
        padding: 2rem 2.5rem;
        border-radius: 1rem;
        margin-bottom: 2rem;
        box-shadow: 0 10px 15px -3px rgba(0, 86, 150, 0.2);
        position: relative;
        overflow: hidden;
    }
    .page-header::before {
        content: '';
        position: absolute;
        top: 0;
        right: 0;
        width: 200px;
        height: 200px;
        background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
        pointer-events: none;
    }
    .back-link {
        color: rgba(255,255,255,0.85);
        text-decoration: none;
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
        font-weight: 500;
        margin-bottom: 1rem;
        transition: color 0.2s ease;
    }
    .back-link:hover {
        color: white;
    }
    .form-section {
        background: white;
        border-radius: 1rem;
        border: 1px solid #e2e8f0;
        padding: 1.5rem;
        margin-bottom: 1.5rem;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        transition: all 0.3s ease;
    }
    .form-section:hover {
        box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        transform: translateY(-2px);
    }
    .form-section-header {
        display: flex;
        align-items: center;
        gap: 0.75rem;
        margin-bottom: 1.5rem;
        padding-bottom: 1rem;
        border-bottom: 2px solid #f1f5f9;
    }
    .form-section-icon {
        width: 40px;
        height: 40px;
        border-radius: 0.75rem;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
        background: linear-gradient(135deg, #005696, #007bbd);
    }
    .form-label {
        display: block;
        font-weight: 600;
        color: #1e293b;
        font-size: 0.9rem;
        margin-bottom: 0.5rem;
    }
    .severity-option {
        cursor: pointer;
        padding: 12px 16px;
        border: 2px solid #e2e8f0;
        border-radius: 12px;
        text-align: center;
        transition: all 0.2s ease;
    }
    .severity-option:hover {
        border-color: #005696;
        background: #f8fafc;
    }
    .severity-option.selected {
        border-color: #005696;
        background: #eef6fb;
        box-shadow: 0 0 0 3px rgba(0, 86, 150, 0.1);
    }
    .severity-option.selected i {
        transform: scale(1.2);
    }
    .file-upload-area {
        border: 2px dashed #e2e8f0;
        border-radius: 12px;
        padding: 30px;
        text-align: center;
        cursor: pointer;
        transition: all 0.2s ease;
    }
    .file-upload-area:hover {
        border-color: #005696;
        background: #f8fafc;
    }
    .field-error {
        color: #ef4444;
        font-size: 0.75rem;
        margin-top: 0.25rem;
    }
    .form-control, .form-select {
        width: 100%;
        padding: 0.75rem 1rem;
        border: 2px solid #cbd5e1;
        border-radius: 0.75rem;
        font-size: 0.95rem;
        transition: all 0.2s;
        background: white;
        color: #1e293b;
        font-family: 'Inter', sans-serif;
    }
    .form-control:focus, .form-select:focus {
        outline: none;
        border-color: #005696;
        box-shadow: 0 0 0 4px rgba(0, 86, 150, 0.1);
    }
    textarea.form-control {
        resize: vertical;
        min-height: 120px;
    }
</style>
{% endblock %}

{% block content %}
<!-- Page Header -->
<div class="page-header">
    <a href="{% url 'observations:observation_list' %}" class="back-link">
        <i data-lucide="arrow-left" class="w-4 h-4"></i>
        {% trans "Back to Observations" %}
    </a>
    <div class="flex items-center gap-3">
        <div class="w-12 h-12 bg-white/20 rounded-xl flex items-center justify-center">
            <i data-lucide="eye" class="w-6 h-6"></i>
        </div>
        <div>
            <h1 class="text-2xl font-bold">{% trans "New Observation" %}</h1>
            <p class="text-sm opacity-75">{% trans "Submit a staff observation report" %}</p>
        </div>
    </div>
</div>

<!-- Messages -->
{% if messages %}
{% for message in messages %}
{% if message.tags == 'error' %}
<div class="bg-red-50 border border-red-200 rounded-xl p-4 mb-4 text-sm text-red-800">
{% elif message.tags == 'warning' %}
<div class="bg-yellow-50 border border-yellow-200 rounded-xl p-4 mb-4 text-sm text-yellow-800">
{% else %}
<div class="bg-green-50 border border-green-200 rounded-xl p-4 mb-4 text-sm text-green-800">
{% endif %}
    {{ message }}
</div>
{% endfor %}
{% endif %}

<!-- Form -->
<form method="post" enctype="multipart/form-data" id="observationForm" data-loading data-loading-text="{% trans 'Creating...' %}">
    {% csrf_token %}
    {% if communication_request %}
    <input type="hidden" name="comm_req" value="{{ communication_request.id }}">
    {% endif %}

    <!-- Observation Details -->
    <div class="form-section">
        <div class="form-section-header">
            <div class="form-section-icon">
                <i data-lucide="file-text" class="w-5 h-5 text-white"></i>
            </div>
            <div>
                <h5 class="mb-0 font-bold text-navy text-lg">{% trans "Observation Details" %}</h5>
                <small class="text-slate">{% trans "Describe what was observed" %}</small>
            </div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
            <!-- Category -->
            <div>
                <label class="form-label">
                    <i data-lucide="tag" class="w-3.5 h-3.5 inline-block mr-1"></i>
                    {% trans "Category" %}
                    <span class="text-xs font-normal text-slate normal-case">({% trans "optional" %})</span>
                </label>
                {{ form.category }}
                {% if form.category.errors %}
                <div class="field-error">{{ form.category.errors.0 }}</div>
                {% endif %}
            </div>

            <!-- Sub-Category -->
            <div>
                <label class="form-label">
                    <i data-lucide="folder-tree" class="w-3.5 h-3.5 inline-block mr-1"></i>
                    {% trans "Sub-Category" %}
                    <span class="text-xs font-normal text-slate normal-case">({% trans "optional" %})</span>
                </label>
                {{ form.sub_category }}
                {% if form.sub_category.errors %}
                <div class="field-error">{{ form.sub_category.errors.0 }}</div>
                {% endif %}
            </div>

            <!-- Title -->
            <div>
                <label class="form-label">
                    <i data-lucide="type" class="w-3.5 h-3.5 inline-block mr-1"></i>
                    {% trans "Title" %}
                    <span class="text-xs font-normal text-slate normal-case">({% trans "optional" %})</span>
                </label>
                {{ form.title }}
                {% if form.title.errors %}
                <div class="field-error">{{ form.title.errors.0 }}</div>
                {% endif %}
            </div>

            <!-- Description -->
            <div class="md:col-span-2">
                <label class="form-label">
                    <i data-lucide="align-left" class="w-3.5 h-3.5 inline-block mr-1"></i>
                    {% trans "Description" %}
                    <span class="text-red-500">*</span>
                </label>
                {{ form.description }}
                {% if form.description.errors %}
                <div class="field-error">{{ form.description.errors.0 }}</div>
                {% endif %}
                <p class="text-xs text-slate mt-1">{% trans "Please describe what you observed in detail (at least 10 characters)." %}</p>
            </div>

            <!-- Description (English) -->
            <div class="md:col-span-2">
                <label class="form-label">
                    <i data-lucide="languages" class="w-3.5 h-3.5 inline-block mr-1"></i>
                    {% trans "Description (English)" %}
                    <span class="text-xs font-normal text-slate normal-case">({% trans "optional" %})</span>
                </label>
                {{ form.description_en }}
                {% if form.description_en.errors %}
                <div class="field-error">{{ form.description_en.errors.0 }}</div>
                {% endif %}
            </div>
        </div>
    </div>

    <!-- Location & Timing -->
    <div class="form-section">
        <div class="form-section-header">
            <div class="form-section-icon" style="background: linear-gradient(135deg, #0891b2, #06b6d4);">
                <i data-lucide="map-pin" class="w-5 h-5 text-white"></i>
            </div>
            <div>
                <h5 class="mb-0 font-bold text-navy text-lg">{% trans "Location & Timing" %}</h5>
                <small class="text-slate">{% trans "Where and when did this occur?" %}</small>
            </div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-3 gap-5 mb-5">
            <!-- Structured Location -->
            <div>
                <label class="form-label">
                    <i data-lucide="map-pin" class="w-3.5 h-3.5 inline-block mr-1"></i>
                    {% trans "Location" %}
                </label>
                {{ form.location }}
                {% if form.location.errors %}
                <div class="field-error">{{ form.location.errors.0 }}</div>
                {% endif %}
            </div>

            <!-- Section -->
            <div>
                <label class="form-label">
                    <i data-lucide="layout-grid" class="w-3.5 h-3.5 inline-block mr-1"></i>
                    {% trans "Section" %}
                </label>
                {{ form.main_section }}
                {% if form.main_section.errors %}
                <div class="field-error">{{ form.main_section.errors.0 }}</div>
                {% endif %}
            </div>

            <!-- Subsection -->
            <div>
                <label class="form-label">
                    <i data-lucide="grid-3x3" class="w-3.5 h-3.5 inline-block mr-1"></i>
                    {% trans "Subsection" %}
                </label>
                {{ form.subsection }}
                {% if form.subsection.errors %}
                <div class="field-error">{{ form.subsection.errors.0 }}</div>
                {% endif %}
            </div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
            <!-- Location Text (free text fallback) -->
            <div>
                <label class="form-label">
                    <i data-lucide="pencil" class="w-3.5 h-3.5 inline-block mr-1"></i>
                    {% trans "Location (free text)" %}
                    <span class="text-xs font-normal text-slate normal-case">({% trans "use if dropdown above doesn't match" %})</span>
                </label>
                {{ form.location_text }}
                {% if form.location_text.errors %}
                <div class="field-error">{{ form.location_text.errors.0 }}</div>
                {% endif %}
            </div>

            <!-- Incident Date/Time -->
            <div>
                <label class="form-label">
                    <i data-lucide="calendar" class="w-3.5 h-3.5 inline-block mr-1"></i>
                    {% trans "When did this occur?" %}
                </label>
                {{ form.incident_datetime }}
                {% if form.incident_datetime.errors %}
                <div class="field-error">{{ form.incident_datetime.errors.0 }}</div>
                {% endif %}
            </div>
        </div>
    </div>

    <!-- Assignment -->
    <div class="form-section">
        <div class="form-section-header">
            <div class="form-section-icon" style="background: linear-gradient(135deg, #7c3aed, #8b5cf6);">
                <i data-lucide="user-check" class="w-5 h-5 text-white"></i>
            </div>
            <div>
                <h5 class="mb-0 font-bold text-navy text-lg">{% trans "Assignment" %}</h5>
                <small class="text-slate">{% trans "Optionally assign to a department or user" %}</small>
            </div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
            <!-- Assigned Department -->
            <div>
                <label class="form-label">
                    <i data-lucide="building" class="w-3.5 h-3.5 inline-block mr-1"></i>
                    {% trans "Department" %}
                    <span class="text-xs font-normal text-slate normal-case">({% trans "optional" %})</span>
                </label>
                {{ form.assigned_department }}
                {% if form.assigned_department.errors %}
                <div class="field-error">{{ form.assigned_department.errors.0 }}</div>
                {% endif %}
            </div>

            <!-- Assigned To -->
            <div>
                <label class="form-label">
                    <i data-lucide="user" class="w-3.5 h-3.5 inline-block mr-1"></i>
                    {% trans "Assign To" %}
                    <span class="text-xs font-normal text-slate normal-case">({% trans "optional" %})</span>
                </label>
                {{ form.assigned_to }}
                {% if form.assigned_to.errors %}
                <div class="field-error">{{ form.assigned_to.errors.0 }}</div>
                {% endif %}
            </div>
        </div>
    </div>

    <!-- Communication & Patient Info -->
    <div class="form-section">
        <div class="form-section-header">
            <div class="form-section-icon" style="background: linear-gradient(135deg, #dc2626, #f97316);">
                <i data-lucide="phone-call" class="w-5 h-5 text-white"></i>
            </div>
            <div>
                <h5 class="mb-0 font-bold text-navy text-lg">{% trans "Communication & Patient Info" %}</h5>
                <small class="text-slate">{% trans "Who was contacted and when?" %}</small>
            </div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
            <!-- Patient File Number -->
            <div>
                <label class="form-label">
                    <i data-lucide="file-digit" class="w-3.5 h-3.5 inline-block mr-1"></i>
                    {% trans "Patient File Number" %}
                    <span class="text-xs font-normal text-slate normal-case">({% trans "optional" %})</span>
                </label>
                {{ form.patient_file_number }}
                {% if form.patient_file_number.errors %}
                <div class="field-error">{{ form.patient_file_number.errors.0 }}</div>
                {% endif %}
            </div>

            <!-- Person Noted -->
            <div>
                <label class="form-label">
                    <i data-lucide="user-check" class="w-3.5 h-3.5 inline-block mr-1"></i>
                    {% trans "Person Noted / Informed" %}
                    <span class="text-xs font-normal text-slate normal-case">({% trans "optional" %})</span>
                </label>
                {{ form.person_noted }}
                {% if form.person_noted.errors %}
                <div class="field-error">{{ form.person_noted.errors.0 }}</div>
                {% endif %}
            </div>

            <!-- Department Noted -->
            <div>
                <label class="form-label">
                    <i data-lucide="building" class="w-3.5 h-3.5 inline-block mr-1"></i>
                    {% trans "Department Noted" %}
                    <span class="text-xs font-normal text-slate normal-case">({% trans "optional" %})</span>
                </label>
                {{ form.department_noted }}
                {% if form.department_noted.errors %}
                <div class="field-error">{{ form.department_noted.errors.0 }}</div>
                {% endif %}
            </div>

            <!-- Communication Method -->
            <div>
                <label class="form-label">
                    <i data-lucide="at-sign" class="w-3.5 h-3.5 inline-block mr-1"></i>
                    {% trans "Communication Via" %}
                    <span class="text-xs font-normal text-slate normal-case">({% trans "optional" %})</span>
                </label>
                {{ form.communication_method }}
                {% if form.communication_method.errors %}
                <div class="field-error">{{ form.communication_method.errors.0 }}</div>
                {% endif %}
            </div>

            <!-- Communication Date/Time -->
            <div>
                <label class="form-label">
                    <i data-lucide="clock" class="w-3.5 h-3.5 inline-block mr-1"></i>
                    {% trans "Communication Date/Time" %}
                    <span class="text-xs font-normal text-slate normal-case">({% trans "optional" %})</span>
                </label>
                {{ form.communication_datetime }}
                {% if form.communication_datetime.errors %}
                <div class="field-error">{{ form.communication_datetime.errors.0 }}</div>
                {% endif %}
            </div>
        </div>
    </div>

    <!-- Attachments -->
    <div class="form-section">
        <div class="form-section-header">
            <div class="form-section-icon" style="background: linear-gradient(135deg, #059669, #10b981);">
                <i data-lucide="paperclip" class="w-5 h-5 text-white"></i>
            </div>
            <div>
                <h5 class="mb-0 font-bold text-navy text-lg">{% trans "Attachments" %}</h5>
                <small class="text-slate">{% trans "Upload supporting documents or images" %}</small>
            </div>
        </div>

        <div class="file-upload-area" onclick="document.getElementById('attachments').click()">
            <i data-lucide="cloud-upload" class="w-8 h-8 text-navy mx-auto mb-2"></i>
            <p class="text-sm font-semibold text-slate mb-1">{% trans "Click to upload files" %}</p>
            <p class="text-xs text-slate mb-0">{% trans "Images, PDF, Word, Excel (max 10MB each)" %}</p>
        </div>
        <input type="file" id="attachments" name="attachments" multiple
               accept=".jpg,.jpeg,.png,.gif,.pdf,.doc,.docx,.xls,.xlsx"
               style="display: none;" onchange="updateFileList()">
        <div id="fileList" class="mt-2"></div>
    </div>

    <!-- Actions -->
    <div class="flex gap-3 justify-end">
        <a href="{% url 'observations:observation_list' %}" class="px-6 py-3 border-2 border-slate-200 text-slate rounded-xl font-bold text-sm hover:bg-slate-50 transition flex items-center gap-2">
            <i data-lucide="x" class="w-4 h-4"></i>
            {% trans "Cancel" %}
        </a>
        <button type="submit" class="px-8 py-3 bg-navy text-white rounded-xl font-bold text-sm hover:bg-blue transition shadow-lg shadow-navy/25 flex items-center gap-2">
            <i data-lucide="send" class="w-4 h-4"></i>
            {% trans "Create Observation" %}
        </button>
    </div>
</form>
{% endblock %}

{% block extra_js %}
<script>
{% trans "Select Section" as trans_select_section %}
{% trans "Select Subsection" as trans_select_subsection %}
{% trans "Select assignee (optional)" as trans_select_assignee %}

function selectSeverity(el) {
    document.querySelectorAll('.severity-option').forEach(o => o.classList.remove('selected'));
    el.classList.add('selected');
    document.getElementById('severityInput').value = el.dataset.value;
    lucide.createIcons();
}

function updateFileList() {
    const input = document.getElementById('attachments');
    const fileList = document.getElementById('fileList');
    fileList.innerHTML = '';

    if (input.files.length > 0) {
        let html = '<div class="space-y-2 mt-3">';
        for (let i = 0; i < input.files.length; i++) {
            html += '<div class="flex items-center gap-2 bg-slate-50 rounded-lg px-3 py-2 text-sm text-slate"><i data-lucide="file" class="w-4 h-4"></i>' + input.files[i].name + '</div>';
        }
        html += '</div>';
        fileList.innerHTML = html;
        lucide.createIcons();
    }
}

document.addEventListener('DOMContentLoaded', function() {
    const selSection = '{{ trans_select_section|escapejs }}';
    const selSubsection = '{{ trans_select_subsection|escapejs }}';
    const selAssignee = '{{ trans_select_assignee|escapejs }}';

    const locationSelect = document.getElementById('locationSelect');
    const sectionSelect = document.getElementById('mainSectionSelect');
    const subsectionSelect = document.getElementById('subsectionSelect');

    function loadSections(locationId) {
        sectionSelect.innerHTML = '<option value="">' + selSection + '</option>';
        subsectionSelect.innerHTML = '<option value="">' + selSubsection + '</option>';
        if (!locationId) return;
        fetch('/complaints/public/api/locations/' + locationId + '/sections/')
            .then(r => r.json())
            .then(data => {
                if (data.sections) {
                    data.sections.forEach(function(s) {
                        const opt = document.createElement('option');
                        opt.value = s.id;
                        opt.textContent = s.name;
                        sectionSelect.appendChild(opt);
                    });
                }
            });
    }

    function loadSubsections(locationId, sectionId) {
        subsectionSelect.innerHTML = '<option value="">' + selSubsection + '</option>';
        if (!locationId || !sectionId) return;
        fetch('/complaints/public/api/locations/' + locationId + '/sections/' + sectionId + '/subsections/')
            .then(r => r.json())
            .then(data => {
                if (data.subsections) {
                    data.subsections.forEach(function(s) {
                        const opt = document.createElement('option');
                        opt.value = s.id;
                        opt.textContent = s.name;
                        subsectionSelect.appendChild(opt);
                    });
                }
            });
    }

    if (locationSelect) {
        locationSelect.addEventListener('change', function() {
            loadSections(this.value);
        });
    }
    if (sectionSelect) {
        sectionSelect.addEventListener('change', function() {
            loadSubsections(locationSelect.value, this.value);
        });
    }

    const deptSelect = document.getElementById('id_assigned_department');
    const assigneeSelect = document.getElementById('id_assigned_to');

    if (deptSelect && assigneeSelect) {
        deptSelect.addEventListener('change', function() {
            const deptId = this.value;
            if (!deptId) {
                assigneeSelect.innerHTML = '<option value="">' + selAssignee + '</option>';
                return;
            }

            fetch("{% url 'observations:get_users_by_department' %}?department_id=" + deptId)
                .then(response => response.json())
                .then(data => {
                    let html = '<option value="">' + selAssignee + '</option>';
                    data.users.forEach(function(user) {
                        html += '<option value="' + user.id + '">' + user.name + '</option>';
                    });
                    assigneeSelect.innerHTML = html;
                })
                .catch(function() {
                    console.error('Failed to load users for department');
                });
        });
    }
});
</script>
{% endblock %}
