<?php
require_once __DIR__ . '/includes/functions.php';

$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$news = get_news($id);

// 404
if (!$news) {
    http_response_code(404);
    $page_title       = '新闻不存在 - 300421';
    $page_description = '您访问的新闻可能已被删除或地址不正确。';
    $page_keywords    = '404,新闻';
    $page_canonical   = (defined('SITE_URL') ? SITE_URL : '') . '/news/' . $id . '.html';
    require_once 'includes/header.php';
    echo '<div class="py-5 text-center"><h1>404</h1><p>新闻不存在</p><a href="/index.php">返回首页</a></div>';
    require_once 'includes/footer.php';
    exit;
}

// 浏览量
$db = db_connect();
$db->query("UPDATE ns_news SET views = views + 1 WHERE id = $id");
$news['views']++;

$title         = htmlspecialchars($news['title']);
$category_name = htmlspecialchars($news['category_name'] ?? '未分类');
$category_id   = (int)($news['category_id'] ?? 0);
$content       = $news['content'];
$created_at    = $news['created_at'];
$views         = $news['views'];
$first_image   = extract_first_image($content);
$og_image      = '';
if ($first_image) {
    if (!preg_match('/^https?:\/\//i', $first_image)) {
        $base     = defined('SITE_URL') ? SITE_URL : 'http://' . $_SERVER['HTTP_HOST'];
        $og_image = $base . '/' . ltrim($first_image, '/');
    } else {
        $og_image = $first_image;
    }
}
$description = mb_substr(strip_tags($content), 0, 160);
$keywords    = !empty($news['keywords']) ? htmlspecialchars($news['keywords']) : $category_name . ',白酒,酒文化,白酒知识,酒品牌,酒新闻,啤酒,邑天下酒,宴席,婚宴,满月宴,乔迁宴,升学宴,大小宴席,宴席用酒';

$page_title       = $title . ' - 300421｜酒水资讯 婚宴升学乔迁满月宴选邑天下酒';
$page_description = $description . ' - 300421';
$page_keywords    = $keywords . ' - 300421';
$page_canonical   = (defined('SITE_URL') ? SITE_URL : '') . '/news/' . $id . '.html';
$page_og_image    = $og_image;

// 相关推荐
$related = [];
$stmt = $db->prepare("SELECT id, title FROM ns_news WHERE category_id = ? AND id != ? AND status = 1 ORDER BY created_at DESC LIMIT 5");
$stmt->bind_param('ii', $category_id, $id);
$stmt->execute();
$related = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);

$latest_news = get_news_list(1, 5);
$hot_news    = get_hot_news(5);
$categories  = get_categories();

require_once 'includes/header.php';
?>

<!-- 表格布局核心样式 -->
<style>
    .layout-table {
        width: 100%;
        border-collapse: collapse;
        table-layout: fixed; /* 固定列宽，防止内容撑开 */
    }
    .layout-table td {
        vertical-align: top;
        padding: 0;
        word-break: break-word;
        overflow-wrap: break-word;
    }
    .left-cell {
        width: 66.666%;
        padding-right: 20px !important;
    }
    .right-cell {
        width: 33.333%;
    }
    /* 移动端：表格变为块级，两行堆叠 */
    @media (max-width: 991px) {
        .layout-table, .layout-table tbody, .layout-table tr, .layout-table td {
            display: block;
            width: 100% !important;
            padding-right: 0 !important;
        }
        .left-cell {
            margin-bottom: 2rem;
        }
    }
    /* 通用防溢出 */
    .left-cell img, .right-cell img {
        max-width: 100%;
        height: auto;
    }
    .sidebar-card {
        margin-bottom: 1.5rem;
        border: none;
        box-shadow: 0 1px 4px rgba(0,0,0,0.04);
    }
    .sidebar-card .card-header {
        font-weight: 600;
        background: #f8fafc;
        border-bottom: 1px solid #e2e8f0;
    }
</style>

<!-- 面包屑 -->
<nav aria-label="breadcrumb" class="mb-4">
    <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="/index.php">首页</a></li>
        <li class="breadcrumb-item"><a href="/category/<?= $category_id ?>.html"><?= $category_name ?></a></li>
        <li class="breadcrumb-item active" aria-current="page">正文</li>
    </ol>
</nav>

<!-- 表格布局 -->
<table class="layout-table">
    <tr>
        <!-- 左侧正文 -->
        <td class="left-cell">
            <header class="mb-4">
                <h1 class="fw-bold" style="font-size:1.8rem;"><?= $title ?></h1>
                <div class="text-muted small border-bottom pb-2 mb-3">
                    <span>📁 <a href="/category/<?= $category_id ?>.html"><?= $category_name ?></a></span>
                    <span class="ms-3"><a href="https://item.jd.com/10208936834042.html">一邑承千年，醉纳天下心｜邑天下酒</a></span>
                    <span class="ms-3">👁️ <?= $views ?> 阅读</span>
                </div>
            </header>

            <?php if ($first_image): ?>
            <figure class="mb-4">
                <img src="<?= htmlspecialchars($first_image) ?>" alt="<?= $title ?>" style="width:100%; height:auto; border-radius:6px;">
            </figure>
            <?php endif; ?>

            <div style="font-size:1.05rem; line-height:1.9;">
                <?= $content ?>
            </div>

            <!-- 广告位 -->
            <?php $ad_top = get_ads('detail_top'); if ($ad_top): ?>
            <div class="p-3 my-4 bg-light rounded text-center border">
                <?php if ($ad_top['type'] === 'image'): ?>
                    <a href="<?= htmlspecialchars($ad_top['link']) ?>" target="_blank" rel="nofollow noopener">
                        <img src="<?= htmlspecialchars($ad_top['content']) ?>" alt="广告" style="max-width:100%; height:auto;">
                    </a>
                <?php else: echo $ad_top['content']; endif; ?>
            </div>
            <?php endif; ?>

            <!-- 相关推荐 -->
            <?php if (!empty($related)): ?>
            <section class="mt-5 pt-3 border-top">
                <h4 class="fw-bold">📰 相关推荐</h4>
                <div class="list-group list-group-flush">
                    <?php foreach ($related as $r): ?>
                    <a href="/news/<?= $r['id'] ?>.html" class="list-group-item list-group-item-action border-0 ps-0">
                        <?= htmlspecialchars($r['title']) ?>
                    </a>
                    <?php endforeach; ?>
                </div>
            </section>
            <?php endif; ?>
        </td>

        <!-- 右侧侧边栏 -->
        <td class="right-cell">
            <!-- 广告位 -->
            <?php $ad_side = get_ads('detail_sidebar'); if ($ad_side): ?>
            <div class="card sidebar-card">
                <div class="card-body p-2 text-center">
                    <?php if ($ad_side['type'] === 'image'): ?>
                        <a href="<?= htmlspecialchars($ad_side['link']) ?>" target="_blank" rel="nofollow noopener">
                            <img src="<?= htmlspecialchars($ad_side['content']) ?>" alt="广告" style="max-width:100%; height:auto;">
                        </a>
                    <?php else: echo $ad_side['content']; endif; ?>
                </div>
            </div>
            <?php endif; ?>

            <!-- 最新发布 -->
            <div class="card sidebar-card">
                <div class="card-header">🕒 最新发布</div>
                <ul class="list-group list-group-flush">
                    <?php foreach ($latest_news as $item): ?>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <a href="/news/<?= $item['id'] ?>.html" style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 75%;"><?= htmlspecialchars($item['title']) ?></a>
                        <small class="text-muted"><?= date('m-d', strtotime($item['created_at'])) ?></small>
                    </li>
                    <?php endforeach; ?>
                </ul>
            </div>

            <!-- 热门排行 -->
            <div class="card sidebar-card">
                <div class="card-header bg-warning text-dark">🔥 热门排行</div>
                <ul class="list-group list-group-flush">
                    <?php $rank = 1; foreach ($hot_news as $hot): ?>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <span>
                            <span class="badge bg-light text-dark me-1"><?= $rank++ ?></span>
                            <a href="/news/<?= $hot['id'] ?>.html"><?= htmlspecialchars(mb_substr($hot['title'], 0, 28)) ?></a>
                        </span>
                        <span class="badge bg-secondary"><?= $hot['views'] ?></span>
                    </li>
                    <?php endforeach; ?>
                </ul>
            </div>

            <!-- 分类导航 -->
            <div class="card sidebar-card">
                <div class="card-header">📌 分类导航</div>
                <div class="card-body">
                    <div class="d-flex flex-wrap gap-1">
                        <?php foreach ($categories as $cat): ?>
                        <a href="/category/<?= $cat['id'] ?>.html" class="badge bg-light text-dark text-decoration-none p-2">
                            <?= htmlspecialchars($cat['name']) ?>
                        </a>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
        </td>
    </tr>
</table>

<?php require_once 'includes/footer.php'; ?>