{% extends "layouts/admin.twig" %}

{% block title %}{{ ta.content|default('Content') }} - {{ ta.admin_title|default('CodePress Admin') }}{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="bi bi-file-earmark-text"></i> {{ ta.content|default('Content') }}</h2>
    <div>
        <button type="button" class="btn btn-outline-success btn-sm me-1" data-bs-toggle="collapse" data-bs-target="#uploadForm">
            <i class="bi bi-cloud-upload"></i> {{ ta.upload|default('Upload') }}
        </button>
        <button type="button" class="btn btn-outline-secondary btn-sm me-1" data-bs-toggle="modal" data-bs-target="#createDirModal">
            <i class="bi bi-folder-plus"></i> {{ ta.new_folder|default('Nieuwe map') }}
        </button>
        <a href="/admin/content-backup" class="btn btn-outline-info btn-sm me-1">
            <i class="bi bi-archive"></i> {{ ta.backup|default('Backup') }}
        </a>
        <a href="/admin/content-new?dir={{ subdir|url_encode }}" class="btn btn-primary btn-sm">
            <i class="bi bi-plus-lg"></i> {{ ta.new_file|default('Nieuw bestand') }}
        </a>
    </div>
</div>

<div class="collapse mb-4" id="uploadForm">
    <div class="card shadow-sm">
        <div class="card-body">
            <form method="POST" action="/admin/content?dir={{ subdir|url_encode }}" enctype="multipart/form-data">
                <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                <div class="mb-3">
                    <label for="file" class="form-label">{{ ta.select_files|default('Bestanden selecteren') }}</label>
                    <input type="file" class="form-control" id="file" name="file[]" multiple accept="image/jpeg,image/png,image/gif,image/webp,image/svg+xml,application/pdf,application/zip,video/mp4,video/webm,audio/mpeg,audio/wav">
                    <small class="form-text text-muted">{{ ta.allowed_types|default('Toegestaan: JPG, PNG, GIF, WebP, SVG, PDF, ZIP, MP4, WebM, MP3, WAV') }}</small>
                </div>
                <button type="submit" class="btn btn-success">
                    <i class="bi bi-cloud-upload"></i> {{ ta.upload_to_folder|default('Uploaden naar deze map') }}
                </button>
            </form>
        </div>
    </div>
</div>

{% if subdir %}
    {% set parentDir = subdir|split('/')|slice(0, -1)|join('/') %}
    <nav aria-label="breadcrumb" class="mb-3">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="/admin/content"><i class="bi bi-house"></i></a></li>
            {% set crumbPath = '' %}
            {% for crumb in subdir|split('/') %}
                {% set crumbPath = crumbPath ? crumbPath ~ '/' ~ crumb : crumb %}
                <li class="breadcrumb-item {{ crumbPath == subdir ? 'active' : '' }}">
                    {% if crumbPath == subdir %}
                        {{ crumb }}
                    {% else %}
                        <a href="/admin/content?dir={{ crumbPath|url_encode }}">{{ crumb }}</a>
                    {% endif %}
                </li>
            {% endfor %}
        </ol>
    </nav>
{% endif %}

<div class="card shadow-sm">
    <div class="card-header bg-white py-2">
        <div class="input-group input-group-sm">
            <span class="input-group-text bg-white border-end-0"><i class="bi bi-search text-muted"></i></span>
            <input type="search" id="contentFilter" class="form-control border-start-0" placeholder="{{ ta.filter_placeholder|default('Filter op bestands- of mapnaam…') }}" autocomplete="off" aria-label="{{ ta.filter_content|default('Filter content') }}">
            <span class="input-group-text bg-white text-muted" id="contentFilterCount"></span>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table table-hover mb-0">
            <thead>
                <tr>
                    <th>{{ ta.col_name|default('Naam') }}</th>
                    <th>{{ ta.col_type|default('Type') }}</th>
                    <th>{{ ta.col_size|default('Grootte') }}</th>
                    <th>{{ ta.col_modified|default('Gewijzigd') }}</th>
                    <th style="width: 200px;">{{ ta.col_actions|default('Acties') }}</th>
                </tr>
            </thead>
            <tbody>
                {% if items is empty %}
                    <tr><td colspan="5" class="text-muted text-center py-4">{{ ta.no_files_found|default('Geen bestanden gevonden.') }}</td></tr>
                {% else %}
                    {% for item in items %}
                        <tr data-name="{{ item.name|lower }}">
                            <td>
                                {% if item.is_dir %}
                                    <a href="/admin/content?dir={{ item.path|url_encode }}">
                                        <i class="bi bi-folder-fill text-warning"></i> {{ item.name }}
                                    </a>
                                {% else %}
                                    <a href="/admin/content-edit?file={{ item.path|url_encode }}">
                                        {% set icon = item.extension == 'md' ? 'bi-file-text text-primary' : (item.extension == 'php' ? 'bi-file-code text-success' : (item.extension == 'html' ? 'bi-file-earmark text-info' : 'bi-file text-muted')) %}
                                        <i class="bi {{ icon }}"></i> {{ item.name }}
                                    </a>
                                {% endif %}
                            </td>
                            <td>
                                {% if item.is_dir %}
                                    <span class="badge bg-warning text-dark">{{ ta.folder_badge|default('Map') }}</span>
                                {% else %}
                                    <span class="badge bg-secondary">{{ item.extension|upper }}</span>
                                {% endif %}
                            </td>
                            <td class="text-muted">{{ item.size }}</td>
                            <td class="text-muted">{{ item.modified }}</td>
                            <td>
                                {% if item.is_dir %}
                                    <a href="/admin/content-dir-rename?dir={{ item.path|url_encode }}" class="btn btn-sm btn-outline-secondary" title="{{ ta.rename|default('Hernoemen') }}">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                    <a href="/admin/content-move?item={{ item.path|url_encode }}" class="btn btn-sm btn-outline-info" title="{{ ta.move|default('Verplaatsen') }}">
                                        <i class="bi bi-arrows-move"></i>
                                    </a>
                                    <form method="POST" action="/admin/content-dir-delete?dir={{ item.path|url_encode }}" class="d-inline" onsubmit="return confirm('{{ ta.confirm_delete_folder|default('Weet je zeker dat je deze map wilt verwijderen? De map moet leeg zijn.') }}')">
                                        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                                        <button type="submit" class="btn btn-sm btn-outline-danger" title="{{ ta.delete|default('Verwijderen') }}">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </form>
                                {% else %}
                                    <a href="/admin/content-edit?file={{ item.path|url_encode }}" class="btn btn-sm btn-outline-primary" title="{{ ta.edit|default('Bewerken') }}">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                    <a href="/admin/content-move?item={{ item.path|url_encode }}" class="btn btn-sm btn-outline-info" title="{{ ta.move|default('Verplaatsen') }}">
                                        <i class="bi bi-arrows-move"></i>
                                    </a>
                                    <form method="POST" action="/admin/content-delete?file={{ item.path|url_encode }}" class="d-inline" onsubmit="return confirm('{{ ta.confirm_delete_file|default('Weet je zeker dat je dit bestand wilt verwijderen?') }}')">
                                        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                                        <button type="submit" class="btn btn-sm btn-outline-danger" title="{{ ta.delete|default('Verwijderen') }}">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </form>
                                {% endif %}
                            </td>
                        </tr>
                    {% endfor %}
                {% endif %}
                <tr id="contentFilterEmpty" class="d-none">
                    <td colspan="5" class="text-muted text-center py-4">{{ ta.no_filter_results|default('Geen resultaten voor deze filter.') }}</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

<script>
(function () {
    var input = document.getElementById('contentFilter');
    var counter = document.getElementById('contentFilterCount');
    var emptyRow = document.getElementById('contentFilterEmpty');
    if (!input) return;

    var rows = Array.prototype.slice.call(document.querySelectorAll('tbody tr[data-name]'));

    function apply() {
        var q = input.value.trim().toLowerCase();
        var shown = 0;

        rows.forEach(function (row) {
            var match = q === '' || row.getAttribute('data-name').indexOf(q) !== -1;
            row.classList.toggle('d-none', !match);
            if (match) shown++;
        });

        if (emptyRow) {
            emptyRow.classList.toggle('d-none', shown > 0 || rows.length === 0);
        }
        if (counter) {
            counter.textContent = q === '' ? rows.length + ' items' : shown + ' van ' + rows.length;
        }
    }

    input.addEventListener('input', apply);
    input.addEventListener('keydown', function (e) {
        if (e.key === 'Escape') {
            input.value = '';
            apply();
        }
    });

    apply();
})();
</script>

<!-- Create Directory Modal -->
<div class="modal fade" id="createDirModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form method="POST" action="/admin/content-dir-create?dir={{ subdir|url_encode }}">
                <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                <div class="modal-header">
                    <h5 class="modal-title"><i class="bi bi-folder-plus"></i> {{ ta.new_folder_title|default('Nieuwe map aanmaken') }}</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="dirname" class="form-label">{{ ta.folder_name|default('Mapnaam') }}</label>
                        <input type="text" class="form-control" id="dirname" name="dirname" required autofocus>
                        <div class="form-text">{{ ta.name_help|default('Alleen letters, cijfers, punten, underscores en streepjes.') }}</div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{{ ta.cancel|default('Annuleren') }}</button>
                    <button type="submit" class="btn btn-primary"><i class="bi bi-check-lg"></i> {{ ta.create|default('Aanmaken') }}</button>
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}