<?php
session_start();

// --- 1. 極致初始化：確保所有 include 內的 Loop 都不會報錯 ---
$web_id  = "fion"; // 這裡必須定義，因為 inc_list2.php 第 32 行正在循環使用它
$web_id2 = "fionkc21";
$web_title = "【飛揚新聞】";
$web_description = "飛越宇宙、揚起思潮";
$web_name = "飛揚新聞";
$web_url = "https://fion.ikh.tw/";
$urlline = "https://fion.ikh.tw/";
$og_img = $web_url."fb.jpg";
$fb_img = $web_url."fb.jpg";
$sqlt = ""; 

// 預設 HTML 變數
$t_head = $t_navi = $t_hp_img = $t_footer = $t_list1 = $t_list2 = $t_list3 = $t_vw = "";
$site_viewer = 0;

// --- 2. 參數過濾 (加上基本防護) ---
$rid = isset($_GET["id"]) ? str_replace("'", "''", $_GET["id"]) : "";
$kw  = isset($_POST["kw"]) ? $_POST["kw"] : "";
$pn  = isset($_GET["pn"]) ? $_GET["pn"] : "";
$cl  = isset($_GET["c"]) ? $_GET["c"] : "";
$tit = isset($_GET["ti"]) ? $_GET["ti"] : "";

// --- 3. 資料庫連線 ---
$db_config = __DIR__ . '/../dsn_tdbs.php';
$c_tdbs = null;
if (file_exists($db_config)) {
    require_once($db_config);
}

// --- 4. 資料庫優化 ---
if ($c_tdbs) {
    if ($pn == "vw" && !empty($rid)) {
        $sql_up = "UPDATE article_data SET viewer = viewer + 1 WHERE id2 = ?";
        sqlsrv_query($c_tdbs, $sql_up, array($rid));
    }
    if (rand(1, 10) == 1) { 
        $sql_site_up = "UPDATE site_data SET viewer = viewer + 1 WHERE id2 = ?";
        sqlsrv_query($c_tdbs, $sql_site_up, array($web_id2));
    }
    $sql_v = "SELECT viewer FROM site_data WITH (NOLOCK) WHERE id2 = ?";
    $res_v = sqlsrv_query($c_tdbs, $sql_v, array($web_id2));
    if ($res_v && $row_v = sqlsrv_fetch_array($res_v, SQLSRV_FETCH_ASSOC)) {
        $site_viewer = $row_v["viewer"];
    }
}

// --- 5. 模組載入 ---
// 這裡直接 include，不要包在 function 裡
if (file_exists('inc_navi.php'))   include 'inc_navi.php';
if (file_exists('inc_hp_img.php')) include 'inc_hp_img.php';
if (file_exists('inc_footer.php')) include 'inc_footer.php';
if (file_exists('inc_list2.php'))  include 'inc_list2.php'; 
if (file_exists('inc_list1.php'))  include 'inc_list1.php'; 

if ($tit != "" || $kw != "") {
    if (file_exists('inc_list3.php')) include 'inc_list3.php';
}
if ($pn == "vw") {
    if (file_exists('inc_vw.php')) include 'inc_vw.php';
}
if (file_exists('inc_head.php')) include 'inc_head.php';

	if (isset($c_tdbs) && $c_tdbs) {
    sqlsrv_close($c_tdbs);}
?>

<!DOCTYPE html>
<html lang="zh-Hant">
<?php echo $t_head; ?>
<body>
<?php echo $t_navi; ?>
<main>
    <section class="section">
        <div class="container">
            <div class="row">
                <div class="col-lg-8">
                    <div class="row">
                        <?php 
                        if ($tit != '') echo $t_list3; 
                        elseif ($kw != '') echo $t_list3; 
                        elseif ($pn == "vw") echo $t_vw; 
                        else {
                            echo $t_hp_img;
                            echo $t_list1;
                        }
                        ?>
                    </div>
                </div>
                <?php echo $t_list2; ?>
            </div>
        </div>
    </section>
</main>
<?php echo $t_footer; ?>
<script src="/plugins/jquery/jquery.min.js"></script>
<script src="/plugins/bootstrap/bootstrap.min.js"></script>
<script src="/js/script.js"></script>
</body>
</html>