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

{% block title %}{% trans "Standards Compliance Dashboard" %}{% endblock %}

{% block extra_css %}
<style>
    :root {
        --hh-navy: #005696;
        --hh-blue: #007bbd;
        --hh-light: #eef6fb;
        --hh-slate: #64748b;
    }

    .page-header-gradient {
        background: linear-gradient(135deg, var(--hh-navy) 0%, #0069a8 50%, var(--hh-blue) 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;
    }

    .stat-card {
        background: white;
        border-radius: 1rem;
        border: 2px solid #e2e8f0;
        padding: 1.5rem;
        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;
    }

    .btn-transition {
        transition: all 0.2s ease-in-out;
    }
</style>
{% endblock %}

{% block content %}
<div class="px-6 py-4">
    <div class="page-header-gradient">
        <div class="flex justify-between items-center">
            <div>
                <h1 class="text-2xl font-bold mb-1">{% trans "Standards Compliance Dashboard" %}</h1>
                <p class="text-blue-100 text-sm">{{ hospital.name }}</p>
            </div>
            <div class="flex gap-2">
                <a href="{% url 'standards:search' %}" class="btn-transition inline-flex items-center px-4 py-2.5 bg-white/10 border-2 border-white/30 text-white font-semibold rounded-xl hover:bg-white hover:text-navy transition">
                    <i data-lucide="search" class="w-4 h-4 me-2"></i>{% trans "Search Standards" %}
                </a>
                {% if is_px_admin %}
                <a href="{% url 'standards:standard_create_global' %}" class="btn-transition inline-flex items-center px-4 py-2.5 bg-green-500 text-white font-semibold rounded-xl hover:bg-green-600 transition">
                    <i data-lucide="plus" class="w-4 h-4 me-2"></i>{% trans "Create Standard" %}
                </a>
                {% endif %}
            </div>
        </div>
    </div>

    <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-4 mb-6">
        <div class="stat-card">
            <div class="flex items-center">
                <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="clipboard-list" class="w-6 h-6 text-blue-600"></i>
                    </div>
                </div>
                <div class="ml-3">
                    <h6 class="text-slate-500 text-xs font-medium mb-1">{% trans "Total" %}</h6>
                    <h2 class="text-2xl font-bold text-navy">{{ stats.total_standards }}</h2>
                </div>
            </div>
        </div>

        <div class="stat-card">
            <div class="flex items-center">
                <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 class="ml-3">
                    <h6 class="text-slate-500 text-xs font-medium mb-1">{% trans "Met" %}</h6>
                    <h2 class="text-2xl font-bold text-green-600">{{ stats.met }}</h2>
                </div>
            </div>
        </div>

        <div class="stat-card">
            <div class="flex items-center">
                <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="alert-circle" class="w-6 h-6 text-yellow-600"></i>
                    </div>
                </div>
                <div class="ml-3">
                    <h6 class="text-slate-500 text-xs font-medium mb-1">{% trans "Partial" %}</h6>
                    <h2 class="text-2xl font-bold text-yellow-600">{{ stats.partially_met }}</h2>
                </div>
            </div>
        </div>

        <div class="stat-card">
            <div class="flex items-center">
                <div class="flex-shrink-0">
                    <div class="w-12 h-12 bg-red-500/10 rounded-xl flex items-center justify-center">
                        <i data-lucide="x-circle" class="w-6 h-6 text-red-600"></i>
                    </div>
                </div>
                <div class="ml-3">
                    <h6 class="text-slate-500 text-xs font-medium mb-1">{% trans "Not Met" %}</h6>
                    <h2 class="text-2xl font-bold text-red-600">{{ stats.not_met }}</h2>
                </div>
            </div>
        </div>

        <div class="stat-card">
            <div class="flex items-center">
                <div class="flex-shrink-0">
                    <div class="w-12 h-12 bg-slate-500/10 rounded-xl flex items-center justify-center">
                        <i data-lucide="help-circle" class="w-6 h-6 text-slate-600"></i>
                    </div>
                </div>
                <div class="ml-3">
                    <h6 class="text-slate-500 text-xs font-medium mb-1">{% trans "Not Assessed" %}</h6>
                    <h2 class="text-2xl font-bold text-slate-600">{{ stats.not_assessed }}</h2>
                </div>
            </div>
        </div>

        <div class="stat-card">
            <div class="flex items-center">
                <div class="flex-shrink-0">
                    <div class="w-12 h-12 bg-navy/10 rounded-xl flex items-center justify-center">
                        <i data-lucide="building" class="w-6 h-6 text-navy"></i>
                    </div>
                </div>
                <div class="ml-3">
                    <h6 class="text-slate-500 text-xs font-medium mb-1">{% trans "Departments" %}</h6>
                    <h2 class="text-2xl font-bold text-navy">{{ stats.total_departments }}</h2>
                </div>
            </div>
        </div>
    </div>

    <div class="section-card mb-6">
        <div class="section-header">
            <div class="section-icon bg-navy/10">
                <i data-lucide="building-2" class="w-5 h-5 text-navy"></i>
            </div>
            <h5 class="text-lg font-semibold text-navy">{% trans "Departments" %}</h5>
        </div>
        <div class="p-0">
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-slate-100">
                    <thead class="bg-slate-50 border-b">
                        <tr>
                            <th class="px-4 py-3 text-left text-[10px] font-bold text-slate uppercase">
                                {% trans "Department" %}
                            </th>
                            <th class="px-4 py-3 text-left text-[10px] font-bold text-slate uppercase">
                                {% trans "Status" %}
                            </th>
                            <th class="px-4 py-3 text-right text-[10px] font-bold text-slate uppercase">
                                {% trans "Actions" %}
                            </th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-slate-100">
                        {% for department in departments %}
                        <tr class="hover:bg-slate-50 transition">
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="text-sm font-medium text-navy">{{ department.name }}</div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <span class="inline-flex items-center px-2 py-1 rounded-full text-[10px] font-bold uppercase bg-blue-100 text-blue-800">
                                    <i data-lucide="folder-open" class="w-3 h-3 me-1"></i>
                                    {% trans "View Standards" %}
                                </span>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-right">
                                <a href="{% url 'standards:department_standards' pk=department.id %}"
                                   class="btn-transition inline-flex items-center px-3 py-1.5 bg-navy text-white text-xs font-semibold rounded-lg hover:bg-blue transition">
                                    <i data-lucide="list" class="w-3 h-3 me-1"></i>{% trans "View" %}
                                </a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="3" class="px-6 py-12 text-center text-slate">
                                <i data-lucide="inbox" class="w-12 h-12 mx-auto mb-3 text-slate-300"></i>
                                <p>{% trans "No departments found" %}</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <div class="section-icon bg-blue/10">
                <i data-lucide="clock" class="w-5 h-5 text-blue"></i>
            </div>
            <h5 class="text-lg font-semibold text-navy">{% trans "Recent Compliance Updates" %}</h5>
        </div>
        <div class="p-0">
            {% if recent_updates %}
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-slate-100">
                    <thead class="bg-slate-50 border-b">
                        <tr>
                            <th class="px-4 py-3 text-left text-[10px] font-bold text-slate uppercase">
                                {% trans "Standard" %}
                            </th>
                            <th class="px-4 py-3 text-left text-[10px] font-bold text-slate uppercase">
                                {% trans "Department" %}
                            </th>
                            <th class="px-4 py-3 text-left text-[10px] font-bold text-slate uppercase">
                                {% trans "Status" %}
                            </th>
                            <th class="px-4 py-3 text-left text-[10px] font-bold text-slate uppercase">
                                {% trans "Last Updated" %}
                            </th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-slate-100">
                        {% for update in recent_updates %}
                        <tr class="hover:bg-slate-50 transition">
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="text-sm font-medium text-navy">{{ update.standard.code }}</div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="text-sm text-slate">{{ update.department.name }}</div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                {% if update.status == 'met' %}
                                <span class="inline-flex items-center px-2 py-1 rounded-full text-[10px] font-bold uppercase bg-green-100 text-green-800">
                                    <i data-lucide="check-circle" class="w-3 h-3 me-1"></i>{{ update.get_status_display }}
                                </span>
                                {% elif update.status == 'partially_met' %}
                                <span class="inline-flex items-center px-2 py-1 rounded-full text-[10px] font-bold uppercase bg-yellow-100 text-yellow-800">
                                    <i data-lucide="alert-circle" class="w-3 h-3 me-1"></i>{{ update.get_status_display }}
                                </span>
                                {% elif update.status == 'not_met' %}
                                <span class="inline-flex items-center px-2 py-1 rounded-full text-[10px] font-bold uppercase bg-red-100 text-red-800">
                                    <i data-lucide="x-circle" class="w-3 h-3 me-1"></i>{{ update.get_status_display }}
                                </span>
                                {% else %}
                                <span class="inline-flex items-center px-2 py-1 rounded-full text-[10px] font-bold uppercase bg-slate-100 text-navy">
                                    <i data-lucide="help-circle" class="w-3 h-3 me-1"></i>{{ update.get_status_display }}
                                </span>
                                {% endif %}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-slate">
                                {{ update.updated_at|date:"Y-m-d H:i" }}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <div class="text-center py-12 text-slate">
                <i data-lucide="clock" class="w-12 h-12 mx-auto mb-3 text-slate-300"></i>
                <p>{% trans "No recent updates" %}</p>
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
