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

{% block title %}Observation Report - PX360{% endblock %}

{% block content %}
<div class="max-w-[1400px] mx-auto">
    <div class="flex justify-between items-center mb-6">
        <div>
            <h1 class="text-2xl font-bold text-gray-800">{% trans "Observation Report" %}</h1>
            <p class="text-sm text-gray-500 mt-1">{% trans "Staff observation analytics and breakdown" %}</p>
        </div>
        <a href="{% url 'dashboard:observation_report_export' %}?hospital={{ selected_hospital }}&date_from={{ date_from }}&date_to={{ date_to }}"
           class="inline-flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-medium transition"
           style="background-color: #7C3AED; color: white;">
            <i data-lucide="download" class="w-4 h-4" style="color: white;"></i>
            {% trans "Export Excel" %}
        </a>
    </div>

    {% if no_data %}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-12 text-center">
        <div class="w-16 h-16 bg-gray-100 rounded-full flex items-center justify-center mx-auto mb-4">
            <i data-lucide="eye" class="w-8 h-8 text-gray-400"></i>
        </div>
        <h3 class="text-lg font-semibold text-gray-700 mb-2">{% trans "No Observation Data Available" %}</h3>
        <p class="text-gray-500">{% trans "There are no observation records in the database for the selected hospital." %}</p>
    </div>
    {% else %}

    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 mb-6">
        <form method="get" class="flex flex-wrap gap-4 items-end">
            <div class="flex-1 min-w-[200px]">
                <label class="block text-xs font-semibold text-gray-600 mb-1">{% trans "Hospital" %}</label>
                <select name="hospital" class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500" data-tomselect>
                    {% for h in hospitals %}
                    <option value="{{ h.id }}" {% if selected_hospital == h.id|stringformat:'s' %}selected{% endif %}>{{ h.name }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="w-40">
                <label class="block text-xs font-semibold text-gray-600 mb-1">{% trans "From" %}</label>
                <input type="date" name="date_from" value="{{ date_from }}"
                       class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
            </div>
            <div class="w-40">
                <label class="block text-xs font-semibold text-gray-600 mb-1">{% trans "To" %}</label>
                <input type="date" name="date_to" value="{{ date_to }}"
                       class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
            </div>
            <button type="submit" class="px-4 py-2 bg-navy text-white text-sm font-medium rounded-lg hover:bg-navy/90 transition">
                {% trans "Apply" %}
            </button>
        </form>
    </div>

    <div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-6">
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 bg-purple-100 rounded-lg flex items-center justify-center">
                    <i data-lucide="eye" class="w-5 h-5 text-purple-600"></i>
                </div>
                <div>
                    <p class="text-xs text-gray-500 font-medium">{% trans "Total Observations" %}</p>
                    <p class="text-2xl font-bold text-gray-800">{{ total_fmt }}</p>
                </div>
            </div>
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 bg-emerald-100 rounded-lg flex items-center justify-center">
                    <i data-lucide="check-circle" class="w-5 h-5 text-emerald-600"></i>
                </div>
                <div>
                    <p class="text-xs text-gray-500 font-medium">{% trans "Resolution Rate" %}</p>
                    <p class="text-2xl font-bold text-gray-800">{{ summary.resolution_rate }}%</p>
                </div>
            </div>
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 bg-blue-100 rounded-lg flex items-center justify-center">
                    <i data-lucide="alert-triangle" class="w-5 h-5 text-blue-600"></i>
                </div>
                <div>
                    <p class="text-xs text-gray-500 font-medium">{% trans "Critical + High" %}</p>
                    <p class="text-2xl font-bold text-gray-800">{{ summary.by_severity.critical|default:0 }} + {{ summary.by_severity.high|default:0 }}</p>
                </div>
            </div>
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 bg-red-100 rounded-lg flex items-center justify-center">
                    <i data-lucide="bar-chart-3" class="w-5 h-5 text-red-600"></i>
                </div>
                <div>
                    <p class="text-xs text-gray-500 font-medium">{% trans "Resolved" %}</p>
                    <p class="text-2xl font-bold text-gray-800">{{ summary.resolved_count }}</p>
                </div>
            </div>
        </div>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-6">
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <h3 class="text-sm font-semibold text-gray-700 mb-4">{% trans "Daily Trend" %}</h3>
            <div id="daily-chart"></div>
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <h3 class="text-sm font-semibold text-gray-700 mb-4">{% trans "By Status" %}</h3>
            <div id="status-chart"></div>
        </div>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6 mb-6">
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <h3 class="text-sm font-semibold text-gray-700 mb-4">{% trans "By Severity" %}</h3>
            <div id="severity-chart"></div>
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <h3 class="text-sm font-semibold text-gray-700 mb-4">{% trans "By Source" %}</h3>
            <div id="source-chart"></div>
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <h3 class="text-sm font-semibold text-gray-700 mb-4">{% trans "By Category" %}</h3>
            <div id="category-chart"></div>
        </div>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-6">
        {% if employee_breakdown %}
        <div class="bg-white rounded-xl shadow-sm border border-gray-200">
            <div class="p-5 border-b border-gray-200">
                <h3 class="text-lg font-semibold text-gray-800">{% trans "Per Employee" %}</h3>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full text-sm">
                    <thead>
                        <tr class="bg-[#2F75B5] text-white">
                            <th class="px-4 py-3 text-left font-semibold">{% trans "Employee" %}</th>
                            <th class="px-4 py-3 text-center font-semibold">{% trans "Open" %}</th>
                            <th class="px-4 py-3 text-center font-semibold">{% trans "In Progress" %}</th>
                            <th class="px-4 py-3 text-center font-semibold">{% trans "Resolved" %}</th>
                            <th class="px-4 py-3 text-center font-semibold bg-emerald-600">{% trans "Total" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for row in employee_breakdown %}
                        <tr class="border-b border-gray-100 {% cycle '' 'bg-gray-50' %}">
                            <td class="px-4 py-3 font-medium text-gray-700">{{ row.name }}</td>
                            <td class="px-4 py-3 text-center text-blue-600">{{ row.open }}</td>
                            <td class="px-4 py-3 text-center text-amber-600">{{ row.in_progress }}</td>
                            <td class="px-4 py-3 text-center text-emerald-600">{{ row.resolved }}</td>
                            <td class="px-4 py-3 text-center font-semibold bg-emerald-50 text-emerald-700">{{ row.total_fmt }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
        {% endif %}

        {% if department_breakdown %}
        <div class="bg-white rounded-xl shadow-sm border border-gray-200">
            <div class="p-5 border-b border-gray-200">
                <h3 class="text-lg font-semibold text-gray-800">{% trans "Per Department" %}</h3>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full text-sm">
                    <thead>
                        <tr class="bg-[#2F75B5] text-white">
                            <th class="px-4 py-3 text-left font-semibold">{% trans "Department" %}</th>
                            <th class="px-4 py-3 text-center font-semibold">{% trans "Open" %}</th>
                            <th class="px-4 py-3 text-center font-semibold">{% trans "In Progress" %}</th>
                            <th class="px-4 py-3 text-center font-semibold">{% trans "Resolved" %}</th>
                            <th class="px-4 py-3 text-center font-semibold bg-emerald-600">{% trans "Total" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for row in department_breakdown %}
                        <tr class="border-b border-gray-100 {% cycle '' 'bg-gray-50' %}">
                            <td class="px-4 py-3 font-medium text-gray-700">{{ row.name }}</td>
                            <td class="px-4 py-3 text-center text-blue-600">{{ row.open }}</td>
                            <td class="px-4 py-3 text-center text-amber-600">{{ row.in_progress }}</td>
                            <td class="px-4 py-3 text-center text-emerald-600">{{ row.resolved }}</td>
                            <td class="px-4 py-3 text-center font-semibold bg-emerald-50 text-emerald-700">{{ row.total_fmt }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
        {% endif %}
    </div>

    {% endif %}
</div>
{% endblock %}

{% block extra_js %}
{% if not no_data and chart_data_json %}
<script>
(function() {
    var cd = JSON.parse('{{ chart_data_json|escapejs }}');

    new ApexCharts(document.getElementById('daily-chart'), {
        chart: { type: 'bar', height: 280, toolbar: { show: true } },
        plotOptions: { bar: { borderRadius: 3 } },
        dataLabels: { enabled: false },
        series: [{ name: 'Observations', data: cd.daily, color: '#7C3AED' }],
        xaxis: { type: 'category' },
        yaxis: { labels: { formatter: function(v) { return v.toLocaleString(); } } },
        tooltip: { y: { formatter: function(v) { return v + ' observations'; } } },
    }).render();

    new ApexCharts(document.getElementById('status-chart'), {
        chart: { type: 'donut', height: 280 },
        series: cd.by_status.map(function(d) { return d.y; }),
        labels: cd.by_status.map(function(d) { return d.x; }),
        colors: ['#3B82F6', '#06B6D4', '#8B5CF6', '#F59E0B', '#10B981', '#6B7280', '#EF4444', '#9CA3AF', '#22C55E', '#DC2626'],
        legend: { position: 'bottom' },
    }).render();

    new ApexCharts(document.getElementById('severity-chart'), {
        chart: { type: 'bar', height: 280, toolbar: { show: false } },
        plotOptions: { bar: { borderRadius: 3, distributed: true } },
        dataLabels: { enabled: false },
        series: [{ name: 'Count', data: cd.by_severity }],
        xaxis: { type: 'category' },
        legend: { show: false },
    }).render();

    new ApexCharts(document.getElementById('source-chart'), {
        chart: { type: 'pie', height: 280 },
        series: cd.by_source.map(function(d) { return d.y; }),
        labels: cd.by_source.map(function(d) { return d.x; }),
        colors: ['#7C3AED', '#3B82F6', '#10B981', '#F59E0B', '#EF4444', '#6B7280'],
        legend: { position: 'bottom' },
    }).render();

    new ApexCharts(document.getElementById('category-chart'), {
        chart: { type: 'bar', height: 280, toolbar: { show: false } },
        plotOptions: { bar: { borderRadius: 3, horizontal: true } },
        dataLabels: { enabled: false },
        series: [{ name: 'Count', data: cd.by_category, color: '#2F75B5' }],
        xaxis: { type: 'category' },
    }).render();
})();
</script>
{% endif %}
{% endblock %}
