<?php
/**
 * 芋头CMS
 * ============================================================================
 * 版权所有 2016-2028 沅陵县乌托邦网络服务有限公司，并保留所有权利。
 * 网站地址: http://www.yutoucms.cn
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷。
 * ============================================================================
 * Author: 芋头 <mxgsf@qq.com>
 * Date: 2026-6-1
 */
$channelId = isset($_GET['id']) ? intval($_GET['id']) : 0;
$channel = $db->fetch("SELECT * FROM {$db->table('channel')} WHERE id = ?", [$channelId]);
if(empty($channel)) {
    header('Location: /');
    exit;
}

$contents = [];
$listUrl = '';

switch ($channel['channel_type']) {
    case 1: // 文章
        $contents = $db->fetchAll("SELECT * FROM {$db->table('article')} WHERE channel_id = ? AND status = 1 ORDER BY create_time DESC LIMIT 50", [$channelId]);
        break;
    case 2: // 产品
        if($channelId == 22) {
            $contents = $db->fetchAll("SELECT * FROM {$db->table('product')} WHERE status = 1 ORDER BY create_time DESC");
        } else {
            $contents = $db->fetchAll("SELECT * FROM {$db->table('product')} WHERE channel_id = ? AND status = 1 ORDER BY create_time DESC", [$channelId]);
        }
        break;
    case 3: // 单页
        $pageData = $db->fetch("SELECT * FROM {$db->table('page')} WHERE channel_id = ? AND status = 1 ORDER BY id DESC LIMIT 1", [$channelId]);
        if($pageData) {
            $contents = [$pageData];
            $listUrl = getUrl('page', $pageData['id']);
        }
        break;
    case 4: // 图集
        $contents = $db->fetchAll("SELECT * FROM {$db->table('image')} WHERE channel_id = ? AND status = 1 ORDER BY create_time DESC LIMIT 50", [$channelId]);
        break;
    case 5: // 下载
        $contents = $db->fetchAll("SELECT * FROM {$db->table('download')} WHERE channel_id = ? AND status = 1 ORDER BY create_time DESC LIMIT 50", [$channelId]);
        break;
    case 7: // 视频
        $contents = $db->fetchAll("SELECT * FROM {$db->table('video')} WHERE channel_id = ? AND status = 1 ORDER BY create_time DESC LIMIT 50", [$channelId]);
        break;
    case 8: // 留言
        $contents = $db->fetchAll("SELECT * FROM {$db->table('guestbook')} WHERE channel_id = ? AND is_show = 1 ORDER BY create_time DESC LIMIT 50", [$channelId]);
        break;
    case 9: // 招聘
        $contents = $db->fetchAll("SELECT * FROM {$db->table('recruit')} WHERE channel_id = ? AND status = 1 ORDER BY create_time DESC LIMIT 50", [$channelId]);
        break;
}
?>
<!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 dirname(__DIR__) . '/_header.php'; ?>

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

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

<section class="channel-list-page">
<div class="container">

<?php if($channel['channel_type'] == 3 && !empty($contents)): ?>
<?php $pd = $contents[0]; ?>
<div class="page-detail">
<h2 style="text-align:center;font-size:24px;color:#333;margin-bottom:20px;"><?php echo htmlspecialchars($pd['page_title'] ?? $pd['title']); ?></h2>
<div class="page-content-body"><?php echo $pd['page_content'] ?? $pd['content']; ?></div>
</div>
<?php elseif($channel['channel_type'] == 8): ?>
<div class="guestbook-list">
<?php foreach($contents as $g): ?>
<div class="guestbook-item">
<div class="guest-name"><?php echo htmlspecialchars($g['name']); ?></div>
<div class="guest-time"><?php echo date('Y-m-d H:i', strtotime($g['create_time'])); ?></div>
<div class="guest-content"><?php echo nl2br(htmlspecialchars($g['content'])); ?></div>
</div>
<?php endforeach; ?>
<?php if(empty($contents)): ?><p style="text-align:center;color:#999;padding:40px 0;">暂无留言</p><?php endif; ?>
</div>
<?php elseif($channel['channel_type'] == 9): ?>
<div class="recruit-list">
<?php foreach($contents as $r): ?>
<div class="recruit-item">
<h3><?php echo htmlspecialchars($r['title']); ?></h3>
<div class="recruit-meta">
<?php if(!empty($r['department'])): ?><span>部门：<?php echo htmlspecialchars($r['department']); ?></span><?php endif; ?>
<?php if(!empty($r['region'])): ?><span>工作地点：<?php echo htmlspecialchars($r['region']); ?></span><?php endif; ?>
<?php if(!empty($r['salary'])): ?><span>薪资：<?php echo htmlspecialchars($r['salary']); ?></span><?php endif; ?>
<?php if(!empty($r['work_type'])): ?><span>工作类型：<?php echo htmlspecialchars($r['work_type']); ?></span><?php endif; ?>
</div>
<div><?php echo nl2br(htmlspecialchars($r['content'] ?? '')); ?></div>
</div>
<?php endforeach; ?>
<?php if(empty($contents)): ?><p style="text-align:center;color:#999;padding:40px 0;">暂无招聘职位</p><?php endif; ?>
</div>
<?php elseif($channel['channel_type'] == 5): ?>
<div class="download-list" style="max-width:800px;margin:0 auto;">
<?php foreach($contents as $d): ?>
<div class="download-item">
<div class="download-icon">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></svg>
</div>
<div class="download-info">
<h4><?php echo htmlspecialchars($d['title']); ?></h4>
<p><?php echo htmlspecialchars($d['summary'] ?? ''); ?></p>
</div>
<a href="<?php echo htmlspecialchars($d['file_path']); ?>" class="download-btn" download>下载</a>
</div>
<?php endforeach; ?>
<?php if(empty($contents)): ?><p style="text-align:center;color:#999;padding:40px 0;">暂无下载内容</p><?php endif; ?>
</div>
<?php elseif($channel['channel_type'] == 7): ?>
<div class="video-list" style="display:grid;grid-template-columns:repeat(4,1fr);gap:20px;">
<?php foreach($contents as $v): ?>
<div class="channel-card" style="position:relative;">
<a href="<?php echo getUrl('video', $v['id']); ?>" class="channel-card-img" style="position:relative;">
<img src="<?php echo htmlspecialchars($v['video_cover'] ?? $site['template_dir'].'images/nopic.jpg'); ?>">
<div style="position:absolute;inset:0;display:flex;align-items:center;justify-content:center;background:rgba(0,0,0,0.3);">
<svg width="50" height="50" viewBox="0 0 24 24" fill="rgba(255,255,255,0.9)" stroke="none"><polygon points="5 3 19 12 5 21 5 3"/></svg>
</div>
</a>
<div class="channel-card-info"><h3><?php echo htmlspecialchars($v['title']); ?></h3></div>
</div>
<?php endforeach; ?>
<?php if(empty($contents)): ?><p style="text-align:center;color:#999;padding:40px 0;">暂无视频</p><?php endif; ?>
</div>
<?php else: ?>
<div class="channel-grid">
<?php if(!empty($contents)): foreach($contents as $c): ?>
<?php
$img = $c['litpic'] ?? $c['video_cover'] ?? $site['template_dir'].'images/nopic.jpg';
$title = $c['title'] ?? $c['page_title'] ?? '';
$summary = '';
$detailUrl = '#';
if($channel['channel_type'] == 1) {
    $summary = mb_substr(strip_tags($c['content'] ?? ''), 0, 100);
    $detailUrl = getUrl('article', $c['id']);
} elseif($channel['channel_type'] == 2) {
    $summary = $c['summary'] ?? '';
    $detailUrl = getUrl('product', $c['id']);
} elseif($channel['channel_type'] == 4) {
    $summary = $c['summary'] ?? '';
    $detailUrl = getUrl('image', $c['id']);
}
?>
<div class="channel-card">
<a href="<?php echo $detailUrl; ?>" class="channel-card-img">
<img src="<?php echo htmlspecialchars($img); ?>" alt="<?php echo htmlspecialchars($title); ?>">
</a>
<div class="channel-card-info">
<h3><a href="<?php echo $detailUrl; ?>" style="color:#333;"><?php echo htmlspecialchars($title); ?></a></h3>
<p><?php echo htmlspecialchars($summary); ?></p>
<?php if($channel['channel_type'] == 2 && !empty($c['price'])): ?>
<p class="p-price">¥<?php echo htmlspecialchars($c['price']); ?></p>
<?php endif; ?>
</div>
</div>
<?php endforeach; else: ?>
<p style="text-align:center;color:#999;padding:60px 0;">暂无内容</p>
<?php endif; ?>
</div>
<?php endif; ?>

</div>
</section>

<?php include dirname(__DIR__) . '/_footer.php'; ?>

</body>
</html>