<!DOCTYPE html>
{% macro file_upload_form(card_style=false) %}
<form action="{{ url_for('data.upload_files', projectname=project.name) }}" method="POST" enctype="multipart/form-data"
    class="{% if not card_style %}d-inline{% endif %} js-tab-submit" id="fileUploadLocal">
    <input type="file" id="fileInput" name="files" multiple webkitdirectory class="d-none">
    {% if card_style %}
    <button type="button" class="btn btn-link text-decoration-none p-0 w-100 h-100 text-start" id="triggerFileInput">
        <div class="card h-100 border-2 upload-option-card" style="transition: all 0.2s ease;">
            <div class="card-body text-center d-flex flex-column justify-content-center py-5">
                <div class="mb-3">
                    <i class="bi bi-laptop text-primary" style="font-size: 3rem;"></i>
                </div>
                <h5 class="card-title text-dark">Local Files</h5>
                <p class="card-text text-muted small mb-3">Upload files and folders directly from your computer</p>
                <div class="mt-auto">
                    <span class="btn btn-primary">
                        Browse
                    </span>
                </div>
            </div>
        </div>
    </button>
    {% else %}
    <button type="button" class="dropdown-item d-flex align-items-center" id="triggerFileInput">
        <i class="bi bi-laptop me-2 text-primary"></i> Local Files
    </button>
    {% endif %}
</form>
{% endmacro %}

{% block content %}
{% set has_files = project.list_files_and_folders()|length > 0 %}
<div class="container py-4">
    <div class="row">
        <!-- Form Title -->
        <div class="col-12 text-center mb-4">
            <h1 class="fs-3 fw-bold">Import Your Data</h1>
            <p class="text-muted">Choose how you'd like to import your TEI files</p>
            {% if error_message %}
            <div class="alert alert-danger" role="alert">
                {{ error_message }}
            </div>
            {% endif %}
        </div>

        {% if not has_files %}
        <!-- Upload options cards when no files exist -->
        <div class="col-12">
            <div class="row g-4 justify-content-center">
                <!-- Local Files Card -->
                <div class="col-md-4">
                    {{ file_upload_form(card_style=true) }}
                </div>

                <!-- GitHub Card -->
                <div class="col-md-4">
                    <button class="load-modal btn btn-link text-decoration-none p-0 w-100 h-100 text-start"
                        data-url="{{ url_for('data.modal_github', projectname=project.name) }}">
                        <div class="card h-100 border-2 upload-option-card" style="transition: all 0.2s ease;">
                            <div class="card-body text-center d-flex flex-column justify-content-center py-5">
                                <div class="mb-3">
                                    <i class="bi bi-github text-dark" style="font-size: 3rem;"></i>
                                </div>
                                <h5 class="card-title text-dark">GitHub</h5>
                                <p class="card-text text-muted small mb-3">Clone a repository from GitHub</p>
                                <div class="mt-auto">
                                    <span class="btn btn-dark">
                                        Connect
                                    </span>
                                </div>
                            </div>
                        </div>
                    </button>
                </div>

                <!-- Nextcloud Card -->
                <div class="col-md-4">
                    <button class="load-modal btn btn-link text-decoration-none p-0 w-100 h-100 text-start"
                        data-url="{{ url_for('data.modal_nextcloud', projectname=project.name) }}">
                        <div class="card h-100 border-2 upload-option-card" style="transition: all 0.2s ease;">
                            <div class="card-body text-center d-flex flex-column justify-content-center py-5">
                                <div class="mb-3">
                                    <i class="bi bi-cloud text-info" style="font-size: 3rem;"></i>
                                </div>
                                <h5 class="card-title text-dark">Nextcloud</h5>
                                <p class="card-text text-muted small mb-3">Import from your Nextcloud storage</p>
                                <div class="mt-auto">
                                    <span class="btn btn-info text-white">
                                        Connect
                                    </span>
                                </div>
                            </div>
                        </div>
                    </button>
                </div>
            </div>
        </div>

        {% else %}
        <!-- File display when files exist -->
        <div class="col-12">
            <div class="card">
                <!-- Card header with title and upload buttons -->
                <div class="card-header d-flex justify-content-between align-items-center">
                    <span class="fw-semibold"><i class="bi bi-folder2-open me-2"></i>Imported Data</span>
                    <div class="dropdown">
                        <button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button"
                            data-bs-toggle="dropdown" aria-expanded="false">
                            Add More
                        </button>
                        <ul class="dropdown-menu dropdown-menu-end shadow-sm">
                            <li>
                                {{ file_upload_form(card_style=false) }}
                            </li>
                            <li>
                                <button type="button" class="load-modal dropdown-item d-flex align-items-center"
                                    data-url="{{ url_for('data.modal_github', projectname=project.name) }}">
                                    <i class="bi bi-github me-2"></i> GitHub
                                </button>
                            </li>
                            <li>
                                <button type="button" class="load-modal dropdown-item d-flex align-items-center"
                                    data-url="{{ url_for('data.modal_nextcloud', projectname=project.name) }}">
                                    <i class="bi bi-cloud me-2 text-info"></i> Nextcloud
                                </button>
                            </li>
                        </ul>
                    </div>
                </div>
                <!-- List of files and folders -->
                <div class="list-group list-group-flush">
                    {% for item in project.list_files_and_folders() %}
                    <div class="list-group-item list-group-item-action d-flex align-items-center">
                        <!-- Different display for folders and files -->
                        {% if item.type == 'folder' %}
                        <button
                            class="btn btn-link text-decoration-none text-dark p-0 d-flex align-items-center flex-grow-1"
                            data-bs-toggle="modal" data-bs-target="#folderModal{{ loop.index }}">
                            <i class="bi bi-folder-fill text-warning me-2"></i>
                            <span>{{ item.name }}</span>
                        </button>
                        {% else %}
                        <i class="bi bi-file-text me-2"></i>
                        <span class="flex-grow-1">{{ item.name }}</span>
                        {% endif %}

                        <!-- Action buttons for each item -->
                        <div class="btn-group">
                            <button class="btn btn-sm btn-outline-danger delete-folder" data-item-path="{{ item.path }}"
                                data-item-name="{{ item.name }}" data-projectname="{{ project.name }}"
                                data-item-type="{{ item.type }}">
                                <i class="bi bi-trash"></i>
                            </button>
                        </div>
                    </div>
                    {% endfor %}
                </div>

                <!-- Folder modals - rendered at the end of file -->
                {% for item in project.list_files_and_folders() %}
                {% if item.type == 'folder' %}
                {% with folder=item, index=loop.index %}
                {% include 'modal/file_explorer_main.html' %}
                {% endwith %}
                {% endif %}
                {% endfor %}
            </div>
        </div>
        {% endif %}

        {% if has_files %}
        <div class="row mt-4">
            <div class="col-12 text-end">
                <button type="button" class="btn btn-primary btn-next" data-next-target="#tab-select-directories"
                    data-additional-button="#tab-group-collections">
                    <i class="bi bi-chevron-right"></i> Next
                </button>
            </div>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}