GNU/_PAGE/monitoring/upbit/daemon_history/history_btc.php
<?php
// 기본 환경 설정 파일 포함
include_once('./_common.php'); 

// filepath: c:\xampp\htdocs\GNU\_PAGE\monitoring\upbit\daemon_history\history_btc.php
require_once '/home/www/DB/db_upbit.php';

$limit = 15;
$page  = isset($_GET['page']) && (int)$_GET['page'] > 0 ? (int)$_GET['page'] : 1;

$tableMap = [
    '1m'  => ['table' => 'HISTORY_BTC_1m',  'label' => '1분봉',  'unit' => 1],
    '1h'  => ['table' => 'HISTORY_BTC_1h',  'label' => '1시간',  'unit' => 60],
    '1w'  => ['table' => 'HISTORY_BTC_1w',  'label' => '1주일',  'unit' => 10080],
    '4h'  => ['table' => 'HISTORY_BTC_4h',  'label' => '4시간',  'unit' => 240],
    '24h' => ['table' => 'HISTORY_BTC_24h', 'label' => '24시간', 'unit' => 1440],
    '30d' => ['table' => 'HISTORY_BTC_30d', 'label' => '30일',  'unit' => 43200],
];

$tf = isset($_GET['tf']) ? $_GET['tf'] : '1m';
if (!isset($tableMap[$tf])) $tf = '1m';

$tableName  = $tableMap[$tf]['table'];
$tableLabel = $tableMap[$tf]['label'];
$unitValue  = $tableMap[$tf]['unit'];

try {
    $maxSql = "SELECT MAX(id) FROM {$tableName} WHERE market = 'KRW-BTC'";
    $max_id = (int)$db_upbit->query($maxSql)->fetchColumn();

    $total_pages = $max_id > 0 ? (int)ceil($max_id / $limit) : 1;
    if ($page > $total_pages) { $page = $total_pages; }

    $rows = [];
    if ($max_id > 0) {
        $id_to   = $max_id - (($page - 1) * $limit);
        $id_from = $id_to - $limit + 1;
        if ($id_from < 1) $id_from = 1;

        $sql = "SELECT 
                    id, market, candle_date_time_utc, candle_date_time_kst, 
                    opening_price, high_price, low_price, trade_price, 
                    timestamp, candle_acc_trade_price, candle_acc_trade_volume,
                    change_rate
                FROM {$tableName} 
                WHERE market = 'KRW-BTC' AND id BETWEEN :id_from AND :id_to 
                ORDER BY id DESC";

        $stmt = $db_upbit->prepare($sql);
        $stmt->bindValue(':id_from', $id_from, PDO::PARAM_INT);
        $stmt->bindValue(':id_to',   $id_to,   PDO::PARAM_INT);
        $stmt->execute();
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
} catch (PDOException $e) {
    die("DB 오류: " . $e->getMessage());
}

// 헤더 부분 포함
include_once(G5_PATH.'/_head.php');
?>
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <title>BTC History Monitoring</title>
    <style>
        /* 기본 레이아웃 */
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { 
            background-color: #020617; 
            color: #f8fafc; 
            font-family: 'Pretendard', sans-serif;
            width: 100%;
            line-height: 1.5;
        }

        .PageBox {
            padding: 30px 50px;
        }

        /* 상단 헤더 섹션 */
        .header-section {
            display: flex;
            justify-content: space-between;
            align-items: flex-end;
            margin-bottom: 25px;
        }
        .title-area h1 { font-size: 1.6rem; font-weight: 700; margin-bottom: 8px; }
        .title-area p { color: #94a3b8; font-size: 0.9rem; }
        .title-area span { color: #38bdf8; font-weight: 600; }

        /* 셀렉트 박스 UI 복구 */
        .control-panel select {
            background-color: #0c1224;
            color: #fff;
            border: 1px solid #232e42;
            padding: 10px 18px;
            border-radius: 4px;
            outline: none;
            cursor: pointer;
            font-weight: 600;
            transition: all 0.2s;
        }
        .control-panel select:hover { border-color: #38bdf8; background-color: #1a2436; }

        /* 테이블 디자인 */
        .table-wrapper {
            background-color: #0c1224;
            border-radius: 6px;
            border: 1px solid #1a2436;
            overflow-x: auto;
            box-shadow: 0 15px 30px -5px rgba(0, 0, 0, 0.5);
        }
        table { width: 100%; border-collapse: collapse; min-width: 1400px; }
        th {
            background-color: #1a2436;
            color: #94a3b8;
            font-size: 0.75rem;
            text-transform: uppercase;
            padding: 15px 12px;
            border-bottom: 1px solid #232e42;
            letter-spacing: 0.05em;
        }
        td {
            padding: 14px 12px;
            font-size: 0.85rem;
            border-bottom: 1px solid #232e42;
            text-align: right;
            font-variant-numeric: tabular-nums;
        }
        tr:last-child td { border-bottom: none; }
        tr:hover td { background-color: #1a2436; color: #fff; cursor: default; }

        /* 컬럼별 정렬 및 색상 */
        .text-left { text-align: left; }
        .text-center { text-align: center; }
        .up { color: #ef4444; }
        .down { color: #3b82f6; }
        .muted { color: #64748b; }
        .highlight-price { font-weight: 700; color: #fff; background-color: rgba(56, 189, 248, 0.05); }

        /* 페이지네이션 UI 복구 */
        .pagination {
            margin-top: 30px;
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 5px;
        }
        .pagination a, .pagination strong {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            min-width: 38px;
            height: 38px;
            padding: 0 10px;
            border-radius: 4px;
            text-decoration: none;
            font-size: 0.85rem;
            background-color: #0c1224;
            color: #94a3b8;
            border: 1px solid #232e42;
            transition: all 0.2s;
        }
        .pagination a:hover {
            border-color: #38bdf8;
            color: #fff;
            background-color: #1a2436;
        }
        .pagination strong {
            background-color: #38bdf8;
            color: #020617;
            border-color: #38bdf8;
            font-weight: 700;
        }
        .pagination .nav-btn { font-weight: 600; px: 15px; }
    </style>
</head>
<body>

<div class="PageBox">
    <div class="header-section">
        <div class="title-area">
            <h1>BTC Market History</h1>
            <!-- 데이터 원본 및 페이지 정보 복구 -->
            <p>데이터 원본: <span><?php echo $tableName; ?></span> | 현재 페이지: <span><?php echo number_format($page); ?></span> / <?php echo number_format($total_pages); ?></p>
        </div>
        
        <form method="get" class="control-panel">
            <select name="tf" onchange="this.form.submit()">
                <?php foreach ($tableMap as $key => $info): ?>
                    <option value="<?php echo $key; ?>" <?php echo $tf === $key ? 'selected' : ''; ?>>
                        <?php echo $info['label']; ?> 조회
                    </option>
                <?php endforeach; ?>
            </select>
        </form>
    </div>

    <div class="table-wrapper">
        <table>
            <thead>
                <tr>
                    <th class="text-center">ID</th>
                    <th class="text-center">Market</th>
                    <th class="text-center">Unit</th>
                    <th class="text-left">KST 일시</th>
                    <th class="text-left">UTC 일시</th>
                    <th>시가 (Open)</th>
                    <th>고가 (High)</th>
                    <th>저가 (Low)</th>
                    <th>종가 (Close)</th>
                    <th>변화율 (%)</th>
                    <th>누적 거래량</th>
                    <th>누적 거래대금</th>
                    <th class="text-center">Timestamp</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $row): 
                    $c_rate = $row['change_rate'] * 100;
                    $rate_class = ($c_rate > 0) ? 'up' : (($c_rate < 0) ? 'down' : '');
                ?>
                <tr>
                    <td class="text-center muted"><?php echo $row['id']; ?></td>
                    <td class="text-center" style="color:#38bdf8; font-weight:600;"><?php echo $row['market']; ?></td>
                    <td class="text-center muted"><?php echo $unitValue; ?></td>
                    <td class="text-left"><?php echo $row['candle_date_time_kst']; ?></td>
                    <td class="text-left muted" style="font-size: 0.75rem;"><?php echo $row['candle_date_time_utc']; ?></td>
                    <td><?php echo number_format($row['opening_price']); ?></td>
                    <td class="up"><?php echo number_format($row['high_price']); ?></td>
                    <td class="down"><?php echo number_format($row['low_price']); ?></td>
                    <td class="highlight-price"><?php echo number_format($row['trade_price']); ?></td>
                    <td class="<?php echo $rate_class; ?>" style="font-weight:bold;">
                        <?php echo ($c_rate > 0 ? '+' : '') . number_format($c_rate, 2); ?>%
                    </td>
                    <td><?php echo number_format($row['candle_acc_trade_volume'], 3); ?></td>
                    <td class="muted"><?php echo number_format($row['candle_acc_trade_price']); ?></td>
                    <td class="text-center muted" style="font-size: 0.75rem;"><?php echo $row['timestamp']; ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <!-- 페이지네이션 복구 (처음, 이전, 숫자, 다음, 마지막) -->
    <div class="pagination">
        <?php
        $block = 10;
        $start = floor(($page - 1) / $block) * $block + 1;
        $end   = min($start + $block - 1, $total_pages);

        // 처음으로
        if ($page > 1) {
            echo "<a href='?tf={$tf}&page=1' class='nav-btn'>First</a>";
        }

        // 이전 블록
        if ($start > 1) {
            echo "<a href='?tf={$tf}&page=".($start - 1)."' class='nav-btn'>Prev</a>";
        }

        // 숫자 페이지
        for ($i = $start; $i <= $end; $i++) {
            if ($i == $page) echo "<strong>$i</strong>";
            else echo "<a href='?tf={$tf}&page={$i}'>$i</a>";
        }

        // 다음 블록
        if ($end < $total_pages) {
            echo "<a href='?tf={$tf}&page=".($end + 1)."' class='nav-btn'>Next</a>";
        }

        // 마지막으로
        if ($page < $total_pages) {
            echo "<a href='?tf={$tf}&page={$total_pages}' class='nav-btn'>Last</a>";
        }
        ?>
    </div>
</div>

</body>
</html>

<?php require_once G5_PATH.'/_PAGE/tail.php'; ?>