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

{% block title %}{{ section.name_en }} - {% trans "Org Section" %} - 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.25rem;
        box-shadow: 0 2px 4px rgba(0,0,0,0.05);
        transition: all 0.3s ease;
        text-decoration: none;
        color: inherit;
    }
    .stat-card:hover { transform: translateY(-2px); box-shadow: 0 8px 15px 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;
    }
    .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;
        justify-content: space-between;
        gap: 0.75rem;
    }
    .section-icon {
        width: 40px;
        height: 40px;
        border-radius: 0.75rem;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .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">
    <!-- Breadcrumb -->
    <div class="flex items-center gap-2 text-sm text-slate-500 mb-4">
        <a href="{% url 'organizations:department_list' %}" class="hover:text-[#005696] transition">{% trans "Departments" %}</a>
        <i data-lucide="chevron-right" class="w-4 h-4"></i>
        <a href="{% url 'organizations:department_detail' section.department.pk %}" class="hover:text-[#005696] transition">{{ section.department.get_localized_name }}</a>
        <i data-lucide="chevron-right" class="w-4 h-4"></i>
        <span class="text-[#005696] font-semibold">{{ section.name_en }}</span>
    </div>

    <!-- Header -->
    <div class="page-header-gradient">
        <div class="flex flex-col md:flex-row md:items-center justify-between gap-4">
            <div>
                <div class="flex items-center gap-3 mb-2">
                    <div class="w-12 h-12 bg-white/20 rounded-xl flex items-center justify-center">
                        <i data-lucide="layers" class="w-6 h-6"></i>
                    </div>
                    <div>
                        <h1 class="text-2xl font-bold">{{ section.name_en }}</h1>
                        {% if section.name_ar %}
                        <p class="text-white/70 text-sm" dir="rtl">{{ section.name_ar }}</p>
                        {% endif %}
                    </div>
                </div>
                <div class="flex flex-wrap items-center gap-4 text-sm text-white/80 mt-3">
                    {% if section.code %}
                    <span class="flex items-center gap-1"><i data-lucide="hash" class="w-4 h-4"></i> {{ section.code }}</span>
                    {% endif %}
                    <span class="flex items-center gap-1 px-2 py-0.5 bg-white/20 rounded-full text-xs font-bold">{{ section.get_location_type_display }}</span>
                    {% if section.sub_location %}
                    <span class="flex items-center gap-1"><i data-lucide="map-pin" class="w-4 h-4"></i> {{ section.sub_location }}</span>
                    {% endif %}
                    {% if section.floor %}
                    <span class="flex items-center gap-1"><i data-lucide="building" class="w-4 h-4"></i> {% trans "Floor" %} {{ section.floor }}</span>
                    {% endif %}
                    <span class="flex items-center gap-1"><i data-lucide="building-2" class="w-4 h-4"></i> {{ section.department.get_localized_name }}</span>
                </div>
            </div>
            <div class="flex items-center gap-3">
                <a href="{% url 'organizations:orgsection_update' section.pk %}" class="flex items-center gap-2 px-4 py-2 bg-white/20 hover:bg-white/30 rounded-xl text-sm font-semibold transition whitespace-nowrap">
                    <i data-lucide="pencil" class="w-4 h-4"></i> {% trans "Edit Section" %}
                </a>
            </div>
        </div>
    </div>

    <!-- Stat Cards -->
    <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-10 h-10 bg-purple-50 rounded-lg flex items-center justify-center">
                    <i data-lucide="git-branch" class="w-5 h-5 text-purple-600"></i>
                </div>
                <div>
                    <p class="text-[10px] text-slate-400 uppercase font-bold">{% trans "Sub-Sections" %}</p>
                    <p class="text-xl font-bold text-[#005696]">{{ sub_sections_count }}</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Sub-Sections Table -->
    <div class="section-card">
        <div class="section-header">
            <div class="flex items-center gap-3">
                <div class="section-icon bg-purple-100">
                    <i data-lucide="git-branch" class="w-5 h-5 text-purple-600"></i>
                </div>
                <h3 class="font-semibold text-slate-800">{% trans "Sub-Sections" %}</h3>
            </div>
            <a href="{% url 'organizations:orgsubsection_create' %}?section={{ section.pk }}" class="flex items-center gap-2 px-4 py-2 bg-[#005696] text-white rounded-xl text-sm font-semibold hover:bg-[#004670] transition">
                <i data-lucide="plus" class="w-4 h-4"></i> {% trans "Add Sub-Section" %}
            </a>
        </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 (EN)" %}</th>
                        <th class="px-6 py-4 text-left text-xs font-bold text-slate-500 uppercase tracking-wider">{% trans "Name (AR)" %}</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 "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 sub in sub_sections %}
                    <tr class="table-row-hover">
                        <td class="px-6 py-4">
                            <span class="font-semibold text-slate-800">{{ sub.name_en }}</span>
                        </td>
                        <td class="px-6 py-4 text-slate-700" dir="rtl">
                            {{ sub.name_ar|default:"-" }}
                        </td>
                        <td class="px-6 py-4 text-slate-700">
                            {{ sub.code|default:"-" }}
                        </td>
                        <td class="px-6 py-4">
                            {% if sub.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:orgsubsection_update' sub.pk %}" 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:orgsubsection_delete' sub.pk %}" 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="5" class="px-6 py-12 text-center text-slate-500">
                            <i data-lucide="git-branch" class="w-12 h-12 mx-auto mb-3 text-slate-300"></i>
                            <p class="font-semibold">{% trans "No sub-sections found" %}</p>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}
