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

$ary_dataCategory = [
    '' => '請選擇',
    'WE' => '氣象',
    'WA' => '波浪',
    'OC' => '洋流',
    'GE' => '地(震測)'
];
$ary_licenseMode = ["依政府資料開放平臺使用規範", "書面授權", "自由文件授權"];
$ary_downloadType = ["original" => "原始資料", 'processed' => "處理後資料", 'results' => "模擬成果", 'simulation' => "資料使用 / 模擬教學"]; //檔案下載類型
$ary_fileFormat = ["CSV", "grib2", "SEG-Y", "nc4", "NC", "WRF", "STL", "ZIP"]; //檔案格式
$detail_list = [];
if (isset($_REQUEST['rid']) && $_REQUEST['rid'] > 0) {
    $data_id  = $_REQUEST['rid'];
    log_use("DataList_in",$data_id);
    // 從資料庫中讀取詳細資料
    // $sql = "SELECT * FROM data_service WHERE data_id  = :data_id Limit 1";
    $sql = 'SELECT a.*, GROUP_CONCAT(DISTINCT b.fileFormat) AS fileFormats ';
    $sql .= ' FROM data_service  AS a ';
    $sql .= ' LEFT JOIN (SELECT DISTINCT data_id, fileFormat FROM data_service_detail) AS b ON a.data_id = b.data_id ';
    $sql .= " WHERE a.data_id  = :data_id GROUP BY a.data_id Limit 1 ;";
    $sql .= " UPDATE data_service SET viewQty=(viewQty+1) WHERE data_id  = :data_id  ;"; // 觀看次數
    //echo $sql;
    $stmt = $conn->prepare($sql);
    $stmt->execute(array(':data_id' => $data_id));
    $data = $stmt->fetch(PDO::FETCH_ASSOC);
    if (!$data) {
        echo "<script>alert('資料不存在！'); window.location.href = 'data_list.php';</script>";
        exit;
    }
    $mcategory = $data['dataCategory'];
    if (isset($categories[$mcategory])) {
        $categories = [$mcategory => $categories[$mcategory]];
    }

    $sql = "SELECT * FROM data_service_detail WHERE data_id  = :data_id order by downloadType,detail_id ";
    $stmt = $conn->prepare($sql);
    $stmt->execute(array(':data_id' => $data_id));
    $detail_list = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>
<!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>
        .table-section-title {
            display: flex;
            align-items: center;
            font-size: 1.2rem;
            font-weight: bold;
            padding: 10px;
            background-color: #D5F0FF;
            border-radius: 8px 8px 0 0;
        }

        .table-section-title i {
            margin-right: 10px;
        }
    </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="DataList.php">資料服務</a></li>
                    <li class="breadcrumb-item active"><?php echo htmlspecialchars($data['dataTitle'] ?? ''); ?></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($data['dataTitle'] ?? ''); ?></div>
            <p class="data-info"><?php echo htmlspecialchars($data['dataSummary'] ?? ''); ?></p>
            <table class="table table-responsive table-bordered data-table mt-3 mb-4">
                <tr>
                    <th>資料編號</th>
                    <td><?php echo htmlspecialchars($data['dataNo'] ?? ''); ?></td>
                </tr>
                <tr>
                    <th>資料來源單位</th>
                    <td><?php echo htmlspecialchars($data['dataSource'] ?? ''); ?></td>
                </tr>
                <tr>
                    <th>資料時間</th>
                    <td><?= !empty($data['StartDate']) ? date('Y-m', strtotime($data['StartDate'])) : ''; ?>
                        <?= (empty($data['EndDate']) || $data['EndDate'] == "1970-01-01") ? '' : "至 " . date('Y-m', strtotime($data['EndDate'])); ?>
                    </td>
                </tr>
                <tr>
                    <th>資料類別</th>
                    <td><?php echo $ary_dataCategory[$data['dataCategory'] ?? ''] ?? ''; ?>
                    </td>
                </tr>
                <tr>
                    <th>授權模式</th>
                    <td><?php echo htmlspecialchars($data['licenseMode'] ?? ''); ?></td>
                </tr>
                <tr>
                    <th>資料來源</th>
                    <td><?php echo htmlspecialchars($data['source'] ?? ''); ?></td>
                </tr>
                <tr>
                    <th>檔案格式</th>
                    <td><?php echo htmlspecialchars($data['fileFormats'] ?? ''); ?></td>
                </tr>
                <tr>
                    <th>更新時間</th>
                    <td><?= date('Y-m-d H:i', strtotime($data['update_date'])) ?? '' ?></td>
                </tr>
            </table>
            <?php if (!empty($detail_list)):  ?>
                <div class="data-title">下載專區</div>
                <?php
                $downloadTypeKey = "";
                foreach ($detail_list as $index => $detail):
                    // 檢查是否需要開始一個新的下載類別區塊
                    if ($downloadTypeKey !== $detail['downloadType']) :
                        // 如果不是第一筆資料，結束前一個表格
                        if ($index > 0): ?>
                            </tbody>
                            </table>
                        <?php endif;

                        // 更新當前的下載類別鍵
                        $downloadTypeKey = $detail['downloadType'] ?? '';
                        ?>
                        <div class="table-section-title">
                             <i class="bi bi-plus-circle-fill" style="color:#173480;"></i> 
                            <?php
                            echo isset($ary_downloadType[$downloadTypeKey]) ? $ary_downloadType[$downloadTypeKey] : $downloadTypeKey;
                            ?>
                        </div>
                        <table class="table table-responsive table-bordered data-table1  mb-4">
                            <thead>
                                <tr>
                                    <th style="width:27%;">檔案名稱</th>
                                    <th style="width:47%;">說明</th>
                                    <th style="width:13%;">檔案格式</th>
                                    <th>下載檔案</th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php endif; ?>
                            <tr>
                                <td class="align-middle"><?php echo htmlspecialchars($detail['fileName'] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars($detail['file_description'] ?? ''); ?></td>
                                <td class="text-center"><?= strtoupper(htmlspecialchars($detail['fileFormat'] ?? '')); ?></td>
                                <td class="text-center">
                                    <a href="./uploads/data/<?= htmlspecialchars($detail['fileName'] ?? ''); ?>" class="icon-download" download title="資料下載, 另開視窗">
                                        <i class="bi bi-file-earmark-arrow-down"></i>
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                            </tbody>
                        </table>
                    <?php endif; ?>
                    <!-- <div class="table-section-title">
                <i class="bi bi-plus-circle-fill" style="color:#173480;"></i> 原始資料
            </div>
            <table class="table table-responsive table-bordered data-table1  mb-4">
                <thead>
                    <tr>
                        <th style="width:27%;">檔案名稱</th>
                        <th style="width:47%;">說明</th>
                        <th style="width:13%;">檔案格式</th>
                        <th>下載檔案</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td class=" align-middle">檔案名稱</td>
                        <td>檔案說明檔案說明檔案說明檔案說明檔案說明檔案說明檔案說明檔案說明</td>
                        <td class=" text-center">CSV</td>
                        <td class=" text-center">
                            <a href="#" class="icon-download"><i class="bi bi-file-earmark-arrow-down"></i></a>
                        </td>
                    </tr>
                    <tr>
                        <td class=" align-middle">檔案名稱</td>
                        <td>檔案說明檔案說明檔案說明檔案說明檔案說明</td>
                        <td class=" text-center align-middle">CSV</td>
                        <td class=" text-center">
                            <a href="#" class="icon-download"><i class="bi bi-file-earmark-arrow-down"></i></a>
                        </td>
                    </tr>
                    <tr>
                        <td class=" align-middle">檔案名稱檔案名稱檔案名稱</td>
                        <td>檔案說明檔案說明檔案說明檔案說明檔案說明檔案說明檔案說明檔案說明</td>
                        <td class=" text-center align-middle">CSV</td>
                        <td class=" text-center">
                            <a href="#" class="icon-download"><i class="bi bi-file-earmark-arrow-down"></i></a>
                        </td>
                    </tr>
                </tbody>
            </table>

            <div class="table-section-title">
                <i class="bi bi-plus-circle-fill" style="color:#173480;"></i> 處理後資料
            </div>
            <table class="table table-responsive table-bordered data-table1  mb-4">
                <thead>
                    <tr>
                        <th style="width:27%;">檔案名稱</th>
                        <th style="width:47%;">說明</th>
                        <th style="width:13%;">檔案格式</th>
                        <th>下載檔案</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td  class=" align-middle">檔案名稱</td>
                        <td>檔案說明檔案說明檔案說明檔案說明檔案說明檔案說明檔案說明檔案說明</td>
                        <td class=" text-center align-middle">CSV</td>
                        <td class=" text-center">
                            <a href="#" class="icon-download"><i class="bi bi-file-earmark-arrow-down"></i></a>
                        </td>
                    </tr>                    
                </tbody>
            </table>

            <div class="table-section-title">
                <i class="bi bi-plus-circle-fill" style="color:#173480;"></i> 模擬成果
            </div>
            <table class="table table-responsive table-bordered data-table1  mb-4">
                <thead>
                    <tr>
                        <th style="width:27%;">檔案名稱</th>
                        <th style="width:47%;">說明</th>
                        <th style="width:13%;">檔案格式</th>
                        <th>下載檔案</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td class=" align-middle">檔案名稱</td>
                        <td>檔案說明檔案說明檔案說明檔案說明檔案說明檔案說明檔案說明檔案說明</td>
                        <td class=" text-center align-middle">WRF</td>
                        <td class=" text-center">
                            <a href="#" class="icon-download"><i class="bi bi-file-earmark-arrow-down"></i></a>
                        </td>
                    </tr>                    
                </tbody>
            </table>
            <div class="table-section-title">
                <i class="bi bi-plus-circle-fill" style="color:#173480;"></i> 資料使用 / 模擬教材
            </div>
            <table class="table table-responsive table-bordered data-table1  mb-4">
                <thead>
                    <tr>
                        <th style="width:27%;">檔案名稱</th>
                        <th style="width:47%;">說明</th>
                        <th style="width:13%;">檔案格式</th>
                        <th>下載檔案</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td class=" align-middle">檔案名稱</td>
                        <td>檔案說明檔案說明檔案說明檔案說明檔案說明檔案說明檔案說明檔案說明</td>
                        <td class=" text-center align-middle ">WRF</td>
                        <td class=" text-center">
                            <a href="#" class="icon-download"><i class="bi bi-file-earmark-arrow-down"></i></a>
                        </td>
                    </tr>                    
                </tbody>
            </table>
 -->
        </section>
    </main>


    <!-- <a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i class="bi bi-arrow-up-short"></i></a> -->

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


</html>