<?php
define('__KIMS__', true);
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
$widget = isset($_GET['widget']) ? preg_replace('/[^a-zA-Z0-9_\/-]/', '', $_GET['widget']) : '_acc/weather';
$today = isset($_GET['today']) ? preg_replace('/[^0-9]/', '', $_GET['today']) : date('Ymd');
$path = str_replace('widgets/'.$widget.'/'.basename($_SERVER['SCRIPT_FILENAME']), '', $_SERVER['SCRIPT_FILENAME']);
include_once $path.'_core/function/sys.func.php';
include_once __DIR__.'/_weather.lib.php';
$weatherFile = __DIR__.'/data/'.$today.'.txt';
if (!file_exists($weatherFile) || time() - filemtime($weatherFile) > 10800) jusqWeatherUpdate($weatherFile);
$rows = array_values(array_filter(explode('<split>', file_exists($weatherFile) ? file_get_contents($weatherFile) : '')));
$selected = 0;
foreach ($rows as $i => $row) { $v = explode('|', $row); if ($v[0] === (isset($_GET['city']) ? $_GET['city'] : '서울')) $selected = $i; }
$current = isset($rows[$selected]) ? explode('|', $rows[$selected]) : array('서울','정보 없음','','','','105','',37.5665,126.9780);
$url = 'https://api.open-meteo.com/v1/forecast?latitude='.rawurlencode($current[7]).'&longitude='.rawurlencode($current[8]).'&current=temperature_2m,relative_humidity_2m,weather_code,wind_speed_10m&daily=weather_code,temperature_2m_max,temperature_2m_min,precipitation_probability_max&timezone=Asia%2FSeoul&forecast_days=5';
$forecast = json_decode(jusqWeatherRequest($url), true);
$daily = isset($forecast['daily']) ? $forecast['daily'] : array();
?><!doctype html>
<html lang="ko"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>날씨정보 - <?php echo htmlspecialchars($current[0], ENT_QUOTES, 'UTF-8')?></title>
<style>
*{box-sizing:border-box}body{margin:0;padding:22px;background:#f4f7fb;color:#17243b;font-family:Arial,'Noto Sans KR',sans-serif}.card{max-width:420px;margin:auto;background:#fff;border:1px solid #dfe7f1;border-radius:18px;box-shadow:0 14px 34px rgba(7,29,73,.10);overflow:hidden}.head{padding:20px;background:linear-gradient(135deg,#087fd5,#123a79);color:#fff}.top{display:flex;align-items:center;gap:12px}.top img{width:52px;height:52px}.temp{font-size:38px;font-weight:800;margin-left:auto}.meta{font-size:12px;opacity:.88;margin-top:8px}.select{width:100%;margin-top:14px;padding:9px 11px;border:1px solid rgba(255,255,255,.45);border-radius:9px;background:rgba(255,255,255,.14);color:#fff}.select option{color:#17243b}.forecast{display:grid;grid-template-columns:repeat(5,1fr);padding:16px 10px}.day{text-align:center;padding:6px 2px;border-right:1px solid #edf1f6}.day:last-child{border:0}.date{font-size:11px;color:#7b8798}.day img{width:34px;height:34px;margin:7px 0}.desc{font-size:11px;color:#40506a;min-height:28px}.degree{font-size:12px;margin-top:5px}.high{color:#e34b4b;font-weight:700}.low{color:#2981ce}.credit{text-align:center;padding:0 12px 14px;color:#9aa5b5;font-size:10px}.credit a{color:#72839a}
</style></head><body><div class="card"><div class="head"><div class="top"><img src="image/<?php echo (int)$current[5]?>.gif" alt="<?php echo htmlspecialchars($current[1], ENT_QUOTES, 'UTF-8')?>"><div><strong><?php echo htmlspecialchars($current[0], ENT_QUOTES, 'UTF-8')?></strong><div><?php echo htmlspecialchars($current[1], ENT_QUOTES, 'UTF-8')?></div></div><div class="temp"><?php echo htmlspecialchars($current[3], ENT_QUOTES, 'UTF-8')?>°</div></div><div class="meta"><?php echo htmlspecialchars($current[4].' · '.$current[6], ENT_QUOTES, 'UTF-8')?></div><select class="select" onchange="changeCity(this)"><?php foreach($rows as $i=>$row):$v=explode('|',$row)?><option value="<?php echo $i?>"<?php if($i===$selected):?> selected<?php endif?>><?php echo htmlspecialchars($v[0], ENT_QUOTES, 'UTF-8')?></option><?php endforeach?></select></div><div class="forecast">
<?php for($i=0;$i<5;$i++): if(!isset($daily['time'][$i])) continue; $code=(int)$daily['weather_code'][$i]; ?>
<div class="day"><div class="date"><?php echo date('m/d', strtotime($daily['time'][$i]))?></div><img src="image/<?php echo jusqWeatherIcon($code)?>.gif" alt=""><div class="desc"><?php echo jusqWeatherText($code)?></div><div class="degree"><span class="low"><?php echo round($daily['temperature_2m_min'][$i])?>°</span> / <span class="high"><?php echo round($daily['temperature_2m_max'][$i])?>°</span></div></div>
<?php endfor?></div><div class="credit">Weather data by <a href="https://open-meteo.com/" target="_blank" rel="noopener">Open-Meteo</a></div></div>
<script>var rows=<?php echo json_encode($rows, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)?>;function changeCity(el){var i=parseInt(el.value,10),v=rows[i].split('|');localStorage.setItem('jusq-weather-city',i);location.href='_view.php?widget=<?php echo rawurlencode($widget)?>&today=<?php echo $today?>&city='+encodeURIComponent(v[0])+'&x='+v[7]+'&y='+v[8];}</script></body></html>
