<?= $this->extend('layouts/main') ?>

<?= $this->section('content') ?>
<div class="app-page-title">
    <div class="page-title-wrapper">
        <div class="page-title-heading">
            <div class="page-title-icon">
                <i class="pe-7s-monitor icon-gradient bg-mean-fruit"></i>
            </div>
            <div>
                Worker 로그 통합 관리
                <div class="page-title-subheading">
                    API Receiver 및 Worker 서비스의 로그를 한 곳에서 확인할 수 있습니다.
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="main-card mb-3 card">
            <div class="card-header">
                <div class="btn-actions-pane-left">
                    <div class="nav">
                        <button class="btn btn-sm btn-primary" onclick="refreshLogs()">
                            <i class="fa fa-sync"></i> 새로고침
                        </button>
                        <button class="btn btn-sm btn-success" id="autoRefreshBtn" onclick="toggleAutoRefresh()">
                            <i class="fa fa-play"></i> 자동 새로고침 시작
                        </button>
                    </div>
                </div>
                <div class="btn-actions-pane-right">
                    <button class="btn btn-sm btn-info" onclick="downloadLog()">
                        <i class="fa fa-download"></i> 로그 다운로드
                    </button>
                </div>
            </div>
            <div class="card-body">
                <!-- 필터 영역 -->
                <div class="row mb-3">
                    <div class="col-md-4">
                        <label>날짜 선택</label>
                        <select class="form-control" id="dateSelect" onchange="changeDate()">
                            <?php foreach ($availableDates as $availDate): ?>
                                <option value="<?= $availDate ?>" <?= $availDate === $date ? 'selected' : '' ?>>
                                    <?= $availDate ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label>로그 타입</label>
                        <select class="form-control" id="typeSelect" onchange="changeType()">
                            <option value="all" <?= $logType === 'all' ? 'selected' : '' ?>>전체</option>
                            <option value="api_receiver" <?= $logType === 'api_receiver' ? 'selected' : '' ?>>API Receiver</option>
                            <option value="naver_worker" <?= $logType === 'naver_worker' ? 'selected' : '' ?>>Naver Worker</option>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label>레벨 필터</label>
                        <select class="form-control" id="levelFilter" onchange="filterByLevel()">
                            <option value="all">전체</option>
                            <option value="ERROR">ERROR</option>
                            <option value="INFO">INFO</option>
                            <option value="DEBUG">DEBUG</option>
                        </select>
                    </div>
                </div>

                <!-- 통계 영역 -->
                <div class="row mb-3">
                    <div class="col-md-3">
                        <div class="card mb-3 widget-content bg-midnight-bloom">
                            <div class="widget-content-wrapper text-white">
                                <div class="widget-content-left">
                                    <div class="widget-heading">전체 로그</div>
                                    <div class="widget-subheading">Total Logs</div>
                                </div>
                                <div class="widget-content-right">
                                    <div class="widget-numbers text-white">
                                        <span id="totalCount"><?= count($logs) ?></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="card mb-3 widget-content bg-arielle-smile">
                            <div class="widget-content-wrapper text-white">
                                <div class="widget-content-left">
                                    <div class="widget-heading">INFO</div>
                                    <div class="widget-subheading">Information</div>
                                </div>
                                <div class="widget-content-right">
                                    <div class="widget-numbers text-white">
                                        <span id="infoCount"><?= count(array_filter($logs, fn($l) => $l['level'] === 'INFO')) ?></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="card mb-3 widget-content bg-grow-early">
                            <div class="widget-content-wrapper text-white">
                                <div class="widget-content-left">
                                    <div class="widget-heading">ERROR</div>
                                    <div class="widget-subheading">Errors</div>
                                </div>
                                <div class="widget-content-right">
                                    <div class="widget-numbers text-white">
                                        <span id="errorCount"><?= count(array_filter($logs, fn($l) => $l['level'] === 'ERROR')) ?></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="card mb-3 widget-content bg-premium-dark">
                            <div class="widget-content-wrapper text-white">
                                <div class="widget-content-left">
                                    <div class="widget-heading">DEBUG</div>
                                    <div class="widget-subheading">Debug Logs</div>
                                </div>
                                <div class="widget-content-right">
                                    <div class="widget-numbers text-white">
                                        <span id="debugCount"><?= count(array_filter($logs, fn($l) => $l['level'] === 'DEBUG')) ?></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 로그 테이블 -->
                <div class="table-responsive" style="max-height: 600px; overflow-y: auto;">
                    <table class="table table-hover table-striped table-sm" id="logTable">
                        <thead class="thead-dark" style="position: sticky; top: 0; z-index: 10;">
                            <tr>
                                <th style="width: 150px;">시간</th>
                                <th style="width: 80px;">레벨</th>
                                <th style="width: 120px;">소스</th>
                                <th style="width: 150px;">위치</th>
                                <th>메시지</th>
                            </tr>
                        </thead>
                        <tbody id="logTableBody">
                            <?php if (empty($logs)): ?>
                                <tr>
                                    <td colspan="5" class="text-center">로그가 없습니다.</td>
                                </tr>
                            <?php else: ?>
                                <?php foreach ($logs as $log): ?>
                                    <tr class="log-row" data-level="<?= esc($log['level']) ?>">
                                        <td><small><?= esc($log['timestamp']) ?></small></td>
                                        <td>
                                            <span class="badge badge-<?= $log['level'] === 'ERROR' ? 'danger' : ($log['level'] === 'INFO' ? 'success' : 'secondary') ?>">
                                                <?= esc($log['level']) ?>
                                            </span>
                                        </td>
                                        <td>
                                            <span class="badge badge-<?= strpos($log['source'], 'failed') !== false ? 'warning' : 'info' ?>">
                                                <?php 
                                                    $sourceLabel = [
                                                        'api_receiver' => 'API Receiver',
                                                        'naver_worker' => 'Worker',
                                                        'naver_worker_failed' => 'Worker (Failed)'
                                                    ];
                                                    echo $sourceLabel[$log['source']] ?? $log['source'];
                                                ?>
                                            </span>
                                        </td>
                                        <td><small><?= esc($log['location']) ?></small></td>
                                        <td>
                                            <small style="word-break: break-all;">
                                                <?= esc($log['message']) ?>
                                            </small>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
let autoRefreshInterval = null;
let isAutoRefresh = false;

function changeDate() {
    const date = document.getElementById('dateSelect').value;
    const type = document.getElementById('typeSelect').value;
    window.location.href = '<?= base_url('manage/workerlog') ?>?date=' + date + '&type=' + type;
}

function changeType() {
    const date = document.getElementById('dateSelect').value;
    const type = document.getElementById('typeSelect').value;
    window.location.href = '<?= base_url('manage/workerlog') ?>?date=' + date + '&type=' + type;
}

function refreshLogs() {
    location.reload();
}

function toggleAutoRefresh() {
    if (isAutoRefresh) {
        clearInterval(autoRefreshInterval);
        isAutoRefresh = false;
        document.getElementById('autoRefreshBtn').innerHTML = '<i class="fa fa-play"></i> 자동 새로고침 시작';
        document.getElementById('autoRefreshBtn').classList.remove('btn-danger');
        document.getElementById('autoRefreshBtn').classList.add('btn-success');
    } else {
        autoRefreshInterval = setInterval(refreshLogs, 10000); // 10초마다
        isAutoRefresh = true;
        document.getElementById('autoRefreshBtn').innerHTML = '<i class="fa fa-stop"></i> 자동 새로고침 중지';
        document.getElementById('autoRefreshBtn').classList.remove('btn-success');
        document.getElementById('autoRefreshBtn').classList.add('btn-danger');
    }
}

function filterByLevel() {
    const level = document.getElementById('levelFilter').value;
    const rows = document.querySelectorAll('.log-row');
    
    let visibleCount = 0;
    let infoCount = 0;
    let errorCount = 0;
    let debugCount = 0;
    
    rows.forEach(row => {
        const rowLevel = row.getAttribute('data-level');
        if (level === 'all' || rowLevel === level) {
            row.style.display = '';
            visibleCount++;
        } else {
            row.style.display = 'none';
        }
        
        // 통계 계산
        if (rowLevel === 'INFO') infoCount++;
        if (rowLevel === 'ERROR') errorCount++;
        if (rowLevel === 'DEBUG') debugCount++;
    });
    
    // 통계 업데이트
    if (level === 'all') {
        document.getElementById('totalCount').textContent = visibleCount;
    }
}

function downloadLog() {
    const date = document.getElementById('dateSelect').value;
    const type = document.getElementById('typeSelect').value;
    window.location.href = '<?= base_url('manage/workerlog/download') ?>?date=' + date + '&type=' + type;
}

// 페이지 로드 시 스크롤을 테이블 하단으로 (최신 로그가 위에 있으므로 생략)
</script>

<style>
.table-responsive {
    border: 1px solid #dee2e6;
}

.log-row:hover {
    background-color: #f8f9fa !important;
}

.badge {
    font-size: 0.75rem;
}

.widget-content {
    padding: 1rem;
}
</style>

<?= $this->endSection() ?>
