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

$topChannels = $db->fetchAll("SELECT * FROM {$db->table('channel')} WHERE parent_id = 0 AND is_show = 1 ORDER BY list_order ASC, id ASC");
$topChannels = array_values(array_filter($topChannels, function($ch){
    return !in_array(intval($ch['id']), [2,3,10,18], true);
}));
$siteName = $siteConfig['site_name'] ?? '企业官网';
$sitePhone = $siteConfig['site_phone'] ?? '';
$siteMobile = $siteConfig['site_mobile'] ?? $sitePhone;
$siteAddress = $siteConfig['site_address'] ?? '';
$siteDescription = $siteConfig['site_description'] ?? '';
$siteKeywords = $siteConfig['site_keywords'] ?? '';
$siteCompany = $siteConfig['company_intro'] ?? ($siteConfig['site_company'] ?? $siteDescription);
$siteUrl = $siteConfig['site_url'] ?? '';
$customVars = $db->fetchAll("SELECT var_key, var_value, var_type FROM {$db->table('custom_var')}");
$custom = [];
foreach ($customVars as $v) {
    $custom[$v['var_key']] = $v['var_value'];
}
$customQr = $custom['title'] ?? '{$custom.title}';
$siteLogo = trim($siteConfig['site_logo'] ?? '');
if ($siteLogo && strpos($siteLogo, 'http') !== 0 && strpos($siteLogo, '/') !== 0) {
    $siteLogo = '/' . ltrim($siteLogo, '/');
}

function tpl_nav_active($chId) {
    global $module, $id, $db;
    if (($module ?? '') === 'channel' && intval($id ?? 0) === intval($chId)) return true;
    if (in_array(($module ?? ''), ['article','product','page','image','download','video','recruit'], true) && !empty($id)) {
        $tableMap = [
            'article' => 'article',
            'product' => 'product',
            'page' => 'article',
            'image' => 'image',
            'download' => 'download',
            'video' => 'video',
            'recruit' => 'recruit',
        ];
        $table = $tableMap[$module] ?? '';
        if ($table) {
            $row = $db->fetch("SELECT channel_id FROM {$db->table($table)} WHERE id = ?", [intval($id)]);
            if ($row && intval($row['channel_id']) === intval($chId)) return true;
        }
    }
    return false;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php echo isset($pageTitle) && $pageTitle ? htmlspecialchars($pageTitle) . '-' : ''; ?><?php echo htmlspecialchars($siteName); ?></title>
<meta name="keywords" content="<?php echo htmlspecialchars($siteKeywords); ?>">
<meta name="description" content="<?php echo htmlspecialchars($siteDescription); ?>">
<link rel="stylesheet" href="<?php echo $site['template_dir']; ?>skin/css/amazeui.min.css">
<link rel="stylesheet" href="<?php echo $site['template_dir']; ?>skin/css/app.css">
<link rel="stylesheet" href="<?php echo $site['template_dir']; ?>skin/css/openclaw-fix.css">
<?php echo $extraHead ?? ''; ?>
</head>
<body>
<div class="top">
  <div class="jz">
    <div class="tl l">
      <p>欢迎访问<?php echo htmlspecialchars($siteName); ?>！</p>
    </div>
  </div>
</div>
<div class="head">
  <div class="jz">
    <a href="<?php echo getUrl('home'); ?>" class="l p_sj">
      <img src="<?php echo $siteLogo ? htmlspecialchars($siteLogo) : $site['template_dir'] . 'static/upload/image/20220801/1659353401719250.png'; ?>" alt="<?php echo htmlspecialchars($siteName); ?>">
    </a>
    <div class="fw r"><span class="fw-icon">☎</span>
      <p><span>客服电话：</span><?php echo htmlspecialchars($sitePhone ?: $siteMobile ?: '请在后台填写联系电话'); ?></p>
    </div>
  </div>
</div>
<div class="nav">
  <div class="jz">
    <ul class="nav_main">
      <li class="yiji_li"><a class="wh_wbd" href="<?php echo getUrl('home'); ?>">网站首页</a></li>
      <?php foreach ($topChannels as $ch): ?>
      <?php $subChannels = $db->fetchAll("SELECT * FROM {$db->table('channel')} WHERE parent_id = ? AND is_show = 1 ORDER BY list_order ASC, id ASC", [$ch['id']]); ?>
      <li class="yiji_li<?php echo tpl_nav_active($ch['id']) ? ' cur' : ''; ?>">
        <a href="<?php echo getUrl('channel', $ch['id'], $ch['channel_dir'] ?? ''); ?>" class="wh_wbd"><?php echo htmlspecialchars($ch['channel_name']); ?></a>
        <?php if ($subChannels): ?>
        <ul class="nav_c">
          <?php foreach ($subChannels as $sub): ?>
          <li class="erji_li c"><a href="<?php echo getUrl('channel', $sub['id'], $sub['channel_dir'] ?? ''); ?>"><?php echo htmlspecialchars($sub['channel_name']); ?></a></li>
          <?php endforeach; ?>
        </ul>
        <?php endif; ?>
      </li>
      <?php endforeach; ?>
    </ul>
  </div>
</div>
<nav data-am-widget="menu" class="am-menu am-menu-dropdown1 mnav" data-am-menu-collapse>
  <a href="javascript:;" class="am-icon-bars am-icon-sm am-menu-toggle"></a>
  <ul class="am-menu-nav am-avg-sm-1 am-collapse">
    <li><a href="<?php echo getUrl('home'); ?>">网站首页</a></li>
    <?php foreach ($topChannels as $ch): ?>
    <?php $subChannels = $db->fetchAll("SELECT * FROM {$db->table('channel')} WHERE parent_id = ? AND is_show = 1 ORDER BY list_order ASC, id ASC", [$ch['id']]); ?>
    <li class="<?php echo $subChannels ? 'am-parent' : ''; ?>">
      <a href="<?php echo getUrl('channel', $ch['id'], $ch['channel_dir'] ?? ''); ?>" title="<?php echo htmlspecialchars($ch['channel_name']); ?>"><?php echo htmlspecialchars($ch['channel_name']); ?></a>
      <?php if ($subChannels): ?>
      <ul class="am-menu-sub am-collapse am-avg-sm-2">
        <?php foreach ($subChannels as $sub): ?>
        <li><a href="<?php echo getUrl('channel', $sub['id'], $sub['channel_dir'] ?? ''); ?>"><?php echo htmlspecialchars($sub['channel_name']); ?></a></li>
        <?php endforeach; ?>
      </ul>
      <?php endif; ?>
    </li>
    <?php endforeach; ?>
  </ul>
</nav>
