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

{% block title %}{% trans "Search Documents" %} - {% trans "Reference Section" %}{% endblock %}

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

    .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-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;
    }

    .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;
    }

    .form-control-px360 {
        width: 100%;
        padding: 0.625rem 0.875rem;
        border: 2px solid #e2e8f0;
        border-radius: 0.5rem;
        font-size: 0.875rem;
        transition: all 0.2s;
    }
    .form-control-px360:focus {
        outline: none;
        border-color: #005696;
        box-shadow: 0 0 0 3px rgba(0, 86, 150, 0.1);
    }

    .btn-transition {
        transition: all 0.2s ease-in-out;
    }

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

    .empty-state {
        text-align: center;
        padding: 3rem;
    }

    .pagination-btn {
        display: inline-flex;
        align-items: center;
        padding: 0.5rem 1rem;
        border: 1px solid #e2e8f0;
        background: white;
        color: #005696;
        border-radius: 0.5rem;
        font-size: 0.875rem;
        transition: all 0.2s;
    }
    .pagination-btn:hover {
        background: #005696;
        color: white;
        border-color: #005696;
    }
    .pagination-btn.active {
        background: #005696;
        color: white;
        border-color: #005696;
    }
</style>
{% endblock %}

{% block content %}
<div class="px-6 py-4">
    <!-- Page Header -->
    <div class="page-header-gradient mb-6">
        <div class="flex justify-between items-center">
            <div>
                <nav aria-label="breadcrumb" class="mb-1">
                    <ol class="flex items-center space-x-2 text-sm text-blue-100">
                        <li>
                            <a href="{% url 'references:dashboard' %}" class="hover:text-white transition">{% trans "Reference Section" %}</a>
                        </li>
                        <li><span class="mx-2">/</span></li>
                        <li class="text-white font-medium">{% trans "Search" %}</li>
                    </ol>
                </nav>
                <h1 class="text-2xl font-bold">{% trans "Search Documents" %}</h1>
            </div>
            <div>
                <a href="{% url 'references:dashboard' %}" 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="arrow-left" class="w-4 h-4 me-2"></i>{% trans "Back to Dashboard" %}
                </a>
            </div>
        </div>
    </div>

    <!-- Search Form -->
    <div class="section-card mb-6">
        <div class="section-header">
            <div class="section-icon bg-blue/10">
                <i data-lucide="filter" class="w-5 h-5 text-blue"></i>
            </div>
            <h5 class="text-lg font-semibold text-gray-800">{% trans "Filter Documents" %}</h5>
        </div>
        <div class="p-4">
            <form method="get" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1.5">{{ form.search.label }}</label>
                    <input type="text" name="search" value="{{ request.GET.search|default:'' }}" class="form-control-px360" placeholder="{% trans 'Search by title...' %}">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1.5">{{ form.folder.label }}</label>
                    {{ form.folder }}
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1.5">{{ form.file_type.label }}</label>
                    {{ form.file_type }}
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1.5">{{ form.tags.label }}</label>
                    <input type="text" name="tags" value="{{ request.GET.tags|default:'' }}" class="form-control-px360" placeholder="{% trans 'Filter by tags...' %}">
                </div>
                <div class="lg:col-span-4 flex gap-2 pt-2">
                    <button type="submit" 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="search" class="w-4 h-4 me-2"></i>{% trans "Search" %}
                    </button>
                    <a href="{% url 'references:search' %}" class="btn-transition inline-flex items-center px-4 py-2.5 bg-white border-2 border-gray-300 text-gray-700 font-medium rounded-lg hover:bg-gray-50 transition">
                        <i data-lucide="x" class="w-4 h-4 me-2"></i>{% trans "Clear" %}
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Results -->
    <div class="section-card">
        <div class="section-header">
            <div class="section-icon bg-navy/10">
                <i data-lucide="list" class="w-5 h-5 text-navy"></i>
            </div>
            <div class="flex items-center">
                <h5 class="text-lg font-semibold text-gray-800">
                    {% trans "Results" %}
                    {% if page_obj %}
                        <span class="ml-2 badge-px360 badge-navy">{{ page_obj.paginator.count }}</span>
                    {% endif %}
                </h5>
            </div>
        </div>
        <div class="p-0">
            {% if documents %}
                <div class="overflow-x-auto">
                    <table class="data-table">
                        <thead>
                            <tr>
                                <th>{% trans "Name" %}</th>
                                <th>{% trans "Folder" %}</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>
                                        {% if document.description %}
                                            <br><small class="text-slate-500 text-xs">{{ document.description|truncatechars:50 }}</small>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if document.folder %}
                                            <a href="{% url 'references:folder_view' document.folder.id %}" class="text-navy hover:text-blue transition text-sm">
                                                {{ document.folder.name }}
                                            </a>
                                        {% else %}
                                            <span class="text-slate-400 text-sm">-</span>
                                        {% endif %}
                                    </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>
                                            <a href="{% url 'references:document_view' document.id %}" class="p-2 text-slate-600 hover:bg-slate-200 rounded-lg transition" title="{% trans "View" %}">
                                                <i data-lucide="eye" class="w-4 h-4"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>

                <!-- Pagination -->
                {% if page_obj.has_other_pages %}
                    <nav aria-label="Page navigation" class="px-4 py-4 border-t border-gray-100">
                        <ul class="flex justify-center gap-2">
                            {% if page_obj.has_previous %}
                                <li>
                                    <a class="pagination-btn" href="?page=1{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">
                                        {% trans "First" %}
                                    </a>
                                </li>
                                <li>
                                    <a class="pagination-btn" href="?page={{ page_obj.previous_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">
                                        {% trans "Previous" %}
                                    </a>
                                </li>
                            {% endif %}

                            {% for num in page_obj.paginator.page_range %}
                                {% if page_obj.number == num %}
                                    <li><span class="pagination-btn active">{{ num }}</span></li>
                                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                                    <li>
                                        <a class="pagination-btn" href="?page={{ num }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">{{ num }}</a>
                                    </li>
                                {% endif %}
                            {% endfor %}

                            {% if page_obj.has_next %}
                                <li>
                                    <a class="pagination-btn" href="?page={{ page_obj.next_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">
                                        {% trans "Next" %}
                                    </a>
                                </li>
                                <li>
                                    <a class="pagination-btn" href="?page={{ page_obj.paginator.num_pages }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">
                                        {% trans "Last" %}
                                    </a>
                                </li>
                            {% endif %}
                        </ul>
                    </nav>
                {% endif %}
            {% else %}
                <div class="empty-state">
                    <i data-lucide="search" class="w-16 h-16 mx-auto mb-4 text-slate-300"></i>
                    <p class="text-slate-500 mb-4">{% trans "No documents found matching your search criteria" %}</p>
                    <a href="{% url 'references:search' %}" class="btn-transition inline-flex items-center px-4 py-2.5 bg-white border-2 border-gray-300 text-gray-700 font-medium rounded-lg hover:bg-gray-50 transition">
                        <i data-lucide="x" class="w-4 h-4 me-2"></i>{% trans "Clear Filters" %}
                    </a>
                </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
