<?php
/**
 * 2026企业CMS - 新闻列表页（优化版）
 * UI Designer: 像素君
 * 设计系统版本: 2.0
 */
$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" style="display: flex; gap: var(--space-8);">
        <!-- 主内容区 -->
        <div style="flex: 1;">
            <div class="news-header">
                <h1 class="section-title text-left">新闻资讯</h1>
                <p style="color: var(--color-gray-500); margin-top: var(--space-2);">
                    了解最新行业动态和公司新闻
                </p>
            </div>
            
            <?php if (empty($articles)): ?>
                <div class="empty-state">
                    <div class="empty-state-icon">📰</div>
                    <p class="empty-state-text">暂无文章</p>
                </div>
            <?php else: ?>
                <div class="article-list">
                    <?php foreach ($articles as $a): ?>
                        <div 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']); ?>">
                                    </a>
                                </div>
                            <?php endif; ?>
                            <div class="article-info">
                                <h2 class="article-title">
                                    <a href="<?php echo cms_url('article', ['slug' => $a['slug']]); ?>">
                                        <?php echo xss_clean($a['title']); ?>
                                    </a>
                                </h2>
                                <p class="article-summary">
                                    <?php echo xss_clean(str_cut($a['summary'] ?: strip_tags($a['content']), 150)); ?>
                                </p>
                                <div class="article-meta">
                                    <span>
                                        <svg width="14" height="14" viewBox="0 0 14 14" fill="none" style="margin-right: 4px; vertical-align: middle;">
                                            <path d="M7 0C3.13 0 0 3.13 0 7C0 10.87 3.13 14 7 14C10.87 14 14 10.87 14 7C14 3.13 10.87 0 7 0ZM10.5 9.5H6C5.17 9.5 4.5 8.83 4.5 8V3.5H6V8H10.5V9.5Z" fill="currentColor"/>
                                        </svg>
                                        <?php echo date('Y-m-d', strtotime($a['published_at'] ?: $a['created_at'])); ?>
                                    </span>
                                    <span style="margin-left: var(--space-4);">
                                        <svg width="14" height="14" viewBox="0 0 14 14" fill="none" style="margin-right: 4px; vertical-align: middle;">
                                            <path d="M7 0C3.13 0 0 3.13 0 7C0 10.87 3.13 14 7 14C10.87 14 14 10.87 14 7C14 3.13 10.87 0 7 0ZM7 12.25C3.9 12.25 1.75 10.1 1.75 7C1.75 3.9 3.9 1.75 7 1.75C10.1 1.75 12.25 3.9 12.25 7C12.25 10.1 10.1 12.25 7 12.25ZM9.5 6.125H7.625V4.25H6.375V6.125H4.5V7.375H6.375V9.25H7.625V7.375H9.5V6.125Z" fill="currentColor"/>
                                        </svg>
                                        浏览：<?php echo $a['views']; ?>
                                    </span>
                                    <?php if ($a['is_top']): ?>
                                        <span class="article-badge badge-top">置顶</span>
                                    <?php endif; ?>
                                    <?php if ($a['is_recommend']): ?>
                                        <span class="article-badge badge-recommend">推荐</span>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
                
                <!-- 分页 -->
                <?php if ($totalPage > 1): ?>
                    <div class="pagination">
                        <?php if ($page > 1): ?>
                            <a href="<?php echo cms_url('news', ['page' => $page-1]); ?>">上一页</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]); ?>">下一页</a>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>
            <?php endif; ?>
        </div>
        
        <!-- 侧边栏 -->
        <aside class="sidebar">
            <div class="widget">
                <h3 class="widget-title">文章分类</h3>
                <ul class="sidebar-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="sidebar-list">
                    <?php $hotArticles = db_query("SELECT `title`,`slug` FROM `articles` WHERE `status`=1 ORDER BY `views` DESC LIMIT 8"); ?>
                    <?php foreach ($hotArticles as $ha): ?>
                        <li>
                            <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>

<!-- 新闻列表页额外样式 -->
<style>
.news-header {
    margin-bottom: var(--space-8);
}

.article-badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: var(--radius-sm);
    font-size: var(--text-xs);
    font-weight: 600;
    margin-left: var(--space-2);
}

.badge-top {
    background: var(--color-error);
    color: white;
}

.badge-recommend {
    background: var(--color-secondary);
    color: white;
}

.sidebar-list {
    list-style: none;
}

.sidebar-list li {
    padding: var(--space-3) 0;
    border-bottom: 1px solid var(--color-gray-100);
}

.sidebar-list li:last-child {
    border-bottom: none;
}

.sidebar-list li a {
    color: var(--color-gray-600);
    font-size: var(--text-sm);
    transition: all var(--transition-fast);
    display: block;
}

.sidebar-list li a:hover {
    color: var(--color-secondary);
    padding-left: var(--space-2);
}

.empty-state {
    text-align: center;
    padding: var(--space-16);
    color: var(--color-gray-400);
}

.empty-state-icon {
    font-size: 64px;
    margin-bottom: var(--space-4);
}

.empty-state-text {
    font-size: var(--text-lg);
}

@media (max-width: 1024px) {
    .container {
        flex-direction: column;
    }
    
    .sidebar {
        width: 100%;
    }
}

@media (max-width: 768px) {
    .article-item {
        flex-direction: column;
    }
    
    .article-thumb {
        width: 100%;
        height: 200px;
    }
}
</style>
