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

{% block title %}{% trans "Sections" %} - 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 %}
<!-- Page Header -->
<div class="page-header-gradient">
    <div class="flex justify-between items-center">
        <div>
            <h1 class="text-3xl font-bold mb-2">{% trans "Sections" %}</h1>
            <p class="text-white/80">{% trans "Manage hospital sections and departments" %}</p>
        </div>
        <a href="{% url 'organizations:section_create' %}" class="bg-white/20 text-white px-6 py-3 rounded-xl font-bold flex items-center gap-2 hover:bg-white/30 transition backdrop-blur-sm">
            <i data-lucide="plus" class="w-5 h-5"></i> {% trans "Add Section" %}
        </a>
    </div>
</div>

<!-- Filters -->
<div class="section-card mb-6">
    <div class="section-header">
        <div class="section-icon bg-sky-100">
            <i data-lucide="filter" class="w-5 h-5 text-sky-600"></i>
        </div>
        <h3 class="font-semibold text-slate-800">{% trans "Filters" %}</h3>
    </div>
    <div class="p-6">
        <form method="get" class="grid grid-cols-1 md:grid-cols-4 gap-4">
            <div class="md:col-span-2">
                <label class="block text-sm font-semibold text-slate-700 mb-2">{% trans "Search" %}</label>
                <input type="text" class="w-full px-4 py-3 border border-slate-200 rounded-xl text-slate-800 focus:ring-2 focus:ring-[#005696] focus:border-transparent transition" name="search" placeholder="{% trans 'Search sections...' %}" value="{{ filters.search }}">
            </div>
            <div>
                <label class="block text-sm font-semibold text-slate-700 mb-2">{% trans "Department" %}</label>
                <select class="w-full px-4 py-3 border border-slate-200 rounded-xl text-slate-800 focus:ring-2 focus:ring-[#005696] focus:border-transparent transition" name="department" data-tomselect>
                    <option value="">{% trans "All Departments" %}</option>
                    {% for dept in departments %}
                    <option value="{{ dept.id }}" {% if filters.department == dept.id|stringformat:'s' %}selected{% endif %}>{{ dept.get_localized_name }}</option>
                    {% endfor %}
                </select>
            </div>
            <div>
                <label class="block text-sm font-semibold text-slate-700 mb-2">{% trans "Status" %}</label>
                <select class="w-full px-4 py-3 border border-slate-200 rounded-xl text-slate-800 focus:ring-2 focus:ring-[#005696] focus:border-transparent transition" name="status">
                    <option value="">{% trans "All Status" %}</option>
                    <option value="active" {% if filters.status == 'active' %}selected{% endif %}>{% trans "Active" %}</option>
                    <option value="inactive" {% if filters.status == 'inactive' %}selected{% endif %}>{% trans "Inactive" %}</option>
                </select>
            </div>
            <div class="md:col-span-2 flex items-end">
                <div class="flex gap-2 w-full">
                    <button type="submit" class="flex-1 bg-[#005696] text-white px-4 py-3 rounded-xl font-semibold hover:bg-[#004670] transition flex items-center justify-center gap-2">
                        <i data-lucide="search" class="w-4 h-4"></i> {% trans "Filter" %}
                    </button>
                    <a href="{% url 'organizations:section_list' %}" class="bg-slate-100 text-slate-600 px-4 py-3 rounded-xl font-semibold hover:bg-slate-200 transition flex items-center justify-center gap-2">
                        <i data-lucide="x-circle" class="w-4 h-4"></i>
                    </a>
                </div>
            </div>
        </form>
    </div>
</div>

<!-- Table -->
<div class="section-card">
    <div class="section-header">
        <div class="section-icon bg-indigo-100">
            <i data-lucide="layers" class="w-5 h-5 text-indigo-600"></i>
        </div>
        <h3 class="font-semibold text-slate-800">{% trans "Sections List" %}</h3>
    </div>
    <div class="overflow-x-auto">
        <table class="w-full">
            <thead class="bg-slate-50">
                <tr>
                    <th class="px-6 py-4 text-left text-xs font-bold text-slate-500 uppercase tracking-wider">{% trans "Name" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-slate-500 uppercase tracking-wider">{% trans "Code" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-slate-500 uppercase tracking-wider">{% trans "Department" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-slate-500 uppercase tracking-wider">{% trans "Hospital" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-slate-500 uppercase tracking-wider">{% trans "Head" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-slate-500 uppercase tracking-wider">{% trans "Status" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-slate-500 uppercase tracking-wider">{% trans "Actions" %}</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-slate-100">
                {% for section in sections %}
                <tr class="hover:bg-slate-50 transition">
                    <td class="px-6 py-4">
                        <span class="font-semibold text-slate-800">{{ section.name }}</span>
                    </td>
                    <td class="px-6 py-4 text-slate-700">
                        {{ section.code|default:"-" }}
                    </td>
                    <td class="px-6 py-4">
                        <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-sky-100 text-sky-600">
                            {{ section.department.name }}
                        </span>
                    </td>
                    <td class="px-6 py-4 text-slate-700">
                        {{ section.department.hospital.name }}
                    </td>
                    <td class="px-6 py-4 text-slate-700">
                        {{ section.head|default:"-" }}
                    </td>
                    <td class="px-6 py-4">
                        {% if section.status == 'active' %}
                        <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-green-100 text-green-600">{% trans "Active" %}</span>
                        {% else %}
                        <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-slate-100 text-slate-600">{% trans "Inactive" %}</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4">
                        <div class="flex gap-1">
                            <a href="{% url 'organizations:section_update' section.id %}" class="p-2 text-[#005696] hover:bg-sky-50 rounded-lg transition" title="{% trans 'Edit' %}">
                                <i data-lucide="edit-2" class="w-4 h-4"></i>
                            </a>
                            <a href="{% url 'organizations:section_delete' section.id %}" class="p-2 text-red-500 hover:bg-red-50 rounded-lg transition" title="{% trans 'Delete' %}">
                                <i data-lucide="trash-2" class="w-4 h-4"></i>
                            </a>
                        </div>
                    </td>
                </tr>
                {% empty %}
                <tr>
                    <td colspan="7" class="px-6 py-12 text-center text-slate-500">
                        <i data-lucide="layers" class="w-12 h-12 mx-auto mb-3 text-slate-300"></i>
                        <p class="font-semibold">{% trans "No sections found" %}</p>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <div class="px-6 py-4 border-t border-slate-100 flex justify-between items-center">
        <div class="text-sm text-slate-500">
            {% blocktrans with current=page_obj.number total=page_obj.paginator.num_pages %}{{ current }} of {{ total }}{% endblocktrans %}
        </div>
        <div class="flex gap-2">
            {% if page_obj.has_previous %}
            <a href="?page={{ page_obj.previous_page_number }}&{{ filters|urlencode }}" class="px-3 py-2 border border-slate-200 rounded-lg text-sm font-semibold hover:bg-slate-50 transition">
                <i data-lucide="chevron-left" class="w-4 h-4"></i>
            </a>
            {% endif %}
            
            <span class="px-3 py-2 bg-[#005696] text-white rounded-lg text-sm font-bold">
                {{ page_obj.number }}
            </span>
            
            {% if page_obj.has_next %}
            <a href="?page={{ page_obj.next_page_number }}&{{ filters|urlencode }}" class="px-3 py-2 border border-slate-200 rounded-lg text-sm font-semibold hover:bg-slate-50 transition">
                <i data-lucide="chevron-right" class="w-4 h-4"></i>
            </a>
            {% endif %}
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}
