<?php
/**
 * 芋头CMS
 * ============================================================================
 * 版权所有 2016-2028 沅陵县乌托邦网络服务有限公司，并保留所有权利。
 * 网站地址: http://www.yutoucms.cn
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷。
 * ============================================================================
 * Author: 芋头 <mxgsf@qq.com>
 * Date: 2026-5-30
 */
if (!defined('ROOT_PATH')) { exit('Access Denied'); }

if (!isset($channel) && !empty($id)) {
    $channel = $db->fetch("SELECT * FROM {$db->table('channel')} WHERE id = ?", [intval($id)]);
}
$channel = $channel ?? [];
$channelType = intval($channel['channel_type'] ?? 1);
$pageTitle = $channel['channel_name'] ?? '栏目页';
$sideChannels = [];
$contentList = [];
$currentNavId = !empty($_GET['channel_id']) ? intval($_GET['channel_id']) : intval($id ?? 0);

switch ($channelType) {
    case 2:
        if (intval($id) === 22) {
            $sideChannels = $db->fetchAll("SELECT * FROM {$db->table('channel')} WHERE parent_id = 22 AND is_show = 1 ORDER BY list_order ASC, id ASC");
        } else {
            $sideChannels = $db->fetchAll("SELECT * FROM {$db->table('channel')} WHERE parent_id = ? AND is_show = 1 ORDER BY list_order ASC, id ASC", [intval($id)]);
            if (!$sideChannels && !empty($channel) && !empty($channel['parent_id'])) {
                $sideChannels = $db->fetchAll("SELECT * FROM {$db->table('channel')} WHERE parent_id = ? AND is_show = 1 ORDER BY list_order ASC, id ASC", [intval($channel['parent_id'])]);
            }
        }
        $channelIds = [intval($id)];
        foreach ($sideChannels as $sub) { $channelIds[] = intval($sub['id']); }
        $channelIds = array_values(array_unique(array_filter($channelIds)));
        if ($channelIds) {
            $placeholders = implode(',', array_fill(0, count($channelIds), '?'));
            $contentList = $db->fetchAll("SELECT p.*, c.channel_name FROM {$db->table('product')} p LEFT JOIN {$db->table('channel')} c ON p.channel_id=c.id WHERE p.status=1 AND p.channel_id IN ({$placeholders}) ORDER BY p.create_time DESC LIMIT 100", $channelIds);
        }
        if (!$contentList && !empty($sideChannels)) {
            $fallbackIds = array_map(function($x){ return intval($x['id']); }, $sideChannels);
            $fallbackIds = array_values(array_unique(array_filter($fallbackIds)));
            if ($fallbackIds) {
                $placeholders = implode(',', array_fill(0, count($fallbackIds), '?'));
                $contentList = $db->fetchAll("SELECT p.*, c.channel_name FROM {$db->table('product')} p LEFT JOIN {$db->table('channel')} c ON p.channel_id=c.id WHERE p.status=1 AND p.channel_id IN ({$placeholders}) ORDER BY p.create_time DESC LIMIT 100", $fallbackIds);
            }
        }
        break;
    case 4:
        $sideChannels = $db->fetchAll("SELECT * FROM {$db->table('channel')} WHERE parent_id = ? AND is_show = 1 ORDER BY list_order ASC, id ASC", [intval($id)]);
        if (!$sideChannels && !empty($channel) && !empty($channel['parent_id'])) {
            $sideChannels = $db->fetchAll("SELECT * FROM {$db->table('channel')} WHERE parent_id = ? AND is_show = 1 ORDER BY list_order ASC, id ASC", [intval($channel['parent_id'])]);
        }
        $channelIds = [intval($id)];
        foreach ($sideChannels as $sub) { $channelIds[] = intval($sub['id']); }
        $channelIds = array_values(array_unique(array_filter($channelIds)));
        if ($channelIds) {
            $placeholders = implode(',', array_fill(0, count($channelIds), '?'));
            $contentList = $db->fetchAll("SELECT i.*, c.channel_name FROM {$db->table('image')} i LEFT JOIN {$db->table('channel')} c ON i.channel_id=c.id WHERE i.status=1 AND i.channel_id IN ({$placeholders}) ORDER BY i.create_time DESC LIMIT 100", $channelIds);
        }
        break;
    case 5:
        $sideChannels = $db->fetchAll("SELECT * FROM {$db->table('channel')} WHERE parent_id = ? AND is_show = 1 ORDER BY list_order ASC, id ASC", [intval($id)]);
        $contentList = $db->fetchAll("SELECT d.*, c.channel_name FROM {$db->table('download')} d LEFT JOIN {$db->table('channel')} c ON d.channel_id=c.id WHERE d.status=1 AND d.channel_id = ? ORDER BY d.create_time DESC LIMIT 100", [intval($id)]);
        break;
    case 7:
        $sideChannels = $db->fetchAll("SELECT * FROM {$db->table('channel')} WHERE parent_id = ? AND is_show = 1 ORDER BY list_order ASC, id ASC", [intval($id)]);
        $contentList = $db->fetchAll("SELECT v.*, c.channel_name FROM {$db->table('video')} v LEFT JOIN {$db->table('channel')} c ON v.channel_id=c.id WHERE v.status=1 AND v.channel_id = ? ORDER BY v.create_time DESC LIMIT 100", [intval($id)]);
        break;
    case 9:
        $sideChannels = $db->fetchAll("SELECT * FROM {$db->table('channel')} WHERE parent_id = ? AND is_show = 1 ORDER BY list_order ASC, id ASC", [intval($id)]);
        if (!$sideChannels && !empty($channel) && !empty($channel['parent_id'])) {
            $sideChannels = $db->fetchAll("SELECT * FROM {$db->table('channel')} WHERE parent_id = ? AND is_show = 1 ORDER BY list_order ASC, id ASC", [intval($channel['parent_id'])]);
        }
        $channelIds = [intval($id)];
        foreach ($sideChannels as $sub) { $channelIds[] = intval($sub['id']); }
        $channelIds = array_values(array_unique(array_filter($channelIds)));
        if ($channelIds) {
            $placeholders = implode(',', array_fill(0, count($channelIds), '?'));
            $contentList = $db->fetchAll("SELECT r.*, c.channel_name FROM {$db->table('recruit')} r LEFT JOIN {$db->table('channel')} c ON r.channel_id=c.id WHERE r.status=1 AND r.channel_id IN ({$placeholders}) ORDER BY r.create_time DESC LIMIT 100", $channelIds);
        }
        if (!$contentList && !empty($sideChannels)) {
            $fallbackIds = array_map(function($x){ return intval($x['id']); }, $sideChannels);
            $fallbackIds = array_values(array_unique(array_filter($fallbackIds)));
            if ($fallbackIds) {
                $placeholders = implode(',', array_fill(0, count($fallbackIds), '?'));
                $contentList = $db->fetchAll("SELECT r.*, c.channel_name FROM {$db->table('recruit')} r LEFT JOIN {$db->table('channel')} c ON r.channel_id=c.id WHERE r.status=1 AND r.channel_id IN ({$placeholders}) ORDER BY r.create_time DESC LIMIT 100", $fallbackIds);
            }
        }
        break;
    default:
        if (intval($id) === 23) {
            $sideChannels = $db->fetchAll("SELECT * FROM {$db->table('channel')} WHERE parent_id = 23 AND is_show = 1 ORDER BY list_order ASC, id ASC");
        } else {
            $sideChannels = $db->fetchAll("SELECT * FROM {$db->table('channel')} WHERE parent_id = ? AND is_show = 1 ORDER BY list_order ASC, id ASC", [intval($id)]);
            if (!$sideChannels && !empty($channel) && !empty($channel['parent_id'])) {
                $sideChannels = $db->fetchAll("SELECT * FROM {$db->table('channel')} WHERE parent_id = ? AND is_show = 1 ORDER BY list_order ASC, id ASC", [intval($channel['parent_id'])]);
            }
        }
        $channelIds = [intval($id)];
        foreach ($sideChannels as $sub) { $channelIds[] = intval($sub['id']); }
        $channelIds = array_values(array_unique(array_filter($channelIds)));
        if ($channelType == 1 || $channelType == 3) {
            if ($channelIds) {
                $placeholders = implode(',', array_fill(0, count($channelIds), '?'));
                $contentList = $db->fetchAll("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 AND a.channel_id IN ({$placeholders}) ORDER BY a.create_time DESC LIMIT 100", $channelIds);
            }
            if (!$contentList && !empty($sideChannels)) {
                $fallbackIds = array_map(function($x){ return intval($x['id']); }, $sideChannels);
                $fallbackIds = array_values(array_unique(array_filter($fallbackIds)));
                if ($fallbackIds) {
                    $placeholders = implode(',', array_fill(0, count($fallbackIds), '?'));
                    $contentList = $db->fetchAll("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 AND a.channel_id IN ({$placeholders}) ORDER BY a.create_time DESC LIMIT 100", $fallbackIds);
                }
            }
        }
        break;
}

if (!empty($_GET['channel_id'])) {
    $filterId = intval($_GET['channel_id']);
    if ($channelType == 2) {
        $contentList = $db->fetchAll("SELECT p.*, c.channel_name FROM {$db->table('product')} p LEFT JOIN {$db->table('channel')} c ON p.channel_id=c.id WHERE p.status=1 AND p.channel_id=? ORDER BY p.create_time DESC LIMIT 100", [$filterId]);
    } elseif ($channelType == 9) {
        $contentList = $db->fetchAll("SELECT r.*, c.channel_name FROM {$db->table('recruit')} r LEFT JOIN {$db->table('channel')} c ON r.channel_id=c.id WHERE r.status=1 AND r.channel_id=? ORDER BY r.create_time DESC LIMIT 100", [$filterId]);
    } else {
        $contentList = $db->fetchAll("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 AND a.channel_id=? ORDER BY a.create_time DESC LIMIT 100", [$filterId]);
    }
}

require_once __DIR__ . '/../_header.php';
?>
<div class="bannern c" style="background:url(<?php echo $site['template_dir']; ?>static/upload/image/20220801/1659354277651388.jpg) no-repeat center;"></div>
<div class="n_cont c">
  <div class="jz c">
    <div class="ny_nav" id="my_sticky">
      <div class="wjssbt c"><?php echo htmlspecialchars($channel['channel_name'] ?? '栏目'); ?></div>
      <ul class="wjsndh c">
        <?php foreach ($sideChannels as $sub): ?>
        <li class="c"><a href="?m=channel&id=<?php echo intval($id); ?>&channel_id=<?php echo intval($sub['id']); ?>"<?php echo $currentNavId === intval($sub['id']) ? ' style="background:#108dee;color:#fff;"' : ''; ?>><?php echo htmlspecialchars($sub['channel_name']); ?> <span>></span></a></li>
        <?php endforeach; ?>
      </ul>
      <div class="search_l">
        <form method="get" action="<?php echo getUrl('search'); ?>">
          <input type="hidden" name="m" value="search">
          <input type="text" class="ip" name="keyword" placeholder="请输入关键字">
          <input type="submit" class="btn" value="搜索">
        </form>
      </div>
    </div>
    <div class="ny_zqw ny_zqw_chb">
      <div class="nw_top"><span class="top_tzbt l"><?php echo htmlspecialchars($channel['channel_name'] ?? '栏目'); ?></span><span class="top_mbx r am-icon-home">您的位置： <a href="<?php echo getUrl('home'); ?>">首页</a> >> <a href="<?php echo getUrl('channel', intval($id)); ?>"><?php echo htmlspecialchars($channel['channel_name'] ?? '栏目'); ?></a></span></div>
      <div class="c"></div>
      <div class="abc-filter"></div>
      <div class="bjbld">
        <?php if ($channelType == 2): ?>
        <ul class="jsanl">
          <?php foreach ($contentList as $item): ?>
          <li><a href="<?php echo getUrl('product', $item['id']); ?>" title="<?php echo htmlspecialchars($item['title']); ?>"><span class="dpdzfd"><img src="<?php echo !empty($item['litpic']) ? htmlspecialchars($item['litpic']) : $site['template_dir'] . 'static/upload/image/20220801/1659323496152728.jpg'; ?>" alt="<?php echo htmlspecialchars($item['title']); ?>"></span><span class="jsalnr"><span class="jsrbt"><?php echo htmlspecialchars($item['title']); ?></span><span class="jsrnr"><?php echo htmlspecialchars(mb_substr(strip_tags($item['description'] ?? $item['content'] ?? ''), 0, 60)); ?></span><span class="jsrgd">查看详情</span></span></a></li>
          <?php endforeach; ?>
        </ul>
        <?php elseif ($channelType == 4): ?>
        <ul class="jsanl">
          <?php foreach ($contentList as $item): ?>
          <li><a href="<?php echo getUrl('image', $item['id']); ?>" title="<?php echo htmlspecialchars($item['title']); ?>"><span class="dpdzfd"><img src="<?php echo !empty($item['litpic']) ? htmlspecialchars($item['litpic']) : $site['template_dir'] . 'static/upload/image/20220801/1659323496152728.jpg'; ?>" alt="<?php echo htmlspecialchars($item['title']); ?>"></span><span class="jsalnr"><span class="jsrbt"><?php echo htmlspecialchars($item['title']); ?></span><span class="jsrnr"><?php echo htmlspecialchars(mb_substr(strip_tags($item['description'] ?? $item['content'] ?? ''), 0, 60)); ?></span><span class="jsrgd">查看详情</span></span></a></li>
          <?php endforeach; ?>
        </ul>
        <?php elseif ($channelType == 5): ?>
        <ul class="wjzlb">
          <?php foreach ($contentList as $item): ?>
          <li class="c"><a href="<?php echo getUrl('download', $item['id']); ?>" title="<?php echo htmlspecialchars($item['title']); ?>"><span class="wjll l"><span class="wjlbt"><i class="am-icon-angle-right"></i><?php echo htmlspecialchars($item['title']); ?></span><span class="wjlrq"><?php echo !empty($item['create_time']) ? date('Y-m-d', $item['create_time']) : ''; ?></span></span></a></li>
          <?php endforeach; ?>
        </ul>
        <?php elseif ($channelType == 7): ?>
        <ul class="wjzlb">
          <?php foreach ($contentList as $item): ?>
          <li class="c"><a href="<?php echo getUrl('video', $item['id']); ?>" title="<?php echo htmlspecialchars($item['title']); ?>"><span class="wjll l"><span class="wjlbt"><i class="am-icon-angle-right"></i><?php echo htmlspecialchars($item['title']); ?></span><span class="wjlrq"><?php echo !empty($item['create_time']) ? date('Y-m-d', $item['create_time']) : ''; ?></span></span></a></li>
          <?php endforeach; ?>
        </ul>
        <?php elseif ($channelType == 9): ?>
        <div class="job_list_box">
          <?php foreach ($contentList as $item): ?>
          <div class="job_item">
            <div class="job_top">
              <h3><?php echo htmlspecialchars($item['title']); ?></h3>
              <span class="job_more"><a href="<?php echo getUrl('recruit', $item['id']); ?>">查看详情</a></span>
            </div>
            <div class="job_meta">
              <?php if (!empty($item['department'])): ?><span>部门：<?php echo htmlspecialchars($item['department']); ?></span><?php endif; ?>
              <?php if (!empty($item['region'])): ?><span>地点：<?php echo htmlspecialchars($item['region']); ?></span><?php endif; ?>
              <?php if (!empty($item['salary'])): ?><span>薪资：<?php echo htmlspecialchars($item['salary']); ?></span><?php endif; ?>
            </div>
          </div>
          <?php endforeach; ?>
        </div>
        <?php else: ?>
        <ul class="wjzlb">
          <?php foreach ($contentList as $item): ?>
          <li class="c"><a href="<?php echo getUrl('article', $item['id']); ?>" title="<?php echo htmlspecialchars($item['title']); ?>"><span class="wjll l"><span class="wjlbt"><i class="am-icon-angle-right"></i><?php echo htmlspecialchars($item['title']); ?></span><span class="wjlrq"><?php echo !empty($item['create_time']) ? date('Y-m-d', $item['create_time']) : ''; ?></span></span></a></li>
          <?php endforeach; ?>
        </ul>
        <?php endif; ?>
      </div>
    </div>
  </div>
</div>
<?php require_once __DIR__ . '/../_footer.php'; ?>
