<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Listado de Contratos</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        .header {
            text-align: center;
            color: white;
            margin-bottom: 30px;
        }

        .header h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
        }

        .header p {
            font-size: 1.1rem;
            opacity: 0.9;
        }

        /* Barra de usuario */
        .user-bar {
            background: rgba(255, 255, 255, 0.1);
            border-radius: 15px;
            padding: 15px 20px;
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            color: white;
            backdrop-filter: blur(10px);
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .user-avatar {
            width: 40px;
            height: 40px;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            font-size: 1.2rem;
        }

        .logout-btn {
            background: rgba(255, 255, 255, 0.2);
            color: white;
            border: 1px solid rgba(255, 255, 255, 0.3);
            padding: 8px 16px;
            border-radius: 20px;
            cursor: pointer;
            transition: all 0.3s ease;
            text-decoration: none;
            font-size: 0.9rem;
        }

        .logout-btn:hover {
            background: rgba(255, 255, 255, 0.3);
            border-color: rgba(255, 255, 255, 0.5);
        }

        /* Navegación por pestañas */
        .tab-navigation {
            display: flex;
            justify-content: center;
            margin-bottom: 30px;
            gap: 10px;
        }

        .tab-button {
            background: rgba(255, 255, 255, 0.1);
            color: white;
            border: 2px solid rgba(255, 255, 255, 0.3);
            padding: 12px 24px;
            border-radius: 25px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 1rem;
            font-weight: 500;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }

        .tab-button:hover {
            background: rgba(255, 255, 255, 0.2);
            border-color: rgba(255, 255, 255, 0.6);
            transform: translateY(-2px);
        }

        .tab-button.active {
            background: white;
            color: #667eea;
            border-color: white;
        }

        .tab-content {
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            min-height: 400px;
        }

        /* Componentes reutilizables */
        .btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 12px 30px;
            border-radius: 25px;
            font-size: 1rem;
            cursor: pointer;
            transition: all 0.3s ease;
            text-transform: uppercase;
            font-weight: 600;
            letter-spacing: 1px;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.3);
        }

        .btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .btn-secondary {
            background: linear-gradient(135deg, #6c757d 0%, #495057 100%);
        }

        .btn-secondary:hover {
            background: linear-gradient(135deg, #5a6268 0%, #3d4142 100%);
        }

        .btn-danger {
            background: linear-gradient(135deg, #dc3545 0%, #c82333 100%);
        }

        .btn-danger:hover {
            background: linear-gradient(135deg, #c82333 0%, #a71e2a 100%);
        }

        .btn-success {
            background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
        }

        .btn-success:hover {
            background: linear-gradient(135deg, #218838 0%, #1ec085 100%);
        }

        .card {
            background: #f8f9fa;
            border-radius: 15px;
            padding: 25px;
            margin-bottom: 20px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            transition: transform 0.3s ease;
        }

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

        .card h3 {
            color: #333;
            margin-bottom: 15px;
            font-size: 1.3rem;
            border-bottom: 2px solid #667eea;
            padding-bottom: 8px;
        }

        .status-message {
            padding: 15px;
            border-radius: 10px;
            margin: 20px 0;
            font-weight: 500;
            display: none;
        }

        .status-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .status-error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid #f3f3f3;
            border-top: 3px solid #667eea;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        /* Responsivo */
        @media (max-width: 768px) {
            .container {
                padding: 10px;
            }
            
            .header h1 {
                font-size: 2rem;
            }
            
            .tab-navigation {
                flex-direction: column;
                align-items: center;
            }
            
            .tab-button {
                width: 200px;
                justify-content: center;
            }
        }

        /* Estilos de la tarjeta de contrato */
        .contract-card {
            background: white;
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 15px;
            box-shadow: 0 3px 12px rgba(0,0,0,0.1);
            border-left: 4px solid #667eea;
            transition: all 0.3s ease;
        }

        .contract-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 20px rgba(0,0,0,0.15);
        }

        .contract-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 2px solid #f0f0f0;
        }

        .contract-header h4 {
            color: #333;
            margin: 0;
            font-size: 1.2rem;
            font-weight: 600;
        }

        .contract-type-badge {
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            padding: 4px 12px;
            border-radius: 15px;
            font-size: 0.8rem;
            font-weight: 500;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .contract-details {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 8px;
            margin-bottom: 15px;
        }

        .detail-row {
            display: flex;
            flex-direction: column;
            gap: 2px;
        }

        .detail-row.full-width {
            grid-column: 1 / -1;
        }

        .detail-label {
            font-size: 0.85rem;
            color: #666;
            font-weight: 500;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .detail-value {
            color: #333;
            font-weight: 400;
            word-break: break-word;
        }

        .detail-value.highlight {
            color: #28a745;
            font-weight: 600;
        }

        .contract-footer {
            padding-top: 10px;
            border-top: 1px solid #f0f0f0;
        }

        .creation-date {
            color: #999;
            font-style: italic;
        }

        @media (max-width: 768px) {
            .contract-details {
                grid-template-columns: 1fr;
            }
            
            .contract-header {
                flex-direction: column;
                align-items: flex-start;
                gap: 10px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>Listado de Contratos</h1>
            <p>Consulta y exporta todos los contratos procesados</p>
        </div>

        
        <div class="user-bar">
            <div class="user-info">
                <div class="user-avatar">
                    A
                </div>
                <div>
                    <div style="font-weight: 600;">admin</div>
                    <div style="font-size: 0.8rem; opacity: 0.8;">Administrador</div>
                </div>
            </div>
            <a href="/logout" class="logout-btn">
                🚪 Cerrar Sesión
            </a>
        </div>
        

        
        <nav class="tab-navigation">
            <a href="/" class="tab-button ">
                🏠 Inicio
            </a>
            <a href="/contratos" class="tab-button active">
                📋 Listado de Contratos
            </a>
        </nav>
        

        <div class="tab-content">
            <div class="contracts-page">
    <h1>📋 Todos los Contratos</h1>

    <div class="contracts-actions">
        <button id="refreshAllBtn" class="btn">
            🔄 Actualizar Lista
        </button>
        <button id="downloadCsvBtn" class="btn btn-secondary">
            📥 Descargar CSV
        </button>
        <button id="deleteAllBtn" class="btn btn-danger">
            🗑️ Borrar Todos
        </button>
    </div>

    <div id="allContractsList" class="contracts-list-full">
        <div class="no-contracts-full">
            <div class="icon">📄</div>
            <p>Cargando contratos...</p>
        </div>
    </div>

    <div id="statusMessage" class="status-message"></div>
</div>

<style>
    .contracts-page h1 {
        color: #333;
        margin-bottom: 25px;
        text-align: center;
        font-size: 2rem;
    }

    .contracts-actions {
        display: flex;
        justify-content: center;
        gap: 15px;
        margin-bottom: 30px;
        flex-wrap: wrap;
    }

    .contracts-list-full {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
        gap: 20px;
    }

    .no-contracts-full {
        grid-column: 1 / -1;
        text-align: center;
        color: #666;
        padding: 60px 30px;
        background: #f0f2f5;
        border-radius: 15px;
        border: 2px dashed #ccc;
    }

    .no-contracts-full .icon {
        font-size: 4rem;
        margin-bottom: 20px;
        opacity: 0.4;
    }

    /* Sobreescribir estilos de contract-card para la vista de lista completa */
    .contracts-list-full .contract-card {
        margin-bottom: 0; /* Eliminar margen inferior si ya está en el grid gap */
    }
</style>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        cargarTodosLosContratos();

        document.getElementById('refreshAllBtn').addEventListener('click', cargarTodosLosContratos);
        document.getElementById('downloadCsvBtn').addEventListener('click', descargarCSV);
        document.getElementById('deleteAllBtn').addEventListener('click', eliminarTodosLosContratos);
    });

    async function cargarTodosLosContratos() {
        try {
            const contratos = await realizarPeticion('/api/contratos');
            const container = document.getElementById('allContractsList');
            
            const hasContracts = contratos.length > 0;
            document.getElementById('downloadCsvBtn').disabled = !hasContracts;
            document.getElementById('deleteAllBtn').disabled = !hasContracts;

            if (contratos.length === 0) {
                container.innerHTML = `
                    <div class="no-contracts-full">
                        <div class="icon">📄</div>
                        <p>No hay contratos procesados aún.<br>¡Sube algunos contratos desde la pestaña de Inicio!</p>
                    </div>
                `;
            } else {
                container.innerHTML = contratos.map(crearTarjetaContratoHTML).join('');
            }
        } catch (error) {
            document.getElementById('allContractsList').innerHTML = `
                <div class="no-contracts-full">
                    <div class="icon">❌</div>
                    <p>Error al cargar contratos: ${error.message}</p>
                </div>
            `;
            mostrarEstado(`Error al cargar contratos: ${error.message}`, 'error');
        }
    }

    async function descargarCSV() {
        try {
            document.getElementById('downloadCsvBtn').disabled = true;
            document.getElementById('downloadCsvBtn').innerHTML = '<span class="loading"></span> Descargando...';

            const response = await fetch('/api/contratos/download/csv');
            
            if (response.ok) {
                const blob = await response.blob();
                const url = window.URL.createObjectURL(blob);
                const nombreArchivo = generarNombreArchivoConFecha('contratos', 'csv');
                descargarArchivo(url, nombreArchivo);
                
                mostrarEstado('¡Archivo CSV descargado exitosamente!', 'success');
            } else {
                const errorData = await response.json();
                mostrarEstado(`Error: ${errorData.error}`, 'error');
            }
        } catch (error) {
            mostrarEstado(`Error descargando CSV: ${error.message}`, 'error');
        } finally {
            document.getElementById('downloadCsvBtn').disabled = false;
            document.getElementById('downloadCsvBtn').innerHTML = '📥 Descargar CSV';
        }
    }

    async function eliminarTodosLosContratos() {
        if (!confirmarAccion('¿Estás seguro de que quieres eliminar TODOS los contratos? Esta acción no se puede deshacer.')) {
            return;
        }

        try {
            document.getElementById('deleteAllBtn').disabled = true;
            document.getElementById('deleteAllBtn').innerHTML = '<span class="loading"></span> Eliminando...';
            
            await realizarPeticion('/api/contratos', {
                method: 'DELETE'
            });
            
            mostrarEstado('Todos los contratos han sido eliminados exitosamente.', 'success');
            cargarTodosLosContratos(); // Recargar la lista
        } catch (error) {
            mostrarEstado(`Error eliminando contratos: ${error.message}`, 'error');
        } finally {
            document.getElementById('deleteAllBtn').disabled = false;
            document.getElementById('deleteAllBtn').innerHTML = '🗑️ Borrar Todos';
        }
    }
</script> 
        </div>
    </div>

    <script src="/js/utils.js"></script>
</body>
</html> 