{% extends 'layouts/base.html' %}
{% load static %}

{% block title %}Gerenciamento de Mídia{% endblock %}

{% block extra_css %}
<style>
.media-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 1.5rem;
    margin-top: 2rem;
    padding: 0 0.5rem;
}

.media-card {
    border: none;
    border-radius: 12px;
    overflow: hidden;
    transition: all 0.3s ease;
    background: white;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    position: relative;
}

.media-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 25px rgba(0,0,0,0.15);
}

.media-preview {
    width: 100%;
    height: 220px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
    position: relative;
    overflow: hidden;
}

.media-preview img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s ease;
}

.media-card:hover .media-preview img {
    transform: scale(1.05);
}

.media-preview video {
    max-width: 100%;
    max-height: 100%;
}

.media-preview .file-icon {
    text-align: center;
    transition: transform 0.3s ease;
}

.media-card:hover .media-preview .file-icon {
    transform: scale(1.1);
}

.media-info {
    padding: 1.25rem;
    background: white;
}

.media-title {
    font-weight: 600;
    margin-bottom: 0.75rem;
    font-size: 1rem;
    color: #2c3e50;
    line-height: 1.3;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.media-meta {
    font-size: 0.85rem;
    color: #6c757d;
    margin-bottom: 0.4rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.media-meta i {
    width: 14px;
    text-align: center;
}

.media-actions {
    padding: 1rem 1.25rem;
    background: #f8f9fa;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
}

.media-actions .btn-group {
    display: flex;
    gap: 0.25rem;
}

.media-actions .btn {
    font-size: 0.8rem;
    padding: 0.4rem 0.6rem;
    border-radius: 6px;
    transition: all 0.2s ease;
}

.media-actions .btn:hover {
    transform: translateY(-1px);
}

.stats-card {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-radius: 12px;
    padding: 1.5rem;
    margin-bottom: 2rem;
    text-align: center;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
    transition: transform 0.3s ease;
}

.stats-card:hover {
    transform: translateY(-2px);
}

.stats-card h3 {
    font-size: 2rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
}

.stats-card p {
    font-size: 0.9rem;
    opacity: 0.9;
}

.filter-section {
    background: white;
    border-radius: 12px;
    padding: 1.5rem;
    margin-bottom: 2rem;
    box-shadow: 0 2px 10px rgba(0,0,0,0.08);
    border: 1px solid #e9ecef;
}

.filter-section h5 {
    color: #2c3e50;
    margin-bottom: 1rem;
    font-weight: 600;
}

.section-divider {
    margin: 2rem 0;
    border: none;
    height: 1px;
    background: linear-gradient(90deg, transparent, #dee2e6, transparent);
}

.upload-zone {
    border: 2px dashed #ccc;
    border-radius: 8px;
    padding: 2rem;
    text-align: center;
    background: #f8f9fa;
    margin-bottom: 1rem;
    transition: border-color 0.2s;
}

.upload-zone:hover {
    border-color: #007bff;
}

.upload-zone.dragover {
    border-color: #007bff;
    background: #e3f2fd;
}

.file-type-badge {
    display: inline-block;
    padding: 0.2rem 0.5rem;
    border-radius: 12px;
    font-size: 0.7rem;
    font-weight: bold;
    text-transform: uppercase;
}

.file-type-image { background: #e3f2fd; color: #1976d2; }
.file-type-video { background: #f3e5f5; color: #7b1fa2; }
.file-type-audio { background: #e8f5e8; color: #388e3c; }
.file-type-document { background: #fff3e0; color: #f57c00; }
.file-type-other { background: #f5f5f5; color: #616161; }

.media-browser-modal .modal-dialog {
    max-width: 90vw;
}

.media-browser-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 0.5rem;
}

.media-browser-item {
    border: 2px solid transparent;
    border-radius: 4px;
    cursor: pointer;
    transition: border-color 0.2s;
}

.media-browser-item:hover,
.media-browser-item.selected {
    border-color: #007bff;
}

.media-browser-preview {
    width: 100%;
    height: 120px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f8f9fa;
}

.media-browser-preview img {
    max-width: 100%;
    max-height: 100%;
    object-fit: cover;
}

/* Badges personalizados */
.file-type-badge {
    font-size: 0.75rem;
    padding: 0.25rem 0.5rem;
    border-radius: 20px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.file-type-image { 
    background: linear-gradient(135deg, #e3f2fd, #bbdefb); 
    color: #1565c0; 
    border: 1px solid #90caf9;
}

.file-type-video { 
    background: linear-gradient(135deg, #f3e5f5, #e1bee7); 
    color: #6a1b9a; 
    border: 1px solid #ce93d8;
}

.file-type-audio { 
    background: linear-gradient(135deg, #e8f5e8, #c8e6c9); 
    color: #2e7d32; 
    border: 1px solid #a5d6a7;
}

.file-type-document { 
    background: linear-gradient(135deg, #fff3e0, #ffe0b2); 
    color: #ef6c00; 
    border: 1px solid #ffcc02;
}

.file-type-other { 
    background: linear-gradient(135deg, #f5f5f5, #eeeeee); 
    color: #424242; 
    border: 1px solid #bdbdbd;
}

/* Animações suaves */
@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.media-card {
    animation: fadeInUp 0.5s ease forwards;
}

.media-card:nth-child(even) {
    animation-delay: 0.1s;
}

.media-card:nth-child(3n) {
    animation-delay: 0.2s;
}

/* Badge de status privado */
.badge-warning {
    background: linear-gradient(135deg, #ff9800, #f57c00) !important;
    border: none;
    box-shadow: 0 2px 4px rgba(255, 152, 0, 0.3);
}

/* Responsividade melhorada */
@media (max-width: 768px) {
    .media-grid {
        grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
        gap: 1rem;
        padding: 0;
    }
    
    .media-browser-grid {
        grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    }
    
    .btn-group {
        flex-direction: column;
        gap: 0.5rem;
    }
    
    .btn-group .btn {
        width: 100%;
    }
}

@media (max-width: 576px) {
    .media-grid {
        grid-template-columns: 1fr;
    }
    
    .media-actions {
        flex-direction: column;
        gap: 0.5rem;
    }
    
    .media-actions .btn-group {
        width: 100%;
    }
}

/* Correção para barra de progresso - garantir que 100% seja visualmente 100% */
.progress .progress-bar {
  transition: width 0.6s ease;
  border-radius: inherit;
}

/* Força 100% visual - funciona com ponto ou vírgula como separador decimal */
.progress .progress-bar[style*="width: 100%"],
.progress .progress-bar[style*="width: 100,0%"],
.progress .progress-bar[style*="width: 100.0%"] {
  width: 100% !important;
}

/* Garantir que barras de progresso pequenas sejam visíveis */
.progress .progress-bar {
  min-width: 0.5em;
}

/* Melhorar aparência da barra de progresso */
.progress {
  border-radius: 8px;
  overflow: hidden;
  box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
}
</style>
{% endblock %}

{% block extra_js %}
<script>
// Função para drag and drop
function setupDragAndDrop() {
    const uploadZone = document.querySelector('.upload-zone');
    if (!uploadZone) return;

    ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
        uploadZone.addEventListener(eventName, preventDefaults, false);
    });

    function preventDefaults(e) {
        e.preventDefault();
        e.stopPropagation();
    }

    ['dragenter', 'dragover'].forEach(eventName => {
        uploadZone.addEventListener(eventName, highlight, false);
    });

    ['dragleave', 'drop'].forEach(eventName => {
        uploadZone.addEventListener(eventName, unhighlight, false);
    });

    function highlight(e) {
        uploadZone.classList.add('dragover');
    }

    function unhighlight(e) {
        uploadZone.classList.remove('dragover');
    }

    uploadZone.addEventListener('drop', handleDrop, false);

    function handleDrop(e) {
        const dt = e.dataTransfer;
        const files = dt.files;
        const fileInput = document.querySelector('#id_file, #id_files');
        if (fileInput) {
            fileInput.files = files;
        }
    }
}

// Função para preview de arquivos
function setupFilePreview() {
    const fileInput = document.querySelector('#id_file');
    if (!fileInput) return;

    fileInput.addEventListener('change', function(e) {
        const file = e.target.files[0];
        if (!file) return;

        const previewContainer = document.querySelector('.file-preview');
        if (!previewContainer) return;

        const reader = new FileReader();
        reader.onload = function(e) {
            if (file.type.startsWith('image/')) {
                previewContainer.innerHTML = `<img src="${e.target.result}" style="max-width: 200px; max-height: 200px; border-radius: 4px;">`;
            } else {
                previewContainer.innerHTML = `<div class="file-info">
                    <i class="fas fa-file"></i>
                    <div>${file.name}</div>
                    <div class="text-muted">${(file.size / 1024 / 1024).toFixed(2)} MB</div>
                </div>`;
            }
        };
        reader.readAsDataURL(file);
    });
}

// Função para confirmação de exclusão
function confirmDelete(event) {
    if (!confirm('Tem certeza que deseja deletar este arquivo? Esta ação não pode ser desfeita.')) {
        event.preventDefault();
        return false;
    }
    return true;
}

// Função para corrigir barras de progresso com 100%
function fixProgressBars() {
    document.querySelectorAll('.progress-bar').forEach(bar => {
        const width = bar.style.width;
        // Converte vírgula para ponto e verifica se é 100%
        const normalizedWidth = width ? width.replace(',', '.') : '';
        if (normalizedWidth && (normalizedWidth.includes('100%') || normalizedWidth.includes('100.0%'))) {
            bar.style.width = '100%';
        }
    });
}

// Inicializar quando o DOM estiver pronto
document.addEventListener('DOMContentLoaded', function() {
    setupDragAndDrop();
    setupFilePreview();
    fixProgressBars(); // Corrigir barras de progresso
    
    // Adicionar confirmação aos botões de delete
    document.querySelectorAll('.btn-delete').forEach(btn => {
        btn.addEventListener('click', confirmDelete);
    });
});
</script>
{% endblock %}
