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

{% block title %}{% if current_folder %}{{ current_folder.name }}{% else %}{% trans "Folders" %}{% endif %} - {% 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>
                    {% for item in breadcrumb %}
                        <li class="breadcrumb-item{% if forloop.last %} active{% endif %}">
                            {% if not forloop.last %}
                                <a href="{% url 'references:folder_view' item.id %}">{{ item.name }}</a>
                            {% else %}
                                {{ item.name }}
                            {% endif %}
                        </li>
                    {% endfor %}
                </ol>
            </nav>
            {% if current_folder %}
                <h1 class="h3 mb-0">{{ current_folder.name }}</h1>
                <p class="text-muted small mb-0">{{ current_folder.description }}</p>
            {% else %}
                <h1 class="h3 mb-0">{% trans "Folders" %}</h1>
            {% endif %}
        </div>
        <div class="d-flex gap-2">
            <a href="{% url 'references:search' %}" class="btn btn-outline-primary">
                <i class="fas fa-search me-2"></i>{% trans "Search" %}
            </a>
            {% if current_folder %}
            <a href="{% url 'references:folder_create_in_parent' parent_pk=current_folder.id %}" class="btn btn-primary">
                <i class="fas fa-folder-plus me-2"></i>{% trans "New Folder" %}
            </a>
            {% else %}
            <a href="{% url 'references:folder_create' %}" class="btn btn-primary">
                <i class="fas fa-folder-plus 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 btn-success">
                <i class="fas fa-upload me-2"></i>{% trans "Upload Document" %}
            </a>
            {% else %}
            <a href="{% url 'references:document_create' %}" class="btn btn-success">
                <i class="fas fa-upload me-2"></i>{% trans "Upload Document" %}
            </a>
            {% endif %}
        </div>
    </div>

    <!-- Subfolders -->
    {% if subfolders %}
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">{% trans "Folders" %}</h5>
            </div>
            <div class="card-body">
                <div class="row">
                    {% for folder in subfolders %}
                        <div class="col-md-4 col-lg-3 mb-3">
                            <a href="{% url 'references:folder_view' folder.id %}" class="card h-100 text-decoration-none text-dark folder-card">
                                <div class="card-body">
                                    <div class="d-flex align-items-center mb-2">
                                        <div class="flex-shrink-0">
                                            {% if folder.icon %}
                                                <i class="{{ folder.icon }} fa-2x text-primary"></i>
                                            {% else %}
                                                <i class="fas fa-folder fa-2x text-primary"></i>
                                            {% endif %}
                                        </div>
                                        <div class="flex-grow-1 ms-3">
                                            <h6 class="mb-0">{{ folder.name }}</h6>
                                            <small class="text-muted">{{ folder.document_count }} {% trans "documents" %}</small>
                                        </div>
                                    </div>
                                    {% if folder.description %}
                                        <p class="small text-muted mb-0">{{ folder.description|truncatewords:8 }}</p>
                                    {% endif %}
                                </div>
                            </a>
                        </div>
                    {% endfor %}
                </div>
            </div>
        </div>
    {% endif %}

    <!-- Documents -->
    <div class="card">
        <div class="card-header">
            <h5 class="mb-0">{% trans "Documents" %}</h5>
        </div>
        <div class="card-body">
            {% if documents %}
                <div class="table-responsive">
                    <table class="table table-hover">
                        <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-decoration-none">
                                            <i class="{{ document.get_file_icon }} me-2"></i>
                                            {{ document.title }}
                                        </a>
                                    </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>
                                        </div>
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% else %}
                <div class="text-center py-5">
                    <i class="fas fa-file-alt fa-3x mb-3 text-muted"></i>
                    <p class="text-muted mb-3">{% 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 btn-primary">
                        <i class="fas fa-upload me-2"></i>{% trans "Upload Document" %}
                    </a>
                    {% else %}
                    <a href="{% url 'references:document_create' %}" class="btn btn-primary">
                        <i class="fas fa-upload me-2"></i>{% trans "Upload Document" %}
                    </a>
                    {% endif %}
                </div>
            {% endif %}
        </div>
    </div>
</div>

<style>
.folder-card:hover {
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    transform: translateY(-2px);
    transition: all 0.2s ease;
}
</style>
{% endblock %}
