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

{% block title %}{% trans "Observations" %} - 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);
    }
    .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;
    }

    
    .anonymous-badge {
        background: #f3f4f6;
        color: #6b7280;
        padding: 2px 8px;
        border-radius: 10px;
        font-size: 0.75rem;
        font-style: italic;
    }
    
    .observation-row:hover {
        background: #f8fafc;
        cursor: pointer;
    }
    
    .stat-card {
        border-left: 4px solid;
        transition: transform 0.2s;
    }
    .stat-card:hover {
        transform: translateY(-2px);
    }
    
    .tracking-code {
        font-family: 'Courier New', monospace;
        font-weight: 600;
        color: #005696;
    }
</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="eye" class="w-7 h-7"></i>
                {% trans "Observations" %}
            </h1>
            <p class="text-sm mt-1 opacity-75">{% trans "Manage and triage staff-reported observations" %}</p>
        </div>
        {% if user.is_px_admin or user.is_hospital_admin or user.is_px_employee %}
        <div class="flex gap-3">
            <a href="{% url 'observations:observation_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 Observation" %}
            </a>
            {% comment %} <a href="{% url 'observations:category_list' %}" 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="tags" class="w-4 h-4"></i> {% trans "Categories" %}
            </a> {% endcomment %}
            <a href="{% url 'core:public_submit_landing' %}" class="bg-white/20 text-white border-2 border-white/50 px-5 py-2.5 rounded-xl text-sm font-bold hover:bg-white/30 flex items-center gap-2 transition" target="_blank">
                <i data-lucide="plus" class="w-4 h-4"></i> {% trans "Public Form" %}
            </a>
        </div>
        {% endif %}
    </div>
</div>

<!-- Statistics Cards -->
<div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-6">
    <div class="bg-white p-5 rounded-2xl shadow-sm border border-slate-100 border-l-4 border-l-blue-500">
        <div class="flex justify-between items-center">
            <div>
                <p class="text-xs font-bold text-slate uppercase tracking-wider mb-1">{% trans "Total" %}</p>
                <p class="text-2xl font-black text-navy">{{ stats.total }}</p>
            </div>
            <div class="p-3 bg-blue-50 rounded-xl">
                <i data-lucide="list" class="w-6 h-6 text-blue-500"></i>
            </div>
        </div>
    </div>
    <div class="bg-white p-5 rounded-2xl shadow-sm border border-slate-100 border-l-4 border-l-cyan-500">
        <div class="flex justify-between items-center">
            <div>
                <p class="text-xs font-bold text-slate uppercase tracking-wider mb-1">{% trans "New" %}</p>
                <p class="text-2xl font-black text-navy">{{ stats.new }}</p>
            </div>
            <div class="p-3 bg-cyan-50 rounded-xl">
                <i data-lucide="inbox" class="w-6 h-6 text-cyan-500"></i>
            </div>
        </div>
    </div>
    <div class="bg-white p-5 rounded-2xl shadow-sm border border-slate-100 border-l-4 border-l-amber-500">
        <div class="flex justify-between items-center">
            <div>
                <p class="text-xs font-bold text-slate uppercase tracking-wider mb-1">{% trans "In Progress" %}</p>
                <p class="text-2xl font-black text-navy">{{ stats.in_progress }}</p>
            </div>
            <div class="p-3 bg-amber-50 rounded-xl">
                <i data-lucide="clock" class="w-6 h-6 text-amber-500"></i>
            </div>
        </div>
    </div>
    {% comment %} <div class="bg-white p-5 rounded-2xl shadow-sm border border-slate-100 border-l-4 border-l-slate-400">
        <div class="flex justify-between items-center">
            <div>
                <p class="text-xs font-bold text-slate uppercase tracking-wider mb-1">{% trans "Anonymous" %}</p>
                <p class="text-2xl font-black text-navy">{{ stats.anonymous_count }}</p>
            </div>
            <div class="p-3 bg-slate-100 rounded-xl">
                <i data-lucide="user-x" class="w-6 h-6 text-slate-500"></i>
            </div>
        </div>
    </div> {% endcomment %}
</div>

<!-- Filter Panel -->
<div class="section-card mb-6" id="filterPanel">
    <div class="section-header">
        <div class="section-icon" style="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>
    
    <div class="filter-body px-6 py-4 bg-slate-50">
        <form method="get" action="{% url 'observations:observation_list' %}" id="filterForm">
            <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                <!-- Search -->
                <div>
                    <label class="block text-xs font-bold text-slate uppercase mb-2">{% trans "Search" %}</label>
                    <input type="text" name="search" 
                           placeholder="{% trans 'Tracking code, description...' %}" 
                           value="{{ filters.search }}"
                           class="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy focus:border-transparent transition">
                </div>
                
                <!-- Status -->
                <div>
                    <label class="block text-xs font-bold text-slate uppercase mb-2">{% trans "Status" %}</label>
                    <select name="status" class="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy focus:border-transparent transition">
                        <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>
                
                <!-- Severity -->
                <div>
                    <label class="block text-xs font-bold text-slate uppercase mb-2">{% trans "Severity" %}</label>
                    <select name="severity" class="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy focus:border-transparent transition">
                        <option value="">{% trans "All Severities" %}</option>
                        <option value="low" {% if filters.severity == 'low' %}selected{% endif %}>{% trans "Low" %}</option>
                        <option value="medium" {% if filters.severity == 'medium' %}selected{% endif %}>{% trans "Medium" %}</option>
                        <option value="high" {% if filters.severity == 'high' %}selected{% endif %}>{% trans "High" %}</option>
                        <option value="critical" {% if filters.severity == 'critical' %}selected{% endif %}>{% trans "Critical" %}</option>
                    </select>
                </div>
                
                <!-- Category -->
                <div>
                    <label class="block text-xs font-bold text-slate uppercase mb-2">{% trans "Category" %}</label>
                    <select name="category" class="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy focus:border-transparent transition">
                        <option value="">{% trans "All Categories" %}</option>
                        {% for cat in categories %}
                        <option value="{{ cat.id }}" {% if filters.category == cat.id|stringformat:"s" %}selected{% endif %}>
                            {{ cat.get_localized_name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                
                <!-- Department -->
                <div>
                    <label class="block text-xs font-bold text-slate uppercase mb-2">{% trans "Department" %}</label>
                    <select name="assigned_department" class="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy focus:border-transparent transition" data-tomselect>
                        <option value="">{% trans "All Departments" %}</option>
                        {% for dept in departments %}
                        <option value="{{ dept.id }}" {% if filters.assigned_department == dept.id|stringformat:"s" %}selected{% endif %}>
                            {{ dept.get_localized_name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                
                <!-- Assigned To -->
                <div>
                    <label class="block text-xs font-bold text-slate uppercase mb-2">{% trans "Assigned To" %}</label>
                    <select name="assigned_to" class="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy focus:border-transparent transition">
                        <option value="">{% trans "All Users" %}</option>
                        {% for user_obj in assignable_users %}
                        <option value="{{ user_obj.id }}" {% if filters.assigned_to == user_obj.id|stringformat:"s" %}selected{% endif %}>
                            {{ user_obj.get_full_name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                
                <!-- Anonymous Filter -->
                {% comment %} <div>
                    <label class="block text-xs font-bold text-slate uppercase mb-2">{% trans "Reporter Type" %}</label>
                    <select name="is_anonymous" class="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy focus:border-transparent transition">
                        <option value="">{% trans "All" %}</option>
                        <option value="yes" {% if filters.is_anonymous == 'yes' %}selected{% endif %}>{% trans "Anonymous Only" %}</option>
                        <option value="no" {% if filters.is_anonymous == 'no' %}selected{% endif %}>{% trans "Identified Only" %}</option>
                    </select>
                </div> {% endcomment %}
                
                <!-- Date Range -->
                <div>
                    <label class="block text-xs font-bold text-slate uppercase mb-2">{% trans "Date From" %}</label>
                    <input type="date" name="date_from" value="{{ filters.date_from }}"
                           class="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy focus:border-transparent transition">
                </div>
                <div>
                    <label class="block text-xs font-bold text-slate uppercase mb-2">{% trans "Date To" %}</label>
                    <input type="date" name="date_to" value="{{ filters.date_to }}"
                           class="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy focus:border-transparent transition">
                </div>
            </div>
            
            <div class="mt-4 flex gap-3">
                <button type="submit" class="bg-navy text-white px-5 py-2.5 rounded-xl text-sm font-bold hover:bg-blue transition flex items-center gap-2">
                    <i data-lucide="search" class="w-4 h-4"></i> {% trans "Apply Filters" %}
                </button>
                <a href="{% url 'observations:observation_list' %}" class="border-2 border-slate-200 text-slate px-5 py-2.5 rounded-xl text-sm font-bold hover:bg-white transition flex items-center gap-2">
                    <i data-lucide="x-circle" class="w-4 h-4"></i> {% trans "Clear" %}
                </a>
            </div>
        </form>
    </div>
</div>

<!-- Table Toolbar -->
<div class="section-card mb-0 rounded-b-none border-b-0">
    <div class="section-header border-b-0">
        <div class="section-icon" style="background: linear-gradient(135deg, #005696, #007bbd);">
            <i data-lucide="table" style="width: 20px; height: 20px; color: white;"></i>
        </div>
        <h5 class="mb-0 fw-bold">{% trans "Observations List" %}</h5>
        <div class="ms-auto text-sm text-slate">
            {% trans "Showing" %} <span class="font-bold text-navy">{{ page_obj.start_index }}</span> {% trans "to" %} <span class="font-bold text-navy">{{ page_obj.end_index }}</span> {% trans "of" %} <span class="font-bold text-navy">{{ page_obj.paginator.count }}</span> {% trans "observations" %}
        </div>
    </div>
</div>

<!-- Observations Table -->
<div class="section-card rounded-t-none border-t-0 mb-6">
    <div class="overflow-x-auto">
        <table class="w-full">
            <thead class="bg-gray-50">
                <tr>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Reference" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Date" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Category" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Description" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Severity" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Status" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Reporter" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Department" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Assigned" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Actions" %}</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-100">
                {% for observation in observations %}
                {% url 'observations:observation_detail' observation.id as observation_detail_url %}
                <tr class="observation-row" onclick="window.location='{{ observation_detail_url }}'">
                    <td class="px-6 py-4">
                        <span class="tracking-code text-sm">{{ observation.tracking_code }}</span>
                    </td>
                    <td class="px-6 py-4">
                        <small class="text-gray-600">{{ observation.created_at|date:"M d, Y" }}</small><br>
                        <small class="text-gray-400">{{ observation.created_at|time:"H:i" }}</small>
                    </td>
                    <td class="px-6 py-4">
                        {% if observation.category %}
                        <span class="inline-flex items-center px-2.5 py-1 rounded-lg text-xs font-bold bg-slate-100 text-slate">
                            {{ observation.category.get_localized_name }}
                        </span>
                        {% else %}
                        <span class="text-gray-400">-</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4">
                        <div class="max-w-[200px]">
                            {% if observation.title %}
                            <strong class="text-gray-800 text-sm block">{{ observation.title|truncatewords:5 }}</strong>
                            {% endif %}
                            <small class="text-gray-400">{{ observation.description|truncatewords:10 }}</small>
                        </div>
                    </td>
                    <td class="px-6 py-4">
                        <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase
                            {% if observation.severity == 'low' %}bg-green-100 text-green-700
                            {% elif observation.severity == 'medium' %}bg-yellow-100 text-yellow-700
                            {% elif observation.severity == 'high' %}bg-orange-100 text-orange-700
                            {% elif observation.severity == 'critical' %}bg-red-500 text-white
                            {% else %}bg-slate-100 text-slate-600{% endif %}">
                            {{ observation.get_severity_display }}
                        </span>
                    </td>
                    <td class="px-6 py-4">
                        <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase
                            {% if observation.status == 'open' %}bg-amber-100 text-amber-700
                            {% elif observation.status == 'in_progress' %}bg-blue-100 text-blue-700
                            {% elif observation.status == 'resolved' %}bg-green-100 text-green-700
                            {% elif observation.status == 'closed' %}bg-slate-100 text-slate-600
                            {% else %}bg-slate-100 text-slate-600{% endif %}">
                            {{ observation.get_status_display }}
                        </span>
                    </td>
                    <td class="px-6 py-4">
                        {% if observation.is_anonymous %}
                        <span class="anonymous-badge">{% trans "Anonymous" %}</span>
                        {% else %}
                        <small class="text-gray-600">{{ observation.reporter_display }}</small>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4">
                        {% if observation.assigned_department %}
                        <small class="text-gray-600">{{ observation.assigned_department.name|truncatewords:3 }}</small>
                        {% else %}
                        <span class="text-gray-400">-</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4">
                        {% if observation.assigned_to %}
                        <small class="text-gray-600">{{ observation.assigned_to.get_full_name }}</small>
                        {% else %}
                        <span class="text-gray-400 italic">{% trans "Unassigned" %}</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4" onclick="event.stopPropagation();">
                        <a href="{% url 'observations:observation_detail' observation.id %}" 
                           class="inline-flex items-center gap-1 px-3 py-2 text-navy bg-light rounded-lg hover:bg-light transition font-medium text-sm">
                            <i data-lucide="eye" class="w-4 h-4"></i>
                        </a>
                    </td>
                </tr>
                {% empty %}
                <tr>
                    <td colspan="10" class="text-center py-8 text-gray-500">
                        <i data-lucide="inbox" class="w-12 h-12 mx-auto mb-3 text-gray-300"></i>
                        <p>{% trans "No observations found" %}</p>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>

    {% include "partials/pagination.html" %}
{% endblock %}

{% block extra_js %}
<script>
function toggleFilters() {
    const panel = document.getElementById('filterPanel');
    const body = panel.querySelector('.filter-body');
    const icon = document.getElementById('filterToggleIcon');
    
    if (body.style.display === 'none') {
        body.style.display = 'block';
        icon.setAttribute('data-lucide', 'chevron-up');
    } else {
        body.style.display = 'none';
        icon.setAttribute('data-lucide', 'chevron-down');
    }
    lucide.createIcons();
}
</script>
{% endblock %}
