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

{% block title %}{% trans "Departments" %} - 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);
    }
    .stat-card {
        background: white;
        border-radius: 1rem;
        border: 2px solid #e2e8f0;
        padding: 1.5rem;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
        transition: all 0.3s ease;
    }
    .stat-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        border-color: #005696;
    }
    .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;
    }
    .table-row-hover {
        transition: all 0.2s ease;
        border-left: 3px solid transparent;
    }
    .table-row-hover:hover {
        background: #f8fafc;
        border-left-color: #007bbd;
    }
</style>
{% endblock %}

{% block content %}
<div class="p-6">
    <div class="page-header-gradient">
        <div class="flex justify-between items-center">
            <div>
                <h1 class="text-2xl font-bold mb-1">{% trans "Departments" %}</h1>
                <p class="text-blue-100 text-sm">{% trans "Manage hospital departments, staff, and assigned items" %}</p>
            </div>
            <div class="flex gap-2">
                {% if can_create %}
                <a href="{% url 'organizations:department_create' %}" class="inline-flex items-center px-4 py-2.5 bg-white text-navy font-medium rounded-xl hover:bg-blue-50 transition">
                    <i data-lucide="plus" class="w-4 h-4 me-2"></i>{% trans "Add Department" %}
                </a>
                {% endif %}
            </div>
        </div>
    </div>

    <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
        <div class="stat-card">
            <div class="flex items-center gap-3">
                <div class="w-12 h-12 bg-navy/10 rounded-xl flex items-center justify-center">
                    <i data-lucide="building-2" class="w-6 h-6 text-navy"></i>
                </div>
                <div>
                    <p class="text-xs text-slate-500 font-medium">{% trans "Departments" %}</p>
                    <p class="text-2xl font-bold text-navy">{{ total_count }}</p>
                </div>
            </div>
        </div>
        <div class="stat-card">
            <div class="flex items-center gap-3">
                <div class="w-12 h-12 bg-green-500/10 rounded-xl flex items-center justify-center">
                    <i data-lucide="check-circle" class="w-6 h-6 text-green-600"></i>
                </div>
                <div>
                    <p class="text-xs text-slate-500 font-medium">{% trans "Active" %}</p>
                    <p class="text-2xl font-bold text-green-700">{{ active_count }}</p>
                </div>
            </div>
        </div>
        <div class="stat-card">
            <div class="flex items-center gap-3">
                <div class="w-12 h-12 bg-blue-500/10 rounded-xl flex items-center justify-center">
                    <i data-lucide="user-check" class="w-6 h-6 text-blue"></i>
                </div>
                <div>
                    <p class="text-xs text-slate-500 font-medium">{% trans "With Managers" %}</p>
                    <p class="text-2xl font-bold text-blue">{{ with_managers }}</p>
                </div>
            </div>
        </div>
        <div class="stat-card">
            <div class="flex items-center gap-3">
                <div class="w-12 h-12 bg-purple-500/10 rounded-xl flex items-center justify-center">
                    <i data-lucide="users" class="w-6 h-6 text-purple-600"></i>
                </div>
                <div>
                    <p class="text-xs text-slate-500 font-medium">{% trans "Total Staff" %}</p>
                    <p class="text-2xl font-bold text-purple-700">{{ total_staff }}</p>
                </div>
            </div>
        </div>
    </div>

    <div class="section-card">
        <div class="p-4 border-b border-slate-100 flex items-center justify-between bg-slate-50/50">
            <div class="flex items-center gap-3">
                <div class="w-9 h-9 bg-navy/10 rounded-lg flex items-center justify-center">
                    <i data-lucide="building-2" class="w-4 h-4 text-navy"></i>
                </div>
                <h5 class="text-sm font-bold text-navy uppercase tracking-wide">{% trans "Department List" %}</h5>
            </div>
            <form method="get" class="flex gap-2">
                <div class="relative">
                    <i data-lucide="search" class="w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-slate-400"></i>
                    <input type="text" name="search" value="{{ search }}" placeholder="{% trans 'Search departments...' %}"
                        class="pl-9 pr-4 py-2 border border-slate-200 rounded-lg text-sm focus:ring-2 focus:ring-navy/20 outline-none">
                </div>
                <select name="category" onchange="this.form.submit()"
                    class="px-3 py-2 border border-slate-200 rounded-lg text-sm focus:ring-2 focus:ring-navy/20 outline-none">
                    <option value="">{% trans "All Categories" %}</option>
                    {% for val, label in categories %}
                    <option value="{{ val }}" {% if category_filter == val %}selected{% endif %}>{{ label }}</option>
                    {% endfor %}
                </select>
            </form>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead class="bg-gray-50 border-b border-gray-200">
                    <tr>
                        <th class="px-6 py-3 text-left text-[10px] font-bold text-slate uppercase tracking-wider">{% trans "Department" %}</th>
                        <th class="px-6 py-3 text-left text-[10px] font-bold text-slate uppercase tracking-wider">{% trans "Category" %}</th>
                        <th class="px-6 py-3 text-left text-[10px] font-bold text-slate uppercase tracking-wider">{% trans "Manager" %}</th>
                        <th class="px-6 py-3 text-center text-[10px] font-bold text-slate uppercase tracking-wider">{% trans "Staff" %}</th>
                        <th class="px-6 py-3 text-center text-[10px] font-bold text-slate uppercase tracking-wider">{% trans "Complaints" %}</th>
                        <th class="px-6 py-3 text-center text-[10px] font-bold text-slate uppercase tracking-wider">{% trans "Inquiries" %}</th>
                        <th class="px-6 py-3 text-center text-[10px] font-bold text-slate uppercase tracking-wider">{% trans "Observations" %}</th>
                        <th class="px-6 py-3 text-left text-[10px] font-bold text-slate uppercase tracking-wider">{% trans "Status" %}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    {% for dept in departments %}
                    <tr class="table-row-hover cursor-pointer" onclick="window.location='{% url 'organizations:department_detail' pk=dept.pk %}'">
                        <td class="px-6 py-4">
                            <div class="flex items-center gap-3">
                                <div class="w-9 h-9 bg-blue/10 rounded-lg flex items-center justify-center">
                                    <i data-lucide="building-2" class="w-4 h-4 text-blue"></i>
                                </div>
                                <div>
                                    <span class="font-bold text-navy text-sm">{{ dept.get_localized_name }}</span>
                                    {% if dept.parent %}
                                    <p class="text-[10px] text-slate">{% trans "Sub of" %} {{ dept.parent.get_localized_name }}</p>
                                    {% endif %}
                                </div>
                            </div>
                        </td>
                        <td class="px-6 py-4">
                            {% if dept.category %}
                            <span class="inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-[10px] font-bold uppercase
                                {% if dept.category == 'nursing' %}bg-pink-50 text-pink-700
                                {% elif dept.category == 'support_services' %}bg-orange-50 text-orange-700
                                {% elif dept.category == 'medical' %}bg-blue-50 text-blue-700
                                {% elif dept.category == 'non_medical' %}bg-slate-100 text-slate-600
                                {% endif %}">
                                {{ dept.get_category_display }}
                            </span>
                            {% else %}
                            <span class="text-slate-400 text-sm">-</span>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4">
                            {% if dept.manager %}
                            <div class="flex items-center gap-2">
                                <div class="w-7 h-7 bg-navy/10 rounded-full flex items-center justify-center">
                                    <i data-lucide="user" class="w-3 h-3 text-navy"></i>
                                </div>
                                <span class="text-sm text-slate-700">{{ dept.manager.get_full_name }}</span>
                            </div>
                            {% else %}
                            <span class="text-slate-400 text-sm">-</span>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4 text-center">
                            <span class="text-sm font-bold text-navy">{{ dept.staff_count }}</span>
                        </td>
                        <td class="px-6 py-4 text-center">
                            {% if dept.open_complaints > 0 %}
                            <span class="inline-flex items-center gap-1 px-2 py-0.5 bg-red-50 text-red-700 rounded-full text-xs font-bold">
                                {{ dept.open_complaints }}
                            </span>
                            {% else %}
                            <span class="text-sm text-slate-400">0</span>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4 text-center">
                            {% if dept.pending_inquiries > 0 %}
                            <span class="inline-flex items-center gap-1 px-2 py-0.5 bg-amber-50 text-amber-700 rounded-full text-xs font-bold">
                                {{ dept.pending_inquiries }}
                            </span>
                            {% else %}
                            <span class="text-sm text-slate-400">0</span>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4 text-center">
                            {% if dept.open_observations > 0 %}
                            <span class="inline-flex items-center gap-1 px-2 py-0.5 bg-purple-50 text-purple-700 rounded-full text-xs font-bold">
                                {{ dept.open_observations }}
                            </span>
                            {% else %}
                            <span class="text-sm text-slate-400">0</span>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4">
                            <span class="inline-flex items-center px-2 py-1 rounded-full text-[10px] font-bold uppercase
                                {% if dept.status == 'active' %}bg-green-100 text-green-700{% else %}bg-slate-100 text-slate-600{% endif %}">
                                {{ dept.get_status_display }}
                            </span>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="9" class="px-6 py-12 text-center">
                            <div class="flex flex-col items-center">
                                <div class="w-16 h-16 bg-slate-100 rounded-full flex items-center justify-center mb-4">
                                    <i data-lucide="building-2" class="w-8 h-8 text-slate-300"></i>
                                </div>
                                <p class="text-slate font-medium mb-2">{% trans "No departments found" %}</p>
                                {% if can_create %}
                                <a href="{% url 'organizations:department_create' %}"
                                   class="inline-flex items-center px-4 py-2 bg-navy text-white font-medium rounded-lg hover:bg-blue transition text-sm">
                                    <i data-lucide="plus" class="w-4 h-4 me-2"></i>{% trans "Add Department" %}
                                </a>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}
