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

{% block title %}{% trans "Adverse Actions" %} - PX360{% endblock %}

{% block extra_css %}
<style>
    :root {
        --hh-navy: #005696;
        --hh-blue: #007bbd;
        --hh-light: #eef6fb;
        --hh-slate: #64748b;
        --hh-success: #10b981;
        --hh-warning: #f59e0b;
        --hh-danger: #ef4444;
        --hh-purple: #8b5cf6;
    }

    .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);
    }

    .section-card {
        background: white;
        border-radius: 1rem;
        border: 2px solid #e2e8f0;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        overflow: hidden;
        transition: all 0.3s ease;
    }

    .section-card:hover {
        border-color: #005696;
        box-shadow: 0 10px 25px -5px rgba(0, 86, 150, 0.15);
    }

    .section-header {
        padding: 1rem 1.5rem;
        border-bottom: 2px solid #e2e8f0;
        background: linear-gradient(to right, #f8fafc, #f1f5f9);
        display: flex;
        align-items: center;
        gap: 0.75rem;
    }

    .section-icon {
        width: 40px;
        height: 40px;
        border-radius: 0.75rem;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .section-icon.primary {
        background: linear-gradient(135deg, #005696, #007bbd);
        color: white;
    }

    .section-icon.secondary {
        background: linear-gradient(135deg, #f1f5f9, #e2e8f0);
        color: #005696;
    }

    .data-table th {
        background: linear-gradient(135deg, var(--hh-light), #e0f2fe);
        padding: 0.875rem 1rem;
        text-align: left;
        font-size: 0.75rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        color: var(--hh-navy);
        border-bottom: 2px solid #bae6fd;
    }

    .data-table td {
        padding: 1rem;
        border-bottom: 1px solid #f1f5f9;
        color: #475569;
        font-size: 0.875rem;
    }

    .data-table tbody tr {
        transition: background-color 0.2s ease;
    }

    .data-table tbody tr:hover {
        background-color: var(--hh-light);
        cursor: pointer;
    }

    .severity-badge {
        display: inline-flex;
        align-items: center;
        gap: 0.375rem;
        padding: 0.375rem 0.75rem;
        border-radius: 9999px;
        font-size: 0.75rem;
        font-weight: 700;
    }

    .severity-badge.low { background: linear-gradient(135deg, #dcfce7, #bbf7d0); color: #166534; }
    .severity-badge.medium { background: linear-gradient(135deg, #fef3c7, #fde68a); color: #92400e; }
    .severity-badge.high { background: linear-gradient(135deg, #fee2e2, #fecaca); color: #991b1b; }
    .severity-badge.critical { background: linear-gradient(135deg, #7f1d1d, #991b1b); color: white; }

    .status-badge {
        display: inline-flex;
        align-items: center;
        gap: 0.375rem;
        padding: 0.375rem 0.75rem;
        border-radius: 9999px;
        font-size: 0.75rem;
        font-weight: 700;
    }

    .status-badge.reported { background: linear-gradient(135deg, #e0f2fe, #bae6fd); color: #075985; }
    .status-badge.under_investigation { background: linear-gradient(135deg, #fef3c7, #fde68a); color: #92400e; }
    .status-badge.resolved { background: linear-gradient(135deg, #dcfce7, #bbf7d0); color: #166534; }
    .status-badge.closed { background: linear-gradient(135deg, #f1f5f9, #e2e8f0); color: #475569; }

    .hh-btn-primary {
        background: linear-gradient(135deg, var(--hh-navy) 0%, var(--hh-blue) 100%);
        color: white;
        padding: 0.625rem 1.25rem;
        border-radius: 0.75rem;
        font-weight: 600;
        border: none;
        cursor: pointer;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
        text-decoration: none;
        font-size: 0.875rem;
    }

    .hh-btn-primary:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 16px rgba(0, 86, 150, 0.3);
    }

    .hh-btn-secondary {
        background: white;
        color: #475569;
        padding: 0.625rem 1.25rem;
        border-radius: 0.75rem;
        font-weight: 600;
        border: 2px solid #e2e8f0;
        cursor: pointer;
        transition: all 0.3s ease;
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
        text-decoration: none;
        font-size: 0.875rem;
    }

    .hh-btn-secondary:hover {
        background: #f1f5f9;
        border-color: #cbd5e1;
        transform: translateY(-1px);
    }

    @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="px-4 py-6">
    <!-- Page Header -->
    <div class="page-header-gradient animate-in">
        <div class="flex items-center justify-between">
            <div>
                <h1 class="text-2xl font-bold mb-2">
                    <i data-lucide="shield-alert" class="w-7 h-7 inline-block me-2"></i>
                    {% trans "Adverse Actions" %}
                </h1>
                <p class="text-white/90">{% trans "Track and manage adverse actions related to complaints" %}</p>
            </div>
            <a href="{% url 'complaints:complaint_list' %}" class="hh-btn hh-btn-secondary">
                <i data-lucide="arrow-left" class="w-4 h-4"></i>
                {% trans "Back to Complaints" %}
            </a>
        </div>
    </div>

    <!-- Filters -->
    <div class="section-card mb-6 animate-in">
        <div class="section-header">
            <div class="section-icon secondary">
                <i data-lucide="filter" class="w-5 h-5"></i>
            </div>
            <h2 class="text-lg font-bold text-navy m-0">{% trans "Filters" %}</h2>
        </div>
        <div class="p-6">
            <form method="get" class="flex flex-wrap gap-4">
                <div class="flex-1 min-w-[250px]">
                    <label class="block text-sm font-semibold text-slate mb-1.5">{% trans "Search" %}</label>
                    <input type="text" name="search" value="{{ filters.search|default:'' }}"
                           placeholder="{% trans 'Reference or description...' %}"
                           class="w-full px-4 py-2.5 border-2 border-slate-200 rounded-xl focus:outline-none focus:border-blue focus:ring-2 focus:ring-blue/20">
                </div>
                <div>
                    <label class="block text-sm font-semibold text-slate mb-1.5">{% trans "Status" %}</label>
                    <select name="status" class="px-4 py-2.5 border-2 border-slate-200 rounded-xl focus:outline-none focus:border-blue bg-white">
                        <option value="">{% trans "All Statuses" %}</option>
                        {% for value, label in status_choices %}
                        <option value="{{ value }}" {% if filters.status == value %}selected{% endif %}>{{ label }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div>
                    <label class="block text-sm font-semibold text-slate mb-1.5">{% trans "Severity" %}</label>
                    <select name="severity" class="px-4 py-2.5 border-2 border-slate-200 rounded-xl focus:outline-none focus:border-blue bg-white">
                        <option value="">{% trans "All Severities" %}</option>
                        {% for value, label in severity_choices %}
                        <option value="{{ value }}" {% if filters.severity == value %}selected{% endif %}>{{ label }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div>
                    <label class="block text-sm font-semibold text-slate mb-1.5">{% trans "Type" %}</label>
                    <select name="action_type" class="px-4 py-2.5 border-2 border-slate-200 rounded-xl focus:outline-none focus:border-blue bg-white">
                        <option value="">{% trans "All Types" %}</option>
                        {% for value, label in action_type_choices %}
                        <option value="{{ value }}" {% if filters.action_type == value %}selected{% endif %}>{{ label }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="flex items-end">
                    <button type="submit" class="hh-btn hh-btn-primary h-[46px]">
                        <i data-lucide="search" class="w-4 h-4"></i>
                        {% trans "Filter" %}
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Adverse Actions Table -->
    <div class="section-card animate-in">
        <div class="section-header flex items-center justify-between">
            <div class="flex items-center gap-3">
                <div class="section-icon primary">
                    <i data-lucide="shield-alert" class="w-5 h-5"></i>
                </div>
                <h2 class="text-lg font-bold text-navy m-0">{% trans "All Adverse Actions" %} ({{ page_obj.paginator.count }})</h2>
            </div>
            <a href="#" class="hh-btn hh-btn-primary">
                <i data-lucide="plus" class="w-4 h-4"></i>
                {% trans "New Adverse Action" %}
            </a>
        </div>
        <div class="p-0">
            {% if page_obj %}
            <div class="overflow-x-auto">
                <table class="w-full data-table">
                    <thead>
                        <tr>
                            <th>{% trans "Complaint" %}</th>
                            <th>{% trans "Type" %}</th>
                            <th>{% trans "Severity" %}</th>
                            <th>{% trans "Date" %}</th>
                            <th class="text-center">{% trans "Status" %}</th>
                            <th class="text-center">{% trans "Escalated" %}</th>
                            <th class="text-right">{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-100">
                        {% for action in page_obj %}
                        <tr onclick="window.location='{% url 'complaints:adverse_action_edit' action.pk %}'">
                            <td>
                                <a href="{% url 'complaints:complaint_detail' action.complaint.id %}" 
                                   class="font-semibold text-navy hover:text-blue transition">
                                    {{ action.complaint.reference_number|truncatechars:15 }}
                                </a>
                                <p class="text-xs text-slate mt-1">{{ action.complaint.title|truncatechars:30 }}</p>
                            </td>
                            <td>
                                <span class="text-sm font-medium text-slate-700">{{ action.get_action_type_display }}</span>
                            </td>
                            <td>
                                <span class="severity-badge {{ action.severity }}">
                                    <i data-lucide="{% if action.severity == 'low' %}arrow-down{% elif action.severity == 'medium' %}minus{% elif action.severity == 'high' %}arrow-up{% else %}zap{% endif %}" class="w-3 h-3"></i>
                                    {{ action.get_severity_display }}
                                </span>
                            </td>
                            <td>
                                <div class="text-sm text-slate">
                                    <p>{{ action.incident_date|date:"Y-m-d" }}</p>
                                </div>
                            </td>
                            <td class="text-center">
                                <span class="status-badge {{ action.status }}">
                                    <i data-lucide="{% if action.status == 'reported' %}circle{% elif action.status == 'under_investigation' %}clock{% elif action.status == 'resolved' %}check-circle{% else %}check{% endif %}" class="w-3 h-3"></i>
                                    {{ action.get_status_display }}
                                </span>
                            </td>
                            <td class="text-center">
                                {% if action.is_escalated %}
                                <span class="inline-flex items-center gap-1 text-red-600 font-bold text-sm">
                                    <i data-lucide="triangle-alert" class="w-4 h-4"></i>
                                    {% trans "Yes" %}
                                </span>
                                {% else %}
                                <span class="text-slate-400 text-sm">{% trans "No" %}</span>
                                {% endif %}
                            </td>
                            <td class="text-right">
                                <div class="flex items-center justify-end gap-2">
                                    <a href="{% url 'complaints:adverse_action_edit' action.pk %}" 
                                       class="p-2 text-blue hover:bg-blue-50 rounded-lg transition" 
                                       title="{% trans 'Edit' %}">
                                        <i data-lucide="edit" class="w-4 h-4"></i>
                                    </a>
                                    <a href="{% url 'complaints:adverse_action_delete' action.pk %}" 
                                       class="p-2 text-red-500 hover:bg-red-50 rounded-lg transition" 
                                       title="{% trans 'Delete' %}"
                                       onclick="event.stopPropagation();">
                                        <i data-lucide="trash-2" class="w-4 h-4"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="7" class="py-12 text-center">
                                <div class="flex flex-col items-center">
                                    <div class="w-16 h-16 bg-slate-100 rounded-full flex items-center justify-center mb-4">
                                        <i data-lucide="shield-alert" class="w-8 h-8 text-slate-400"></i>
                                    </div>
                                    <p class="text-slate font-medium">{% trans "No adverse actions found" %}</p>
                                    <p class="text-slate text-sm mt-1">{% trans "Create your first adverse action to get started" %}</p>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- Pagination -->
            {% if page_obj.has_other_pages %}
            <div class="p-4 border-t border-slate-200">
                <div class="flex items-center justify-between">
                    <p class="text-sm text-slate">
                        {% blocktrans with start=page_obj.start_index end=page_obj.end_index total=page_obj.paginator.count %}
                            Showing {{ start }} to {{ end }} of {{ total }} adverse actions
                        {% endblocktrans %}
                    </p>
                    <div class="flex gap-2">
                        {% if page_obj.has_previous %}
                        <a href="?page={{ page_obj.previous_page_number }}{% if filters.search %}&search={{ filters.search }}{% endif %}{% if filters.status %}&status={{ filters.status }}{% endif %}{% if filters.severity %}&severity={{ filters.severity }}{% endif %}{% if filters.action_type %}&action_type={{ filters.action_type }}{% endif %}"
                           class="px-4 py-2 border border-slate-200 rounded-lg hover:bg-slate-50 transition text-sm font-medium">
                            {% trans "Previous" %}
                        </a>
                        {% endif %}
                        {% if page_obj.has_next %}
                        <a href="?page={{ page_obj.next_page_number }}{% if filters.search %}&search={{ filters.search }}{% endif %}{% if filters.status %}&status={{ filters.status }}{% endif %}{% if filters.severity %}&severity={{ filters.severity }}{% endif %}{% if filters.action_type %}&action_type={{ filters.action_type }}{% endif %}"
                           class="px-4 py-2 border border-slate-200 rounded-lg hover:bg-slate-50 transition text-sm font-medium">
                            {% trans "Next" %}
                        </a>
                        {% endif %}
                    </div>
                </div>
            </div>
            {% endif %}
            {% endif %}
        </div>
    </div>
</div>

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