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

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

// 获取栏目信息
if (!isset($channel) && isset($id) && $id > 0) {
    $channel = $db->fetch("SELECT * FROM {$db->table('channel')} WHERE id=? AND is_show=1", [$id]);
}

// 根据栏目类型确定分类名称和数据
$typeConfig = [
    1 => ['label' => '新闻分类', 'table' => 'article'],
    2 => ['label' => '产品分类', 'table' => 'product'],
    3 => ['label' => '单页分类', 'table' => 'article'],
    4 => ['label' => '图集分类', 'table' => 'image'],
    5 => ['label' => '下载分类', 'table' => 'download'],
    7 => ['label' => '视频分类', 'table' => 'video'],
    8 => ['label' => '留言分类', 'table' => 'guestbook'],
    9 => ['label' => '招聘分类', 'table' => 'recruit'],
];
$channelType = intval($channel['channel_type'] ?? 1);
$cfg = $typeConfig[$channelType] ?? ['label' => '内容分类', 'table' => 'article'];
$catLabel = $cfg['label'];
$tbl = $cfg['table'];

// 同类型所有栏目（用于左侧分类）
$allSameTypeChannels = $db->fetchAll("SELECT * FROM {$db->table('channel')} WHERE channel_type = ? AND is_show = 1 ORDER BY list_order ASC, id ASC", [$channelType]);

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

// 按栏目筛选内容
$displayList = [];
if ($curChannelId > 0) {
    $displayList = $db->fetchAll("SELECT * FROM {$db->table($tbl)} WHERE channel_id=? AND status=1 ORDER BY create_time DESC LIMIT 100", [$curChannelId]);
} else {
    $channelIds = array_column($allSameTypeChannels, 'id');
    if (!empty($channelIds)) {
        $placeholders = implode(',', array_fill(0, count($channelIds), '?'));
        $displayList = $db->fetchAll("SELECT * FROM {$db->table($tbl)} WHERE channel_id IN ({$placeholders}) AND status=1 ORDER BY create_time DESC LIMIT 100", $channelIds);
    }
}
?>

<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);"><?php echo $catLabel; ?></h3>
<ul style="list:none;padding:0;margin:0;">
<li>
<a href="?m=channel&id=<?php echo intval($id); ?>&channel_id=0" 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;">
全部
<span style="font-size:12px;color:var(--gray-light);"><?php echo count($displayList); ?></span>
</a>
</li>
<?php foreach ($allSameTypeChannels as $ch): ?>
<li>
<a href="?m=channel&id=<?php echo intval($id); ?>&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;">
<?php echo htmlspecialchars($ch['channel_name']); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</aside>

<!-- 右侧内容列表 -->
<div>
<?php if (!empty($displayList)): ?>
<div style="display:grid;grid-template-columns:repeat(4,1fr);gap:20px;">
<?php foreach ($displayList as $item): ?>
<a href="<?php echo getUrl($tbl, $item['id']); ?>" style="background:#fff;border-radius:8px;overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,0.06);transition:all 0.3s;text-decoration:none;" onmouseover="this.style.transform='translateY(-4px)';this.style.boxShadow='0 8px 24px rgba(0,0,0,0.12)'" onmouseout="this.style.transform='none';this.style.boxShadow='0 2px 8px rgba(0,0,0,0.06)'">
<div style="height:200px;overflow:hidden;">
<?php if (!empty($item['litpic'])): ?>
<img src="<?php echo htmlspecialchars($item['litpic']); ?>" style="width:100%;height:100%;object-fit:cover;" alt="<?php echo htmlspecialchars($item['title']); ?>">
<?php else: ?>
<img src="https://picsum.photos/400/300?random=<?php echo intval($item['id']); ?>" style="width:100%;height:100%;object-fit:cover;" alt="">
<?php endif; ?>
</div>
<div style="padding:16px;">
<h3 style="font-size:15px;font-weight:600;color:var(--dark);margin-bottom:8px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"><?php echo htmlspecialchars($item['title']); ?></h3>
<span style="font-size:12px;color:var(--gray-light);"><?php echo date('Y-m-d', $item['create_time']); ?></span>
</div>
</a>
<?php endforeach; ?>
</div>
<?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'; ?>