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

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

{% block content %}
<div class="container-fluid px-4">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-1">
                    <li class="breadcrumb-item">
                        <a href="{% url 'references:dashboard' %}">{% trans "Reference Section" %}</a>
                    </li>
                    <li class="breadcrumb-item active">{% trans "Search" %}</li>
                </ol>
            </nav>
            <h1 class="h3 mb-0">{% trans "Search Documents" %}</h1>
        </div>
        <div>
            <a href="{% url 'references:dashboard' %}" class="btn btn-outline-primary">
                <i class="fas fa-arrow-left me-2"></i>{% trans "Back to Dashboard" %}
            </a>
        </div>
    </div>

    <!-- Search Form -->
    <div class="card mb-4">
        <div class="card-header">
            <h5 class="mb-0">{% trans "Filter Documents" %}</h5>
        </div>
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-4">
                    <label for="{{ form.search.id_for_label }}" class="form-label">{{ form.search.label }}</label>
                    {{ form.search }}
                </div>
                <div class="col-md-3">
                    <label for="{{ form.folder.id_for_label }}" class="form-label">{{ form.folder.label }}</label>
                    {{ form.folder }}
                </div>
                <div class="col-md-2">
                    <label for="{{ form.file_type.id_for_label }}" class="form-label">{{ form.file_type.label }}</label>
                    {{ form.file_type }}
                </div>
                <div class="col-md-3">
                    <label for="{{ form.tags.id_for_label }}" class="form-label">{{ form.tags.label }}</label>
                    {{ form.tags }}
                </div>
                <div class="col-12">
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-search me-2"></i>{% trans "Search" %}
                    </button>
                    <a href="{% url 'references:search' %}" class="btn btn-outline-secondary">
                        <i class="fas fa-times me-2"></i>{% trans "Clear" %}
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Results -->
    <div class="card">
        <div class="card-header">
            <div class="d-flex justify-content-between align-items-center">
                <h5 class="mb-0">
                    {% trans "Results" %}
                    {% if page_obj %}
                        <span class="badge bg-primary ms-2">{{ page_obj.paginator.count }}</span>
                    {% endif %}
                </h5>
            </div>
        </div>
        <div class="card-body">
            {% if documents %}
                <div class="table-responsive">
                    <table class="table table-hover">
                        <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-decoration-none">
                                            <i class="{{ document.get_file_icon }} me-2"></i>
                                            {{ document.title }}
                                        </a>
                                        {% if document.description %}
                                            <br><small class="text-muted">{{ document.description|truncatechars:50 }}</small>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if document.folder %}
                                            <a href="{% url 'references:folder_view' document.folder.id %}" class="text-decoration-none">
                                                {{ document.folder.name }}
                                            </a>
                                        {% else %}
                                            <span class="text-muted">-</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <span class="badge bg-light text-dark">{{ document.file_type|upper }}</span>
                                    </td>
                                    <td>{{ document.get_file_size_display }}</td>
                                    <td>
                                        {% if document.is_latest_version %}
                                            <span class="badge bg-success">{{ document.version }}</span>
                                        {% else %}
                                            <span class="badge bg-secondary">{{ document.version }}</span>
                                        {% endif %}
                                    </td>
                                    <td>{{ document.download_count }}</td>
                                    <td>{{ document.updated_at|date:"M d, Y" }}</td>
                                    <td class="text-end">
                                        <div class="btn-group btn-group-sm">
                                            <a href="{% url 'references:api_document_download' document.id %}" class="btn btn-outline-primary" title="{% trans "Download" %}">
                                                <i class="fas fa-download"></i>
                                            </a>
                                            <a href="{% url 'references:document_view' document.id %}" class="btn btn-outline-secondary" title="{% trans "View" %}">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>

                <!-- Pagination -->
                {% if page_obj.has_other_pages %}
                    <nav aria-label="Page navigation" class="mt-3">
                        <ul class="pagination justify-content-center">
                            {% if page_obj.has_previous %}
                                <li class="page-item">
                                    <a class="page-link" href="?page=1{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">
                                        {% trans "First" %}
                                    </a>
                                </li>
                                <li class="page-item">
                                    <a class="page-link" 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 class="page-item active"><span class="page-link">{{ num }}</span></li>
                                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                                    <li class="page-item">
                                        <a class="page-link" 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 class="page-item">
                                    <a class="page-link" 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 class="page-item">
                                    <a class="page-link" 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="text-center py-5">
                    <i class="fas fa-search fa-3x mb-3 text-muted"></i>
                    <p class="text-muted mb-3">{% trans "No documents found matching your search criteria" %}</p>
                    <a href="{% url 'references:search' %}" class="btn btn-outline-primary">
                        <i class="fas fa-times me-2"></i>{% trans "Clear Filters" %}
                    </a>
                </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
