<?php
date_default_timezone_set('Europe/Madrid');
//Script Creado por Carlos de www.meteoviso.es
/*
//Extraer preddicion del fichero XML de Aemet y mostrar en pantalla probado en php 8.4.1 necesario CURL
Listado de comarcas AEMET  
Andalucía
6115: Cádiz
6116: Huelva
6117: Málaga
6118: Granada
6119: Almería
6123: Jaén
6124: Córdoba
6141: Sevilla
Aragón
6023: Huesca
6024: Teruel
6031: Zaragoza
Asturias
7027: Asturias
Cantabria
7039: Cantabria
Castilla-La Mancha
6101: Albacete
6102: Ciudad Real
6103: Cuenca
6104: Guadalajara
6105: Toledo
Castilla y León
6021: Ávila
6022: Burgos
6025: León
6026: Palencia
6027: Salamanca
6028: Segovia
6029: Soria
6032: Valladolid
6033: Zamora
Cataluña
8011: Barcelona
8012: Girona
8013: Lleida
8014: Tarragona
Comunidad de Madrid
8039: Madrid
Comunidad Valenciana
8015: Alicante
8016: Castellón
8017: Valencia
Extremadura
6036: Badajoz
6037: Cáceres
Galicia
6041: A Coruña
6042: Lugo
6043: Ourense
6044: Pontevedra
Islas Baleares
6020: Mallorca
6021: Menorca
6022: Ibiza y Formentera
Islas Canarias
1010: Tenerife
1011: Gran Canaria
1012: Lanzarote
1013: Fuerteventura
1014: La Palma
1015: La Gomera
1016: El Hierro
La Rioja
7038: La Rioja
Navarra
7040: Navarra
País Vasco
8018: Álava
8019: Guipúzcoa
8020: Vizcaya
Región de Murcia
7035: Murcia
Ceuta y Melilla
8050: Ceuta
8051: Melilla

Solo configurar nuestor id del listado anterior en la variable $comarcaCode linea 94
Si usamos
www.tuweb.com/prediccion.php?show=today (solo mostrara prediccion hoy)
www.tuweb.com/prediccion.php?show=tomorrow (solo mostrara prediccion mañana)
Si no indicamos nada saldra hoy y mañana


*/
 
// Función para obtener la predicción
function getWeatherPrediction($date) {
    $comarcaCode = "6141"; // Personaliza este valor según sea necesario
    $baseUrl = "https://www.aemet.es/es/api-eltiempo/prediccion/{date}/PB/8/$comarcaCode";
    $url = str_replace("{date}", $date, $baseUrl);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($ch);
    
    if (curl_errno($ch)) {
        echo 'Error en cURL: ' . curl_error($ch);
        return false;
    }
    
    curl_close($ch);
    
    $xml = simplexml_load_string($response);
    
    if ($xml === false) {
        echo "Error al parsear XML";
        return false;
    }
    
    $prediction = $xml->xpath('//txt_prediccion/p');
    return $prediction[0] ?? "No se encontró predicción";
}

// Obtener el parámetro de visualización (por defecto muestra ambos)
$show = isset($_GET['show']) ? $_GET['show'] : 'both';

$today = date('Y-m-d');
$tomorrow = date('Y-m-d', strtotime('+1 day'));

$todayPrediction = ($show == 'today' || $show == 'both') ? getWeatherPrediction($today) : null;
$tomorrowPrediction = ($show == 'tomorrow' || $show == 'both') ? getWeatherPrediction($tomorrow) : null;
?>

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Predicción Meteorológica</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 20px;
            background: linear-gradient(135deg, #87CEEB, #1E90FF);
            min-height: 100vh;
            color: #333;
        }
        
        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .header {
            text-align: center;
            margin-bottom: 40px;
            color: white;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
        }
        
        .prediction-card {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 15px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            transition: transform 0.3s ease;
        }
        
        .prediction-card:hover {
            transform: translateY(-5px);
        }
        
        .prediction-date {
            color: #1E90FF;
            font-size: 1.5em;
            margin-bottom: 10px;
            font-weight: bold;
        }
        
        .prediction-text {
            line-height: 1.6;
            color: #444;
        }
        
        .weather-icon {
            font-size: 2em;
            margin-bottom: 10px;
            color: #1E90FF;
        }

        .controls {
            text-align: center;
            margin-bottom: 20px;
        }

        .controls a {
            display: inline-block;
            padding: 10px 20px;
            margin: 0 10px;
            background-color: rgba(255, 255, 255, 0.9);
            color: #1E90FF;
            text-decoration: none;
            border-radius: 25px;
            transition: all 0.3s ease;
        }

        .controls a:hover {
            background-color: #1E90FF;
            color: white;
            transform: scale(1.05);
        }

        .controls a.active {
            background-color: #1E90FF;
            color: white;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>☀️ Predicción Meteorológica ☁️</h1>
        </div>

     
        
        <?php if ($todayPrediction): ?>
        <div class="prediction-card">
            <div class="weather-icon">🌤️</div>
            <div class="prediction-date">
                Hoy (<?php echo date('d/m/Y'); ?>)
            </div>
            <div class="prediction-text">
                <?php echo $todayPrediction; ?>
            </div>
        </div>
        <?php endif; ?>
        
        <?php if ($tomorrowPrediction): ?>
        <div class="prediction-card">
            <div class="weather-icon">🌅</div>
            <div class="prediction-date">
                Mañana (<?php echo date('d/m/Y', strtotime('+1 day')); ?>)
            </div>
            <div class="prediction-text">
                <?php echo $tomorrowPrediction; ?>
            </div>
        </div>
        <?php endif; ?>
    </div>
</body>
</html>