<?php
include_once 'src/inc/db/function.php';
// 設定每頁顯示的資料數量
$itemsPerPage = 10; // 每頁顯示 10 筆資料

// 取得當前頁碼，若無設定則預設為第 1 頁
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? intval($_GET['page']) : 1;
$page = max(1, $page); // 確保頁碼不小於 1
// 計算總筆數
$sqlTotal = 'SELECT COUNT(*) FROM system_news';
$totalItems = $conn->query($sqlTotal)->fetchColumn();

// 計算總頁數
$totalPages = ceil($totalItems / $itemsPerPage);

// 計算查詢起始位置（OFFSET）
$offset = ($page - 1) * $itemsPerPage;

// 查詢目前頁面所需的資料
$sql = 'SELECT * FROM system_news LIMIT :limit OFFSET :offset';
$stmt = $conn->prepare($sql);
$stmt->bindValue(':limit', $itemsPerPage, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$news_list = $stmt->fetchAll(PDO::FETCH_ASSOC);

// 計算顯示的頁碼範圍
$displayPages = 3; // 每次最多顯示的頁碼數量
$startPage = max(1, $page - floor($displayPages / 2));
$endPage = min($totalPages, $startPage + $displayPages - 1);

// 確保顯示頁碼範圍不超過總頁數
if ($endPage - $startPage + 1 < $displayPages) {
    $startPage = max(1, $endPage - $displayPages + 1);
}

?>
<!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>
        /* .underline {
            text-decoration: underline;
        } */

        .news-text {
            font-family: Microsoft JhengHei;

            /* font-size: 18px;
font-weight: 700;
line-height: 23.94px;
letter-spacing: 0.05em;
text-align: left; */

            color: #000000;
        }

        .news-text:hover {
            color: #173480;
            font-weight: bolder;
        }

        #table1 th {
            border: 2px solid #fff;
            background-color: #ECF1FF;
            /* 背景顏色設為淺藍色 */
        }

        #table1 td {
            border: 2px solid #ECF1FF;
        }

        #table1 tr:hover td {
            background-color: #ECFAFE;
            /* 背景顏色設為淺藍色 */
        }

        .date-badge {
            background-color: #518dff;
            color: white;
            padding: 6px;
            width: 120px;
            font-family: Microsoft JhengHei;
            font-weight: 400;
            line-height: 21.28px;

        }



        /* .page-link {
            border: none;
        } */
    </style>

</head>


<body class="d-flex flex-column min-vh-100">

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

    <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 active">系統公告</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>
                <!-- <a href="#"><img src="./assets/img/回上一頁.png" class="img img-fluid" alt="回上一頁"  /></a> -->
            </span>
        </div>
        <section class="section" style="max-width:1280px;">

            <div class="d-flex align-items-center">
                <div class="head-line" style="width:80px;"></div>
                <span class="head-title mx-2 ">系統公告</span>
                <div class="head-line " style="flex-grow: 1;"> </div>
            </div>


            <!-- 表格區域 -->
            <table id="table1" class="table text-center table-bordered">
                <thead>
                    <tr>
                        <th style="width:8%;">流水號</th>
                        <th style="width:12%;min-width:120px;">發佈日期</th>
                        <th>標題</th>
                        <th style="width:12%;">發佈單位</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (count($news_list) > 0): $rno = ($page - 1) * $itemsPerPage; ?>
                        <?php foreach ($news_list as $news): ?>
                            <tr>
                                <td class=" text-center"><?= $rno += 1; ?></td>
                                <td>
                                    <span class="date-badge"><?= !empty($news['publish_date']) ? date('Y-m-d', strtotime($news['publish_date'])) : ''; ?></span>
                                </td>
                                <td style="text-align: left;" onclick="goNews(<?= $news['news_id'] ?? 0; ?>)">
                                    <a href="SystemNews_in.php?id=<?= $news['news_id'] ?? 0; ?>" class="news-text">
                                        <?php echo htmlspecialchars($news['title'] ?? ''); ?>
                                </td>
                                <td><?php echo htmlspecialchars($news['publish_unit'] ?? ''); ?></a></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="6" class="text-center">目前沒有公告資料。</td>
                        </tr>
                    <?php endif; ?>


                    <!-- <tr>
                        <td>1</td>
                        <td><span class="date-badge">2024-04-01</span></td>
                        <td style="text-align: left;">
                            <a href="SystemNews_in.php?id=1" class="news-text">
                                系統公告系統公告系統公告系統公告系統公告系統公告系統公告系統公告系統公告
                            </a>
                        </td>
                        <td>系統管理員</td>
                    </tr>
                    <tr>
                        <td>2</td>
                        <td><span class="date-badge">2024-04-01</span></td>
                        <td style="text-align: left;">
                            <a href="SystemNews_in.php?id=1" class="news-text">
                                系統維修公告
                            </a>
                        </td>
                        <td>系統管理員</td>
                    </tr>
                    <tr>
                        <td>3</td>
                        <td><span class="date-badge">2024-04-01</span></td>
                        <td style="text-align: left;">系統公告</td>
                        <td>系統管理員</td>
                    </tr>
                    <tr>
                        <td>4</td>
                        <td><span class="date-badge">2024-04-01</span></td>
                        <td style="text-align: left;">
                            <a href="SystemNews_in.php?id=1" class="news-text">
                                系統公告系統公告公告系統公告系統公告公告
                            </a>
                        </td>
                        <td>系統管理員</td>
                    </tr> -->
                </tbody>
            </table>

            <!-- 分頁 -->
            <div style="display: flex; justify-content: center; align-items: center; margin-top: 30px;">
                <span>共<?php echo $totalPages; ?>頁&nbsp; 每頁10筆</span>&nbsp;&nbsp;
                <nav aria-label="Page navigation" class="ms-4">
                    <ul class="pagination">
                        <?php if ($startPage > 1) : ?>
                            <li class="page-item "><a class="page-link" href="?page=1">1</a></li>
                            <li class="page-item disabled"><span class="page-link">...</span></li>
                        <?php endif; ?>

                        <?php for ($i = $startPage; $i <= $endPage; $i++) : ?>
                            <li class="page-item  <?php echo $i == $page ? 'active' : ''; ?>"><a class="page-link" href="?page=<?php echo $i; ?>"><?php echo $i; ?></a></li>
                        <?php endfor; ?>

                        <?php if ($endPage < $totalPages) : ?>
                            <li class="page-item disabled"><span class="page-link">...</span></li>
                            <li class="page-item "><a class="page-link" href="?page=<?php echo $totalPages; ?>"><?php echo $totalPages; ?></a></li>
                        <?php endif; ?>
                    </ul>
                </nav>
            </div>
        </section>


        <!--  <section class="section ">
            
          
                <div style="float:right ;display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
                  
                    <button onclick="window.history.back();" style="border: none; background-color: #f1f1f1; border-radius: 8px; padding: 5px 10px; cursor: pointer;">回上一頁</button>
                </div>
               <div class="card1" >  
                <table class="table table-hover table-responsive table-bordered">
                    <thead>
                        <tr class="table-primary">
                            <th scope="col" class="align-middle">流水號</th>
                            <th scope="col" class="align-middle">發布日期</th>
                            <th scope="col" class="align-middle">標題</th>
                            <th scope="col" class="align-middle">發布單位</th>
                        </tr>
                    </thead>

                    <tbody>

                        <tr>
                            <td>
                                1
                            </td>
                            <td>
                                2024-04-01
                            </td>
                            <td>
                                <a href="SystemNews_in.php?id=1" class="underline">
                                    系統維修公告
                                </a>
                            </td>
                            <td>
                                系統管理員
                            </td>
                        </tr>

                        <?php

                        foreach ($resultData['rows'] as $row) {

                        ?>

                            <tr>
                                <td>
                                    <?php
                                    $index++;
                                    echo $index;
                                    ?>
                                    1
                                </td>
                                <td>
                                    <?php echo $row['publish_date'] ? htmlspecialchars($row['publish_date'], ENT_QUOTES, 'UTF-8') : ''; ?>
                                    2024-04-01
                                </td>
                                <td>
                                    <a href="SystemNews_in.php?id=<?php echo $row['announcement_id'] ? htmlspecialchars($row['announcement_id'], ENT_QUOTES, 'UTF-8') : 0; ?>" class="underline">
                                        <?php echo $row['title'] ? htmlspecialchars($row['title'], ENT_QUOTES, 'UTF-8') : ''; ?>
                                    </a>
                                    <a href="SystemNews_in.php?id=1" class="underline">
                                        系統維修公告
                                    </a>
                                </td>
                                <td>
                                    <?php echo $row['publish_unit'] ? htmlspecialchars($row['publish_unit'], ENT_QUOTES, 'UTF-8') : ''; ?>
                                    系統管理員
                                </td>
                            </tr>

                        <?php

                        }

                        ?>

                    </tbody>
                </table>
              
               
            </div>

            <br><br>
        </section> -->


    </main>

    <!--BEGIN FOOTER-->
    <?php include_once "template_footer.php"; ?>
    <script>
        showSidebar("sidebar", "SystemNews");

        function goNews(mid) {
            if (mid > 0) {
                window.location.href = 'SystemNews_in.php?id=' + mid;
            }
        }
    </script>

</body>


</html>