<?php
/**
 * CloudForge Theme - Category Article List
 */
?>
<?php echo category_breadcrumb($category['id']); ?>

<section class="category-hero">
    <div class="container">
        <span class="section-label">// CATEGORY</span>
        <h1><?php echo htmlspecialchars($category['name']); ?></h1>
        <?php if ($category['description']): ?>
        <p class="category-hero-desc"><?php echo htmlspecialchars($category['description']); ?></p>
        <?php endif; ?>
        <p class="category-hero-count">共 <?php echo $total; ?> 篇文章</p>
    </div>
</section>

<div class="main-content">
    <div class="container container--sidebar">
        <div class="content-main">
            <?php if (empty($articles)): ?>
            <div class="empty-state">
                <svg width="48" height="48" viewBox="0 0 48 48" fill="none" opacity="0.3"><rect x="8" y="10" width="32" height="28" rx="2" stroke="currentColor" stroke-width="1.5"/><path d="M14 18h20M14 24h20M14 30h12" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/></svg>
                <p>该分类下暂无文章</p>
            </div>
            <?php else: ?>
            <div class="article-list">
                <?php foreach ($articles as $article): ?>
                <article class="article-item">
                    <?php if ($article['cover_image']): ?>
                    <div class="article-thumb">
                        <a href="<?php echo cms_url('article', ['slug' => $article['slug']]); ?>">
                            <img src="<?php echo $article['cover_image']; ?>" alt="<?php echo htmlspecialchars($article['title']); ?>" loading="lazy">
                        </a>
                    </div>
                    <?php endif; ?>
                    <div class="article-info">
                        <div class="article-title">
                            <a href="<?php echo cms_url('article', ['slug' => $article['slug']]); ?>"><?php echo htmlspecialchars($article['title']); ?></a>
                        </div>
                        <div class="article-meta">
                            <time><?php echo date('Y/m/d', strtotime($article['published_at'] ?? $article['created_at'])); ?></time>
                            <span class="meta-sep"></span>
                            <span><?php echo $article['views']; ?> views</span>
                        </div>
                        <?php if ($article['summary']): ?>
                        <div class="article-summary"><?php echo htmlspecialchars($article['summary']); ?></div>
                        <?php endif; ?>
                        <a href="<?php echo cms_url('article', ['slug' => $article['slug']]); ?>" class="link-arrow">
                            <span>阅读全文</span>
                            <svg width="14" height="14" viewBox="0 0 16 16" fill="none"><path d="M3 8h10M9 4l4 4-4 4" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>
                        </a>
                    </div>
                </article>
                <?php endforeach; ?>
            </div>

            <?php if ($totalPage > 1): ?>
            <div class="pagination">
                <?php
                $urlPattern = '/index.php?p=category&slug=' . $category['slug'] . '&page=%d';
                echo paginate($total, $page, $pageSize, $urlPattern);
                ?>
            </div>
            <?php endif; ?>
            <?php endif; ?>
        </div>

        <aside class="sidebar">
            <div class="widget">
                <h3 class="widget-title">// 子分类</h3>
                <?php
                $subcategories = db_query("SELECT * FROM `categories` WHERE `parent_id`=? AND `status`=1 ORDER BY `sort_order` ASC", [$category['id']]);
                if ($subcategories):
                ?>
                <ul class="widget-list">
                    <?php foreach ($subcategories as $subcat): ?>
                    <li><a href="<?php echo cms_url('category', ['slug' => $subcat['slug']]); ?>"><?php echo htmlspecialchars($subcat['name']); ?></a></li>
                    <?php endforeach; ?>
                </ul>
                <?php else: ?>
                <p class="widget-empty">暂无子分类</p>
                <?php endif; ?>
            </div>
            <div class="widget">
                <h3 class="widget-title">// 热门文章</h3>
                <ul class="widget-list widget-list--ranked">
                    <?php $hotArticles = db_query("SELECT `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>
        </aside>
    </div>
</div>
