{% load static %}
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Gallery</title>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/7.0.0/css/all.min.css" crossorigin="anonymous" referrerpolicy="no-referrer">
        <link href="{% static 'css/styles.css' %}" rel="stylesheet">
        <noscript>
            <link href="{% static 'css/noscript.css' %}" rel="stylesheet">
        </noscript>
    </head>
    <body class="theme-{{ theme }}">
        <div class="app-shell d-flex">
            <aside class="sidebar d-none d-lg-flex flex-column">
                <div class="d-flex align-items-center user-row">
                    <span><i class="fa-solid fa-circle-user fa-lg"></i></span>
                    <span class="user-name text-truncate">{{ request.user.username }}</span>
                    <a class="btn btn-sm btn-plain ms-auto" href="{{ theme_toggle_url }}" aria-label="Toggle theme">
                        {% if theme == 'dark' %}
                            <i class="fa-solid fa-sun"></i>
                        {% else %}
                            <i class="fa-solid fa-moon"></i>
                        {% endif %}
                    </a>
                </div>

                <hr>

                {% if not is_special %}
                <form action="{{ search_action_url }}" method="get">
                    <div class="input-group input-group-sm">
                        <input type="search" name="search" class="form-control search-input" placeholder="Search..." value="{{ search_text }}">
                        <span class="input-group-text search-addon"><i class="fa-solid fa-magnifying-glass"></i></span>
                        {% if search_text %}
                        <a class="btn btn-sm btn-plain ms-auto" href="{{ clear_search_url }}" aria-label="Clear search">
                            <i class="fa-solid fa-xmark"></i>
                        </a>
                        {% endif %}
                    </div>
                </form>
                {% endif %}

                <hr>

                <a href="/gallery/favorites/" class="sidebar-link {% if active_special == 'favorites' %}active-sort{% endif %}">Favorites</a>
                <a href="/gallery/most-visited/" class="sidebar-link {% if active_special == 'most-visited' %}active-sort{% endif %}">Most visited</a>
                <a href="/gallery/recent/" class="sidebar-link {% if active_special == 'recent' %}active-sort{% endif %}">Recently visited</a>

                {% if path != '' or is_special %}
                <hr>
                {% endif %}

                {% if path != '' %}
                <a href="{{ back_url }}" class="subdir-item">
                    {% if back_thumb %}
                        <img src="{{ back_thumb }}" class="subdir-thumb" alt="back thumb">
                    {% else %}
                        <span class="subdir-fallback"><i class="fa-solid fa-image"></i></span>
                    {% endif %}
                    <span>Back</span>
                </a>
                {% endif %}

                {% if path != '' or is_special %}
                <a href="{{ home_url }}" class="subdir-item">
                    {% if home_thumb %}
                        <img src="{{ home_thumb }}" class="subdir-thumb" alt="home thumb">
                    {% else %}
                        <span class="subdir-fallback"><i class="fa-solid fa-image"></i></span>
                    {% endif %}
                    <span>Home</span>
                </a>
                {% endif %}

                <hr>

                <div class="sidebar-scroll flex-grow-1">
                    {% for subdir in subdirs %}
                        <a href="{{ subdir.path }}" class="subdir-item">
                            {% if subdir.thumbnail %}
                                <img src="{{ subdir.thumbnail }}" alt="{{ subdir.name }}" class="subdir-thumb">
                            {% else %}
                                <span class="subdir-fallback"><i class="fa-solid fa-image"></i></span>
                            {% endif %}
                            <span>{{ subdir.name|truncatechars:30 }}</span>
                        </a>
                    {% empty %}
                        <small class="text-secondary">No subdirectories</small>
                    {% endfor %}
                </div>

                <hr>

                <form method="post" action="{% url 'logout' %}">
                    {% csrf_token %}
                    <button type="submit" class="btn btn-sm btn-plain w-100">
                        <i class="fa-solid fa-arrow-up-from-bracket"></i>
                        <span class="ms-1">Logout</span>
                    </button>
                </form>
            </aside>

            <div class="noscript-sidebar sidebar d-lg-none" style="display:none;">
                <div class="d-flex align-items-center user-row">
                    <span><i class="fa-solid fa-circle-user fa-lg"></i></span>
                    <span class="user-name text-truncate">{{ request.user.username }}</span>
                    <a class="btn btn-sm btn-plain ms-auto" href="{{ theme_toggle_url }}" aria-label="Toggle theme">
                        {% if theme == 'dark' %}
                            <i class="fa-solid fa-sun"></i>
                        {% else %}
                            <i class="fa-solid fa-moon"></i>
                        {% endif %}
                    </a>
                </div>

                <hr>

                {% if not is_special %}
                <form action="{{ search_action_url }}" method="get">
                    <div class="input-group input-group-sm">
                        <input type="search" name="search" class="form-control search-input" placeholder="Search..." value="{{ search_text }}">
                        <span class="input-group-text search-addon"><i class="fa-solid fa-magnifying-glass"></i></span>
                    </div>
                </form>
                {% endif %}

                <hr>

                <a href="/gallery/favorites/" class="sidebar-link {% if active_special == 'favorites' %}active-sort{% endif %}">Favorites</a>
                <a href="/gallery/most-visited/" class="sidebar-link {% if active_special == 'most-visited' %}active-sort{% endif %}">Most visited</a>
                <a href="/gallery/recent/" class="sidebar-link {% if active_special == 'recent' %}active-sort{% endif %}">Recently visited</a>

                {% if path != '' or is_special %}
                <hr>
                {% endif %}

                {% if path != '' %}
                <a href="{{ back_url }}" class="subdir-item">
                    {% if back_thumb %}
                        <img src="{{ back_thumb }}" class="subdir-thumb" alt="back thumb">
                    {% else %}
                        <span class="subdir-fallback"><i class="fa-solid fa-image"></i></span>
                    {% endif %}
                    <span>Back</span>
                </a>
                {% endif %}

                {% if path != '' or is_special %}
                <a href="{{ home_url }}" class="subdir-item">
                    {% if home_thumb %}
                        <img src="{{ home_thumb }}" class="subdir-thumb" alt="home thumb">
                    {% else %}
                        <span class="subdir-fallback"><i class="fa-solid fa-image"></i></span>
                    {% endif %}
                    <span>Home</span>
                </a>
                {% endif %}

                <hr>

                <div class="sidebar-scroll" style="max-height: 200px;">
                    {% for subdir in subdirs %}
                        <a href="{{ subdir.path }}" class="subdir-item">
                            {% if subdir.thumbnail %}
                                <img src="{{ subdir.thumbnail }}" alt="{{ subdir.name }}" class="subdir-thumb">
                            {% else %}
                                <span class="subdir-fallback"><i class="fa-solid fa-image"></i></span>
                            {% endif %}
                            <span>{{ subdir.name|truncatechars:30 }}</span>
                        </a>
                    {% empty %}
                        <small class="text-secondary">No subdirectories</small>
                    {% endfor %}
                </div>

                <hr>

                <form method="post" action="{% url 'logout' %}">
                    {% csrf_token %}
                    <button type="submit" class="btn btn-sm btn-plain w-100">
                        <i class="fa-solid fa-arrow-up-from-bracket"></i>
                        <span class="ms-1">Logout</span>
                    </button>
                </form>
            </div>

            <main class="main-area d-flex flex-column flex-grow-1">
                <div class="top-bar d-flex align-items-center gap-2">
                    <button class="btn btn-sm btn-plain sidebar-toggle d-lg-none" type="button" data-bs-toggle="offcanvas" data-bs-target="#sidebarOffcanvas" aria-controls="sidebarOffcanvas" aria-label="Open menu">
                        <i class="fa-solid fa-bars"></i>
                    </button>

                    <div class="breadcrumb-wrap flex-grow-1">
                        {% for crumb in breadcrumbs %}
                            {% if not forloop.first %}
                                <span class="crumb-sep">/</span>
                            {% endif %}
                            {% if crumb.path %}
                                <a href="{{ crumb.path }}" class="{% if not forloop.last %}crumb-link{% else %}crumb-link-last{% endif %}">{{ crumb.label|truncatechars:45 }}</a>
                            {% else %}
                                <span class="{% if not forloop.last %}crumb-link{% else %}crumb-link-last{% endif %}">{{ crumb.label|truncatechars:45 }}</span>
                            {% endif %}
                        {% endfor %}
                    </div>

                    {% if not is_special %}
                    <div class="dropdown ms-auto">
                        <button class="btn btn-sm btn-plain" type="button" data-bs-toggle="dropdown" aria-expanded="false" title="{{ sort_label }}" aria-label="Sort options">
                            <i class="fa-solid fa-arrow-down-short-wide"></i>
                        </button>
                        <ul class="dropdown-menu dropdown-menu-end sort-menu">
                            {% for option in sort_options %}
                                <li>
                                    <a class="dropdown-item {% if option.is_active %}active-sort{% endif %}" href="{{ option.url }}">{{ option.label }}</a>
                                </li>
                            {% endfor %}
                        </ul>
                    </div>
                    {% endif %}
                </div>

                {% if not is_special %}
                <noscript>
                    <div class="pt-2">
                        {% for option in sort_options %}
                            <a href="{{ option.url }}" class="me-3 {% if option.is_active %}text-decoration-underline{% endif %}">{{ option.label }}</a>
                        {% endfor %}
                    </div>
                </noscript>
                {% endif %}

                <section class="gallery-scroll flex-grow-1">
                    <div class="gallery-grid">
                        {% for image in images %}
                            <a href="{{ image.path }}" class="thumb-card">
                                {% if image.thumbnail %}
                                    <img src="{{ image.thumbnail }}" alt="{{ image.name }}">
                                {% else %}
                                    <div class="thumb-fallback"><i class="fa-solid fa-image"></i></div>
                                {% endif %}
                            </a>
                        {% empty %}
                            <p class="mb-0">No images found.</p>
                        {% endfor %}
                    </div>
                </section>
            </main>
        </div>

        <div class="offcanvas offcanvas-start" tabindex="-1" id="sidebarOffcanvas" aria-labelledby="sidebarOffcanvasLabel">
            <div class="offcanvas-header pb-2">
                <div class="d-flex align-items-center user-row w-100 gap-2">
                    <span><i class="fa-solid fa-circle-user fa-lg"></i></span>
                    <span class="user-name w-100 text-truncate">{{ request.user.username }}</span>
                    <a class="btn btn-sm btn-plain" href="{{ theme_toggle_url }}" aria-label="Toggle theme">
                        {% if theme == 'dark' %}
                            <i class="fa-solid fa-sun"></i>
                        {% else %}
                            <i class="fa-solid fa-moon"></i>
                        {% endif %}
                    </a>
                    <button type="button" class="btn btn-sm btn-plain sidebar-close" data-bs-dismiss="offcanvas" aria-label="Close menu">
                        <i class="fa-solid fa-xmark"></i>
                    </button>
                </div>
            </div>
            <div class="offcanvas-body pt-0 d-flex flex-column">
                <form action="{{ search_action_url }}" method="get">
                    <div class="input-group input-group-sm">
                        <input type="search" name="search" class="form-control search-input" placeholder="Search..." value="{{ search_text }}">
                        <span class="input-group-text search-addon"><i class="fa-solid fa-magnifying-glass"></i></span>
                    </div>
                </form>

                <hr>

                <a href="/gallery/favorites/" class="sidebar-link {% if active_special == 'favorites' %}active-sort{% endif %}">Favorites</a>
                <a href="/gallery/most-visited/" class="sidebar-link {% if active_special == 'most-visited' %}active-sort{% endif %}">Most visited</a>
                <a href="/gallery/recent/" class="sidebar-link {% if active_special == 'recent' %}active-sort{% endif %}">Recently visited</a>

                {% if path != '' or is_special %}
                <hr>
                {% endif %}

                {% if path != '' %}
                <a href="{{ back_url }}" class="subdir-item">
                    {% if back_thumb %}
                        <img src="{{ back_thumb }}" class="subdir-thumb" alt="back thumb">
                    {% else %}
                        <span class="subdir-fallback"><i class="fa-solid fa-image"></i></span>
                    {% endif %}
                    <span>Back</span>
                </a>
                {% endif %}

                {% if path != '' or is_special %}
                <a href="{{ home_url }}" class="subdir-item">
                    {% if home_thumb %}
                        <img src="{{ home_thumb }}" class="subdir-thumb" alt="home thumb">
                    {% else %}
                        <span class="subdir-fallback"><i class="fa-solid fa-image"></i></span>
                    {% endif %}
                    <span>Home</span>
                </a>
                {% endif %}

                <hr>

                <div class="sidebar-scroll flex-grow-1">
                    {% for subdir in subdirs %}
                        <a href="{{ subdir.path }}" class="subdir-item">
                            {% if subdir.thumbnail %}
                                <img src="{{ subdir.thumbnail }}" alt="{{ subdir.name }}" class="subdir-thumb">
                            {% else %}
                                <span class="subdir-fallback"><i class="fa-solid fa-image"></i></span>
                            {% endif %}
                            <span>{{ subdir.name|truncatechars:30 }}</span>
                        </a>
                    {% empty %}
                        <small class="text-secondary">No subdirectories</small>
                    {% endfor %}
                </div>

                <hr>

                <form method="post" action="{% url 'logout' %}">
                    {% csrf_token %}
                    <button type="submit" class="btn btn-sm btn-plain w-100">
                        <i class="fa-solid fa-arrow-up-from-bracket"></i>
                        <span class="ms-1">Logout</span>
                    </button>
                </form>
            </div>
        </div>

        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
    </body>
</html>
