{% extends 'base.html' %}

{% block title %}IvoryOS | Design Database{% endblock %}
{% block body %}
<!-- Header: Deck Filter & Search -->
<div class="d-flex justify-content-between align-items-center mb-3">
    <!-- Deck Filter Dropdown -->
    <div class="dropdown">
        <button class="btn btn-primary dropdown-toggle" type="button" id="deckFilterDropdown" data-bs-toggle="dropdown"
            aria-expanded="false">
            Deck: {{ current_deck_name if current_deck_name and current_deck_name != 'ALL' else 'All Decks' }}
        </button>
        <ul class="dropdown-menu" aria-labelledby="deckFilterDropdown">
            {% for deck in deck_list %}
            <li>
                <a class="dropdown-item {% if deck == current_deck_name %}active{% endif %}"
                    href="{{ url_for('library.load_from_database', deck_name=deck) }}">
                    {{ deck }}
                </a>
            </li>
            {% endfor %}
        </ul>
    </div>

    <!-- Search Form -->
    <form id="search" class="d-flex" method="GET"
        action="{{ url_for('library.load_from_database', deck_name=current_deck_name or 'ALL') }}">
        <div class="input-group">
            <input type="search" name="keyword" id="keyword" class="form-control" placeholder="Search workflows..."
                value="{{ request.args.get('keyword', '') }}">
            <button type="submit" class="btn btn-primary">
                <i class="bi bi-search"></i>
            </button>
        </div>
    </form>
</div>

<table class="table table-hover align-middle" id="workflowLibrary">
    <thead class="table-light">
        <tr>
            <th scope="col">
                <a href="{{ url_for('library.load_from_database', deck_name=current_deck_name, keyword=request.args.get('keyword', ''), sort_by='name', order='desc' if current_sort_by == 'name' and current_order == 'asc' else 'asc') }}"
                    class="text-dark text-decoration-none">
                    Workflow name {% if current_sort_by == 'name' %}<i
                        class="bi bi-arrow-{{ 'up' if current_order == 'asc' else 'down' }}"></i>{% endif %}
                </a>
            </th>
            <th scope="col">
                <a href="{{ url_for('library.load_from_database', deck_name=current_deck_name, keyword=request.args.get('keyword', ''), sort_by='deck', order='desc' if current_sort_by == 'deck' and current_order == 'asc' else 'asc') }}"
                    class="text-dark text-decoration-none">
                    Deck {% if current_sort_by == 'deck' %}<i
                        class="bi bi-arrow-{{ 'up' if current_order == 'asc' else 'down' }}"></i>{% endif %}
                </a>
            </th>
            <th scope="col">Description</th>
            <th scope="col">
                <a href="{{ url_for('library.load_from_database', deck_name=current_deck_name, keyword=request.args.get('keyword', ''), sort_by='status', order='desc' if current_sort_by == 'status' and current_order == 'asc' else 'asc') }}"
                    class="text-dark text-decoration-none">
                    Status {% if current_sort_by == 'status' %}<i
                        class="bi bi-arrow-{{ 'up' if current_order == 'asc' else 'down' }}"></i>{% endif %}
                </a>
            </th>
            <th scope="col">
                <a href="{{ url_for('library.load_from_database', deck_name=current_deck_name, keyword=request.args.get('keyword', ''), sort_by='created', order='asc' if current_sort_by == 'created' and current_order == 'desc' else 'desc') }}"
                    class="text-dark text-decoration-none">
                    Created {% if current_sort_by == 'created' %}<i
                        class="bi bi-arrow-{{ 'up' if current_order == 'asc' else 'down' }}"></i>{% endif %}
                </a>
            </th>
            <th scope="col">
                <a href="{{ url_for('library.load_from_database', deck_name=current_deck_name, keyword=request.args.get('keyword', ''), sort_by='modified', order='asc' if current_sort_by == 'modified' and current_order == 'desc' else 'desc') }}"
                    class="text-dark text-decoration-none">
                    Modified {% if current_sort_by == 'modified' %}<i
                        class="bi bi-arrow-{{ 'up' if current_order == 'asc' else 'down' }}"></i>{% endif %}
                </a>
            </th>
            <th scope="col">
                <a href="{{ url_for('library.load_from_database', deck_name=current_deck_name, keyword=request.args.get('keyword', ''), sort_by='author', order='desc' if current_sort_by == 'author' and current_order == 'asc' else 'asc') }}"
                    class="text-dark text-decoration-none">
                    Author {% if current_sort_by == 'author' %}<i
                        class="bi bi-arrow-{{ 'up' if current_order == 'asc' else 'down' }}"></i>{% endif %}
                </a>
            </th>
            <th scope="col">
                <a href="{{ url_for('library.load_from_database', deck_name=current_deck_name, keyword=request.args.get('keyword', ''), sort_by='registered', order='asc' if current_sort_by == 'registered' and current_order == 'desc' else 'desc') }}"
                    class="text-dark text-decoration-none">
                    Registered {% if current_sort_by == 'registered' %}<i
                        class="bi bi-arrow-{{ 'up' if current_order == 'asc' else 'down' }}"></i>{% endif %}
                </a>
            </th>
            <th scope="col"></th>
        </tr>
    </thead>
    <tbody>
        {% for script in scripts %}
        <tr>
            <td>
                <a href="{{ url_for('library.workflow_script', script_name=script.name) }}" title="{{ script.name }}">{{
                    script.name }}</a>
            </td>
            <td>{{ script.deck }}</td>
            <td>{{ script.description }}</td>
            <td>{{ script.status }}</td>
            <td>{{ script.time_created }}</td>
            <td>{{ script.last_modified }}</td>
            <td>{{ script.author }}</td>
            <td>{{ script.registered }}</td>
            <td>
                {#not workflow.status == "finalized" or#}
                {% set username = current_user.get_id() %}
                {% if username == 'admin' or username == script.author %}
                <a href="#" class="text-danger"
                    data-delete-url="{{ url_for('library.workflow_script', script_name=script.name) }}"
                    onclick="deleteWorkflow(this); return false;">
                    Delete
                </a>
                {% else %}
                <a class="disabled-link">delete</a>
                {% endif %}
            <td>
        </tr>
        {% endfor %}
    </tbody>
</table>

{# paging#}
<div class="pagination justify-content-center">
    <div class="page-item {{ 'disabled' if not scripts.has_prev else '' }}">
        <a class="page-link"
            href="{{ url_for('library.load_from_database', page=scripts.prev_num, deck_name=current_deck_name, keyword=request.args.get('keyword', ''), sort_by=current_sort_by, order=current_order) }}">Previous</a>
    </div>

    {% for num in scripts.iter_pages() %}
    {% if num %}
    <div class="page-item {{ 'active' if num == scripts.page else '' }}">
        <a class="page-link"
            href="{{ url_for('library.load_from_database', page=num, deck_name=current_deck_name, keyword=request.args.get('keyword', ''), sort_by=current_sort_by, order=current_order) }}">{{
            num }}</a>
    </div>
    {% else %}
    <div class="page-item disabled">
        <span class="page-link">…</span>
    </div>
    {% endif %}
    {% endfor %}

    <div class="page-item {{ 'disabled' if not scripts.has_next else '' }}">
        <a class="page-link"
            href="{{ url_for('library.load_from_database', page=scripts.next_num, deck_name=current_deck_name, keyword=request.args.get('keyword', ''), sort_by=current_sort_by, order=current_order) }}">Next</a>
    </div>
</div>
<script src="{{ url_for('static', filename='js/db_delete.js') }}"></script>
{% endblock %}