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

{% block title %}{% if current_folder %}{{ current_folder.name }}{% else %}{% trans "Folders" %}{% endif %} - {% trans "Reference Section" %}{% endblock %}

{% block extra_css %}
<style>
    /* PX360 App Theme Variables */
    :root {
        --hh-navy: #005696;
        --hh-blue: #007bbd;
        --hh-light: #eef6fb;
        --hh-slate: #64748b;
    }

    /* Page Header */
    .page-header-gradient {
        background: linear-gradient(135deg, var(--hh-navy) 0%, #0069a8 50%, var(--hh-blue) 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 Cards */
    .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;
    }

    /* Folder Cards */
    .folder-card {
        background: white;
        border: 2px solid #e2e8f0;
        border-radius: 0.75rem;
        padding: 1rem;
        transition: all 0.3s ease;
        text-decoration: none;
        color: inherit;
        display: block;
    }
    .folder-card:hover {
        transform: translateY(-4px);
        box-shadow: 0 12px 24px -8px rgba(0, 86, 150, 0.2);
        border-color: #005696;
    }

    /* Table Styling */
    .data-table {
        width: 100%;
        border-collapse: collapse;
    }
    .data-table th {
        background: #f8fafc;
        border-bottom: 2px solid #e2e8f0;
        padding: 0.75rem 1rem;
        text-align: left;
        font-size: 0.75rem;
        font-weight: 600;
        color: #64748b;
        text-transform: uppercase;
        letter-spacing: 0.025em;
    }
    .data-table td {
        padding: 1rem;
        border-bottom: 1px solid #f1f5f9;
        vertical-align: middle;
    }
    .data-table tr:hover td {
        background: #f8fafc;
    }

    /* Button Transitions */
    .btn-transition {
        transition: all 0.2s ease-in-out;
    }

    /* Badge */
    .badge-px360 {
        display: inline-flex;
        align-items: center;
        padding: 0.25rem 0.75rem;
        border-radius: 9999px;
        font-size: 0.75rem;
        font-weight: 500;
    }
    .badge-slate {
        background: #f1f5f9;
        color: #64748b;
    }
    .badge-green {
        background: #dcfce7;
        color: #166534;
    }

    /* Empty State */
    .empty-state {
        text-align: center;
        padding: 3rem;
    }
</style>
{% endblock %}

{% block content %}
<div class="px-6 py-4">
    <!-- Page Header -->
    <div class="page-header-gradient">
        <div class="flex justify-between items-start">
            <div>
                <nav aria-label="breadcrumb" class="mb-1">
                    <ol class="flex items-center space-x-2 text-sm text-blue-100 flex-wrap">
                        <li>
                            <a href="{% url 'references:dashboard' %}" class="hover:text-white transition">{% trans "Reference Section" %}</a>
                        </li>
                        {% for item in breadcrumb %}
                            <li><span class="mx-2">/</span></li>
                            <li{% if forloop.last %} class="text-white font-medium"{% endif %}>
                                {% if not forloop.last %}
                                    <a href="{% url 'references:folder_view' item.id %}" class="hover:text-white transition">{{ item.name }}</a>
                                {% else %}
                                    {{ item.name }}
                                {% endif %}
                            </li>
                        {% endfor %}
                    </ol>
                </nav>
                {% if current_folder %}
                    <h1 class="text-2xl font-bold">{{ current_folder.name }}</h1>
                    {% if current_folder.description %}
                        <p class="text-blue-100 text-sm mt-1">{{ current_folder.description }}</p>
                    {% endif %}
                {% else %}
                    <h1 class="text-2xl font-bold">{% trans "Folders" %}</h1>
                {% endif %}
            </div>
            <div class="flex gap-2">
                <a href="{% url 'references:search' %}" class="btn-transition inline-flex items-center px-4 py-2.5 bg-white/10 border-2 border-white/30 text-white font-medium rounded-xl hover:bg-white hover:text-navy transition">
                    <i data-lucide="search" class="w-4 h-4 me-2"></i>{% trans "Search" %}
                </a>
                {% if current_folder %}
                <a href="{% url 'references:folder_create_in_parent' parent_pk=current_folder.id %}" class="btn-transition 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="folder-plus" class="w-4 h-4 me-2"></i>{% trans "New Folder" %}
                </a>
                {% else %}
                <a href="{% url 'references:folder_create' %}" class="btn-transition 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="folder-plus" class="w-4 h-4 me-2"></i>{% trans "New Folder" %}
                </a>
                {% endif %}
                {% if current_folder %}
                <a href="{% url 'references:document_create_in_folder' folder_pk=current_folder.id %}" class="btn-transition inline-flex items-center px-4 py-2.5 bg-green-500 text-white font-medium rounded-xl hover:bg-green-600 transition">
                    <i data-lucide="upload" class="w-4 h-4 me-2"></i>{% trans "Upload" %}
                </a>
                {% else %}
                <a href="{% url 'references:document_create' %}" class="btn-transition inline-flex items-center px-4 py-2.5 bg-green-500 text-white font-medium rounded-xl hover:bg-green-600 transition">
                    <i data-lucide="upload" class="w-4 h-4 me-2"></i>{% trans "Upload" %}
                </a>
                {% endif %}
            </div>
        </div>
    </div>

    <!-- Subfolders -->
    {% if subfolders %}
        <div class="section-card mb-6">
            <div class="section-header">
                <div class="section-icon bg-navy/10">
                    <i data-lucide="folder" class="w-5 h-5 text-navy"></i>
                </div>
                <h5 class="text-lg font-semibold text-gray-800">{% trans "Folders" %}</h5>
            </div>
            <div class="p-4">
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4">
                    {% for folder in subfolders %}
                        <a href="{% url 'references:folder_view' folder.id %}" class="folder-card">
                            <div class="flex items-center mb-2">
                                <div class="flex-shrink-0">
                                    {% if folder.icon %}
                                        <i class="{{ folder.icon }} w-8 h-8 text-navy"></i>
                                    {% else %}
                                        <i data-lucide="folder" class="w-8 h-8 text-navy"></i>
                                    {% endif %}
                                </div>
                                <div class="ml-3">
                                    <h6 class="font-semibold text-gray-800 text-sm">{{ folder.name }}</h6>
                                    <small class="text-slate-500 text-xs">{{ folder.document_count }} {% trans "documents" %}</small>
                                </div>
                            </div>
                            {% if folder.description %}
                                <p class="text-slate-500 text-xs">{{ folder.description|truncatewords:8 }}</p>
                            {% endif %}
                        </a>
                    {% endfor %}
                </div>
            </div>
        </div>
    {% endif %}

    <!-- Documents -->
    <div class="section-card">
        <div class="section-header">
            <div class="section-icon bg-blue/10">
                <i data-lucide="file-text" class="w-5 h-5 text-blue"></i>
            </div>
            <h5 class="text-lg font-semibold text-gray-800">{% trans "Documents" %}</h5>
        </div>
        <div class="p-0">
            {% if documents %}
                <div class="overflow-x-auto">
                    <table class="data-table">
                        <thead>
                            <tr>
                                <th>{% trans "Name" %}</th>
                                <th>{% trans "Type" %}</th>
                                <th>{% trans "Size" %}</th>
                                <th>{% trans "Version" %}</th>
                                <th>{% trans "Downloads" %}</th>
                                <th>{% trans "Modified" %}</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for document in documents %}
                                <tr>
                                    <td>
                                        <a href="{% url 'references:document_view' document.id %}" class="text-navy hover:text-blue transition font-medium">
                                            <i class="{{ document.get_file_icon }} me-2"></i>
                                            {{ document.title }}
                                        </a>
                                    </td>
                                    <td>
                                        <span class="badge-px360 badge-slate">{{ document.file_type|upper }}</span>
                                    </td>
                                    <td class="text-sm text-slate-600">{{ document.get_file_size_display }}</td>
                                    <td>
                                        {% if document.is_latest_version %}
                                            <span class="badge-px360 badge-green">{{ document.version }}</span>
                                        {% else %}
                                            <span class="badge-px360 badge-slate">{{ document.version }}</span>
                                        {% endif %}
                                    </td>
                                    <td class="text-sm text-slate-600">{{ document.download_count }}</td>
                                    <td class="text-sm text-slate-600">{{ document.updated_at|date:"M d, Y" }}</td>
                                    <td>
                                        <div class="flex items-center justify-end gap-1">
                                            <a href="{% url 'references:api_document_download' document.id %}" class="p-2 text-navy hover:bg-navy hover:text-white rounded-lg transition" title="{% trans "Download" %}">
                                                <i data-lucide="download" class="w-4 h-4"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% else %}
                <div class="empty-state">
                    <i data-lucide="file-text" class="w-16 h-16 mx-auto mb-4 text-slate-300"></i>
                    <p class="text-slate-500 mb-4">{% trans "No documents in this folder" %}</p>
                    {% if current_folder %}
                    <a href="{% url 'references:document_create_in_folder' folder_pk=current_folder.id %}" class="btn-transition inline-flex items-center px-4 py-2.5 bg-navy text-white font-medium rounded-lg hover:bg-blue transition">
                        <i data-lucide="upload" class="w-4 h-4 me-2"></i>{% trans "Upload Document" %}
                    </a>
                    {% else %}
                    <a href="{% url 'references:document_create' %}" class="btn-transition inline-flex items-center px-4 py-2.5 bg-navy text-white font-medium rounded-lg hover:bg-blue transition">
                        <i data-lucide="upload" class="w-4 h-4 me-2"></i>{% trans "Upload Document" %}
                    </a>
                    {% endif %}
                </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
