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

{% block title %}{% if adverse_action %}{% trans "Edit Adverse Action" %}{% else %}{% trans "Report Adverse Action" %}{% endif %} - PX360{% endblock %}

{% block extra_css %}
<style>
.page-header-gradient {
    background: linear-gradient(135deg, #005696 0%, #0069a8 50%, #007bbd 100%);
    color: white;
    padding: 1.5rem 2rem;
    border-radius: 1rem;
    margin-bottom: 1.5rem;
    box-shadow: 0 10px 15px -3px rgba(0, 86, 150, 0.2);
}

.form-section {
    background: #fff;
    border: 2px solid #e2e8f0;
    border-radius: 1rem;
    padding: 1.5rem;
    margin-bottom: 1.5rem;
    transition: all 0.3s ease;
}
.form-section:hover {
    border-color: #005696;
    box-shadow: 0 4px 12px rgba(0, 86, 150, 0.1);
}

.form-label {
    display: block;
    font-size: 0.875rem;
    font-weight: 600;
    color: #1e293b;
    margin-bottom: 0.5rem;
}

.form-control, .form-select {
    width: 100%;
    padding: 0.75rem 1rem;
    border: 2px solid #e2e8f0;
    border-radius: 0.75rem;
    font-size: 0.875rem;
    transition: all 0.2s ease;
}
.form-control:focus, .form-select:focus {
    outline: none;
    border-color: #005696;
    box-shadow: 0 0 0 3px rgba(0, 86, 150, 0.1);
}

.hh-btn-primary {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1.5rem;
    background: #005696;
    color: white;
    border-radius: 0.75rem;
    font-weight: 600;
    transition: all 0.2s ease;
    border: none;
    cursor: pointer;
}
.hh-btn-primary:hover {
    background: #007bbd;
}

.hh-btn-secondary {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1.5rem;
    background: white;
    color: #64748b;
    border: 2px solid #e2e8f0;
    border-radius: 0.75rem;
    font-weight: 600;
    transition: all 0.2s ease;
}
.hh-btn-secondary:hover {
    background: #f1f5f9;
    border-color: #005696;
}
</style>
{% endblock %}

{% block content %}
<div class="p-6 max-w-4xl mx-auto">
    <!-- Gradient Header -->
    <div class="page-header-gradient">
        <div class="flex items-center gap-4">
            <a href="{% url 'complaints:complaint_detail' complaint.id %}" class="text-white/80 hover:text-white transition">
                <i data-lucide="arrow-left" class="w-6 h-6"></i>
            </a>
            <div>
                <h1 class="text-2xl font-bold">
                    {% if adverse_action %}{% trans "Edit Adverse Action" %}{% else %}{% trans "Report Adverse Action" %}{% endif %}
                </h1>
                <p class="text-white/80 mt-1">
                    {% trans "Complaint" %}: {{ complaint.reference_number }}
                </p>
            </div>
        </div>
    </div>

    <!-- Form -->
    <div class="form-section">
        <form method="post">
            {% csrf_token %}
            
            <!-- Action Type -->
            <div class="mb-6">
                <label class="form-label">
                    {% trans "Action Type" %}
                    <span class="text-red-500">*</span>
                </label>
                <select name="action_type" required class="form-select">
                    {% for value, label in action_type_choices %}
                    <option value="{{ value }}" {% if adverse_action and adverse_action.action_type == value %}selected{% endif %}>
                        {{ label }}
                    </option>
                    {% endfor %}
                </select>
                <p class="mt-2 text-sm text-[#64748b]">
                    {% trans "Select the type of adverse action that occurred." %}
                </p>
            </div>

            <!-- Severity -->
            <div class="mb-6">
                <label class="form-label">
                    {% trans "Severity Level" %}
                    <span class="text-red-500">*</span>
                </label>
                <div class="grid grid-cols-2 md:grid-cols-4 gap-3">
                    {% for value, label in severity_choices %}
                    <label class="relative cursor-pointer">
                        <input type="radio" name="severity" value="{{ value }}" 
                               {% if adverse_action and adverse_action.severity == value %}checked{% elif not adverse_action and value == 'medium' %}checked{% endif %}
                               class="peer sr-only" required>
                        <div class="border-2 border-gray-200 rounded-xl p-4 text-center hover:border-[#005696] transition peer-checked:border-[#005696] peer-checked:bg-[#eef6fb]">
                            {% if value == 'low' %}
                                <i data-lucide="arrow-down" class="w-6 h-6 mx-auto mb-2 text-green-500"></i>
                            {% elif value == 'medium' %}
                                <i data-lucide="arrow-right" class="w-6 h-6 mx-auto mb-2 text-yellow-500"></i>
                            {% elif value == 'high' %}
                                <i data-lucide="arrow-up" class="w-6 h-6 mx-auto mb-2 text-orange-500"></i>
                            {% else %}
                                <i data-lucide="alert-triangle" class="w-6 h-6 mx-auto mb-2 text-red-500"></i>
                            {% endif %}
                            <span class="text-sm font-medium">{{ label }}</span>
                        </div>
                    </label>
                    {% endfor %}
                </div>
            </div>

            <!-- Incident Date -->
            <div class="mb-6">
                <label class="form-label">
                    {% trans "Incident Date & Time" %}
                    <span class="text-red-500">*</span>
                </label>
                <input type="datetime-local" name="incident_date" required
                       value="{% if adverse_action %}{{ adverse_action.incident_date|date:'Y-m-d\\TH:i' }}{% else %}{% now 'Y-m-d\\TH:i' %}{% endif %}"
                       class="form-control">
            </div>

            <!-- Location -->
            <div class="mb-6">
                <label class="form-label">
                    {% trans "Location" %}
                </label>
                <input type="text" name="location"
                       value="{{ adverse_action.location|default:'' }}"
                       class="form-control"
                       placeholder="{% trans 'e.g., Emergency Room, Clinic B, Main Reception' %}">
            </div>

            <!-- Description -->
            <div class="mb-6">
                <label class="form-label">
                    {% trans "Description" %}
                    <span class="text-red-500">*</span>
                </label>
                <textarea name="description" rows="4" required
                          class="form-control resize-none"
                          placeholder="{% trans 'Describe what happened to the patient...' %}">{{ adverse_action.description|default:'' }}</textarea>
            </div>

            <!-- Patient Impact -->
            <div class="mb-6">
                <label class="form-label">
                    {% trans "Impact on Patient" %}
                </label>
                <textarea name="patient_impact" rows="3"
                          class="form-control resize-none"
                          placeholder="{% trans 'Describe the physical, emotional, or financial impact on the patient...' %}">{{ adverse_action.patient_impact|default:'' }}</textarea>
            </div>

            <!-- Involved Staff -->
            <div class="mb-6">
                <label class="form-label">
                    {% trans "Involved Staff" %}
                </label>
                <select name="involved_staff" multiple
                        class="form-control" data-tomselect
                        size="5">
                    {% for staff in staff_list %}
                    <option value="{{ staff.id }}" {% if staff.id in selected_staff %}selected{% endif %}>
                        {{ staff.get_full_name }} - {{ staff.department.name|default:staff.job_title|default:staff.employee_id }}
                    </option>
                    {% endfor %}
                </select>
                <p class="mt-2 text-sm text-[#64748b]">
                    {% trans "Hold Ctrl/Cmd to select multiple staff members." %}
                </p>
            </div>

            <!-- Info Box -->
            <div class="mb-6 bg-amber-50 border border-amber-200 rounded-xl p-4">
                <div class="flex items-start gap-3">
                    <i data-lucide="alert-triangle" class="w-5 h-5 text-amber-500 flex-shrink-0 mt-0.5"></i>
                    <div>
                        <h4 class="font-medium text-amber-800">{% trans "Important Note" %}</h4>
                        <p class="text-sm text-amber-700 mt-1">
                            {% trans "Adverse actions are serious incidents where a patient experiences negative treatment because of a complaint. This information is used to identify patterns and prevent retaliation against patients." %}
                        </p>
                    </div>
                </div>
            </div>

            <!-- Actions -->
            <div class="flex items-center justify-end gap-3 pt-6 border-t border-gray-100">
                <a href="{% url 'complaints:complaint_detail' complaint.id %}" class="hh-btn hh-btn-secondary">
                    <i data-lucide="x" class="w-4 h-4"></i>
                    {% trans "Cancel" %}
                </a>
                <button type="submit" class="hh-btn hh-btn-primary">
                    <i data-lucide="{% if adverse_action %}save{% else %}plus{% endif %}" class="w-4 h-4"></i>
                    {% if adverse_action %}{% trans "Save Changes" %}{% else %}{% trans "Report Adverse Action" %}{% endif %}
                </button>
            </div>
        </form>
    </div>
</div>

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