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

{% block title %}{% trans "Observation Categories" %} - 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;
    }
</style>
{% endblock %}

{% block content %}
<!-- Back Link -->
<div class="mb-4">
    <a href="{% url 'observations:observation_list' %}" class="inline-flex items-center gap-2 text-slate hover:text-navy transition font-medium text-sm">
        <i data-lucide="arrow-left" class="w-4 h-4"></i> {% trans "Observations" %}
    </a>
</div>

<!-- Page Header -->
<div class="page-header-gradient">
    <div class="flex justify-between items-center">
        <div>
            <h1 class="text-2xl font-bold flex items-center gap-3">
                <i data-lucide="tags" class="w-7 h-7"></i>
                {% trans "Observation Categories" %}
            </h1>
            <p class="text-sm mt-1 opacity-75">{% trans "Manage categories for observation classification" %}</p>
        </div>
        <div class="flex gap-3">
            <a href="{% url 'observations:category_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 "Add Category" %}
            </a>
        </div>
    </div>
</div>

<!-- Categories Table -->
<div class="section-card">
    <div class="section-header">
        <div class="section-icon" style="background: linear-gradient(135deg, #005696, #007bbd);">
            <i data-lucide="list" style="width: 20px; height: 20px; color: white;"></i>
        </div>
        <h5 class="mb-0 font-bold">{% trans "Category List" %}</h5>
    </div>
    <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" style="width: 60px;">{% trans "Order" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Name (English)" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Name (Arabic)" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Icon" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Observations" %}</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 "Actions" %}</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-100">
                {% for category in categories %}
                <tr class="hover:bg-gray-50 transition">
                    <td class="px-6 py-4">
                        <span class="inline-flex items-center justify-center w-8 h-8 bg-gray-100 rounded-lg text-xs font-bold text-gray-600">{{ category.sort_order }}</span>
                    </td>
                    <td class="px-6 py-4">
                        <strong class="text-gray-800 text-sm">{{ category.get_localized_name }}</strong>
                        {% if category.description %}
                        <br><span class="text-xs text-slate">{{ category.description|truncatewords:10 }}</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4" dir="rtl">
                        <span class="text-sm text-gray-600">{{ category.name_ar|default:"-" }}</span>
                    </td>
                    <td class="px-6 py-4">
                        {% if category.icon %}
                        <i class="{{ category.icon }} text-navy"></i>
                        <span class="text-xs text-slate ml-1">{{ category.icon }}</span>
                        {% else %}
                        <span class="text-gray-400">-</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4">
                        <span class="inline-flex items-center px-2.5 py-1 rounded-full text-xs font-bold bg-blue-100 text-blue-700">{{ category.observations.count }}</span>
                    </td>
                    <td class="px-6 py-4">
                        {% if category.is_active %}
                        <span class="inline-flex items-center px-2.5 py-1 rounded-full text-xs font-bold bg-green-100 text-green-700">{% trans "Active" %}</span>
                        {% else %}
                        <span class="inline-flex items-center px-2.5 py-1 rounded-full text-xs font-bold bg-gray-100 text-gray-600">{% trans "Inactive" %}</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4">
                        <div class="flex items-center gap-2">
                            <a href="{% url 'observations:category_edit' category.id %}"
                               class="p-2 rounded-lg text-navy bg-light hover:bg-blue-100 transition" title="{% trans 'Edit' %}">
                                <i data-lucide="pencil" class="w-4 h-4"></i>
                            </a>
                            {% if not category.observations.exists %}
                            <form method="post" action="{% url 'observations:category_delete' category.id %}"
                                  style="display: inline;"
                                  onsubmit="return confirm('{% trans "Are you sure you want to delete this category?" %}');">
                                {% csrf_token %}
                                <button type="submit" class="p-2 rounded-lg text-red-500 bg-red-50 hover:bg-red-100 transition" title="{% trans 'Delete' %}">
                                    <i data-lucide="trash-2" class="w-4 h-4"></i>
                                </button>
                            </form>
                            {% endif %}
                        </div>
                    </td>
                </tr>
                {% empty %}
                <tr>
                    <td colspan="7" class="text-center py-12">
                        <i data-lucide="tags" class="w-12 h-12 mx-auto mb-3 text-gray-300"></i>
                        <p class="text-slate mb-3">{% trans "No categories found" %}</p>
                        <a href="{% url 'observations:category_create' %}" class="bg-navy text-white px-5 py-2.5 rounded-xl text-sm font-bold hover:bg-blue transition inline-flex items-center gap-2">
                            <i data-lucide="plus-circle" class="w-4 h-4"></i> {% trans "Add First Category" %}
                        </a>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>
{% endblock %}
