<?php
/**
 * CloudForge Theme - Article Detail
 */
$pageDescription = $article['summary'] ?: strip_tags(mb_substr($article['content'], 0, 200, 'UTF-8'));
$pageKeywords = $article['seo_keywords'] ?: $article['tags'];
$breadcrumbItems = [
    ['title' => '新闻资讯', 'url' => cms_url('news')],
    ['title' => $article['title']]
];
?>
<?php echo breadcrumb($breadcrumbItems); ?>

<div class="main-content">
    <div class="container container--sidebar">
        <div class="content-main">
            <article class="article-detail">
                <header class="article-detail-header">
                    <h1 class="article-detail-title"><?php echo xss_clean($article['title']); ?></h1>
                    <div class="article-detail-meta">
                        <span class="meta-item">
                            <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><rect x="2" y="3" width="10" height="9" rx="1" stroke="currentColor" stroke-width="1"/><path d="M2 5h10M5 2v2M9 2v2" stroke="currentColor" stroke-width="1" stroke-linecap="round"/></svg>
                            <?php echo date('Y-m-d H:i', strtotime($article['published_at'] ?: $article['created_at'])); ?>
                        </span>
                        <span class="meta-item">
                            <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><circle cx="7" cy="5" r="2.5" stroke="currentColor" stroke-width="1"/><path d="M2 12c0-2.5 2-4 5-4s5 1.5 5 4" stroke="currentColor" stroke-width="1"/></svg>
                            <?php echo xss_clean($article['author_name'] ?: '管理员'); ?>
                        </span>
                        <span class="meta-item">
                            <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M1 7c1.5-3 3.5-4.5 6-4.5S11.5 4 13 7c-1.5 3-3.5 4.5-6 4.5S2.5 10 1 7z" stroke="currentColor" stroke-width="1"/><circle cx="7" cy="7" r="2" stroke="currentColor" stroke-width="1"/></svg>
                            <?php echo $article['views']; ?> views
                        </span>
                    </div>
                    <?php if ($article['tags']): ?>
                    <div class="article-tags">
                        <?php foreach (explode(',', $article['tags']) as $tag): ?>
                        <a href="<?php echo cms_url('search', ['q' => trim($tag)]); ?>" class="tag"><?php echo xss_clean(trim($tag)); ?></a>
                        <?php endforeach; ?>
                    </div>
                    <?php endif; ?>
                </header>

                <div class="article-content">
                    <?php echo $article['content']; ?>
                </div>

                <?php
                $prev = db_query_one("SELECT `id`,`title`,`slug` FROM `articles` WHERE `status`=1 AND `id`<? ORDER BY `id` DESC LIMIT 1", [$article['id']]);
                $next = db_query_one("SELECT `id`,`title`,`slug` FROM `articles` WHERE `status`=1 AND `id`>? ORDER BY `id` ASC LIMIT 1", [$article['id']]);
                ?>
                <?php if ($prev || $next): ?>
                <nav class="article-nav">
                    <?php if ($prev): ?>
                    <a href="<?php echo cms_url('article', ['slug' => $prev['slug']]); ?>" class="article-nav-item article-nav-prev">
                        <span class="article-nav-label">&larr; 上一篇</span>
                        <span class="article-nav-title"><?php echo xss_clean(str_cut($prev['title'], 30)); ?></span>
                    </a>
                    <?php else: ?>
                    <span></span>
                    <?php endif; ?>
                    <?php if ($next): ?>
                    <a href="<?php echo cms_url('article', ['slug' => $next['slug']]); ?>" class="article-nav-item article-nav-next">
                        <span class="article-nav-label">下一篇 &rarr;</span>
                        <span class="article-nav-title"><?php echo xss_clean(str_cut($next['title'], 30)); ?></span>
                    </a>
                    <?php else: ?>
                    <span></span>
                    <?php endif; ?>
                </nav>
                <?php endif; ?>
            </article>
        </div>

        <aside class="sidebar">
            <div class="widget">
                <h3 class="widget-title">// 热门文章</h3>
                <ul class="widget-list widget-list--ranked">
                    <?php $hotArticles = db_query("SELECT `id`,`title`,`slug` FROM `articles` WHERE `status`=1 ORDER BY `views` DESC LIMIT 5"); ?>
                    <?php foreach ($hotArticles as $idx => $ha): ?>
                    <li>
                        <span class="rank-num"><?php echo sprintf('%02d', $idx + 1); ?></span>
                        <a href="<?php echo cms_url('article', ['slug' => $ha['slug']]); ?>"><?php echo xss_clean($ha['title']); ?></a>
                    </li>
                    <?php endforeach; ?>
                </ul>
            </div>
            <div class="widget">
                <h3 class="widget-title">// 推荐产品</h3>
                <ul class="widget-list">
                    <?php $recProducts = db_query("SELECT `id`,`title`,`slug` FROM `products` WHERE `status`=1 AND `is_recommend`=1 LIMIT 5"); ?>
                    <?php foreach ($recProducts as $rp): ?>
                    <li><a href="<?php echo cms_url('product', ['slug' => $rp['slug']]); ?>"><?php echo xss_clean($rp['title']); ?></a></li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </aside>
    </div>
</div>
