<?php
/**
 * 芋头CMS - 家具定制企业网站模板 - 文章列表
 */
if (!defined('ROOT_PATH')) { exit('Access Denied'); }

require_once __DIR__ . '/../_header.php';

// 所有栏目分类
$allChannels = $db->fetchAll("SELECT * FROM {$db->table('channel')} WHERE channel_type = 1 AND is_show = 1 ORDER BY list_order ASC, id ASC");

// 当前选中栏目
$curChannelId = isset($_GET['channel_id']) ? intval($_GET['channel_id']) : 0;

// 按栏目筛选文章
$sql = "SELECT a.*, c.channel_name FROM {$db->table('article')} a LEFT JOIN {$db->table('channel')} c ON a.channel_id=c.id WHERE a.status=1";
$params = [];
if ($curChannelId > 0) {
    $sql .= " AND a.channel_id = ?";
    $params[] = $curChannelId;
}
$sql .= " ORDER BY a.create_time DESC";
$allArticles = $db->fetchAll($sql, $params);
?>

<div style="padding-top:70px;">
<div style="height:280px;background:linear-gradient(135deg,var(--dark) 0%,var(--primary-dark) 100%);display:flex;align-items:center;justify-content:center;">
<div style="text-align:center;color:white;">
<h1 style="font-size:36px;margin-bottom:10px;"><?php echo htmlspecialchars($channel['channel_name'] ?? '新闻资讯'); ?></h1>
</div>
</div>

<section class="section">
<div class="container">
<div style="display:grid;grid-template-columns:220px 1fr;gap:30px;align-items:start;">

<!-- 左侧栏目分类 -->
<aside style="background:#fff;border-radius:10px;padding:20px;box-shadow:0 2px 8px rgba(0,0,0,0.06);position:sticky;top:90px;">
<h3 style="font-size:16px;font-weight:600;color:var(--dark);margin-bottom:16px;padding-bottom:12px;border-bottom:2px solid var(--border);">新闻分类</h3>
<ul style="list:none;padding:0;margin:0;">
<li>
<a href="?m=article" style="display:flex;align-items:center;justify-content:space-between;padding:10px 14px;border-radius:6px;font-size:14px;color:<?php echo $curChannelId == 0 ? 'var(--accent)' : 'var(--text)'; ?>;background:<?php echo $curChannelId == 0 ? 'var(--bg-light)' : 'transparent'; ?>;text-decoration:none;transition:all 0.2s;" onmouseover="this.style.background='var(--bg-light)'" onmouseout="this.style.background='<?php echo $curChannelId == 0 ? 'var(--bg-light)' : 'transparent'; ?>'">
全部新闻
<span style="font-size:12px;color:var(--gray-light);"><?php echo count($allArticles); ?></span>
</a>
</li>
<?php foreach ($allChannels as $ch): ?>
<li>
<a href="?m=article&channel_id=<?php echo $ch['id']; ?>" style="display:flex;align-items:center;justify-content:space-between;padding:10px 14px;border-radius:6px;font-size:14px;color:<?php echo $curChannelId == $ch['id'] ? 'var(--accent)' : 'var(--text)'; ?>;background:<?php echo $curChannelId == $ch['id'] ? 'var(--bg-light)' : 'transparent'; ?>;text-decoration:none;transition:all 0.2s;" onmouseover="this.style.background='var(--bg-light)'" onmouseout="this.style.background='<?php echo $curChannelId == $ch['id'] ? 'var(--bg-light)' : 'transparent'; ?>'">
<?php echo htmlspecialchars($ch['channel_name']); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</aside>

<!-- 右侧文章列表 -->
<div class="news-list-right">
<?php if (!empty($allArticles)): ?>
<?php foreach ($allArticles as $a): ?>
<div style="display:flex;gap:20px;padding:20px 0;border-bottom:1px solid var(--border);">
<?php if (!empty($a['litpic'])): ?>
<div style="width:200px;height:140px;flex-shrink:0;border-radius:8px;overflow:hidden;">
<img src="<?php echo htmlspecialchars($a['litpic']); ?>" style="width:100%;height:100%;object-fit:cover;" alt="">
</div>
<?php endif; ?>
<div style="flex:1;">
<div style="display:flex;align-items:center;gap:10px;margin-bottom:8px;">
<?php if (!empty($a['channel_name'])): ?>
<span style="font-size:12px;padding:2px 8px;background:var(--bg-light);color:var(--accent);border-radius:3px;"><?php echo htmlspecialchars($a['channel_name']); ?></span>
<?php endif; ?>
<span style="font-size:12px;color:var(--gray-light);"><?php echo date('Y-m-d', $a['create_time']); ?></span>
</div>
<h3 style="font-size:18px;color:var(--dark);margin-bottom:10px;"><a href="<?php echo getUrl('article', $a['id']); ?>" style="text-decoration:none;color:inherit;"><?php echo htmlspecialchars($a['title']); ?></a></h3>
<p style="font-size:14px;color:var(--gray);line-height:1.6;"><?php echo htmlspecialchars(mb_substr(strip_tags($a['description'] ?? ''), 0, 120)); ?></p>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<div style="text-align:center;padding:60px 0;color:var(--gray-light);">暂无文章</div>
<?php endif; ?>
</div>

</div>
</div>
</section>
</div>

<?php require_once __DIR__ . '/../_footer.php'; ?>