<?php
/** @var string $content */
use Core\Theme;
$site = Theme::all();
$name = $site['site_name'];
$slogan = $site['site_slogan'];
$phone = $site['contact_phone'];
$logo = $site['site_logo'];
$defaultMode = $site['default_mode'];
$headerStyle = $site['header'];
$icp = $site['icp'];
$gongan = $site['gongan'] ?? '';

// ── 页面级 SEO 数据（控制器通过 $this->view() 或 $data 传入）────
$pageSeo = $pageSeo ?? [];
$seoTitle    = $pageSeo['title']       ?? $site['seo_title'];
$seoDesc     = $pageSeo['description'] ?? $site['seo_description'];
$seoKeywords = $pageSeo['keywords']    ?? $site['seo_keywords'];
$ogImage     = $pageSeo['og_image']    ?? (site_url() . 'assets/img/og-default.png');
$ogType      = $pageSeo['og_type']     ?? 'website';
$jsonLd      = $pageSeo['jsonld'] ?? '';
$breadcrumb  = $pageSeo['breadcrumb'] ?? null;
$noindex     = $pageSeo['noindex'] ?? false;
$canonical   = $pageSeo['canonical'] ?? absolute_url();

// 页面级 title 自动追加站点名后缀（如果没包含）
if (!empty($pageSeo['title']) && mb_strpos($seoTitle, $name) === false) {
    $seoTitle = $seoTitle . ' - ' . $name;
}

$current = trim(parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH), '/');
$nav = [
    ['label' => '首页', 'url' => ''],
    ['label' => '产品中心', 'url' => 'products'],
    ['label' => '新闻动态', 'url' => 'news'],
    ['label' => '客户案例', 'url' => 'cases'],
    ['label' => '关于我们', 'url' => 'page/about'],
    ['label' => '联系我们', 'url' => 'contact'],
];
$isActive = function ($url) use ($current) {
    if ($url === '') return $current === '';
    return strpos($current, $url) === 0;
};
?>
<!doctype html>
<html lang="zh-CN" data-theme="<?php echo e($defaultMode); ?>">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?php echo e($seoTitle); ?></title>
    <meta name="description" content="<?php echo e($seoDesc); ?>">
    <meta name="keywords" content="<?php echo e($seoKeywords); ?>">
    <?php if ($noindex): ?><meta name="robots" content="noindex, follow"><?php endif; ?>
    <link rel="canonical" href="<?php echo e($canonical); ?>">
    <link rel="icon" type="image/png" sizes="32x32" href="<?php echo asset('img/favicon-32.png'); ?>">
    <link rel="icon" type="image/png" sizes="16x16" href="<?php echo asset('img/favicon-16.png'); ?>">
    <link rel="apple-touch-icon" sizes="180x180" href="<?php echo asset('img/apple-touch-icon.png'); ?>">

    <!-- Open Graph (社交分享卡片: 微信/QQ/Facebook/LinkedIn 等) -->
    <meta property="og:title" content="<?php echo e($seoTitle); ?>">
    <meta property="og:description" content="<?php echo e($seoDesc); ?>">
    <meta property="og:image" content="<?php echo e($ogImage); ?>">
    <meta property="og:url" content="<?php echo e($canonical); ?>">
    <meta property="og:type" content="<?php echo e($ogType); ?>">
    <meta property="og:site_name" content="<?php echo e($name); ?>">
    <meta property="og:locale" content="zh_CN">

    <!-- Twitter Card (Twitter/X 分享预览) -->
    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:title" content="<?php echo e($seoTitle); ?>">
    <meta name="twitter:description" content="<?php echo e($seoDesc); ?>">
    <meta name="twitter:image" content="<?php echo e($ogImage); ?>">

    <!-- ── 结构化数据（JSON-LD Schema.org）──── -->
    <!-- Organization - 全局组织信息，所有页面共用 -->
    <script type="application/ld+json">
    {
        "@context": "https://schema.org",
        "@type": "Organization",
        "name": <?php echo json_encode($name, JSON_UNESCAPED_UNICODE); ?>,
        "url": <?php echo json_encode(site_url(), JSON_UNESCAPED_UNICODE); ?>,
        "logo": <?php echo json_encode($logo ?: '', JSON_UNESCAPED_UNICODE); ?>,
        "description": <?php echo json_encode($slogan, JSON_UNESCAPED_UNICODE); ?>,
        "contactPoint": {
            "@type": "ContactPoint",
            "telephone": <?php echo json_encode($phone, JSON_UNESCAPED_UNICODE); ?>,
            "contactType": "customer service",
            "availableLanguage": ["Chinese"]
        }
    }
    </script>
    <!-- WebSite - 搜索框结构化数据，可在 SERP 中显示站内搜索框 -->
    <script type="application/ld+json">
    {
        "@context": "https://schema.org",
        "@type": "WebSite",
        "name": <?php echo json_encode($name, JSON_UNESCAPED_UNICODE); ?>,
        "url": <?php echo json_encode(site_url(), JSON_UNESCAPED_UNICODE); ?>
    }
    </script>
    <?php if ($breadcrumb): ?>
    <!-- BreadcrumbList - 面包屑导航 -->
    <script type="application/ld+json">
    {
        "@context": "https://schema.org",
        "@type": "BreadcrumbList",
        "itemListElement": [
            <?php $bcCount = count($breadcrumb); $bcIdx = 0; foreach ($breadcrumb as $bc): $bcIdx++; ?>
            {
                "@type": "ListItem",
                "position": <?php echo $bcIdx; ?>,
                "name": <?php echo json_encode($bc['name'], JSON_UNESCAPED_UNICODE); ?>,
                "item": <?php echo json_encode($bc['url'] ?? '', JSON_UNESCAPED_UNICODE); ?>
            }<?php if ($bcIdx < $bcCount) echo ','; echo "\n"; ?>
            <?php endforeach; ?>
        ]
    }
    </script>
    <?php endif; ?>
    <?php if ($jsonLd): ?>
    <?php echo $jsonLd; ?>

    <?php endif; ?>

    <link rel="stylesheet" href="<?php echo asset('css/theme.css'); ?>">
    <link rel="stylesheet" href="<?php echo asset('css/site.css'); ?>">
    <script>
        (function () {
            try {
                var t = localStorage.getItem('site_theme');
                if (t) document.documentElement.setAttribute('data-theme', t);
            } catch (e) {}
        })();
    </script>
</head>
<body class="header-<?php echo e($headerStyle); ?>">

<header class="site-header" id="siteHeader">
    <div class="container nav-inner">
        <a class="brand" href="<?php echo site_url(); ?>">
            <?php $logoSrc = $logo ? (preg_match('#^https?://|^\/\/#i', (string)$logo) ? $logo : site_url(ltrim($logo, '/'))) : ''; ?>
            <?php if ($logoSrc): ?><img src="<?php echo e($logoSrc); ?>" alt="<?php echo e($name); ?>" class="brand-logo"><?php else: ?><span class="brand-mark">❄</span><?php endif; ?>
        </a>
        <nav class="nav-links" id="navLinks">
            <?php foreach ($nav as $n): ?>
                <a href="<?php echo site_url($n['url']); ?>" class="<?php echo $isActive($n['url']) ? 'active' : ''; ?>"><?php echo e($n['label']); ?></a>
            <?php endforeach; ?>
        </nav>
        <div class="nav-actions">
            <button class="theme-toggle" id="themeToggle" aria-label="切换明暗">🌓</button>
            <a class="btn btn-primary magnetic" href="<?php echo site_url('contact'); ?>">免费拿样</a>
            <button class="nav-burger" id="navBurger" aria-label="菜单">☰</button>
        </div>
    </div>
</header>

<main class="site-main">
    <?php echo $content; ?>
</main>

<footer class="site-footer">
    <div class="container footer-grid">
        <div>
            <div class="brand">
                <?php if ($logoSrc): ?><img src="<?php echo e($logoSrc); ?>" alt="<?php echo e($name); ?>" class="brand-logo"><?php else: ?><span class="brand-mark">❄</span><span class="brand-name"><?php echo e($name); ?></span><?php endif; ?>
            </div>
            <p class="footer-slogan"><?php echo e($slogan); ?></p>
            <p class="footer-line">电话：<a href="tel:<?php echo e($phone); ?>"><?php echo e($phone); ?></a></p>
            <p class="footer-line">邮箱：<?php echo e($site['contact_email']); ?></p>
            <p class="footer-line">地址：<?php echo e($site['contact_address']); ?></p>
        </div>
        <div>
            <h4>产品系列</h4>
            <a href="<?php echo site_url('products'); ?>">水冷循环降温服</a>
            <a href="<?php echo site_url('products'); ?>">相变蓄冷凝胶服</a>
            <a href="<?php echo site_url('products'); ?>">涡扇风冷降温服</a>
            <a href="<?php echo site_url('products'); ?>">工业降温工装</a>
        </div>
        <div>
            <h4>快速链接</h4>
            <a href="<?php echo site_url('page/about'); ?>">关于我们</a>
            <a href="<?php echo site_url('page/service'); ?>">服务与优势</a>
            <a href="<?php echo site_url('cases'); ?>">客户案例</a>
            <a href="<?php echo site_url('news'); ?>">新闻动态</a>
            <a href="<?php echo site_url('order/query'); ?>">订单查询</a>
        </div>
        <div>
            <h4>关注我们</h4>
            <p class="footer-line">扫码或来电，获取专属降温方案</p>
            <a class="btn btn-ghost" href="<?php echo site_url('contact'); ?>">立即咨询</a>
        </div>
    </div>
    <div class="footer-bottom container">
        <span>© <?php echo date('Y'); ?> <?php echo e($name); ?> · 科技降温服装定制</span>
        <?php if ($icp): ?><span><?php echo e($icp); ?></span><?php endif; ?>
        <?php if ($gongan): ?><span><a href="https://beian.mps.gov.cn/" target="_blank" rel="noopener"><?php echo e($gongan); ?></a></span><?php endif; ?>
    </div>
</footer>

<script src="<?php echo asset('js/main.js'); ?>"></script>
</body>
</html>
