<?php
if ($my['uid'] != 1) exit;
$dashboardConfig=array(); $configFile=$g['path_var'].'jusq.dashboard.php';
if(is_file($configFile)) include $configFile;
$stats=null;$error='';
if(!empty($dashboardConfig['endpoint'])&&!empty($dashboardConfig['token'])){
	$context=stream_context_create(array('http'=>array('header'=>'X-JusQ-Dashboard-Token: '.$dashboardConfig['token']."\r\n",'timeout'=>5,'ignore_errors'=>true)));
	$result=@file_get_contents($dashboardConfig['endpoint'],false,$context); $stats=$result!==false?json_decode($result,true):null;
	if(!is_array($stats))$error='배포 통계 서버에 연결할 수 없습니다.';
}else $error='배포 통계 대시보드가 설정되지 않았습니다.';
$latest=(string)($d['admin']['version']??'');
?>
<link rel="stylesheet" href="<?php echo $g['url_module']?>/lang.korean/admin/_pc/deployment.css" />
<div class="deploy-wrap">
 <div class="deploy-head"><div><span>JUSQ DISTRIBUTION</span><h2>배포 설치 실황</h2><p>동의한 설치본의 익명 운영 신호와 버전 현황입니다.</p></div><div class="live-dot">LIVE</div></div>
 <?php if($error):?><div class="deploy-error"><?php echo htmlspecialchars($error,ENT_QUOTES,'UTF-8')?></div><?php else:?>
 <div class="deploy-cards">
  <div><small>누적 설치</small><strong><?php echo number_format((int)$stats['total'])?></strong><em>installations</em></div>
  <div><small>최근 7일 활성</small><strong><?php echo number_format((int)$stats['active_7d'])?></strong><em>active sites</em></div>
  <div><small>최근 30일 활성</small><strong><?php echo number_format((int)$stats['active_30d'])?></strong><em>active sites</em></div>
  <div><small>업데이트 필요</small><strong><?php $outdated=0;foreach((array)$stats['items'] as $i)if(version_compare((string)$i['version'],$latest,'<'))$outdated++;echo number_format($outdated)?></strong><em>latest <?php echo htmlspecialchars($latest)?></em></div>
 </div>
 <div class="deploy-grid"><section><h3>버전 분포</h3><?php foreach((array)$stats['versions'] as $v=>$count):?><div class="version-row"><span><?php echo htmlspecialchars($v)?></span><b><?php echo number_format($count)?></b></div><?php endforeach?></section>
 <section class="wide"><h3>최근 설치 및 활성 현황</h3><table><thead><tr><th>사이트</th><th>버전</th><th>환경</th><th>최근 이벤트</th><th>마지막 통신</th></tr></thead><tbody><?php foreach((array)$stats['items'] as $i):?><tr><td><?php echo htmlspecialchars($i['host']?:'비공개')?></td><td><span class="version-pill<?php echo version_compare((string)$i['version'],$latest,'<')?' old':''?>"><?php echo htmlspecialchars($i['version'])?></span></td><td>PHP <?php echo htmlspecialchars($i['php'])?> · <?php echo htmlspecialchars($i['os'])?></td><td><?php echo htmlspecialchars($i['event'])?></td><td><?php echo htmlspecialchars(str_replace('T',' ',substr((string)$i['seen_at'],0,19)))?> UTC</td></tr><?php endforeach?></tbody></table></section></div>
 <p class="privacy-note">회원정보, 비밀번호, 데이터베이스 정보와 원문 IP는 수집·저장하지 않습니다. 설치자가 통계 전송에 동의한 사이트만 표시됩니다.</p>
 <?php endif?>
</div>
