<?php
/**
 * 辰迅工具箱 - 首页
 */
require_once 'config.php';

// 获取工具分类及其工具
$db = getDB();
$stmt = $db->query("SELECT tc.*, COUNT(t.id) as tool_count 
                     FROM tool_categories tc 
                     LEFT JOIN tools t ON tc.id = t.category_id AND t.is_active = 1 
                     GROUP BY tc.id 
                     ORDER BY tc.sort_order");
$categories = $stmt->fetchAll();

// 获取热门工具
$stmt = $db->query("SELECT t.*, tc.name as category_name 
                    FROM tools t 
                    LEFT JOIN tool_categories tc ON t.category_id = tc.id 
                    WHERE t.is_active = 1 
                    ORDER BY t.is_popular DESC, t.usage_count DESC 
                    LIMIT 8");
$popularTools = $stmt->fetchAll();

// 获取精选文章
$stmt = $db->query("SELECT a.*, ac.name as category_name 
                    FROM articles a 
                    LEFT JOIN article_categories ac ON a.category_id = ac.id 
                    WHERE a.is_published = 1 
                    ORDER BY a.is_featured DESC, a.view_count DESC 
                    LIMIT 4");
$featuredArticles = $stmt->fetchAll();

// 获取顶部广告
$stmt = $db->prepare("SELECT * FROM ads WHERE position_id = 1 AND is_active = 1 
                      AND (start_date IS NULL OR start_date <= CURDATE()) 
                      AND (end_date IS NULL OR end_date >= CURDATE()) LIMIT 1");
$stmt->execute();
$topAd = $stmt->fetch();

// 获取侧边栏广告
$stmt = $db->prepare("SELECT * FROM ads WHERE position_id = 3 AND is_active = 1 
                      AND (start_date IS NULL OR start_date <= CURDATE()) 
                      AND (end_date IS NULL OR end_date >= CURDATE()) LIMIT 1");
$stmt->execute();
$sidebarAd = $stmt->fetch();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php include "seo_head.php"; ?>
    <title><?php echo SITE_NAME; ?> - 实用的在线工具箱</title>
    <meta name="description" content="<?php echo SITE_NAME; ?>提供编码转换、格式转换、开发工具、图片处理等多种实用在线工具，助力高效工作。">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary-color: #4F46E5;
            --primary-light: #818CF8;
            --primary-dark: #3730A3;
            --bg-light: #F9FAFB;
            --text-dark: #1F2937;
            --text-gray: #6B7280;
        }
        
        * { box-sizing: border-box; }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background: var(--bg-light);
            color: var(--text-dark);
            line-height: 1.6;
        }
        
        /* 导航栏 */
        .navbar {
            background: white;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            padding: 1rem 0;
        }
        
        .navbar-brand {
            font-weight: 700;
            font-size: 1.5rem;
            color: var(--primary-color) !important;
        }
        
        .navbar-brand i {
            margin-right: 0.5rem;
        }
        
        .nav-link {
            color: var(--text-dark) !important;
            font-weight: 500;
            padding: 0.5rem 1rem !important;
            border-radius: 8px;
            transition: all 0.2s;
        }
        
        .nav-link:hover {
            background: var(--bg-light);
            color: var(--primary-color) !important;
        }
        
        /* 搜索栏 */
        .search-section {
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-dark) 100%);
            padding: 4rem 0;
            margin-bottom: 3rem;
        }
        
        .search-box {
            background: white;
            border-radius: 16px;
            padding: 0.5rem;
            display: flex;
            box-shadow: 0 10px 40px rgba(0,0,0,0.2);
        }
        
        .search-box input {
            flex: 1;
            border: none;
            padding: 1rem 1.5rem;
            font-size: 1.1rem;
            outline: none;
            border-radius: 12px;
        }
        
        .search-box button {
            background: var(--primary-color);
            color: white;
            border: none;
            padding: 1rem 2rem;
            border-radius: 12px;
            font-weight: 600;
            cursor: pointer;
            transition: background 0.2s;
        }
        
        .search-box button:hover {
            background: var(--primary-dark);
        }
        
        /* 广告横幅 */
        .ad-banner {
            background: #f3f4f6;
            border-radius: 12px;
            padding: 1rem;
            text-align: center;
            margin-bottom: 2rem;
            min-height: 100px;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
        }
        
        .ad-banner img {
            max-width: 100%;
            max-height: 100px;
        }
        
        /* 分类卡片 */
        .category-section {
            margin-bottom: 3rem;
        }
        
        .category-header {
            display: flex;
            align-items: center;
            margin-bottom: 1.5rem;
        }
        
        .category-header h2 {
            font-size: 1.5rem;
            font-weight: 700;
            margin: 0;
            display: flex;
            align-items: center;
            gap: 0.75rem;
        }
        
        .category-header h2 i {
            color: var(--primary-color);
        }
        
        /* 工具卡片 */
        .tool-card {
            background: white;
            border-radius: 12px;
            padding: 1.5rem;
            text-align: center;
            transition: all 0.3s;
            border: 1px solid #e5e7eb;
            height: 100%;
            cursor: pointer;
        }
        
        .tool-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 12px 24px rgba(0,0,0,0.1);
            border-color: var(--primary-light);
        }
        
        .tool-icon {
            width: 64px;
            height: 64px;
            background: linear-gradient(135deg, var(--primary-color), var(--primary-light));
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 1rem;
            font-size: 1.75rem;
            color: white;
        }
        
        .tool-card h5 {
            font-weight: 600;
            margin-bottom: 0.5rem;
        }
        
        .tool-card p {
            color: var(--text-gray);
            font-size: 0.875rem;
            margin: 0;
        }
        
        .popular-badge {
            background: #FEF3C7;
            color: #D97706;
            padding: 0.25rem 0.75rem;
            border-radius: 20px;
            font-size: 0.75rem;
            font-weight: 600;
        }
        
        /* 文章卡片 */
        .article-card {
            background: white;
            border-radius: 12px;
            overflow: hidden;
            transition: all 0.3s;
            border: 1px solid #e5e7eb;
            height: 100%;
        }
        
        .article-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 12px 24px rgba(0,0,0,0.1);
        }
        
        .article-card img {
            width: 100%;
            height: 160px;
            object-fit: cover;
        }
        
        .article-card .card-body {
            padding: 1.25rem;
        }
        
        .article-card .category-tag {
            background: var(--bg-light);
            color: var(--primary-color);
            padding: 0.25rem 0.75rem;
            border-radius: 20px;
            font-size: 0.75rem;
            font-weight: 500;
        }
        
        .article-card h5 {
            font-weight: 600;
            margin: 0.75rem 0;
            line-height: 1.4;
        }
        
        .article-card h5 a {
            color: var(--text-dark);
            text-decoration: none;
        }
        
        .article-card h5 a:hover {
            color: var(--primary-color);
        }
        
        .article-card p {
            color: var(--text-gray);
            font-size: 0.875rem;
            margin: 0;
        }
        
        .article-meta {
            display: flex;
            align-items: center;
            gap: 1rem;
            margin-top: 1rem;
            font-size: 0.8rem;
            color: var(--text-gray);
        }
        
        /* 侧边栏 */
        .sidebar {
            position: sticky;
            top: 2rem;
        }
        
        .sidebar-card {
            background: white;
            border-radius: 12px;
            padding: 1.5rem;
            margin-bottom: 1.5rem;
            border: 1px solid #e5e7eb;
        }
        
        .sidebar-card h4 {
            font-size: 1.1rem;
            font-weight: 600;
            margin-bottom: 1rem;
            padding-bottom: 0.75rem;
            border-bottom: 2px solid var(--primary-color);
        }
        
        .quick-link {
            display: flex;
            align-items: center;
            padding: 0.75rem;
            border-radius: 8px;
            color: var(--text-dark);
            text-decoration: none;
            transition: all 0.2s;
        }
        
        .quick-link:hover {
            background: var(--bg-light);
            color: var(--primary-color);
        }
        
        .quick-link i {
            width: 32px;
            height: 32px;
            background: var(--bg-light);
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 0.75rem;
            color: var(--primary-color);
        }
        
        /* 页脚 */
        footer {
            background: #1F2937;
            color: white;
            padding: 3rem 0 1.5rem;
            margin-top: 4rem;
        }
        
        footer h5 {
            font-weight: 600;
            margin-bottom: 1.5rem;
        }
        
        footer a {
            color: #9CA3AF;
            text-decoration: none;
            transition: color 0.2s;
        }
        
        footer a:hover {
            color: white;
        }
        
        .footer-bottom {
            border-top: 1px solid #374151;
            padding-top: 1.5rem;
            margin-top: 2rem;
            text-align: center;
            color: #9CA3AF;
        }
        
        /* 浮动按钮 */
        .float-btns {
            position: fixed;
            right: 2rem;
            bottom: 2rem;
            display: flex;
            flex-direction: column;
            gap: 0.75rem;
            z-index: 1000;
        }
        
        .float-btn {
            width: 48px;
            height: 48px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            text-decoration: none;
            transition: all 0.3s;
            box-shadow: 0 4px 12px rgba(0,0,0,0.2);
        }
        
        .float-btn:hover {
            transform: scale(1.1);
            color: white;
        }
        
        .float-btn.bbs {
            background: #FF6B6B;
        }
        
        .float-btn.admin {
            background: var(--primary-color);
        }
        
        /* 分类列表展示 */
        .category-block {
            background: white;
            border-radius: 16px;
            padding: 2rem;
            margin-bottom: 2rem;
            border: 1px solid #e5e7eb;
        }
        
        .category-title {
            display: flex;
            align-items: center;
            margin-bottom: 1.5rem;
            padding-bottom: 1rem;
            border-bottom: 2px solid var(--bg-light);
        }
        
        .category-title h3 {
            font-size: 1.25rem;
            font-weight: 600;
            margin: 0;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }
        
        .category-title h3 i {
            color: var(--primary-color);
        }
        
        .category-title span {
            margin-left: auto;
            color: var(--text-gray);
            font-size: 0.875rem;
        }
        
        /* 响应式 */
        @media (max-width: 768px) {
            .search-section {
                padding: 2rem 0;
            }
            
            .float-btns {
                right: 1rem;
                bottom: 1rem;
            }
        }
    </style>
<?php include "baidu_push.php"; ?>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg">
        <div class="container">
            <a class="navbar-brand" href="<?php echo SITE_URL; ?>">
                <i class="fas fa-tools"></i><?php echo SITE_NAME; ?>
            </a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item"><a class="nav-link" href="<?php echo SITE_URL; ?>"><i class="fas fa-home mr-1"></i>首页</a></li>
                    <li class="nav-item"><a class="nav-link" href="<?php echo SITE_URL; ?>/articles.php"><i class="fas fa-newspaper mr-1"></i>文章</a></li>
                    <li class="nav-item"><a class="nav-link" href="<?php echo BBS_URL; ?>" target="_blank"><i class="fas fa-comments mr-1"></i>论坛</a></li>
                    <li class="nav-item"><a class="nav-link" href="<?php echo ADMIN_URL; ?>"><i class="fas fa-user-shield mr-1"></i>管理</a></li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- 搜索区 -->
    <section class="search-section">
        <div class="container">
            <h1 class="text-center text-white mb-4" style="font-size: 2.5rem; font-weight: 700;">
                发现实用工具，提升工作效率
            </h1>
            <div class="row justify-content-center">
                <div class="col-md-8">
                    <form action="<?php echo SITE_URL; ?>/tools.php" method="get" class="search-box">
                        <input type="text" name="search" placeholder="搜索工具名称或功能..." autocomplete="off">
                        <button type="submit"><i class="fas fa-search mr-2"></i>搜索</button>
                    </form>
                </div>
            </div>
        </div>
    </section>

    <!-- 主内容 -->
    <div class="container">
        <!-- 顶部广告 -->
        <?php if($topAd): ?>
        <div class="ad-banner">
            <?php 
            if($topAd['image']): ?>
                <a href="<?php echo htmlspecialchars($topAd['link']); ?>" target="_blank">
                    <img src="<?php echo htmlspecialchars($topAd['image']); ?>" alt="<?php echo htmlspecialchars($topAd['title']); ?>">
                </a>
            <?php elseif($topAd['code']): ?>
                <?php echo $topAd['code']; ?>
            <?php elseif($topAd['content']): ?>
                <a href="<?php echo htmlspecialchars($topAd['link']); ?>" target="_blank"><?php echo htmlspecialchars($topAd['content']); ?></a>
            <?php endif; ?>
        </div>
        <?php endif; ?>

        <div class="row">
            <!-- 主内容区 -->
            <div class="col-lg-9">
                <!-- 热门工具 -->
                <?php if(!empty($popularTools)): ?>
                <section class="category-section">
                    <div class="category-header">
                        <h2><i class="fas fa-fire"></i>热门工具</h2>
                    </div>
                    <div class="row">
                        <?php foreach($popularTools as $tool): ?>
                        <div class="col-md-3 col-6 mb-4">
                            <a href="<?php echo SITE_URL; ?>/tool.php?id=<?php echo $tool['id']; ?>" style="text-decoration: none;">
                                <div class="tool-card">
                                    <?php if($tool['is_popular']): ?>
                                    <span class="popular-badge"><i class="fas fa-crown mr-1"></i>热门</span>
                                    <?php endif; ?>
                                    <div class="tool-icon">
                                        <i class="fas <?php echo htmlspecialchars($tool['icon']); ?>"></i>
                                    </div>
                                    <h5><?php echo htmlspecialchars($tool['name']); ?></h5>
                                    <p><?php echo htmlspecialchars($tool['description']); ?></p>
                                </div>
                            </a>
                        </div>
                        <?php endforeach; ?>
                    </div>
                </section>
                <?php endif; ?>

                <!-- 分类工具 -->
                <?php foreach($categories as $category): 
                    $stmt = $db->prepare("SELECT * FROM tools WHERE category_id = ? AND is_active = 1 ORDER BY sort_order LIMIT 8");
                    $stmt->execute([$category['id']]);
                    $tools = $stmt->fetchAll();
                    if(empty($tools)) continue;
                ?>
                <div class="category-block">
                    <div class="category-title">
                        <h3><i class="fas <?php echo htmlspecialchars($category['icon']); ?>"></i><?php echo htmlspecialchars($category['name']); ?></h3>
                        <span><?php echo htmlspecialchars($category['description']); ?></span>
                    </div>
                    <div class="row">
                        <?php foreach($tools as $tool): ?>
                        <div class="col-md-3 col-6 mb-3">
                            <a href="<?php echo SITE_URL; ?>/tool.php?id=<?php echo $tool['id']; ?>" style="text-decoration: none;">
                                <div class="tool-card" style="padding: 1rem;">
                                    <div class="tool-icon" style="width: 48px; height: 48px; font-size: 1.25rem; margin-bottom: 0.75rem;">
                                        <i class="fas <?php echo htmlspecialchars($tool['icon']); ?>"></i>
                                    </div>
                                    <h5 style="font-size: 1rem;"><?php echo htmlspecialchars($tool['name']); ?></h5>
                                </div>
                            </a>
                        </div>
                        <?php endforeach; ?>
                    </div>
                </div>
                <?php endforeach; ?>

                <!-- 文章推荐 -->
                <?php if(!empty($featuredArticles)): ?>
                <section class="category-section">
                    <div class="category-header">
                        <h2><i class="fas fa-book-open"></i>精选文章</h2>
                        <a href="<?php echo SITE_URL; ?>/articles.php" class="btn btn-sm btn-outline-primary ml-auto">查看更多</a>
                    </div>
                    <div class="row">
                        <?php foreach($featuredArticles as $article): ?>
                        <div class="col-md-6 mb-4">
                            <a href="<?php echo SITE_URL; ?>/article.php?id=<?php echo $article['id']; ?>" style="text-decoration: none;">
                                <div class="article-card">
                                    <?php if($article['cover_image']): ?>
                                    <img src="<?php echo htmlspecialchars($article['cover_image']); ?>" alt="<?php echo htmlspecialchars($article['title']); ?>">
                                    <?php else: ?>
                                    <div style="height: 160px; background: linear-gradient(135deg, var(--primary-color), var(--primary-light)); display: flex; align-items: center; justify-content: center;">
                                        <i class="fas fa-image" style="font-size: 3rem; color: white; opacity: 0.5;"></i>
                                    </div>
                                    <?php endif; ?>
                                    <div class="card-body">
                                        <?php if($article['category_name']): ?>
                                        <span class="category-tag"><?php echo htmlspecialchars($article['category_name']); ?></span>
                                        <?php endif; ?>
                                        <h5><?php echo htmlspecialchars($article['title']); ?></h5>
                                        <p><?php echo mb_substr(strip_tags($article['summary']), 0, 80); ?>...</p>
                                        <div class="article-meta">
                                            <span><i class="far fa-eye mr-1"></i><?php echo $article['view_count']; ?> 阅读</span>
                                            <span><i class="far fa-clock mr-1"></i><?php echo date('m-d', strtotime($article['created_at'])); ?></span>
                                        </div>
                                    </div>
                                </div>
                            </a>
                        </div>
                        <?php endforeach; ?>
                    </div>
                </section>
                <?php endif; ?>
            </div>

            <!-- 侧边栏 -->
            <div class="col-lg-3">
                <div class="sidebar">
                    <!-- 快捷入口 -->
                    <div class="sidebar-card">
                        <h4><i class="fas fa-rocket mr-2"></i>快捷入口</h4>
                        <a href="<?php echo BBS_URL; ?>" class="quick-link" target="_blank">
                            <i class="fas fa-comments"></i>
                            <span>访问论坛</span>
                        </a>
                        <a href="<?php echo ADMIN_URL; ?>" class="quick-link">
                            <i class="fas fa-cog"></i>
                            <span>管理后台</span>
                        </a>
                    </div>

                    <!-- 侧边栏广告 -->
                    <?php if($sidebarAd): ?>
                    <div class="sidebar-card" style="padding: 0.75rem;">
                        <?php 
                        if($sidebarAd['image']): ?>
                            <a href="<?php echo htmlspecialchars($sidebarAd['link']); ?>" target="_blank">
                                <img src="<?php echo htmlspecialchars($sidebarAd['image']); ?>" alt="<?php echo htmlspecialchars($sidebarAd['title']); ?>" style="width: 100%; border-radius: 8px;">
                            </a>
                        <?php elseif($sidebarAd['code']): ?>
                            <?php echo $sidebarAd['code']; ?>
                        <?php endif; ?>
                    </div>
                    <?php endif; ?>

                    <!-- 热门文章 -->
                    <?php
                    $stmt = $db->query("SELECT * FROM articles WHERE is_published = 1 ORDER BY view_count DESC LIMIT 5");
                    $hotArticles = $stmt->fetchAll();
                    ?>
                    <?php if(!empty($hotArticles)): ?>
                    <div class="sidebar-card">
                        <h4><i class="fas fa-chart-line mr-2"></i>热门文章</h4>
                        <?php foreach($hotArticles as $i => $hot): ?>
                        <a href="<?php echo SITE_URL; ?>/article.php?id=<?php echo $hot['id']; ?>" class="quick-link" style="border-bottom: 1px solid #f3f4f6;">
                            <span style="background: var(--primary-color); color: white; width: 20px; height: 20px; border-radius: 4px; display: inline-flex; align-items: center; justify-content: center; font-size: 0.75rem; margin-right: 0.5rem;"><?php echo $i + 1; ?></span>
                            <span style="flex: 1;"><?php echo htmlspecialchars($hot['title']); ?></span>
                        </a>
                        <?php endforeach; ?>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

    <!-- 页脚 -->
    <footer>
        <div class="container">
            <div class="row">
                <div class="col-md-4 mb-4">
                    <h5><i class="fas fa-tools mr-2"></i><?php echo SITE_NAME; ?></h5>
                    <p style="color: #9CA3AF; font-size: 0.9rem;">提供各类实用的在线工具，帮助用户提升工作效率，解决日常问题。</p>
                </div>
                <div class="col-md-4 mb-4">
                    <h5>快速链接</h5>
                    <ul class="list-unstyled">
                        <li><a href="<?php echo SITE_URL; ?>"><i class="fas fa-home mr-2"></i>网站首页</a></li>
                        <li><a href="<?php echo SITE_URL; ?>/articles.php"><i class="fas fa-newspaper mr-2"></i>文章列表</a></li>
                        <li><a href="<?php echo BBS_URL; ?>" target="_blank"><i class="fas fa-comments mr-2"></i>访问论坛</a></li>
                    </ul>
                </div>
                <div class="col-md-4 mb-4">
                    <h5>关于我们</h5>
                    <ul class="list-unstyled">
                        <li><a href="#"><i class="fas fa-info-circle mr-2"></i>关于我们</a></li>
                        <li><a href="#"><i class="fas fa-envelope mr-2"></i>联系我们</a></li>
                        <li><a href="<?php echo ADMIN_URL; ?>"><i class="fas fa-user-shield mr-2"></i>管理登录</a></li>
                    </ul>
                </div>
            </div>
            <div class="footer-bottom">
                <p>&copy; <?php echo date('Y'); ?> <?php echo SITE_NAME; ?> All Rights Reserved. | <a href="<?php echo BBS_URL; ?>" target="_blank">辰迅研论圈</a></p>
            </div>
        </div>
    </footer>

    <!-- 浮动按钮 -->
    <div class="float-btns">
        <a href="<?php echo BBS_URL; ?>" class="float-btn bbs" title="访问论坛" target="_blank">
            <i class="fas fa-comments"></i>
        </a>
        <a href="<?php echo ADMIN_URL; ?>" class="float-btn admin" title="管理后台">
            <i class="fas fa-cog"></i>
        </a>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
