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

{% block title %}{% trans "Acknowledgement 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 %}
<div class="p-6 md:p-8 bg-gradient-to-br from-light to-blue-50 min-h-screen">
    <!-- Page Header -->
    <div class="page-header-gradient">
        <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="folder" class="w-6 h-6 text-white"></i>
                </div>
                <div>
                    <h1 class="text-2xl font-bold">
                        {% trans "Acknowledgement Categories" %}
                    </h1>
                    <p class="text-white/80 text-sm">
                        {% trans "Manage categories for acknowledgement items" %}
                    </p>
                </div>
            </div>
            <div class="flex gap-3">
                <a href="{% url 'accounts:acknowledgements:ack_dashboard' %}" class="inline-flex items-center gap-2 px-5 py-2.5 bg-white/20 border border-white/30 text-white rounded-xl font-semibold hover:bg-white/30 transition">
                    <i data-lucide="arrow-left" class="w-4 h-4"></i>
                    {% trans "Back" %}
                </a>
                <a href="{% url 'accounts:acknowledgements:ack_category_create' %}" class="inline-flex items-center gap-2 px-5 py-2.5 bg-white text-blue-700 rounded-xl font-semibold hover:bg-blue-50 transition shadow-lg">
                    <i data-lucide="plus" class="w-4 h-4"></i>
                    {% trans "New Category" %}
                </a>
            </div>
        </div>
    </div>

    <!-- Categories List -->
    <div class="section-card">
        <div class="section-header">
            <div class="section-icon bg-blue-100">
                <i data-lucide="list" class="w-5 h-5 text-blue-600"></i>
            </div>
            <h2 class="text-lg font-bold text-navy">{% trans "All Categories" %}</h2>
        </div>
        <div class="p-6">
            {% if categories %}
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead>
                        <tr class="border-b border-slate-200">
                            <th class="text-left py-3 px-4 text-xs font-semibold text-slate uppercase">{% trans "Category" %}</th>
                            <th class="text-left py-3 px-4 text-xs font-semibold text-slate uppercase">{% trans "Code" %}</th>
                            <th class="text-left py-3 px-4 text-xs font-semibold text-slate uppercase">{% trans "Order" %}</th>
                            <th class="text-left py-3 px-4 text-xs font-semibold text-slate uppercase">{% trans "Status" %}</th>
                            <th class="text-right py-3 px-4 text-xs font-semibold text-slate uppercase">{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for category in categories %}
                        <tr class="border-b border-slate-100 hover:bg-slate-50 transition">
                            <td class="py-4 px-4">
                                <div class="flex items-center gap-3">
                                    <div class="w-10 h-10 rounded-xl flex items-center justify-center" style="background-color: {{ category.color }}20">
                                        <i data-lucide="{{ category.icon|default:'folder' }}" class="w-5 h-5" style="color: {{ category.color }}"></i>
                                    </div>
                                    <div>
                                        <p class="font-semibold text-navy">{{ category.get_localized_name }}</p>
                                        {% if category.name_ar %}
                                        <p class="text-sm text-slate">{{ category.name_ar }}</p>
                                        {% endif %}
                                    </div>
                                </div>
                            </td>
                            <td class="py-4 px-4">
                                <span class="inline-flex items-center px-2 py-1 bg-slate-100 text-slate-700 rounded-lg text-xs font-mono">
                                    {{ category.code|default:"-" }}
                                </span>
                            </td>
                            <td class="py-4 px-4 text-slate">{{ category.order }}</td>
                            <td class="py-4 px-4">
                                <button onclick="toggleCategory('{{ category.id }}')" class="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-xs font-semibold transition {% if category.is_active %}bg-emerald-100 text-emerald-700{% else %}bg-red-100 text-red-700{% endif %}">
                                    <i data-lucide="{% if category.is_active %}check-circle{% else %}x-circle{% endif %}" class="w-3.5 h-3.5"></i>
                                    {% if category.is_active %}{% trans "Active" %}{% else %}{% trans "Inactive" %}{% endif %}
                                </button>
                            </td>
                            <td class="py-4 px-4 text-right">
                                <a href="{% url 'accounts:acknowledgements:ack_category_edit' category.id %}" class="inline-flex items-center gap-1 px-3 py-1.5 bg-blue-50 text-blue-700 rounded-lg text-sm font-semibold hover:bg-blue-100 transition">
                                    <i data-lucide="edit-2" class="w-4 h-4"></i>
                                    {% trans "Edit" %}
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <div class="text-center py-16">
                <div class="w-20 h-20 bg-gradient-to-br from-blue-100 to-indigo-100 rounded-full flex items-center justify-center mx-auto mb-4">
                    <i data-lucide="folder-open" class="w-10 h-10 text-blue-500"></i>
                </div>
                <h3 class="text-lg font-bold text-navy mb-2">{% trans "No Categories Yet" %}</h3>
                <p class="text-slate mb-6">{% trans "Create your first acknowledgement category to get started." %}</p>
                <a href="{% url 'accounts:acknowledgements:ack_category_create' %}" class="inline-flex items-center gap-2 px-6 py-3 bg-gradient-to-r from-blue to-navy text-white rounded-xl font-semibold hover:from-navy hover:to-blue transition shadow-lg shadow-blue-200">
                    <i data-lucide="plus" class="w-5 h-5"></i>
                    {% trans "Create Category" %}
                </a>
            </div>
            {% endif %}
        </div>
    </div>
</div>

<script>
function toggleCategory(categoryId) {
    fetch(`/accounts/acknowledgements/admin/categories/${categoryId}/toggle/`, {
        method: 'POST',
        headers: {
            'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]')?.value || '',
            'Content-Type': 'application/json',
        },
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            location.reload();
        }
    });
}

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