<?php
$weather_file = $g['path_widget'].$wdgvar['widget_id'].'/data/'.$date['today'].'.txt';
$weather_time = file_exists($weather_file) ? filemtime($weather_file) : 0;
include_once $g['path_widget'].$wdgvar['widget_id'].'/_weather.lib.php';
if (!file_exists($weather_file) || (time() - $weather_time) > 10800) jusqWeatherUpdate($weather_file);
$wdata = file_exists($weather_file) ? file_get_contents($weather_file) : '';
$warr = array_values(array_filter(explode('<split>', $wdata)));
$weather_id = 'jusq-weather-'.substr(md5($wdgvar['widget_id'].microtime(true)), 0, 8);
$img_path = $g['s'].'/widgets/'.$wdgvar['widget_id'].'/image';
$configured_city = isset($wdgvar['city']) ? trim($wdgvar['city']) : '';
?>
<div id="<?php echo $weather_id?>" class="jusq-weather-widget" aria-live="polite">
    <button type="button" class="jusq-weather-current" aria-label="지역별 날씨 자세히 보기">
        <img class="jusq-weather-icon" src="<?php echo $img_path?>/105.gif" alt="" />
        <span class="jusq-weather-city">날씨 불러오는 중</span>
        <strong class="jusq-weather-temp"></strong>
    </button>
    <span class="jusq-weather-desc"></span>
</div>
<style>
#<?php echo $weather_id?>{display:flex!important;flex-direction:column!important;align-items:stretch!important;justify-content:center!important;gap:2px!important;width:100%!important;min-height:82px!important;height:auto!important;padding:12px 14px!important;border:1px solid #dfe8f3!important;border-radius:14px!important;background:linear-gradient(135deg,#f8fbff 0%,#eef6ff 100%)!important;box-shadow:0 8px 20px rgba(22,70,125,.06)!important;color:#465269;font-family:Arial,'Noto Sans KR',sans-serif;font-size:12px;box-sizing:border-box!important}
#<?php echo $weather_id?> .jusq-weather-current{display:flex!important;align-items:center!important;gap:8px!important;width:100%!important;height:auto!important;min-height:36px!important;margin:0!important;border:0!important;outline:0!important;background:transparent!important;padding:0!important;color:inherit!important;cursor:pointer;font:inherit!important;text-align:left!important;box-shadow:none!important}
#<?php echo $weather_id?> .jusq-weather-icon{display:block!important;flex:0 0 34px!important;width:34px!important;height:34px!important;margin:0!important;padding:0!important;border:0!important;outline:0!important;background:transparent!important;box-shadow:none!important;object-fit:contain!important}
#<?php echo $weather_id?> .jusq-weather-city{display:block!important;width:auto!important;height:auto!important;min-height:0!important;margin:0!important;padding:0!important;border:0!important;outline:0!important;background:transparent!important;box-shadow:none!important;color:#263a55!important;font-weight:700!important;white-space:nowrap!important}
#<?php echo $weather_id?> .jusq-weather-temp{display:block!important;width:auto!important;height:auto!important;margin-left:auto!important;padding:0!important;border:0!important;outline:0!important;background:transparent!important;box-shadow:none!important;color:#ef5350!important;font-size:15px!important;font-weight:700!important;white-space:nowrap!important}
#<?php echo $weather_id?> .jusq-weather-desc{display:block!important;width:100%!important;height:auto!important;min-height:0!important;margin:0!important;padding:0!important;border:0!important;outline:0!important;background:transparent!important;box-shadow:none!important;color:#74849a!important;line-height:18px!important;text-align:right!important;white-space:nowrap!important}
</style>
<script>
(function(){
var rows=<?php echo json_encode($warr, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)?>;
var root=document.getElementById('<?php echo $weather_id?>');
if(!root||!rows.length){if(root)root.innerHTML='<span>날씨 정보를 불러오지 못했습니다.</span>';return;}
var configuredCity=<?php echo json_encode($configured_city, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)?>;
var index=parseInt(localStorage.getItem('jusq-weather-city')||'0',10);if(index<0||index>=rows.length)index=0;
if(configuredCity){for(var i=0;i<rows.length;i++){if(rows[i].split('|')[0]===configuredCity){index=i;break;}}}
function draw(){var v=rows[index].split('|');root.querySelector('.jusq-weather-icon').src='<?php echo $img_path?>/'+v[5]+'.gif';root.querySelector('.jusq-weather-icon').alt=v[1];root.querySelector('.jusq-weather-city').textContent=v[0];root.querySelector('.jusq-weather-temp').textContent=v[3]+'°C';root.querySelector('.jusq-weather-desc').textContent=v[1];root.querySelector('.jusq-weather-current').title=v[4]+' · '+v[6];}
root.querySelector('.jusq-weather-current').onclick=function(){var v=rows[index].split('|');window.open('<?php echo $g['s']?>/widgets/<?php echo $wdgvar['widget_id']?>/_view.php?widget=<?php echo rawurlencode($wdgvar['widget_id'])?>&today=<?php echo $date['today']?>&city='+encodeURIComponent(v[0])+'&x='+v[7]+'&y='+v[8],'jusqWeather','width=430,height=480,scrollbars=yes,resizable=yes');};
draw();if(!configuredCity)setInterval(function(){index=(index+1)%rows.length;draw();},5000);
})();
</script>
