<?php
include_once 'src/inc/db/function.php';

$news = false;
if (isset($_REQUEST['id']) && $_REQUEST['id'] > 0) {
    $news_id =  htmlspecialchars($_REQUEST['id'], ENT_QUOTES, 'UTF-8');

    if ($news_id) {
        // 從資料庫中讀取該公告的詳細資料
        $sql = "SELECT * FROM system_news WHERE news_id = :news_id";
        // $sql = "SELECT * FROM system_news WHERE news_id = ?";
        $stmt = $conn->prepare($sql);
        // $stmt->bindParam(':news_id', $news_id);
        $stmt->execute([':news_id' => $news_id]);
        $news = $stmt->fetch(PDO::FETCH_ASSOC);
    }
}

if (!$news) {
    echo "<script>alert('公告不存在！'); </script>";
    header("Location: SystemNews.php");
    // echo "<script>alert('公告不存在！'); window.location.href = 'SystemNews.php';</script>";
    exit;
}
$file_names = explode('，', $news['attachment_url']);
$title = mb_strlen($news['title'] ?? '') > 10 ? mb_substr($news['title'], 0, 10) . '...' : ($news['title'] ?? '');


?>
<!DOCTYPE html>

<head>
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <?php include_once "template_css.php"; ?>
    <!-- <title>系統公告</title> -->

    <style>
        .data-title {
            font-family: Microsoft JhengHei;
            font-size: 20px;
            font-weight: 700;
            color: #173480;
        }

        .data-info {
            margin-bottom: 15px;
            font-size: 18px;
            font-weight: 400;
        }
    </style>
</head>

<body>
    <!--BEGIN HEADER-->
    <?php include_once "template_header.php"; ?>
    <!--END HEADER-->

    <!--BEGIN SIDEBAR-->
    <?php include_once "template_sidebar.php"; ?>
    <!--END SIDEBAR-->

    <main id="main" class="main">
        <div class="pagetitle d-flex  justify-content-between align-items-center">
            <nav>
                <ol class="breadcrumb">
                    <li class="breadcrumb-item "><a href="index.php"><i class="bi bi-house-door-fill"></i> 首頁</a></li>
                    <li class="breadcrumb-item"><a href="SystemNews.php">系統公告</a></li>
                    <li class="breadcrumb-item active"><?=$title?></li>
                </ol>
            </nav>
            <span class="float: right;">
                <a href="#" onclick="window.history.back();" class="btn btn-sm  btn-outline-primary"><i class="bi bi-arrow-90deg-left"></i> 回上一頁</a>
            </span>
        </div>

        <section class="section">
            <div class="data-title"><?php echo htmlspecialchars($news['title'] ?? ''); ?></div>
            <div class="row mt-3 mb-4">
                <div class="col-md-4">發布日期：<b><?= !empty($news['publish_date']) ? date('Y-m-d', strtotime($news['publish_date'] ?? '')) : ''; ?></b></div>
                <div class="col-md-4"> <span> 發布單位：</span><b><?php echo htmlspecialchars($news['publish_unit'] ?? ''); ?></b> </div>

            </div>
            <p class="data-info" style="min-height:250px;">
                <?php echo htmlspecialchars($news['content'] ?? ''); ?>
                <!-- 摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要 -->
            </p>

            <?php if (!empty($file_names[0])): ?>
                <div class="row " style="margin: 3rem 1rem 2rem 1rem;">
                    <div class="data-info" style="width: 200px; border-bottom :1px solid #000;">附件下載</div>
                    <ul id="file-list">
                        <?php
                        foreach ($file_names as $file_name): ?>
                            <?php if (!empty($file_name)): ?>
                                <li class="list-group-item mb-1">
                                    <i class="bi bi-download"></i>&nbsp;
                                    <a href="./uploads/news/<?php echo htmlspecialchars($file_name); ?>" class="news-text" alt="download" title="download" target="_blank"><?php echo htmlspecialchars($file_name); ?></a>
                                </li>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </ul>

                    <!-- <a href="#" class="news-text mb-1" alt="download">
                    <i class="bi bi-download"></i>&nbsp;檔案名稱檔案名稱.pdf
                </a>
                <a href="#" class="news-text mb-1" alt="download">
                    <i class="bi bi-download"></i>&nbsp;檔案名稱檔案名稱.pdf
                </a>
                <a href="#" class="news-text mb-1" alt="download">
                    <i class="bi bi-download"></i>&nbsp;檔案下載.pdf
                </a> -->
                </div>
            <?php endif; ?>
        </section>
    </main>

    <?php include_once "template_footer.php"; ?>
    <script>
        showSidebar("sidebar", "SystemNews");
    </script>
</body>


</html>