<?php
// 資料庫連線設定
$host = 'localhost';
$dbname = 'web';
$username = 'root';     // 請換成您的資料庫使用者帳號
$password = 'labndr'; // 請換成您的資料庫密碼

try {
    // 建立 PDO 連線
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // --- 分頁設定 ---
    $limit = 50; // 每頁最多 50 筆
    $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
    if ($page < 1) $page = 1;
    $offset = ($page - 1) * $limit;

    // 取得總筆數
    $stmtTotal = $pdo->query("SELECT COUNT(*) FROM `others`");
    $totalRows = $stmtTotal->fetchColumn();
    $totalPages = ceil($totalRows / $limit);

    // 查詢當前頁面的其它資料，依 id 排序
    $stmt = $pdo->prepare("SELECT title, name, personnel, year, month FROM `others` ORDER BY `id` ASC LIMIT :limit OFFSET :offset");
    $stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
    $stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
    $stmt->execute();
    $others = $stmt->fetchAll(PDO::FETCH_ASSOC);

} catch (PDOException $e) {
    $error_message = "資料庫連線失敗：" . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="zh-TW">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>其它 | 積體電路設計與光電系統應用實驗室</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: "PingFang TC", "Helvetica Neue", Helvetica, Arial, sans-serif; line-height: 1.6; color: #333; background-color: #f9f9f9; }
        a { text-decoration: none; color: #0056b3; transition: color 0.3s; }
        header { background-color: #ffffff; box-shadow: 0 2px 4px rgba(0,0,0,0.1); position: fixed; width: 100%; top: 0; z-index: 1000; }
        .nav-container { max-width: 1200px; margin: 0 auto; padding: 1rem 2rem; display: flex; justify-content: space-between; align-items: center; }
        .logo { font-size: 1.2rem; font-weight: bold; color: #0056b3; }
        nav ul { list-style: none; display: flex; gap: 1.5rem; align-items: center; }
        nav a { color: #333; font-weight: 500; }

        .dropdown { position: relative; }
        .dropdown-menu { display: none; position: absolute; top: 100%; left: 0; background-color: #fff; box-shadow: 0 4px 10px rgba(0,0,0,0.15); border-radius: 6px; padding: 8px 0; min-width: 160px; z-index: 1001; }
        .dropdown-menu li { display: block; width: 100%; }
        .dropdown-menu a { padding: 8px 16px; color: #333; display: block; font-size: 0.95rem; white-space: nowrap; }
        .dropdown-menu a:hover { background-color: #f0f7ff; color: #0056b3; }
        .dropdown:hover .dropdown-menu { display: block; }

        .page-content { max-width: 1000px; margin: 120px auto 60px; padding: 40px; background: #fff; border-radius: 8px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }
        .page-title { font-size: 2rem; color: #7f8c8d; border-bottom: 3px solid #7f8c8d; padding-bottom: 10px; margin-bottom: 30px; }
        
        .item-card { padding: 15px 20px; border-left: 4px solid #7f8c8d; background: #fdfdfd; margin-bottom: 15px; border-radius: 0 4px 4px 0; box-shadow: 0 1px 3px rgba(0,0,0,0.05); }
        .item-title { font-weight: bold; font-size: 1.05rem; color: #111; }
        .item-sub { color: #555; font-size: 0.95rem; margin-top: 4px; }
        .item-meta { color: #7f8c8d; font-size: 0.9rem; margin-top: 4px; font-weight: 500; }
        .error-box { color: red; padding: 10px; margin-bottom: 15px; background: #ffe6e6; border-radius: 4px; }
        
        /* 分頁樣式 */
        .pagination { display: flex; justify-content: center; gap: 8px; margin-top: 30px; }
        .pagination a, .pagination span { padding: 8px 12px; border: 1px solid #ddd; border-radius: 4px; color: #333; font-size: 0.9rem; }
        .pagination a:hover { background-color: #f0f7ff; border-color: #0056b3; color: #0056b3; }
        .pagination .active { background-color: #7f8c8d; color: #fff; border-color: #7f8c8d; }

        footer { background-color: #333; color: #fff; text-align: center; padding: 30px 20px; }
    </style>
</head>
<body>
    <header>
        <div class="nav-container">
            <div class="logo"><a href="index.html" style="color: inherit;">積體電路設計與光電系統應用實驗室</a></div>
            <nav>
                <ul>
                    <li><a href="index.html">首頁</a></li>
                    <li class="dropdown">
                        <a href="#" style="color: #7f8c8d; font-weight: bold;">研究成果 ▾</a>
                        <ul class="dropdown-menu">
                            <li><a href="journals.php">期刊論文</a></li>
                            <li><a href="conferences.php">會議論文</a></li>
                            <li><a href="projects.php">研究計畫</a></li>
                            <li><a href="patents.php">專利</a></li>
                            <li><a href="awards.php">獲獎榮譽</a></li>
                            <li><a href="activities.php">學術活動</a></li>
                            <li><a href="extensions.php">推廣工作</a></li>
                            <li><a href="others.php">其它</a></li>
                        </ul>
                    </li>
                </ul>
            </nav>
        </div>
    </header>

    <div class="page-content">
        <h1 class="page-title">其它 (Others)</h1>
        
        <?php if (isset($error_message)): ?>
            <div class="error-box"><?php echo htmlspecialchars($error_message); ?></div>
        <?php elseif (!empty($others)): ?>
            <?php foreach ($others as $row): ?>
                <div class="item-card">
                    <div class="item-title"><?php echo htmlspecialchars($row['title']); ?></div>
                    <div class="item-sub">
                        人員：<?php echo htmlspecialchars($row['personnel']); ?> 
                        <?php if (!empty($row['name'])): ?>
                            | 名稱：<?php echo htmlspecialchars($row['name']); ?>
                        <?php endif; ?>
                    </div>
                    <div class="item-meta">
                        <?php if (!empty($row['year'])): ?>
                            時間：<?php echo htmlspecialchars($row['year']); ?>年
                            <?php if (!empty($row['month'])): ?>
                                <?php echo htmlspecialchars($row['month']); ?>月
                            <?php endif; ?>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endforeach; ?>

            <!-- 分頁按鈕列 -->
            <?php if ($totalPages > 1): ?>
                <div class="pagination">
                    <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                        <?php if ($i == $page): ?>
                            <span class="active"><?php echo $i; ?></span>
                        <?php else: ?>
                            <a href="?page=<?php echo $i; ?>"><?php echo $i; ?></a>
                        <?php endif; ?>
                    <?php endfor; ?>
                </div>
            <?php endif; ?>

        <?php else: ?>
            <p>目前尚無其它資料。</p>
        <?php endif; ?>
    </div>

    <footer><p>&copy; 2026 積體電路設計與光電系統應用實驗室. All rights reserved.</p></footer>
</body>
</html>
