<?php
/**
 * 芋头CMS
 * ============================================================================
 * 版权所有 2016-2028 沅陵县乌托邦网络服务有限公司，并保留所有权利。
 * 网站地址: http://www.yutoucms.cn
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷。
 * ============================================================================
 * Author: 芋头 <mxgsf@qq.com>
 * Date: 2026-6-1
 */
$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$pageSize = 10;
$offset = ($page - 1) * $pageSize;
$channelId = isset($_GET['id']) ? intval($_GET['id']) : 0;
$channel = $db->fetch("SELECT * FROM {$db->table('channel')} WHERE id = ? AND channel_type = 1", [$channelId]);
if(empty($channel)) { header('Location: /'); exit; }
$totalRow = $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('article')} WHERE channel_id = ? AND status = 1", [$channelId]);
$total = $totalRow ? intval($totalRow['cnt']) : 0;
$articles = $db->fetchAll("SELECT * FROM {$db->table('article')} WHERE channel_id = ? AND status = 1 ORDER BY create_time DESC LIMIT ? OFFSET ?", [$channelId, $pageSize, $offset]);
$totalPages = $total > 0 ? ceil($total / $pageSize) : 1;
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo htmlspecialchars($channel['channel_name']); ?> - <?php echo htmlspecialchars($site['site_name']); ?></title>
<link rel="stylesheet" href="<?php echo $site['template_dir']; ?>css/style.css">
</head>
<body>
<?php include __DIR__ . '/../_header.php'; ?>

<div class="channel-banner channel-banner-article">
<div class="container">
<h1><?php echo htmlspecialchars($channel['channel_name']); ?></h1>
</div>
</div>

<div class="breadcrumb">
<div class="container">
<a href="/">首页</a><span>&gt;</span><?php echo htmlspecialchars($channel['channel_name']); ?>
</div>
</div>

<section class="list-page">
<div class="container">
<div class="article-list">
<?php if(!empty($articles)): foreach($articles as $a): ?>
<div class="article-list-item">
<a href="<?php echo getUrl('article', $a['id']); ?>" class="article-list-img">
<img src="<?php echo htmlspecialchars($a['litpic'] ?: $site['template_dir'].'images/nopic.jpg'); ?>" alt="<?php echo htmlspecialchars($a['title']); ?>">
</a>
<div class="article-list-body">
<div class="article-list-date"><?php echo date('Y-m-d', strtotime($a['create_time'])); ?></div>
<h3><a href="<?php echo getUrl('article', $a['id']); ?>"><?php echo htmlspecialchars($a['title']); ?></a></h3>
<p><?php echo htmlspecialchars(mb_substr(strip_tags($a['content'] ?? ''), 0, 200)); ?></p>
<a href="<?php echo getUrl('article', $a['id']); ?>" class="article-list-more">阅读更多 &gt;</a>
</div>
</div>
<?php endforeach; else: ?>
<p style="text-align:center;color:#999;padding:60px 0;">暂无文章</p>
<?php endif; ?>
</div>

<?php if($totalPages > 1): ?>
<div class="pagination">
<a href="?m=channel&id=<?php echo $channelId; ?>&page=<?php echo $page - 1; ?>" class="<?php echo $page <= 1 ? 'disabled' : ''; ?>">上一页</a>
<?php for($i = max(1, $page - 2); $i <= min($totalPages, $page + 2); $i++): ?>
<a href="?m=channel&id=<?php echo $channelId; ?>&page=<?php echo $i; ?>" class="<?php echo $i == $page ? 'current' : ''; ?>"><?php echo $i; ?></a>
<?php endfor; ?>
<a href="?m=channel&id=<?php echo $channelId; ?>&page=<?php echo $page - 1; ?>" class="<?php echo $page >= $totalPages ? 'disabled' : ''; ?>">下一页</a>
</div>
<?php endif; ?>
</div>
</section>
<?php include __DIR__ . '/../_footer.php'; ?>
</body>
</html>