<?php
// 数据库配置
$db_host = 'localhost';
$db_user = 'new_hnbaobao';
$db_pass = 'd3atFbKkfYSeGKD5';
$db_name = 'new_hnbaobao';

// 连接数据库
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) { die('数据库连接失败：' . mysqli_connect_error()); }
mysqli_set_charset($conn, 'utf8mb4');

// 获取分类参数
$category = $_GET['category'] ?? '';

// 构建查询条件
$where = "status=1";
if (!empty($category)) {
    $category = mysqli_real_escape_string($conn, $category);
    $where .= " AND category='$category'";
}

// 查询知识库列表
$sql = "SELECT * FROM health_knowledge WHERE $where ORDER BY sort ASC, create_time DESC";
$result = mysqli_query($conn, $sql);
$knowledge_list = [];
while ($row = mysqli_fetch_assoc($result)) {
    $knowledge_list[] = $row;
}

// 查询所有分类
$category_sql = "SELECT DISTINCT category FROM health_knowledge WHERE status=1";
$category_result = mysqli_query($conn, $category_sql);
$categories = [];
while ($row = mysqli_fetch_assoc($category_result)) {
    $categories[] = $row['category'];
}

// 统计各分类数量
$category_count = [];
foreach ($categories as $cat) {
    $count_sql = "SELECT COUNT(*) AS num FROM health_knowledge WHERE status=1 AND category='$cat'";
    $count_result = mysqli_query($conn, $count_sql);
    $count_row = mysqli_fetch_assoc($count_result);
    $category_count[$cat] = $count_row['num'];
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>健康知识库 - imt.hnbaobao.com</title>
    <link rel="stylesheet" href="css/layout.css">
    <style>
        /* 知识库专属样式 - 匹配目标站细节 */
        .category-list {
            list-style: none;
            padding: 0;
            margin: 0;
        }
        .category-item {
            margin-bottom: 8px;
        }
        .category-item a {
            text-decoration: none;
            color: #666;
            font-size: 14px;
        }
        .category-item a.active {
            color: #1890ff;
            font-weight: 700;
        }
        .article-item {
            border-bottom: 1px solid #f0f0f0;
            padding: 16px 0;
        }
        .article-item:last-child {
            border-bottom: none;
        }
        .article-title {
            font-size: 18px;
            font-weight: 700;
            margin: 0 0 8px;
            color: #333;
        }
        .article-meta {
            font-size: 12px;
            color: #8c8c8c;
            margin: 0 0 8px;
        }
        .article-meta span {
            margin-right: 10px;
        }
        .article-content {
            line-height: 1.8;
            color: #666;
        }
    </style>
</head>
<body>
    <!-- 头部：与目标站保持一致 -->
    <header class="header">
        <div class="container header-inner">
            <div class="logo-wrap">
                <a href="/" class="logo">优芷懿健康管理</a>
                <nav class="nav-main">
                    <div class="nav-item"><a href="/">首页</a></div>
                    <div class="nav-item"><a href="health_knowledge.php" class="active">健康知识库</a></div>
                    <div class="nav-item"><a href="/#service">服务项目</a></div>
                    <div class="nav-item"><a href="/#about">关于我们</a></div>
                    <div class="nav-item"><a href="/#contact">联系我们</a></div>
                    <div class="nav-item"><a href="/#reserve">在线预约</a></div>
                </nav>
            </div>
        </div>
    </header>

    <!-- 主体：目标站双栏布局核心 -->
    <main class="main">
        <div class="container">
            <div class="layout-two-col">
                <!-- 左侧分类栏：目标站侧边栏布局 -->
                <div class="sidebar-col">
                    <div class="content-block">
                        <h3 style="font-size: 16px; margin: 0 0 16px; font-weight: 700;">知识分类</h3>
                        <ul class="category-list">
                            <li class="category-item">
                                <a href="health_knowledge.php" class="<?php echo empty($category) ? 'active' : ''; ?>">
                                    全部文章 (<?php echo count($knowledge_list); ?>)
                                </a>
                            </li>
                            <?php foreach ($categories as $cat): ?>
                            <li class="category-item">
                                <a href="health_knowledge.php?category=<?php echo urlencode($cat); ?>" 
                                   class="<?php echo $category == $cat ? 'active' : ''; ?>">
                                    <?php echo $cat; ?> (<?php echo $category_count[$cat]; ?>)
                                </a>
                            </li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                </div>

                <!-- 右侧内容栏：目标站文章列表布局 -->
                <div class="main-col">
                    <div class="content-block">
                        <h2 style="font-size: 20px; margin: 0 0 16px; font-weight: 700;">健康知识库</h2>
                        <?php if (empty($knowledge_list)): ?>
                            <p style="color: #8c8c8c; text-align: center; padding: 40px 0;">暂无相关健康知识文章</p>
                        <?php else: ?>
                            <?php foreach ($knowledge_list as $item): ?>
                            <div class="article-item">
                                <h3 class="article-title"><?php echo $item['title']; ?></h3>
                                <div class="article-meta">
                                    <span class="category" style="color: #1890ff;"><?php echo $item['category']; ?></span>
                                    <span class="date"><?php echo date('Y-m-d H:i', strtotime($item['create_time'])); ?></span>
                                </div>
                                <div class="article-content">
                                    <?php echo $item['content']; ?>
                                </div>
                            </div>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <!-- 页脚：与目标站保持一致 -->
    <footer class="footer">
        <div class="container">
            © 2026 imt.hnbaobao.com 湖南优芷懿健康管理有限责任公司 版权所有
        </div>
    </footer>

    <script>
        // 访问统计
        fetch('count_visit.php?page=health_knowledge').catch(err => console.log('统计失败：', err));
    </script>
</body>
</html>