{% extends 'layouts/base.html' %}
{% load i18n %}
{% load rca_filters %}

{% block title %}{% trans "Root Cause Analysis" %} - 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);
    }
    .stat-card {
        background: white;
        border-radius: 1rem;
        border: 2px solid #e2e8f0;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
        transition: all 0.3s ease;
    }
    .stat-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        border-color: #005696;
    }
    .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;
    }
    .filter-btn.active { background-color: #005696; color: white; }
    .filter-btn:not(.active) { background-color: transparent; border: 1px solid #e2e8f0; color: #64748b; }
    .filter-btn:not(.active):hover { background-color: #f8fafc; }
</style>
{% endblock %}

{% block content %}
<!-- Page Header -->
<div class="page-header-gradient">
    <div class="flex justify-between items-center">
        <div>
            <h1 class="text-2xl font-bold mb-1 flex items-center gap-3">
                <i data-lucide="search" class="w-7 h-7"></i>
                {% trans "Root Cause Analysis" %}
            </h1>
            <p class="text-sm opacity-75">{% trans "Investigate incidents and implement corrective actions" %}</p>
        </div>
        <a href="{% url 'rca:rca_create' %}" class="bg-white text-[#005696] px-5 py-2.5 rounded-xl text-sm font-bold hover:bg-gray-100 flex items-center gap-2 transition">
            <i data-lucide="plus-circle" class="w-4 h-4"></i> {% trans "New RCA" %}
        </a>
    </div>
</div>

<!-- Stat Cards -->
<div class="grid grid-cols-5 gap-4 mb-6">
    <div class="stat-card p-4 flex items-center gap-3">
        <div class="flex-shrink-0">
            <div class="w-12 h-12 bg-navy/10 rounded-xl flex items-center justify-center">
                <i data-lucide="layers" class="w-6 h-6 text-navy"></i>
            </div>
        </div>
        <div>
            <h6 class="text-slate-500 text-xs font-medium">{% trans "Total" %}</h6>
            <h2 class="text-2xl font-bold text-gray-800">{{ total_count }}</h2>
        </div>
    </div>
    <div class="stat-card p-4 flex items-center gap-3">
        <div class="flex-shrink-0">
            <div class="w-12 h-12 bg-slate-100 rounded-xl flex items-center justify-center">
                <i data-lucide="file-text" class="w-6 h-6 text-slate-500"></i>
            </div>
        </div>
        <div>
            <h6 class="text-slate-500 text-xs font-medium">{% trans "Draft" %}</h6>
            <h2 class="text-2xl font-bold text-gray-800">{{ draft_count }}</h2>
        </div>
    </div>
    <div class="stat-card p-4 flex items-center gap-3">
        <div class="flex-shrink-0">
            <div class="w-12 h-12 bg-blue-500/10 rounded-xl flex items-center justify-center">
                <i data-lucide="loader" class="w-6 h-6 text-blue-600"></i>
            </div>
        </div>
        <div>
            <h6 class="text-slate-500 text-xs font-medium">{% trans "In Progress" %}</h6>
            <h2 class="text-2xl font-bold text-gray-800">{{ in_progress_count }}</h2>
        </div>
    </div>
    <div class="stat-card p-4 flex items-center gap-3">
        <div class="flex-shrink-0">
            <div class="w-12 h-12 bg-yellow-500/10 rounded-xl flex items-center justify-center">
                <i data-lucide="clipboard-check" class="w-6 h-6 text-yellow-600"></i>
            </div>
        </div>
        <div>
            <h6 class="text-slate-500 text-xs font-medium">{% trans "Review" %}</h6>
            <h2 class="text-2xl font-bold text-gray-800">{{ review_count }}</h2>
        </div>
    </div>
    <div class="stat-card p-4 flex items-center gap-3">
        <div class="flex-shrink-0">
            <div class="w-12 h-12 bg-green-500/10 rounded-xl flex items-center justify-center">
                <i data-lucide="check-circle" class="w-6 h-6 text-green-600"></i>
            </div>
        </div>
        <div>
            <h6 class="text-slate-500 text-xs font-medium">{% trans "Approved" %}</h6>
            <h2 class="text-2xl font-bold text-gray-800">{{ approved_count }}</h2>
        </div>
    </div>
</div>

<!-- RCA Section Card -->
<div class="section-card">
    <!-- Section Header with Filters -->
    <div class="section-header flex items-center justify-between">
        <div class="flex items-center gap-3">
            <div class="section-icon bg-purple-500/10">
                <i data-lucide="search" class="w-5 h-5 text-purple-600"></i>
            </div>
            <h5 class="text-lg font-semibold text-gray-800">{% trans "RCA Registry" %}</h5>
        </div>
        <div class="flex items-center gap-3">
            <a href="?" class="filter-btn px-4 py-1.5 rounded-full text-xs font-semibold transition {% if not request.GET.status %}active{% endif %}">
                {% trans "All" %}
            </a>
            <a href="?status=draft" class="filter-btn px-4 py-1.5 rounded-full text-xs font-semibold transition {% if request.GET.status == 'draft' %}active{% endif %}">
                {% trans "Draft" %}
            </a>
            <a href="?status=in_progress" class="filter-btn px-4 py-1.5 rounded-full text-xs font-semibold transition {% if request.GET.status == 'in_progress' %}active{% endif %}">
                {% trans "In Progress" %}
            </a>
            <a href="?status=review" class="filter-btn px-4 py-1.5 rounded-full text-xs font-semibold transition {% if request.GET.status == 'review' %}active{% endif %}">
                {% trans "Review" %}
            </a>
            <a href="?status=approved" class="filter-btn px-4 py-1.5 rounded-full text-xs font-semibold transition {% if request.GET.status == 'approved' %}active{% endif %}">
                {% trans "Approved" %}
            </a>
            <a href="?status=closed" class="filter-btn px-4 py-1.5 rounded-full text-xs font-semibold transition {% if request.GET.status == 'closed' %}active{% endif %}">
                {% trans "Closed" %}
            </a>
            <div class="h-4 w-[1px] bg-slate-200 mx-2"></div>
            <button onclick="toggleFilters()" class="flex items-center gap-2 text-xs font-bold text-blue uppercase tracking-tight hover:underline">
                <i data-lucide="sliders-horizontal" class="w-3 h-3"></i> {% trans "Advanced" %}
            </button>
        </div>
    </div>

    <!-- Advanced Filters -->
    <div id="advancedFilters" class="hidden bg-slate-50 px-6 py-4 border-b">
        <form method="get" class="flex flex-wrap gap-4">
            {% if request.GET.status %}
            <input type="hidden" name="status" value="{{ request.GET.status }}">
            {% endif %}
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Severity" %}</label>
                <select name="severity" class="px-3 py-1.5 bg-white border rounded-lg text-xs">
                    <option value="">{% trans "All" %}</option>
                    <option value="low" {% if request.GET.severity == 'low' %}selected{% endif %}>{% trans "Low" %}</option>
                    <option value="medium" {% if request.GET.severity == 'medium' %}selected{% endif %}>{% trans "Medium" %}</option>
                    <option value="high" {% if request.GET.severity == 'high' %}selected{% endif %}>{% trans "High" %}</option>
                    <option value="critical" {% if request.GET.severity == 'critical' %}selected{% endif %}>{% trans "Critical" %}</option>
                </select>
            </div>
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Priority" %}</label>
                <select name="priority" class="px-3 py-1.5 bg-white border rounded-lg text-xs">
                    <option value="">{% trans "All" %}</option>
                    <option value="low" {% if request.GET.priority == 'low' %}selected{% endif %}>{% trans "Low" %}</option>
                    <option value="medium" {% if request.GET.priority == 'medium' %}selected{% endif %}>{% trans "Medium" %}</option>
                    <option value="high" {% if request.GET.priority == 'high' %}selected{% endif %}>{% trans "High" %}</option>
                    <option value="critical" {% if request.GET.priority == 'critical' %}selected{% endif %}>{% trans "Critical" %}</option>
                </select>
            </div>
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Search" %}</label>
                <input type="text" name="search" value="{{ request.GET.search }}" placeholder="{% trans 'Search...' %}" class="px-3 py-1.5 bg-white border rounded-lg text-xs w-48">
            </div>
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "From" %}</label>
                <input type="date" name="date_from" value="{{ request.GET.date_from }}" class="px-3 py-1.5 bg-white border rounded-lg text-xs">
            </div>
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "To" %}</label>
                <input type="date" name="date_to" value="{{ request.GET.date_to }}" class="px-3 py-1.5 bg-white border rounded-lg text-xs">
            </div>
            <button type="submit" class="px-4 py-1.5 bg-navy text-white rounded-lg text-xs font-bold">{% trans "Apply" %}</button>
            <a href="?" class="px-4 py-1.5 border rounded-lg text-xs font-semibold text-slate hover:bg-white">{% trans "Clear" %}</a>
        </form>
    </div>

    <p class="px-6 py-2 text-xs font-bold text-slate uppercase bg-slate-50 border-b-2 border-slate-200">
        {% trans "Showing:" %} <span class="text-navy">{{ page_obj.start_index|default:0 }}-{{ page_obj.end_index|default:0 }} {% trans "of" %} {{ page_obj.paginator.count|default:0 }}</span>
    </p>

    <!-- Table -->
    <div class="p-0">
        <table class="w-full text-left border-collapse">
            <thead class="bg-slate-50 border-b uppercase text-[10px] font-bold text-slate tracking-wider">
                <tr>
                    <th class="px-6 py-4">{% trans "Title" %}</th>
                    <th class="px-6 py-4">{% trans "Status" %}</th>
                    <th class="px-6 py-4 text-center">{% trans "Severity" %}</th>
                    <th class="px-6 py-4 text-center">{% trans "Priority" %}</th>
                    <th class="px-6 py-4">{% trans "Hospital" %}</th>
                    <th class="px-6 py-4">{% trans "Assigned To" %}</th>
                    <th class="px-6 py-4">{% trans "Created" %}</th>
                    <th class="px-6 py-4 text-right">{% trans "Actions" %}</th>
                </tr>
            </thead>
            <tbody class="text-sm divide-y divide-slate-100">
                {% for rca in rcas %}
                <tr class="hover:bg-light/30 transition-colors cursor-pointer group" onclick="window.location.href='{% url 'rca:rca_detail' rca.pk %}'">
                    <td class="px-6 py-4">
                        <div class="font-semibold text-slate-800">{{ rca.title|truncatechars:60 }}</div>
                        <div class="text-[10px] text-slate font-medium mt-0.5">{{ rca.created_at|date:"M d, Y" }}</div>
                    </td>
                    <td class="px-6 py-4">
                        <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase {{ rca.status|status_color }}">
                            {{ rca.get_status_display }}
                        </span>
                    </td>
                    <td class="px-6 py-4 text-center">
                        <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase {{ rca.severity|severity_color }}">
                            {{ rca.get_severity_display }}
                        </span>
                    </td>
                    <td class="px-6 py-4 text-center">
                        <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase
                            {% if rca.priority == 'critical' %}bg-red-500 text-white
                            {% elif rca.priority == 'high' %}bg-orange-100 text-orange-700
                            {% elif rca.priority == 'medium' %}bg-yellow-100 text-yellow-700
                            {% else %}bg-green-100 text-green-700{% endif %}">
                            {{ rca.get_priority_display }}
                        </span>
                    </td>
                    <td class="px-6 py-4 text-slate-600">{{ rca.hospital }}</td>
                    <td class="px-6 py-4">
                        {% if rca.assigned_to %}
                        <small class="text-gray-600">{{ rca.assigned_to.get_full_name }}</small>
                        {% else %}
                        <span class="text-gray-400 italic">{% trans "Unassigned" %}</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4 text-slate-500 text-xs">{{ rca.created_at|date:"Y-m-d" }}</td>
                    <td class="px-6 py-4 text-right">
                        <div class="flex justify-end gap-2 opacity-0 group-hover:opacity-100 transition-opacity">
                            <a href="{% url 'rca:rca_detail' rca.pk %}" class="p-1.5 text-navy hover:bg-white rounded border border-transparent hover:border-slate-200" title="{% trans 'View' %}" onclick="event.stopPropagation()">
                                <i data-lucide="eye" class="w-4 h-4"></i>
                            </a>
                            {% if rca.status != 'closed' %}
                            <a href="{% url 'rca:rca_update' rca.pk %}" class="p-1.5 text-yellow-600 hover:bg-white rounded border border-transparent hover:border-slate-200" title="{% trans 'Edit' %}" onclick="event.stopPropagation()">
                                <i data-lucide="pencil" class="w-4 h-4"></i>
                            </a>
                            {% endif %}
                        </div>
                    </td>
                </tr>
                {% empty %}
                <tr>
                    <td colspan="8" class="px-6 py-12 text-center">
                        <i data-lucide="search" class="w-12 h-12 mx-auto text-slate-300 mb-3"></i>
                        <p class="text-slate mb-3">{% trans "No Root Cause Analyses found." %}</p>
                        <a href="{% url 'rca:rca_create' %}" class="inline-flex items-center gap-2 bg-navy text-white px-4 py-2 rounded-xl text-sm font-semibold hover:bg-blue transition">
                            <i data-lucide="plus-circle" class="w-4 h-4"></i> {% trans "Create First RCA" %}
                        </a>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>

        <!-- Pagination -->
        {% if page_obj.has_other_pages %}
        <div class="bg-slate-50 px-8 py-4 flex items-center justify-between border-t">
            <div class="flex items-center gap-4">
                <span class="text-xs text-slate font-medium">
                    {% trans "Showing" %} <span class="font-bold text-navy">{{ page_obj.start_index }}-{{ page_obj.end_index }}</span> {% trans "of" %} <span class="font-bold text-navy">{{ page_obj.paginator.count }}</span>
                </span>
                <form method="get" class="flex items-center gap-2" id="pageSizeForm">
                    {% for key, value in request.GET.items %}
                        {% if key != 'page_size' and key != 'page' %}
                        <input type="hidden" name="{{ key }}" value="{{ value }}">
                        {% endif %}
                    {% endfor %}
                    <label class="text-xs text-slate">{% trans "Show" %}</label>
                    <select name="page_size" onchange="document.getElementById('pageSizeForm').submit()" class="px-2 py-1 bg-white border border-slate-200 rounded-lg text-xs focus:outline-none focus:ring-2 focus:ring-navy">
                        <option value="20" {% if page_obj.paginator.per_page == 20 %}selected{% endif %}>20</option>
                        <option value="50" {% if page_obj.paginator.per_page == 50 %}selected{% endif %}>50</option>
                        <option value="100" {% if page_obj.paginator.per_page == 100 %}selected{% endif %}>100</option>
                    </select>
                </form>
            </div>
            <div class="flex gap-2">
                {% if page_obj.has_previous %}
                <a href="?page={{ page_obj.previous_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}"
                   class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 transition">
                    <i data-lucide="chevron-left" class="w-4 h-4 text-slate"></i>
                </a>
                {% else %}
                <span class="w-8 h-8 flex items-center justify-center rounded-lg border bg-slate-100 text-slate-300 cursor-not-allowed">
                    <i data-lucide="chevron-left" class="w-4 h-4"></i>
                </span>
                {% endif %}

                {% for num in page_obj.paginator.page_range %}
                    {% if num == page_obj.number %}
                    <span class="w-8 h-8 flex items-center justify-center rounded-lg bg-navy text-white text-xs font-bold">{{ num }}</span>
                    {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                    <a href="?page={{ num }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}"
                       class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 text-xs font-bold text-slate transition">
                        {{ num }}
                    </a>
                    {% elif num == 1 or num == page_obj.paginator.num_pages %}
                    <a href="?page={{ num }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}"
                       class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 text-xs font-bold text-slate transition">
                        {{ num }}
                    </a>
                    {% elif num == page_obj.number|add:'-3' or num == page_obj.number|add:'3' %}
                    <span class="w-8 h-8 flex items-center justify-center text-xs text-slate">...</span>
                    {% endif %}
                {% endfor %}

                {% if page_obj.has_next %}
                <a href="?page={{ page_obj.next_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}"
                   class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 transition">
                    <i data-lucide="chevron-right" class="w-4 h-4 text-slate"></i>
                </a>
                {% else %}
                <span class="w-8 h-8 flex items-center justify-center rounded-lg border bg-slate-100 text-slate-300 cursor-not-allowed">
                    <i data-lucide="chevron-right" class="w-4 h-4"></i>
                </span>
                {% endif %}
            </div>
        </div>
        {% endif %}
    </div>
</div>

<script>
function toggleFilters() {
    const filters = document.getElementById('advancedFilters');
    filters.classList.toggle('hidden');
}
</script>
{% endblock %}
