<?php
/**
 * 社区系统 入口页
 * 功能：自动检测是否已安装，未安装则跳转到安装向导
 *
 * 注意：不能只看 config/config.php 是否存在 —— 安装包里就带着这个文件，
 * 且"配置写错/库连不上/缺表缺列"时文件同样存在。这里做真实体检。
 */

require_once __DIR__ . '/common/install_state.php';
$health = qixiInstallState(__DIR__);
if ($health['state'] !== 'ok') {
    // 未安装 或 装得不完整（配置无效 / 连不上库 / 缺核心表字段）→ 引导去安装向导
    // 必须禁掉缓存：否则浏览器会把这次的 302 记下来，等真正装好后仍然一直跳到安装页
    header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
    header('Pragma: no-cache');
    header('Expires: 0');
    header('Location: install.php', true, 302);
    exit;
}

// 已安装，显示服务状态页
require_once __DIR__ . '/admin/guard_ip.php';
$title = '服务状态';
$site = '栖屿社区';
$rootBase = rtrim(str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])), '/');
$logoUrl = $rootBase . '/admin/assets/img/logo.png';
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="robots" content="noindex,nofollow">
<title><?= $title ?> · <?= $site ?></title>
<style>
  /* 与后台登录页 / 404 页同一套品牌语言 */
  body{margin:0;min-height:100vh;display:flex;align-items:center;justify-content:center;padding:24px;box-sizing:border-box;overflow-x:hidden;
    background:linear-gradient(135deg,#4d8af0 0%,#1a56db 100%);
    font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"PingFang SC","Hiragino Sans GB","Microsoft YaHei",sans-serif;
    color:#1f2937}
  body *{box-sizing:border-box}
  .bg-overlay{position:fixed;inset:0;pointer-events:none;
    background:radial-gradient(circle at 18% 16%,rgba(255,255,255,.20),transparent 45%),radial-gradient(circle at 82% 84%,rgba(255,255,255,.13),transparent 45%)}
  .shapes{position:fixed;inset:0;overflow:hidden;pointer-events:none}
  .shapes i{position:absolute;border-radius:50%;background:rgba(255,255,255,.12);border:1px solid rgba(255,255,255,.18);animation:sFloat 6s ease-in-out infinite alternate}
  .shapes i:nth-child(1){width:200px;height:200px;top:8%;left:6%}
  .shapes i:nth-child(2){width:120px;height:120px;top:64%;left:14%;animation-delay:1.2s}
  .shapes i:nth-child(3){width:170px;height:170px;top:14%;right:8%;animation-delay:.6s}
  .shapes i:nth-child(4){width:100px;height:100px;bottom:10%;right:16%;animation-delay:1.8s}
  @keyframes sFloat{from{transform:translateY(0) rotate(0)}to{transform:translateY(-20px) rotate(180deg)}}

  .card{position:relative;z-index:1;width:100%;max-width:440px;padding:36px 32px 30px;text-align:center;
    background:rgba(255,255,255,.97);border-radius:22px;
    box-shadow:0 24px 64px rgba(10,32,88,.30),0 8px 22px rgba(10,32,88,.16);
    -webkit-backdrop-filter:blur(20px);backdrop-filter:blur(20px)}

  .mark{display:flex;align-items:center;justify-content:center;gap:10px;font-size:13px;font-weight:600;color:#4b5563}
  .mark img{width:30px;height:30px;padding:3px;border-radius:9px;background:#f2f6ff;object-fit:contain}
  .mark .mk{display:none;width:30px;height:30px;border-radius:9px;align-items:center;justify-content:center;
    background:linear-gradient(135deg,#4d8af0,#1a56db);color:#fff;font-size:14px;font-weight:700}

  .state{display:inline-flex;align-items:center;gap:8px;margin-top:24px;padding:6px 14px 6px 10px;border-radius:999px;
    background:#f0fdf4;border:1px solid #bbf7d0;color:#15803d;font-size:12.5px;font-weight:600}
  .dot{position:relative;width:9px;height:9px;border-radius:50%;background:#22c55e;box-shadow:0 0 0 4px rgba(34,197,94,.16)}
  .dot::after{content:"";position:absolute;inset:-4px;border-radius:50%;border:1px solid rgba(34,197,94,.5);animation:pulse 2.4s ease-out infinite}
  @keyframes pulse{0%{transform:scale(.7);opacity:.9}70%{transform:scale(1.6);opacity:0}100%{opacity:0}}

  h1{margin:16px 0 0;font-size:24px;font-weight:700;line-height:1.35;letter-spacing:-.3px;color:#1f2937}
  p{margin:10px 0 0;font-size:13.5px;line-height:1.8;color:#6b7280}

  .links{display:flex;gap:10px;justify-content:center;flex-wrap:wrap;margin-top:26px}
  .links a{display:inline-flex;align-items:center;justify-content:center;height:44px;padding:0 22px;
    border-radius:11px;font-size:14px;font-weight:600;text-decoration:none;
    transition:transform .25s ease,box-shadow .25s ease,background .25s ease,border-color .25s ease}
  .links a.primary{background:linear-gradient(135deg,#4d8af0,#1a56db);color:#fff;
    box-shadow:0 4px 14px rgba(77,138,240,.35)}
  .links a.primary:hover{transform:translateY(-1px);box-shadow:0 8px 20px rgba(77,138,240,.30)}
  .links a.ghost{background:#fff;border:1px solid #eef0f4;color:#4b5563}
  .links a.ghost:hover{border-color:#c9d8f2;background:#f7f9fc}
  .links a:focus-visible{outline:2px solid #1a56db;outline-offset:3px}

  .foot{margin-top:22px;padding-top:16px;border-top:1px solid #eef0f4;font-size:11.5px;line-height:1.8;color:#6b7280}
  .foot code{font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:11px;
    padding:1px 6px;border-radius:5px;background:#f2f4f7;color:#4b5563}

  @media (max-width:480px){
    body{padding:14px}
    .card{padding:30px 22px 24px;border-radius:16px}
    .shapes{display:none}
    h1{font-size:21px}
  }
  @media (prefers-reduced-motion:reduce){.shapes i{animation:none}.dot::after{animation:none}}
</style>
</head>
<body>
<div class="bg-overlay"></div>
<div class="shapes"><i></i><i></i><i></i><i></i></div>
<main class="card">
  <div class="mark">
    <img src="<?= $logoUrl ?>" alt="" onerror="this.style.display='none';document.getElementById('mkFb').style.display='flex'">
    <span class="mk" id="mkFb" aria-hidden="true">栖</span>
    <span><?= $site ?></span>
  </div>
  <div class="state"><span class="dot" aria-hidden="true"></span>服务运行中</div>
  <h1>后端服务一切正常</h1>
  <p>API 已就绪，安装状态体检通过，社区可以正常收发数据。</p>
  <div class="links">
    <span class="ghost" style="display:inline-flex;align-items:center;justify-content:center;height:44px;padding:0 22px;border-radius:11px;font-size:14px;font-weight:600;background:#f7f9fc;border:1px solid #eef0f4;color:#9ca3af;cursor:not-allowed;">管理后台（内部）</span>
    <a class="ghost" href="install.php">系统信息</a>
  </div>
  <div class="foot">
    这个地址只作为服务状态页，客户端请直接调用 <code>/api/index.php</code>
  </div>
</main>
</body>
</html>