<?php
/**
 * 前台首页
 * rh888av.com
 * SEO 优化：TDK、结构化数据、响应式设计
 */

define('IN_APP', true);

// 加载配置
if (file_exists('config_real.php')) {
    require_once 'config_real.php';
} else {
    // 未安装则跳转
    header('Location: /install.php');
    exit;
}

require_once 'includes/common.php';

// 获取数据
$categories = getCategories(0);
$recommend_videos = getVideoList('AND is_recommend=1', 'sort ASC, created_at DESC', 12);
$latest_videos = getVideoList('', 'created_at DESC', 24);
$hot_videos = getVideoList('', 'hits DESC', 10);

// 获取广告
$home_banner_ads = getAds('home_nav_bottom');
$video_card_ads = getAds('video_card');
$seo_title = getConfig('site_name', 'RH888AV') . ' - 成人色站,久久综合精品在线视频,高清无码看片';
$seo_keywords = getConfig('site_keywords', '在线视频，电影，电视剧，综艺，动漫，高清影视');
$seo_description = getConfig('site_description', '成人色站,久久综合精品在线视频,高清无码看片提供最新最全的高清电影、电视剧、综艺、动漫在线观看');
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta name="force-rendering" content="webkit">
    
    <!-- SEO -->
    <title><?php echo htmlSafe($seo_title); ?></title>
    <meta name="keywords" content="<?php echo htmlSafe($seo_keywords); ?>">
    <meta name="description" content="<?php echo htmlSafe($seo_description); ?>">
    
    <!-- 移动端优化 -->
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <meta name="applicable-device" content="pc,mobile">
    
    <!-- Canonical URL - 动态获取当前域名 -->
    <link rel="canonical" href="<?php echo (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST']; ?>/">
    
    <!-- Favicon -->
    <link rel="icon" href="/favicon.ico" type="image/x-icon">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    
    <!-- 结构化数据 Schema.org -->
    <script type="application/ld+json">
    {
        "@context": "https://schema.org",
        "@type": "WebSite",
        "name": "<?php echo htmlSafe(getConfig('site_name')); ?>",
        "url": "<?php echo SITE_URL; ?>",
        "potentialAction": {
            "@type": "SearchAction",
            "target": "<?php echo SITE_URL; ?>/search.php?keyword={search_term_string}",
            "query-input": "required name=search_term_string"
        }
    }
    </script>
    
    <!-- 样式 -->
    <link rel="stylesheet" href="/assets/css/style.css">

    <!-- 预加载关键资源 -->
    <link rel="preload" href="/assets/css/style.css" as="style">
</head>
<body>
    <!-- 主题切换按钮 -->
    <button class="theme-toggle" id="themeToggle" title="切换主题" aria-label="切换主题">
        <span id="themeIcon">🌙</span>
    </button>
    
    <!-- 主题切换样式 -->
    <style>
        :root {
            --bg-body: #f5f5f5;
            --bg-card: #ffffff;
            --bg-header: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            --text-primary: #333333;
            --text-secondary: #666666;
            --text-muted: #999999;
            --border-color: #e0e0e0;
            --shadow-card: 0 2px 10px rgba(0,0,0,0.08);
        }
        
        [data-theme="dark"] {
            --bg-body: #0f0f1a;
            --bg-card: #1a1a2e;
            --bg-header: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
            --text-primary: #eaeaea;
            --text-secondary: #b0b0b0;
            --text-muted: #666666;
            --border-color: #2d2d44;
            --shadow-card: 0 2px 10px rgba(0,0,0,0.3);
        }
        
        /* 主题过渡动画 */
        body, .header, .section, .video-card, .footer {
            transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease;
        }
        
        /* 主题切换按钮 */
        .theme-toggle {
            position: fixed;
            bottom: 30px;
            right: 30px;
            width: 56px;
            height: 56px;
            border-radius: 50%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            color: white;
            font-size: 24px;
            cursor: pointer;
            box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
            display: flex;
            align-items: center;
            justify-content: center;
            z-index: 9999;
            transition: all 0.3s ease;
        }
        
        .theme-toggle:hover {
            transform: scale(1.1) rotate(15deg);
            box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
        }
        
        .theme-toggle:active {
            transform: scale(0.95);
        }
    </style>
    <!-- 头部导航 -->
    <header class="header">
        <div class="container">
            <div class="header-inner">
                <div class="logo">
                    <a href="/" title="<?php echo htmlSafe(getConfig('site_name')); ?>">
                        <span class="logo-text"><?php echo htmlSafe(getConfig('site_name')); ?></span>
                    </a>
                </div>
                <nav class="nav">
                    <ul class="nav-list">
                        <li class="nav-item active"><a href="/">首页</a></li>
                        <?php foreach ($categories as $cat): ?>
                        <li class="nav-item">
                            <a href="<?php echo seoUrl('category', $cat['id'], $cat['slug']); ?>">
                                <?php echo htmlSafe($cat['name']); ?>
                            </a>
                        </li>
                        <?php endforeach; ?>
                    </ul>
                </nav>
                <div class="search-box">
                    <form action="/search.php" method="get">
                        <input type="text" name="keyword" placeholder="搜索影片..." autocomplete="off">
                        <button type="submit" class="search-btn">搜索</button>
                    </form>
                </div>
                <button class="mobile-menu-btn" aria-label="菜单">
                    <span></span>
                    <span></span>
                    <span></span>
                </button>
            </div>
        </div>
    </header>

    <!-- 移动端导航 -->
    <div class="mobile-nav">
        <div class="mobile-nav-content">
            <ul>
                <li><a href="/">首页</a></li>
                <?php foreach ($categories as $cat): ?>
                <li><a href="<?php echo seoUrl('category', $cat['id'], $cat['slug']); ?>"><?php echo htmlSafe($cat['name']); ?></a></li>
                <?php endforeach; ?>
            </ul>
        </div>
    </div>

    <!-- 主要内容 -->
    <main class="main">
        <!-- 首页横幅广告（导航栏下方） -->
      <?php if ($home_banner_ads): ?>
        <div class="nav-ad-container" style="text-align:center;padding:15px 0;background:var(--bg-card);box-shadow:var(--shadow-card);">
            <div class="container">
                <div style="margin:0 auto;max-width:960px;">
                    <?php foreach ($home_banner_ads as $ad): ?>
                        <?php echo str_replace(['\\r\\n', '\\r', '\\n', '\r\n', '\r', '\n', 'rn'], '', stripslashes($ad['code'])); ?>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <!-- Banner 区域 -->
        <section class="banner">
            <div class="container">
                <div class="banner-content">
                    <h1>海量高清影视 免费在线观看</h1>
                    <p>每日更新热门电影、电视剧、综艺、动漫</p>
                </div>
            </div>
        </section>

        <!-- 推荐影片 -->
        <section class="section">
            <div class="container">
                <div class="section-header">
                    <h2 class="section-title">
                        <span class="title-icon">★</span>
                        热门推荐
                    </h2>
                    <a href="/recommend.html" class="section-more">更多 ></a>
                </div>
                <div class="video-grid">
                    <?php
                    $ad_index = 0;
                    $videos_per_ad = 4; // 每 4 个视频显示 1 个广告
                    foreach ($recommend_videos as $index => $video):
                        // 从第 1 个开始，每 4 个视频插入广告
                        if ($index % $videos_per_ad == 0 && !empty($video_card_ads)):
                            $ad = $video_card_ads[$ad_index % count($video_card_ads)];
                    ?>
                    <!-- 视频卡片广告 -->
                    <div class="video-card ad-card">
                        <a href="<?php echo !empty($ad['link']) ? htmlSafe($ad['link']) : '#'; ?>" class="video-cover" style="background:#f0f0f0;">
                            <?php echo str_replace(['\\r\\n', '\\r', '\\n', '\r\n', '\r', '\n', 'rn'], '', stripslashes($ad['code'])); ?>
                        </a>
                        <div class="video-info">
                            <h3 class="video-title">
                                <a href="<?php echo !empty($ad['link']) ? htmlSafe($ad['link']) : '#'; ?>">
                                    <?php echo htmlSafe(!empty($ad['title']) ? $ad['title'] : '广告推广'); ?>
                                </a>
                            </h3>
                            <p class="video-meta">
                                <span class="year">广告</span>
                                <span class="score">查看详情</span>
                            </p>
                        </div>
                    </div>
                    <?php
                            $ad_index++;
                        endif;
                    ?>
                    <div class="video-card">
                        <a href="<?php echo seoUrl('video', $video['id'], $video['slug']); ?>" class="video-cover">
                            <img src="<?php echo htmlSafe($video['cover'] ?: '/assets/images/default-cover.jpg'); ?>"
                                 alt="<?php echo htmlSafe($video['title']); ?>"
                                 loading="lazy">
                            <div class="video-overlay">
                                <span class="play-icon">▶</span>
                            </div>
                            <?php if ($video['is_vip']): ?>
                            <span class="vip-tag">VIP</span>
                            <?php endif; ?>
                        </a>
                        <div class="video-info">
                            <h3 class="video-title">
                                <a href="<?php echo seoUrl('video', $video['id'], $video['slug']); ?>">
                                    <?php echo htmlSafe(cutStr($video['title'], 18)); ?>
                                </a>
                            </h3>
                            <p class="video-meta">
                                <span class="year"><?php echo $video['year']; ?></span>
                                <span class="score">评分：<?php echo $video['douban_score']; ?></span>
                            </p>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </section>

        <!-- 最新影片 -->
        <section class="section">
            <div class="container">
                <div class="section-header">
                    <h2 class="section-title">
                        <span class="title-icon">☀</span>
                        最新上线
                    </h2>
                    <a href="/latest.html" class="section-more">更多 ></a>
                </div>
                    <div class="video-grid video-grid-large">
                        <?php
                        $ad_index = 0;
                        $videos_per_ad = 4; // 每 4 个视频显示 1 个广告
                        foreach ($latest_videos as $index => $video):
                            // 从第 1 个开始，每 4 个视频插入广告
                            if ($index % $videos_per_ad == 0 && !empty($video_card_ads)):
                                $ad = $video_card_ads[$ad_index % count($video_card_ads)];
                        ?>
                        <!-- 视频卡片广告 -->
                        <div class="video-card ad-card">
                            <a href="<?php echo !empty($ad['link']) ? htmlSafe($ad['link']) : '#'; ?>" class="video-cover" style="background:#f0f0f0;">
                                <?php echo str_replace(['\\r\\n', '\\r', '\\n', '\r\n', '\r', '\n', 'rn'], '', stripslashes($ad['code'])); ?>
                            </a>
                            <div class="video-info">
                                <h3 class="video-title">
                                    <a href="<?php echo !empty($ad['link']) ? htmlSafe($ad['link']) : '#'; ?>">
                                        <?php echo htmlSafe(!empty($ad['title']) ? $ad['title'] : '广告推广'); ?>
                                    </a>
                                </h3>
                                <p class="video-meta">
                                    <span class="year">广告</span>
                                    <span class="score">查看详情</span>
                                </p>
                            </div>
                        </div>
                        <?php
                                $ad_index++;
                            endif;
                        ?>
                    <div class="video-card">
                        <a href="<?php echo seoUrl('video', $video['id'], $video['slug']); ?>" class="video-cover">
                            <img src="<?php echo htmlSafe($video['cover'] ?: '/assets/images/default-cover.jpg'); ?>"
                                 alt="<?php echo htmlSafe($video['title']); ?>"
                                 loading="lazy">
                            <div class="video-overlay">
                                <span class="play-icon">▶</span>
                            </div>
                        </a>
                        <div class="video-info">
                            <h3 class="video-title">
                                <a href="<?php echo seoUrl('video', $video['id'], $video['slug']); ?>">
                                    <?php echo htmlSafe(cutStr($video['title'], 18)); ?>
                                </a>
                            </h3>
                            <p class="video-desc"><?php echo htmlSafe(cutStr($video['description'], 40)); ?></p>
                            <p class="video-meta">
                                <span class="year"><?php echo $video['year']; ?></span>
                                <span class="hits">播放：<?php echo number_format($video['hits']); ?></span>
                            </p>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </section>

        <!-- 分类导航 -->
        <section class="section category-section">
            <div class="container">
                <div class="section-header">
                    <h2 class="section-title">
                        <span class="title-icon">☼</span>
                        影片分类
                    </h2>
                </div>
                <div class="category-grid">
                    <?php foreach ($categories as $cat): ?>
                    <div class="category-card">
                        <a href="<?php echo seoUrl('category', $cat['id'], $cat['slug']); ?>">
                            <div class="category-icon"><?php echo mb_substr($cat['name'], 0, 1); ?></div>
                            <h3><?php echo htmlSafe($cat['name']); ?></h3>
                            <p><?php echo htmlSafe(cutStr($cat['description'], 30)); ?></p>
                        </a>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </section>

        <!-- 热播排行 -->
        <section class="section">
            <div class="container">
                <div class="section-header">
                    <h2 class="section-title">
                        <span class="title-icon">☾</span>
                        热播排行
                    </h2>
                </div>
                <div class="ranking-list">
                    <?php foreach ($hot_videos as $index => $video): ?>
                    <div class="ranking-item">
                        <span class="ranking-num <?php echo $index < 3 ? 'top' : ''; ?>"><?php echo $index + 1; ?></span>
                        <div class="ranking-info">
                            <h3>
                                <a href="<?php echo seoUrl('video', $video['id'], $video['slug']); ?>">
                                    <?php echo htmlSafe(cutStr($video['title'], 25)); ?>
                                </a>
                            </h3>
                            <p class="ranking-meta">
                                <span>播放：<?php echo number_format($video['hits']); ?></span>
                                <span><?php echo $video['year']; ?></span>
                            </p>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </section>
    </main>

    <!-- 页脚 -->
    <footer class="footer">
        <div class="container">
            <div class="footer-content">
                <div class="footer-links">
                    <a href="/about.html">关于我们</a>
                    <a href="/contact.html">联系我们</a>
                    <a href="/copyright.html">版权声明</a>
                    <a href="/feedback.html">意见反馈</a>
                </div>
                <div class="footer-info">
                    <p><?php echo htmlSafe(getConfig('site_name')); ?> - 为您提供高清影视在线观看服务</p>
                    <p>
                        <?php if (getConfig('site_icp')): ?>
                        <a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">
                            <?php echo htmlSafe(getConfig('site_icp')); ?>
                        </a>
                        <?php endif; ?>
                    </p>
                    <p class="copyright">© <?php echo date('Y'); ?> <?php echo htmlSafe(getConfig('site_name')); ?> All Rights Reserved.</p>
                </div>
            </div>
        </div>
    </footer>

    <!-- 返回顶部 -->
    <a href="#" class="back-to-top" aria-label="返回顶部">↑</a>

    <!-- 统计代码 -->
    <?php 
    $site_stat = getConfig('site_stat', '');
    if (!empty($site_stat)) {
        echo $site_stat;
    }
    ?>

    <!-- JavaScript -->
    <script src="/assets/js/main.js"></script>
    <!-- 主题切换脚本 -->
    <script>
    (function() {
        const themeToggle = document.getElementById('themeToggle');
        const themeIcon = document.getElementById('themeIcon');
        const THEME_KEY = 'frontend_theme';
        
        // 获取当前小时
        function getCurrentHour() {
            return new Date().getHours();
        }
        
        // 判断是否是白天
        function isDaytime() {
            const hour = getCurrentHour();
            return hour >= 6 && hour < 18;
        }
        
        // 设置主题
        function setTheme(theme) {
            if (theme === 'dark') {
                document.documentElement.setAttribute('data-theme', 'dark');
                themeIcon.textContent = '☀️';
                themeToggle.title = '切换到浅色主题';
            } else {
                document.documentElement.removeAttribute('data-theme');
                themeIcon.textContent = '🌙';
                themeToggle.title = '切换到深色主题';
            }
        }
        
        // 自动主题
        function autoTheme() {
            const theme = isDaytime() ? 'light' : 'dark';
            setTheme(theme);
        }
        
        // 切换主题
        function toggleTheme() {
            const currentTheme = document.documentElement.getAttribute('data-theme');
            const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
            setTheme(newTheme);
            localStorage.setItem(THEME_KEY, newTheme);
        }
        
        // 初始化
        function initTheme() {
            const savedTheme = localStorage.getItem(THEME_KEY);
            if (savedTheme) {
                setTheme(savedTheme);
            } else {
                autoTheme();
            }
            // 每小时检查
            setInterval(() => {
                if (!localStorage.getItem(THEME_KEY)) {
                    autoTheme();
                }
            }, 3600000);
        }
        
        if (themeToggle) {
            themeToggle.addEventListener('click', toggleTheme);
        }
        
        initTheme();
    })();
    </script>
</body>
</html>
