<?php
/**
 * Página: Gestión de Tracklist
 * 
 * Muestra estadísticas y detalles de las canciones sugeridas por los invitados
 */

$eventoId = getEventoId();
$evento = obtenerEvento($eventoId);

if (!$evento) {
    die('No se encontraron datos del evento');
}

// Obtener estadísticas
$estadisticas = obtenerEstadisticasTracklist($eventoId);
$tracklistDetallada = obtenerTracklistDetallada($eventoId);
?>
<style>
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
}

.page-header h2 {
    margin: 0;
    font-size: 20px;
    font-weight: 600;
    color: var(--color-text-primary);
}

/* Tabs */
.tabs-wrapper {
    margin-bottom: 24px;
}

.tabs {
    display: flex;
    gap: 8px;
    border-bottom: 2px solid var(--color-border);
    margin-bottom: 20px;
}

.tab-btn {
    padding: 12px 24px;
    background: none;
    border: none;
    border-bottom: 3px solid transparent;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    color: var(--color-text-secondary);
    transition: all 0.2s;
    margin-bottom: -2px;
}

.tab-btn:hover {
    color: var(--color-primary);
}

.tab-btn.active {
    color: var(--color-primary);
    border-bottom-color: var(--color-primary);
}

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
}

/* Header del resumen con botón de exportar */
.resumen-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--color-border);
}

.btn-export-pdf {
    padding: 10px 20px;
    background: var(--color-primary);
    color: white;
    border: none;
    border-radius: var(--border-radius);
    cursor: pointer;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-weight: 500;
    font-size: 14px;
    transition: var(--transition);
    white-space: nowrap;
}

.btn-export-pdf:hover {
    background: var(--color-primary-hover);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.btn-export-pdf svg {
    flex-shrink: 0;
}

/* Cards de estadísticas */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 20px;
    margin-bottom: 30px;
}

.stat-card {
    background: var(--color-bg-secondary);
    padding: 24px;
    border-radius: var(--border-radius);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--color-border);
}

.stat-card h3 {
    margin: 0 0 12px 0;
    font-size: 14px;
    font-weight: 600;
    color: var(--color-text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.stat-card-value {
    font-size: 24px;
    font-weight: 700;
    color: var(--color-primary);
    margin: 0;
}

.stat-card-label {
    font-size: 14px;
    color: var(--color-text-secondary);
    margin-top: 4px;
}

/* Tablas */
.table-container {
    background: var(--color-bg-secondary);
    border-radius: var(--border-radius);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--color-border);
    overflow: hidden;
}

.table-header {
    padding: 20px;
    border-bottom: 1px solid var(--color-border);
    background: var(--color-bg-primary);
}

.table-header h3 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    color: var(--color-text-primary);
}

table {
    width: 100%;
    border-collapse: collapse;
}

thead {
    background: var(--color-bg-primary);
}

th {
    padding: 12px 16px;
    text-align: left;
    font-weight: 600;
    font-size: 13px;
    color: var(--color-text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border-bottom: 2px solid var(--color-border);
}

td {
    padding: 14px 16px;
    border-bottom: 1px solid var(--color-border);
    color: var(--color-text-primary);
    font-size: 14px;
}

tbody tr:hover {
    background: var(--color-bg-primary);
}

tbody tr:last-child td {
    border-bottom: none;
}

.badge {
    display: inline-block;
    padding: 4px 10px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 500;
}

.badge-success {
    background: #d4edda;
    color: #155724;
}

.badge-warning {
    background: #fff3cd;
    color: #856404;
}

.badge-danger {
    background: #f8d7da;
    color: #721c24;
}

.badge-secondary {
    background: #e2e3e5;
    color: #383d41;
}

.empty-state {
    padding: 60px 20px;
    text-align: center;
    color: var(--color-text-secondary);
}

.empty-state p {
    margin: 0;
    font-size: 16px;
}

.ranking-number {
    font-weight: 700;
    color: var(--color-primary);
    font-size: 16px;
}

.votantes-count {
    font-weight: 600;
    color: var(--color-primary);
}
</style>

<div class="page-header">
    <h2 class="card-title" style="margin-bottom: 0;">Gestión de Tracklist</h2>
</div>

<div class="tabs-wrapper">
    <div class="tabs">
        <button type="button" class="tab-btn active" data-tab="tab-resumen">Resumen</button>
        <button type="button" class="tab-btn" data-tab="tab-detallado">Detallado</button>
    </div>

    <!-- TAB RESUMEN -->
    <div id="tab-resumen" class="tab-content active">
        <!-- Header con botón de exportar -->
        <div class="resumen-header">
            <div></div>
            <a href="../../assets/pdfs/pdf_tracklist.php" target="_blank" class="btn btn-export-pdf">
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/>
                    <polyline points="7 10 12 15 17 10"/>
                    <line x1="12" y1="15" x2="12" y2="3"/>
                </svg>
                Exportar a PDF
            </a>
        </div>
        
        <!-- Lo más votado -->
        <div class="stats-grid">
            <div class="stat-card">
                <h3>🎤 Artista Más Votado</h3>
                <?php if ($estadisticas['artista_mas_votado']): ?>
                    <p class="stat-card-value"><?php echo htmlspecialchars($estadisticas['artista_mas_votado']['autor']); ?></p>
                    <p class="stat-card-label"><?php echo $estadisticas['artista_mas_votado']['votos']; ?> sugerencia(s)</p>
                <?php else: ?>
                    <p class="stat-card-value" style="color: var(--color-text-secondary);">-</p>
                    <p class="stat-card-label">Sin datos</p>
                <?php endif; ?>
            </div>
            
            <div class="stat-card">
                <h3>🎵 Canción Más Votada</h3>
                <?php if ($estadisticas['cancion_mas_votada']): ?>
                    <p class="stat-card-value"><?php echo htmlspecialchars($estadisticas['cancion_mas_votada']['nombre_cancion']); ?></p>
                    <p class="stat-card-label">
                        <?php echo htmlspecialchars($estadisticas['cancion_mas_votada']['autor']); ?> - 
                        <?php echo $estadisticas['cancion_mas_votada']['votos']; ?> voto(s)
                    </p>
                <?php else: ?>
                    <p class="stat-card-value" style="color: var(--color-text-secondary);">-</p>
                    <p class="stat-card-label">Sin datos</p>
                <?php endif; ?>
            </div>
        </div>
        
        <!-- Ranking de canciones -->
        <div class="table-container">
            <div class="table-header">
                <h3>📊 Ranking de Canciones</h3>
            </div>
            <?php if (!empty($estadisticas['ranking'])): ?>
                <table>
                    <thead>
                        <tr>
                            <th style="width: 60px;">No.</th>
                            <th>Autor</th>
                            <th>Canción</th>
                            <th style="width: 100px; text-align: center;">Votantes</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($estadisticas['ranking'] as $index => $cancion): ?>
                            <tr>
                                <td>
                                    <span class="ranking-number"><?php echo $index + 1; ?></span>
                                </td>
                                <td><strong><?php echo htmlspecialchars($cancion['autor']); ?></strong></td>
                                <td><?php echo htmlspecialchars($cancion['nombre_cancion']); ?></td>
                                <td style="text-align: center;">
                                    <span class="votantes-count"><?php echo $cancion['votantes']; ?></span>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php else: ?>
                <div class="empty-state">
                    <p>No hay canciones sugeridas aún</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
    
    <!-- TAB DETALLADO -->
    <div id="tab-detallado" class="tab-content">
        <div class="table-container">
            <div class="table-header">
                <h3>📋 Detalle Completo de Sugerencias</h3>
            </div>
            <?php if (!empty($tracklistDetallada)): ?>
                <table>
                    <thead>
                        <tr>
                            <th style="width: 60px;">ID</th>
                            <th>Artista</th>
                            <th>Canción</th>
                            <th>Invitado</th>
                            <th style="width: 100px;">Estado</th>
                            <th style="width: 150px;">Fecha</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($tracklistDetallada as $cancion): ?>
                            <tr>
                                <td><?php echo $cancion['id']; ?></td>
                                <td><strong><?php echo htmlspecialchars($cancion['autor']); ?></strong></td>
                                <td><?php echo htmlspecialchars($cancion['nombre_cancion']); ?></td>
                                <td>
                                    <?php echo htmlspecialchars($cancion['nombre_cortesia'] . ' ' . $cancion['nombre_invitado']); ?>
                                </td>
                                <td>
                                    <?php
                                    $estado = $cancion['estado'];
                                    $badgeClass = 'badge-secondary';
                                    if ($estado === 'Aceptado') {
                                        $badgeClass = 'badge-success';
                                    } elseif ($estado === 'Rechazado') {
                                        $badgeClass = 'badge-danger';
                                    } elseif ($estado === 'Pendiente') {
                                        $badgeClass = 'badge-warning';
                                    }
                                    ?>
                                    <span class="badge <?php echo $badgeClass; ?>"><?php echo htmlspecialchars($estado); ?></span>
                                </td>
                                <td>
                                    <?php 
                                    $fecha = new DateTime($cancion['fecha_creacion']);
                                    echo $fecha->format('d/m/Y H:i');
                                    ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php else: ?>
                <div class="empty-state">
                    <p>No hay canciones sugeridas aún</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<script>
// Funcionalidad de tabs
document.addEventListener('DOMContentLoaded', function() {
    const tabButtons = document.querySelectorAll('.tab-btn');
    const tabContents = document.querySelectorAll('.tab-content');
    
    tabButtons.forEach(button => {
        button.addEventListener('click', function() {
            const targetTab = this.getAttribute('data-tab');
            
            // Remover active de todos
            tabButtons.forEach(btn => btn.classList.remove('active'));
            tabContents.forEach(content => content.classList.remove('active'));
            
            // Agregar active al seleccionado
            this.classList.add('active');
            document.getElementById(targetTab).classList.add('active');
        });
    });
});
</script>
