{#
Project: EPT-MX-ADM
Company: EasyProTech LLC (www.easypro.tech)
Dev: Brabus
Date: Thu 23 Oct 2025 22:41:32 UTC
Status: Created
Telegram: https://t.me/EasyProTech
#}

{% extends "base.html" %}

{% block title %}{{ t('media.title') }} - {{ t('app.name') }}{% endblock %}

{% block content %}
<div class="fade-in-up">
    <!-- Header with buttons -->
    <div class="header-section mb-4">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h1 class="h2 text-gradient mb-0">
                <i class="bi bi-file-earmark-play me-2"></i>{{ t('media.title') }}
                {% if total %}
                <span class="badge bg-primary ms-2">{{ total }}</span>
                {% endif %}
            </h1>
            <div class="header-actions">
                <button type="button" class="btn btn-outline-primary me-2" onclick="window.location.reload()">
                    <i class="bi bi-arrow-clockwise me-1"></i>{{ t('dashboard.refresh') }}
                </button>
                <a href="{{ url_for('media.export_users_media', search=search) }}" class="btn btn-success">
                    <i class="bi bi-file-earmark-arrow-down me-1"></i>{{ t('media.csv_export') }}
                </a>
            </div>
        </div>
        
        <!-- Statistics cards -->
        <div class="row g-3 mb-4">
            <div class="col-md-4">
                <div class="card border-0 shadow-sm">
                    <div class="card-body text-center">
                        <div class="d-flex align-items-center justify-content-center mb-2">
                            <div class="p-2 rounded-circle bg-primary bg-opacity-10">
                                <i class="bi bi-file-earmark-play text-primary fs-4"></i>
                            </div>
                        </div>
                        <h3 class="fw-bold mb-1">{{ media_stats.total_media_files or 0 }}</h3>
                        <p class="text-muted mb-0 small">{{ t('media.total_files') }}</p>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card border-0 shadow-sm">
                    <div class="card-body text-center">
                        <div class="d-flex align-items-center justify-content-center mb-2">
                            <div class="p-2 rounded-circle bg-success bg-opacity-10">
                                <i class="bi bi-hdd text-success fs-4"></i>
                            </div>
                        </div>
                        <h3 class="fw-bold mb-1">{{ media_stats.total_media_size_formatted or '0 B' }}</h3>
                        <p class="text-muted mb-0 small">{{ t('media.total_size') }}</p>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card border-0 shadow-sm">
                    <div class="card-body text-center">
                        <div class="d-flex align-items-center justify-content-center mb-2">
                            <div class="p-2 rounded-circle bg-info bg-opacity-10">
                                <i class="bi bi-people text-info fs-4"></i>
                            </div>
                        </div>
                        <h3 class="fw-bold mb-1">{{ media_stats.users_with_media or 0 }}</h3>
                        <p class="text-muted mb-0 small">{{ t('media.users_with_media') }}</p>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Filters and search -->
        <div class="card">
            <div class="card-header">
                <i class="bi bi-funnel me-2"></i>
                <span>Filters & Search</span>
            </div>
            <div class="card-body">
                <form method="GET" id="filtersForm">
                    <div class="row g-3 align-items-end">
                        <div class="col-md-6">
                            <label class="form-label">{{ t('forms.search') }}</label>
                            <div class="search-box">
                                <i class="bi bi-search search-icon"></i>
                                <input type="text" class="form-control" name="search" value="{{ search }}" 
                                       placeholder="Search by user ID...">
                            </div>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">{{ t('pagination.rows_per_page') }}</label>
                            <select class="form-select" onchange="changeLimit(this.value)">
                                <option value="10" {{ 'selected' if limit == 10 else '' }}>10</option>
                                <option value="25" {{ 'selected' if limit == 25 else '' }}>25</option>
                                <option value="50" {{ 'selected' if limit == 50 else '' }}>50</option>
                                <option value="100" {{ 'selected' if limit == 100 else '' }}>100</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <button type="submit" class="btn btn-primary w-100">
                                <i class="bi bi-search me-1"></i>{{ t('forms.search') }}
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Users with media list -->
    {% if users %}
        <div class="users-grid">
        {% for user in users %}
        <div class="card">
            <div class="card-header d-flex align-items-center">
                <div class="user-avatar me-3">
                    <div class="avatar-circle">
                        <i class="bi bi-person-fill"></i>
                    </div>
                </div>
                <div class="flex-grow-1" style="min-width: 0;">
                    <div class="fw-semibold text-truncate">{{ user.user_id }}</div>
                    {% if user.display_name %}
                    <div class="text-muted small text-truncate">{{ user.display_name }}</div>
                    {% endif %}
                </div>
            </div>
            
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-6">
                        <div class="stat-item">
                            <div class="stat-label">
                                <i class="bi bi-file-earmark-play me-1"></i>
                                Media Files
                            </div>
                            <div class="stat-value">{{ user.media_count }}</div>
                        </div>
                    </div>
                    <div class="col-6">
                        <div class="stat-item">
                            <div class="stat-label">
                                <i class="bi bi-hdd me-1"></i>
                                Storage Used
                            </div>
                            <div class="stat-value">{{ user.media_length_formatted }}</div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="card-footer d-flex gap-2 justify-content-end">
                <a href="{{ url_for('media.user_media', user_id=user.user_id) }}" 
                   class="btn btn-outline-primary btn-sm">
                    <i class="bi bi-eye me-1"></i>View Media
                </a>
            </div>
        </div>
        {% endfor %}
        </div>
    {% else %}
        <div class="empty-state">
            <div class="empty-icon">
                <i class="bi bi-file-earmark-play"></i>
            </div>
            <h5>{{ t('media.no_media_found', default='No users with media found') }}</h5>
            {% if search %}
            <p>{{ t('messages.try_different_search') }}</p>
            <a href="{{ url_for('media.users_media') }}" class="btn btn-outline-primary">
                {{ t('forms.show_all') }}
            </a>
            {% endif %}
        </div>
    {% endif %}

    <!-- Pagination -->
    {% if total > 0 %}
    <div class="d-flex justify-content-between align-items-center mt-4">
        <div class="pagination-text">
            {{ t('pagination.showing') }} 
            {{ ((current_page - 1) * limit + 1) if total > 0 else 0 }} - 
            {{ (current_page * limit) if (current_page * limit) < total else total }} 
            {{ t('pagination.of') }} {{ total }} {{ t('pagination.entries') }}
        </div>
        
        {% if total_pages > 1 %}
        <nav aria-label="Users media pagination">
            <ul class="pagination mb-0">
                {% if current_page > 1 %}
                <li class="page-item">
                    <a class="page-link" href="#" onclick="goToPage(1)">
                        <i class="bi bi-chevron-double-left"></i>
                    </a>
                </li>
                <li class="page-item">
                    <a class="page-link" href="#" onclick="goToPagePrev()">
                        <i class="bi bi-chevron-left"></i>
                    </a>
                </li>
                {% endif %}
                
                {% set start_page = [1, current_page - 2] | max %}
                {% set end_page = [total_pages, current_page + 2] | min %}
                
                {% if start_page > 1 %}
                <li class="page-item disabled">
                    <span class="page-link">...</span>
                </li>
                {% endif %}
                
                {% for page_num in range(start_page, end_page + 1) %}
                <li class="page-item {% if page_num == current_page %}active{% endif %}">
                    <a class="page-link" href="#" onclick="goToPage({{ page_num }})">
                        {{ page_num }}
                    </a>
                </li>
                {% endfor %}
                
                {% if end_page < total_pages %}
                <li class="page-item disabled">
                    <span class="page-link">...</span>
                </li>
                {% endif %}
                
                {% if current_page < total_pages %}
                <li class="page-item">
                    <a class="page-link" href="#" onclick="goToPageNext()">
                        <i class="bi bi-chevron-right"></i>
                    </a>
                </li>
                <li class="page-item">
                    <a class="page-link" href="#" onclick="goToPageLast()">
                        <i class="bi bi-chevron-double-right"></i>
                    </a>
                </li>
                {% endif %}
            </ul>
        </nav>
        {% endif %}
    </div>
    {% endif %}
</div>
{% endblock %}

{% block scripts %}
<script>
// Change limit and reset to page 1
function changeLimit(newLimit) {
    const url = new URL(window.location);
    url.searchParams.set('limit', newLimit);
    url.searchParams.set('page', '1');
    window.location.href = url.toString();
}

// Pagination functions
function goToPage(page) {
    const url = new URL(window.location);
    url.searchParams.set('page', page);
    window.location.href = url.toString();
}

function goToPagePrev() {
    const currentPage = parseInt(new URLSearchParams(window.location.search).get('page')) || 1;
    if (currentPage > 1) {
        goToPage(currentPage - 1);
    }
}

function goToPageNext() {
    const currentPage = parseInt(new URLSearchParams(window.location.search).get('page')) || 1;
    const totalPages = {{ total_pages }};
    if (currentPage < totalPages) {
        goToPage(currentPage + 1);
    }
}

function goToPageLast() {
    const totalPages = {{ total_pages }};
    goToPage(totalPages);
}

// Refresh button
document.querySelector('[data-refresh]')?.addEventListener('click', function() {
    window.location.reload();
});
</script>
{% endblock %}

