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

{% block title %}{% trans "KPI Reports" %} - 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);
    }
    .year-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;
        margin-bottom: 1.5rem;
    }
    .year-card:hover {
        border-color: #005696;
        box-shadow: 0 10px 25px -5px rgba(0, 86, 150, 0.15);
    }
    .year-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;
    }
    .quarter-section {
        padding: 1rem 1.5rem;
        border-bottom: 1px solid #f1f5f9;
    }
    .quarter-section:last-child {
        border-bottom: none;
    }
    .quarter-header {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        margin-bottom: 0.75rem;
    }
    .quarter-badge {
        font-size: 0.65rem;
        font-weight: 800;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        padding: 0.2rem 0.5rem;
        border-radius: 0.375rem;
        background: #e0f2fe;
        color: #075985;
    }
    .quarter-months {
        font-size: 0.7rem;
        color: #94a3b8;
        font-weight: 600;
    }
    .status-completed { background-color: #dcfce7; color: #166534; }
    .status-pending { background-color: #fef9c3; color: #854d0e; }
    .status-generating { background-color: #e0f2fe; color: #075985; }
    .status-failed { background-color: #fee2e2; color: #991b1b; }
    .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; }
    /* Compact report card styles */
    .report-card {
        background: white;
        border: 1px solid #e2e8f0;
        border-radius: 0.75rem;
        padding: 0.875rem;
        transition: all 0.2s ease;
        cursor: pointer;
    }
    .report-card:hover {
        border-color: #005696;
        box-shadow: 0 4px 12px -2px rgba(0, 86, 150, 0.12);
        transform: translateY(-2px);
    }
    .report-card .actions {
        opacity: 0;
        transition: opacity 0.2s ease;
    }
    .report-card:hover .actions {
        opacity: 1;
    }
</style>
{% endblock %}

{% block content %}
<!-- Page Header -->
<div class="page-header-gradient">
    <div class="flex justify-between items-start">
        <div>
            <h1 class="text-2xl font-bold flex items-center gap-3">
                <i data-lucide="bar-chart-3" class="w-7 h-7"></i>
                {% trans "KPI Reports" %}
            </h1>
            <p class="text-sm mt-1 opacity-75">{% trans "Monthly automated reports for MOH and internal KPIs" %}</p>
        </div>
        <div class="flex items-center gap-3">
            <div class="relative group">
                <i data-lucide="search" class="w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-white/70 group-focus-within:text-white"></i>
                <input type="text" id="searchInput" placeholder="{% trans 'Search KPI ID or indicator...' %}" 
                       class="pl-10 pr-4 py-2.5 bg-white/20 border-transparent border focus:border-white/50 focus:bg-white/30 rounded-xl text-sm outline-none w-64 transition-all text-white placeholder-white/70">
            </div>
            <a href="{% url 'analytics:kpi_report_generate' %}" 
               class="bg-white text-[#005696] px-5 py-2.5 rounded-xl text-sm font-bold shadow-lg hover:bg-gray-100 flex items-center gap-2 transition">
                <i data-lucide="plus" class="w-4 h-4"></i> {% trans "Generate Report" %}
            </a>
        </div>
    </div>
</div>

<!-- Statistics Cards -->
<div class="grid grid-cols-4 gap-6 mb-6">
    <div class="bg-white p-6 rounded-2xl border border-slate-200 shadow-sm flex items-center gap-4 hover:border-blue/30 hover:shadow-md transition-all">
        <div class="p-3 bg-blue/10 rounded-xl">
            <i data-lucide="bar-chart-3" class="text-blue w-5 h-5"></i>
        </div>
        <div>
            <p class="text-[10px] font-bold text-slate uppercase tracking-wider">{% trans "Total Reports" %}</p>
            <p class="text-xl font-black text-navy leading-tight">{{ stats.total }}</p>
        </div>
    </div>
    <div class="bg-white p-6 rounded-2xl border border-slate-200 shadow-sm flex items-center gap-4 hover:border-green-400/50 hover:shadow-md transition-all">
        <div class="p-3 bg-green-50 rounded-xl">
            <i data-lucide="check-circle" class="text-green-600 w-5 h-5"></i>
        </div>
        <div>
            <p class="text-[10px] font-bold text-slate uppercase tracking-wider">{% trans "Completed" %}</p>
            <p class="text-xl font-black text-navy leading-tight">{{ stats.completed }}</p>
        </div>
    </div>
    <div class="bg-white p-6 rounded-2xl border border-slate-200 shadow-sm flex items-center gap-4 hover:border-yellow-400/50 hover:shadow-md transition-all">
        <div class="p-3 bg-yellow-50 rounded-xl">
            <i data-lucide="clock" class="text-yellow-600 w-5 h-5"></i>
        </div>
        <div>
            <p class="text-[10px] font-bold text-slate uppercase tracking-wider">{% trans "Pending" %}</p>
            <p class="text-xl font-black text-navy leading-tight">{{ stats.pending }}</p>
        </div>
    </div>
    <div class="bg-white p-6 rounded-2xl border border-slate-200 shadow-sm flex items-center gap-4 hover:border-red-400/50 hover:shadow-md transition-all">
        <div class="p-3 bg-red-50 rounded-xl">
            <i data-lucide="alert-triangle" class="text-red-500 w-5 h-5"></i>
        </div>
        <div>
            <p class="text-[10px] font-bold text-slate uppercase tracking-wider">{% trans "Failed" %}</p>
            <p class="text-xl font-black text-navy leading-tight">{{ stats.failed }}</p>
        </div>
    </div>
</div>

<!-- Filter Section -->
<div class="section-card mb-6" style="background: white; border-radius: 1rem; border: 2px solid #e2e8f0; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1); overflow: hidden;">
    <div class="year-header">
        <div style="width: 40px; height: 40px; border-radius: 0.75rem; display: flex; align-items: center; justify-content: center; background: linear-gradient(135deg, #005696, #007bbd);">
            <i data-lucide="filter" style="width: 20px; height: 20px; color: white;"></i>
        </div>
        <h5 class="mb-0 fw-bold">{% trans "Filters" %}</h5>
        <button onclick="toggleFilters()" class="ms-auto text-slate hover:text-navy transition p-2 rounded-lg hover:bg-white">
            <i data-lucide="chevron-up" id="filterToggleIcon" class="w-5 h-5"></i>
        </button>
    </div>
    
    <!-- Filter Tabs -->
    <div class="px-6 py-4 border-b flex items-center justify-between">
        <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 filters.status %}active{% endif %}">
                {% trans "All Reports" %}
            </a>
            <a href="?status=completed" class="filter-btn px-4 py-1.5 rounded-full text-xs font-semibold transition {% if filters.status == 'completed' %}active{% endif %}">
                {% trans "Completed" %}
            </a>
            <a href="?status=pending" class="filter-btn px-4 py-1.5 rounded-full text-xs font-semibold transition {% if filters.status == 'pending' %}active{% endif %}">
                {% trans "Pending" %}
            </a>
            <a href="?status=failed" class="filter-btn px-4 py-1.5 rounded-full text-xs font-semibold transition {% if filters.status == 'failed' %}active{% endif %}">
                {% trans "Failed" %}
            </a>
        </div>
        <p class="text-[10px] font-bold text-slate uppercase">
            {% trans "Total:" %} <span class="text-navy">{{ stats.total }}</span>
        </p>
    </div>

    <!-- Advanced Filters (Hidden by default) -->
    <div id="advancedFilters" class="hidden px-6 py-4 bg-slate-50 border-b">
        <form method="get" class="flex flex-wrap gap-4">
            {% if filters.status %}
            <input type="hidden" name="status" value="{{ filters.status }}">
            {% endif %}
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Report Type" %}</label>
                <select name="report_type" class="px-3 py-1.5 bg-white border rounded-lg text-xs">
                    <option value="">{% trans "All Types" %}</option>
                    {% for type_value, type_label in report_types %}
                    <option value="{{ type_value }}" {% if filters.report_type == type_value %}selected{% endif %}>
                        {{ type_label }}
                    </option>
                    {% endfor %}
                </select>
            </div>
            
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Year" %}</label>
                <select name="year" class="px-3 py-1.5 bg-white border rounded-lg text-xs">
                    <option value="">{% trans "All Years" %}</option>
                    {% for y in years %}
                    <option value="{{ y }}" {% if filters.year == y|stringformat:'s' %}selected{% endif %}>
                        {{ y }}
                    </option>
                    {% endfor %}
                </select>
            </div>
            
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Month" %}</label>
                <select name="month" class="px-3 py-1.5 bg-white border rounded-lg text-xs">
                    <option value="">{% trans "All Months" %}</option>
                    {% for m, m_label in months %}
                    <option value="{{ m }}" {% if filters.month == m|stringformat:'s' %}selected{% endif %}>
                        {{ m_label }}
                    </option>
                    {% endfor %}
                </select>
            </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>
</div>

<!-- Reports Grid - Grouped by Year and Quarter -->
{% if reports_by_year %}
{% for year, quarters in reports_by_year.items %}
<div class="year-card">
    <!-- Year Header -->
    <div class="year-header cursor-pointer" onclick="toggleYear('year-{{ year }}', this)">
        <h2 class="text-xl font-black text-navy">{{ year }}</h2>
        <span class="px-2.5 py-1 text-[10px] font-bold bg-navy/10 text-navy rounded-full">
            {% with q1_count=quarters|get_item:"1"|length q2_count=quarters|get_item:"2"|length q3_count=quarters|get_item:"3"|length q4_count=quarters|get_item:"4"|length %}
            {% with year_total=q1_count|add:q2_count|add:q3_count|add:q4_count %}
            {{ year_total }} {% trans "reports" %}
            {% endwith %}
            {% endwith %}
        </span>
        <div class="flex-1 h-px bg-slate-200"></div>
        <i data-lucide="chevron-right" class="w-5 h-5 text-slate transition-transform duration-200"></i>
    </div>
    
    <!-- Quarters: Q4, Q3, Q2, Q1 -->
    <div id="year-{{ year }}" class="year-content hidden">
    {% for quarter_num in "4321" %}
        {% with quarter_reports=quarters|get_item:quarter_num %}
        {% if quarter_reports %}
        <div class="quarter-section">
            <div class="quarter-header cursor-pointer" onclick="toggleQuarter('quarter-{{ year }}-{{ quarter_num }}', this)">
                <span class="quarter-badge">Q{{ quarter_num }}</span>
                <span class="quarter-months">
                    {% if quarter_num == "1" %}{% trans "Jan - Mar" %}
                    {% elif quarter_num == "2" %}{% trans "Apr - Jun" %}
                    {% elif quarter_num == "3" %}{% trans "Jul - Sep" %}
                    {% else %}{% trans "Oct - Dec" %}
                    {% endif %}
                </span>
                <span class="text-[10px] text-slate font-semibold ml-auto">{{ quarter_reports|length }} {% trans "reports" %}</span>
                <i data-lucide="chevron-right" class="w-4 h-4 text-slate transition-transform duration-200"></i>
            </div>
            
            <!-- Report Cards Grid - Compact 4 columns -->
            <div id="quarter-{{ year }}-{{ quarter_num }}" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-3 hidden">
                {% for report in quarter_reports %}
                <div class="report-card group" onclick="window.location.href='{% url 'analytics:kpi_report_detail' report.id %}'">
                    <!-- Compact Header -->
                    <div class="flex items-start justify-between mb-2">
                        <div class="flex items-center gap-1.5 flex-wrap">
                            <span class="px-1.5 py-0.5 text-[10px] font-bold rounded bg-navy text-white">
                                {{ report.kpi_id }}
                            </span>
                            <span class="text-[10px] text-slate">{{ report.report_period_display }}</span>
                        </div>
                        <span class="px-1.5 py-0.5 text-[9px] rounded-full font-bold uppercase
                          {% if report.status == 'completed' %}status-completed
                          {% elif report.status == 'failed' %}status-failed
                          {% elif report.status == 'generating' %}status-generating
                          {% else %}status-pending{% endif %}">
                            {{ report.get_status_display }}
                        </span>
                    </div>
                    
                    <!-- Title -->
                    <h3 class="text-xs font-semibold text-navy mb-1.5 line-clamp-2 group-hover:text-blue transition-colors leading-tight">
                        {{ report.indicator_title }}
                    </h3>
                    
                    <!-- Hospital -->
                    <p class="text-[10px] text-slate mb-2 flex items-center gap-1">
                        <i data-lucide="building-2" class="w-2.5 h-2.5 inline"></i>
                        {{ report.hospital.name }}
                    </p>
                    
                    <!-- Compact Metrics Row -->
                    <div class="flex gap-2 mb-2">
                        <div class="flex-1 bg-slate-50 rounded-md px-2 py-1.5 text-center">
                            <div class="text-[9px] font-bold text-slate uppercase">{% trans "Target" %}</div>
                            <div class="text-sm font-black text-navy">{{ report.target_percentage }}%</div>
                        </div>
                        <div class="flex-1 bg-slate-50 rounded-md px-2 py-1.5 text-center">
                            <div class="text-[9px] font-bold text-slate uppercase">{% trans "Result" %}</div>
                            <div class="text-sm font-black {% if report.overall_result >= report.target_percentage %}text-green-600{% else %}text-red-600{% endif %}">
                                {{ report.overall_result }}%
                            </div>
                        </div>
                        <div class="flex-1 bg-slate-50 rounded-md px-2 py-1.5 text-center">
                            <div class="text-[9px] font-bold text-slate uppercase">{% trans "Cases" %}</div>
                            <div class="text-sm font-black text-navy">{{ report.total_denominator }}</div>
                        </div>
                    </div>
                    
                    <!-- Actions -->
                    <div class="actions flex gap-1.5 pt-2 border-t border-slate-100">
                        <button onclick="event.stopPropagation(); window.location.href='{% url 'analytics:kpi_report_detail' report.id %}'" 
                                class="flex-1 bg-navy text-white text-center text-[10px] font-semibold py-1.5 rounded-md flex items-center justify-center gap-1 hover:bg-navy/90 transition">
                            <i data-lucide="eye" class="w-3 h-3"></i> {% trans "View" %}
                        </button>
                        <button onclick="event.stopPropagation(); window.location.href='{% url 'analytics:kpi_report_pdf' report.id %}'" 
                                class="px-2 py-1.5 border border-slate-200 rounded-md hover:bg-slate-50 transition" title="{% trans 'Download PDF' %}">
                            <i data-lucide="file-down" class="w-3 h-3 text-slate"></i>
                        </button>
                        {% if report.status == 'failed' or report.status == 'pending' %}
                        <form method="post" action="{% url 'analytics:kpi_report_regenerate' report.id %}" 
                              class="inline" onclick="event.stopPropagation(); return confirm('{% trans "Regenerate this report?" %}')" data-loading data-loading-text="{% trans '...' %}">
                            {% csrf_token %}
                            <button type="submit" class="px-2 py-1.5 border border-slate-200 rounded-md hover:bg-slate-50 transition" title="{% trans 'Regenerate' %}">
                                <i data-lucide="refresh-cw" class="w-3 h-3 text-slate"></i>
                            </button>
                        </form>
                        {% endif %}
                    </div>
                </div>
                {% endfor %}
            </div>
        </div>
        {% endif %}
        {% endwith %}
    {% endfor %}
    </div>
</div>
{% endfor %}
{% else %}
<div class="section-card text-center py-16" style="background: white; border-radius: 1rem; border: 2px solid #e2e8f0;">
    <i data-lucide="bar-chart-3" class="w-20 h-20 mx-auto text-slate-300 mb-4"></i>
    <h3 class="text-lg font-bold text-navy mb-2">{% trans "No KPI Reports Found" %}</h3>
    <p class="text-slate mb-6">{% trans "Generate your first KPI report to get started." %}</p>
    <a href="{% url 'analytics:kpi_report_generate' %}" class="hh-btn hh-btn-primary inline-flex items-center gap-2">
        <i data-lucide="plus" class="w-4 h-4"></i>
        {% trans "Generate Report" %}
    </a>
</div>
{% endif %}
{% endblock %}

{% block extra_js %}
<script>
function toggleFilters() {
    const filters = document.getElementById('advancedFilters');
    const icon = document.getElementById('filterToggleIcon');
    filters.classList.toggle('hidden');
    if (filters.classList.contains('hidden')) {
        icon.setAttribute('data-lucide', 'chevron-down');
    } else {
        icon.setAttribute('data-lucide', 'chevron-up');
    }
    lucide.createIcons();
}

function toggleYear(yearId, headerEl) {
    const content = document.getElementById(yearId);
    const icon = headerEl.querySelector('[data-lucide="chevron-right"], [data-lucide="chevron-down"]');
    if (content.classList.contains('hidden')) {
        content.classList.remove('hidden');
        icon.setAttribute('data-lucide', 'chevron-down');
        icon.style.transform = 'rotate(0deg)';
    } else {
        content.classList.add('hidden');
        icon.setAttribute('data-lucide', 'chevron-right');
        icon.style.transform = 'rotate(0deg)';
    }
    lucide.createIcons();
}

function toggleQuarter(quarterId, headerEl) {
    const content = document.getElementById(quarterId);
    const icon = headerEl.querySelector('[data-lucide="chevron-right"], [data-lucide="chevron-down"]');
    if (content.classList.contains('hidden')) {
        content.classList.remove('hidden');
        icon.setAttribute('data-lucide', 'chevron-down');
        icon.style.transform = 'rotate(0deg)';
    } else {
        content.classList.add('hidden');
        icon.setAttribute('data-lucide', 'chevron-right');
        icon.style.transform = 'rotate(0deg)';
    }
    lucide.createIcons();
}

// Search functionality
document.getElementById('searchInput')?.addEventListener('keypress', function(e) {
    if (e.key === 'Enter') {
        const value = this.value;
        if (value) {
            window.location.href = '?search=' + encodeURIComponent(value);
        }
    }
});
</script>
{% endblock %}