<?php
/**
 * CloudForge Theme - News List
 */
$pageDescription = '最新新闻资讯、行业动态 - ' . SITE_NAME;
$breadcrumbItems = [['title' => '新闻资讯']];
$urlPattern = (get_setting('url_mode', 1) == 1) ? '?p=news&page=%d' : '/news/%d.html';
$pagination = paginate($total, $page, $pageSize, $urlPattern);
?>
<?php echo breadcrumb($breadcrumbItems); ?>

<div class="main-content">
    <div class="container container--sidebar">
        <div class="content-main">
            <div class="section-header">
                <span class="section-label">// NEWS</span>
                <h2 class="section-title">新闻资讯</h2>
            </div>

            <?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 $a): ?>
                <article class="article-item">
                    <?php if ($a['cover_image']): ?>
                    <div class="article-thumb">
                        <a href="<?php echo cms_url('article', ['slug' => $a['slug']]); ?>">
                            <img src="<?php echo $a['cover_image']; ?>" alt="<?php echo xss_clean($a['title']); ?>" loading="lazy">
                        </a>
                    </div>
                    <?php endif; ?>
                    <div class="article-info">
                        <div class="article-title">
                            <a href="<?php echo cms_url('article', ['slug' => $a['slug']]); ?>"><?php echo xss_clean($a['title']); ?></a>
                        </div>
                        <div class="article-summary"><?php echo xss_clean(str_cut($a['summary'] ?: strip_tags($a['content']), 150)); ?></div>
                        <div class="article-meta">
                            <time><?php echo date('Y/m/d', strtotime($a['published_at'] ?: $a['created_at'])); ?></time>
                            <span class="meta-sep"></span>
                            <span><?php echo $a['views']; ?> views</span>
                            <?php if ($a['is_top']): ?><span class="badge badge--top">置顶</span><?php endif; ?>
                            <?php if ($a['is_recommend']): ?><span class="badge badge--rec">推荐</span><?php endif; ?>
                        </div>
                    </div>
                </article>
                <?php endforeach; ?>
            </div>

            <?php if ($totalPage > 1): ?>
            <div class="pagination">
                <?php if ($page > 1): ?><a href="<?php echo cms_url('news', ['page' => $page - 1]); ?>" class="page-prev">&larr; 上一页</a><?php endif; ?>
                <?php for ($i = max(1, $page - 2); $i <= min($totalPage, $page + 2); $i++): ?>
                    <?php if ($i == $page): ?><span class="current"><?php echo $i; ?></span>
                    <?php else: ?><a href="<?php echo cms_url('news', ['page' => $i]); ?>"><?php echo $i; ?></a>
                    <?php endif; ?>
                <?php endfor; ?>
                <?php if ($page < $totalPage): ?><a href="<?php echo cms_url('news', ['page' => $page + 1]); ?>" class="page-next">下一页 &rarr;</a><?php endif; ?>
            </div>
            <?php endif; ?>
            <?php endif; ?>
        </div>

        <aside class="sidebar">
            <div class="widget">
                <h3 class="widget-title">// 文章分类</h3>
                <ul class="widget-list">
                    <li><a href="<?php echo cms_url('news'); ?>">全部</a></li>
                    <?php $cats = db_query("SELECT * FROM `categories` WHERE `status`=1 ORDER BY `sort_order` ASC"); ?>
                    <?php foreach ($cats as $cat): ?>
                    <li><a href="<?php echo cms_url('news', ['category' => $cat['slug']]); ?>"><?php echo xss_clean($cat['name']); ?></a></li>
                    <?php endforeach; ?>
                </ul>
            </div>
            <div class="widget">
                <h3 class="widget-title">// 热门文章</h3>
                <ul class="widget-list widget-list--ranked">
                    <?php $hotArticles = db_query("SELECT `title`,`slug`,`views` FROM `articles` WHERE `status`=1 ORDER BY `views` DESC LIMIT 8"); ?>
                    <?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>
