{# templates/file_access.html #}
{% extends 'base.html' %}

{# page title #}
{% block title %}Dateizugriffe{% endblock %}
{% block page_id %}file_access{% endblock %}

{# page content #}
{% block content %}

    <!-- Main Container -->
    <div class="container">
      <h2>Top 20 Dateizugriffe</h2>

      <!-- Dropdown Controls -->
      <div class="mb-4 d-flex flex-wrap gap-2">
        <!-- Timeframe Dropdown -->
        <div class="dropdown">
          <button class="btn btn-secondary dropdown-toggle" 
                  type="button" id="timeframeDropdown" data-bs-toggle="dropdown" aria-expanded="false">
            {% if session['timeframe'] == 'last24hours' %}
              Last 24 Hours
            {% elif session['timeframe'] == '7days' %}
              Last 7 Days
            {% elif session['timeframe'] == '14days' %}
              Last 14 Days
            {% elif session['timeframe'] == '30days' %}
              Last 30 Days
            {% elif session['timeframe'] == '365days' %}
              Last 365 Days
            {% else %}
              Select Timeframe
            {% endif %}
          </button>
          <ul class="dropdown-menu" aria-labelledby="timeframeDropdown">
            <li>
              <a class="dropdown-item {% if session['timeframe'] == 'last24hours' %}active{% endif %}" 
                href="{{ url_for('file_access', timeframe='last24hours', category=selected_category) }}">
                Last 24 Hours
              </a>
            </li>
            <li>
              <a class="dropdown-item {% if session['timeframe'] == '7days' %}active{% endif %}" 
                href="{{ url_for('file_access', timeframe='7days', category=selected_category) }}">
                Last 7 Days
              </a>
            </li>
            <li>
              <a class="dropdown-item {% if session['timeframe'] == '14days' %}active{% endif %}" 
                href="{{ url_for('file_access', timeframe='14days', category=selected_category) }}">
                Last 14 Days
              </a>
            </li>
            <li>
              <a class="dropdown-item {% if session['timeframe'] == '30days' %}active{% endif %}" 
                href="{{ url_for('file_access', timeframe='30days', category=selected_category) }}">
                Last 30 Days
              </a>
            </li>
            <li>
              <a class="dropdown-item {% if session['timeframe'] == '365days' %}active{% endif %}" 
                href="{{ url_for('file_access', timeframe='365days', category=selected_category) }}">
                Last 365 Days
              </a>
            </li>
          </ul>
        </div>
        <!-- Category Dropdown -->
        {% if categories %}
        <div class="dropdown">
          <button class="btn btn-secondary dropdown-toggle" 
                  type="button" id="categoryDropdown" data-bs-toggle="dropdown" aria-expanded="false">
            {{ selected_category_label }}
          </button>
          <ul class="dropdown-menu" aria-labelledby="categoryDropdown">
            {% for option in categories %}
            <li>
              <a class="dropdown-item {% if selected_category == option.value %}active{% endif %}" 
                href="{{ url_for('file_access', timeframe=session['timeframe'], category=option.value) }}">
                {{ option.label }}
              </a>
            </li>
            {% endfor %}
          </ul>
        </div>
        {% endif %}
      </div>


      <!-- Detailed Table of Top File Accesses -->
      <div class="card mb-4">
        <div class="card-header">{{ selected_category_label }}</div>
        <div class="card-body">
          {% if top20_files %}
          <div class="table-responsive">
            <table class="table table-striped" id="file-access-table">
              <thead>
                <tr>
                  <th>Access Count</th>
                  <th>File Path</th>
                  <th>Aktion</th>
                </tr>
              </thead>
              <tbody>
                {% for row in top20_files %}
                {% set parent_path = row.rel_path.rsplit('/', 1)[0] if '/' in row.rel_path else '' %}
                <tr>
                  <td>{{ row.access_count }}</td>
                  <td>{{ row.rel_path }}</td>
                  <td>
                    <div class="d-flex gap-2 file-access-actions">
                      <button class="btn btn-light btn-sm play-access-btn" data-path="{{ row.rel_path | e }}" title="Play" aria-label="Play">
                        <i class="bi bi-volume-up"></i>
                      </button>
                      <button class="btn btn-light btn-sm folder-open-btn" data-folder="{{ parent_path | e }}" data-file="{{ row.rel_path | e }}" title="Ordner öffnen" aria-label="Ordner öffnen">
                        <i class="bi bi-folder"></i>
                      </button>
                    </div>
                  </td>
                </tr>
                {% endfor %}
              </tbody>
            </table>
          </div>
          {% else %}
          <p class="mb-0">No data available for the selected timeframe and category.</p>
          {% endif %}
        </div>
      </div>
    </div>
{% endblock %}
    
