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

{% block title %}{% trans "My Observations" %} - {{ source.get_localized_name }}{% endblock %}

{% block extra_css %}
<style>
.page-header-gradient {
    background: linear-gradient(135deg, #7c3aed 0%, #8b5cf6 50%, #a78bfa 100%);
    color: white;
    padding: 1.5rem 2rem;
    border-radius: 1rem;
    margin-bottom: 1.5rem;
    box-shadow: 0 10px 15px -3px rgba(124, 58, 237, 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: #7c3aed;
    box-shadow: 0 10px 25px -5px rgba(124, 58, 237, 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;
}
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}
.animate-in {
    animation: fadeIn 0.5s ease-out forwards;
}
</style>
{% endblock %}

{% block content %}
<div class="p-6 md:p-8 bg-gradient-to-br from-light to-blue-50 min-h-screen">
    <div class="page-header-gradient animate-in">
        <div class="flex flex-col md:flex-row justify-between items-start md:items-center gap-4">
            <div class="flex items-center gap-3">
                <div class="section-icon bg-white/20">
                    <i data-lucide="eye" class="w-6 h-6 text-white"></i>
                </div>
                <div>
                    <h1 class="text-2xl font-bold">{% trans "My Observations" %}</h1>
                    <p class="text-white/80 text-sm">
                        {% trans "View all observations from your source" %}
                        <span class="inline-flex items-center px-2.5 py-0.5 bg-white/20 text-white rounded-full text-xs font-bold ml-2">{{ observations_count }}</span>
                    </p>
                </div>
            </div>
            {% if source_user.can_create_observations %}
            <a href="{% url 'px_sources:source_user_create_observation' %}"
               class="inline-flex items-center gap-2 px-5 py-2.5 bg-white text-purple-700 rounded-xl font-bold hover:bg-gray-100 transition shadow-lg">
                <i data-lucide="plus-circle" class="w-4 h-4"></i>
                {% trans "Create Observation" %}
            </a>
            {% endif %}
        </div>
    </div>

    <div class="section-card mb-8 animate-in">
        <div class="section-header">
            <div class="section-icon bg-purple-100">
                <i data-lucide="filter" class="w-5 h-5 text-purple-600"></i>
            </div>
            <h2 class="text-lg font-bold text-navy">{% trans "Filters" %}</h2>
        </div>
        <div class="p-6">
            <form method="get" class="grid grid-cols-1 md:grid-cols-12 gap-5">
                <div class="md:col-span-4">
                    <label class="block text-sm font-bold text-navy mb-2">{% trans "Search" %}</label>
                    <div class="relative">
                        <input type="text"
                               class="w-full pl-10 pr-4 py-2.5 border-2 border-purple-100 rounded-xl text-navy focus:ring-2 focus:ring-purple-400 focus:border-transparent transition"
                               name="search"
                               placeholder="{% trans 'Description, tracking code, location...' %}"
                               value="{{ search|default:'' }}">
                        <i data-lucide="search" class="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-slate"></i>
                    </div>
                </div>
                <div class="md:col-span-3">
                    <label class="block text-sm font-bold text-navy mb-2">{% trans "Status" %}</label>
                    <select name="status" class="w-full px-3 py-2.5 border-2 border-purple-100 rounded-xl text-navy focus:ring-2 focus:ring-purple-400 focus:border-transparent transition bg-white">
                        <option value="">{% trans "All Statuses" %}</option>
                        <option value="new" {% if status_filter == 'new' %}selected{% endif %}>{% trans "New" %}</option>
                        <option value="triaged" {% if status_filter == 'triaged' %}selected{% endif %}>{% trans "Triaged" %}</option>
                        <option value="assigned" {% if status_filter == 'assigned' %}selected{% endif %}>{% trans "Assigned" %}</option>
                        <option value="in_progress" {% if status_filter == 'in_progress' %}selected{% endif %}>{% trans "In Progress" %}</option>
                        <option value="resolved" {% if status_filter == 'resolved' %}selected{% endif %}>{% trans "Resolved" %}</option>
                        <option value="closed" {% if status_filter == 'closed' %}selected{% endif %}>{% trans "Closed" %}</option>
                    </select>
                </div>
                <div class="md:col-span-3">
                    <label class="block text-sm font-bold text-navy mb-2">{% trans "Category" %}</label>
                    <select name="category" class="w-full px-3 py-2.5 border-2 border-purple-100 rounded-xl text-navy focus:ring-2 focus:ring-purple-400 focus:border-transparent transition bg-white">
                        <option value="">{% trans "All Categories" %}</option>
                        {% for c in categories %}
                        <option value="{{ c.id }}" {% if category_filter == c.id|stringformat:"s" %}selected{% endif %}>{{ c.name_en }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="md:col-span-2 flex items-end">
                    <div class="flex gap-2 w-full">
                        <button type="submit"
                                class="inline-flex items-center justify-center gap-2 px-4 py-2.5 bg-purple-600 text-white rounded-xl font-bold hover:bg-purple-700 transition flex-grow shadow-lg">
                            <i data-lucide="search" class="w-4 h-4"></i>
                            {% trans "Filter" %}
                        </button>
                        <a href="{% url 'px_sources:source_user_observations' %}"
                           class="inline-flex items-center justify-center p-2.5 border-2 border-slate-200 text-slate rounded-xl hover:bg-slate-50 transition"
                           title="{% trans 'Clear filters' %}">
                            <i data-lucide="x-circle" class="w-5 h-5"></i>
                        </a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="section-card animate-in">
        <div class="section-header">
            <div class="section-icon bg-purple-100">
                <i data-lucide="eye" class="w-5 h-5 text-purple-600"></i>
            </div>
            <h2 class="text-lg font-bold text-navy">
                {% trans "All Observations" %}
                <span class="inline-flex items-center px-2.5 py-0.5 bg-purple-100 text-purple-700 rounded-full text-xs font-bold ml-2">{{ observations_count }}</span>
            </h2>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead class="bg-purple-50">
                    <tr>
                        <th class="px-6 py-4 text-left text-xs font-bold text-purple-800 uppercase tracking-wider">{% trans "Tracking Code" %}</th>
                        <th class="text-left py-4 px-6 text-xs font-bold text-purple-800 uppercase tracking-wider">{% trans "Category" %}</th>
                        <th class="text-left py-4 px-6 text-xs font-bold text-purple-800 uppercase tracking-wider">{% trans "Description" %}</th>
                        <th class="text-left py-4 px-6 text-xs font-bold text-purple-800 uppercase tracking-wider">{% trans "Location" %}</th>
                        <th class="text-center py-4 px-6 text-xs font-bold text-purple-800 uppercase tracking-wider">{% trans "Status" %}</th>
                        <th class="text-left py-4 px-6 text-xs font-bold text-purple-800 uppercase tracking-wider">{% trans "Created" %}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-purple-50">
                    {% for obs in observations %}
                    <tr class="hover:bg-purple-50/50 transition">
                        <td class="px-6 py-4 text-sm font-mono text-slate font-medium">{{ obs.tracking_code }}</td>
                        <td class="px-6 py-4 text-sm text-slate">{{ obs.category|default:"-" }}</td>
                        <td class="px-6 py-4">
                            <a href="{% url 'observations:observation_detail' obs.pk %}"
                               class="text-navy font-semibold hover:text-purple-600 transition">
                                {{ obs.description|truncatechars:45 }}
                            </a>
                        </td>
                        <td class="px-6 py-4 text-sm text-slate">{{ obs.location_text|default:"-" }}</td>
                        <td class="px-6 py-4 text-center">
                            {% if obs.status == 'new' %}
                                <span class="inline-flex items-center gap-1 px-3 py-1 rounded-full text-xs font-bold bg-yellow-100 text-yellow-700">
                                    <i data-lucide="circle" class="w-2 h-2 fill-current"></i> {% trans "New" %}
                                </span>
                            {% elif obs.status == 'triaged' %}
                                <span class="inline-flex items-center gap-1 px-3 py-1 rounded-full text-xs font-bold bg-blue-100 text-blue-700">
                                    <i data-lucide="filter" class="w-2 h-2"></i> {% trans "Triaged" %}
                                </span>
                            {% elif obs.status == 'assigned' %}
                                <span class="inline-flex items-center gap-1 px-3 py-1 rounded-full text-xs font-bold bg-indigo-100 text-indigo-700">
                                    <i data-lucide="user-check" class="w-2 h-2"></i> {% trans "Assigned" %}
                                </span>
                            {% elif obs.status == 'in_progress' %}
                                <span class="inline-flex items-center gap-1 px-3 py-1 rounded-full text-xs font-bold bg-purple-100 text-purple-700">
                                    <i data-lucide="loader" class="w-2 h-2"></i> {% trans "In Progress" %}
                                </span>
                            {% elif obs.status == 'resolved' %}
                                <span class="inline-flex items-center gap-1 px-3 py-1 rounded-full text-xs font-bold bg-emerald-100 text-emerald-700">
                                    <i data-lucide="check-circle" class="w-2 h-2"></i> {% trans "Resolved" %}
                                </span>
                            {% else %}
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold bg-slate-100 text-slate-700">{{ obs.get_status_display }}</span>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4 text-sm text-slate">{{ obs.created_at|date:"M d, Y" }}</td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="6" class="px-6 py-12 text-center">
                            <div class="flex flex-col items-center">
                                <div class="w-16 h-16 bg-gradient-to-br from-purple-100 to-indigo-100 rounded-full flex items-center justify-center mb-4">
                                    <i data-lucide="eye" class="w-8 h-8 text-purple-500"></i>
                                </div>
                                <p class="text-slate font-medium">{% trans "No observations found" %}</p>
                                {% if source_user.can_create_observations %}
                                <a href="{% url 'px_sources:source_user_create_observation' %}" class="mt-3 text-purple-600 font-semibold hover:underline">
                                    {% trans "Submit your first observation" %}
                                </a>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        {% if observations.has_other_pages %}
        <div class="px-6 py-4 border-t border-slate-100">
            <div class="flex items-center justify-between">
                <p class="text-sm text-slate">
                    {% blocktrans with start=observations.start_index end=observations.end_index total=observations.paginator.count %}
                    Showing {{ start }} to {{ end }} of {{ total }} observations
                    {% endblocktrans %}
                </p>
                <div class="flex gap-2">
                    {% if observations.has_previous %}
                    <a href="?page={{ observations.previous_page_number }}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if category_filter %}&category={{ category_filter }}{% endif %}{% if search %}&search={{ search }}{% endif %}"
                       class="px-4 py-2 border-2 border-purple-200 text-purple-600 rounded-xl font-semibold hover:bg-purple-50 transition">
                        {% trans "Previous" %}
                    </a>
                    {% endif %}
                    <span class="px-4 py-2 bg-purple-600 text-white rounded-xl font-semibold">
                        {% blocktrans with num=observations.number total=observations.paginator.num_pages %}Page {{ num }} of {{ total }}{% endblocktrans %}
                    </span>
                    {% if observations.has_next %}
                    <a href="?page={{ observations.next_page_number }}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if category_filter %}&category={{ category_filter }}{% endif %}{% if search %}&search={{ search }}{% endif %}"
                       class="px-4 py-2 border-2 border-purple-200 text-purple-600 rounded-xl font-semibold hover:bg-purple-50 transition">
                        {% trans "Next" %}
                    </a>
                    {% endif %}
                </div>
            </div>
        </div>
        {% endif %}
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    lucide.createIcons();
});
</script>
{% endblock %}
