<?php
if (!defined("_GNUBOARD_")) exit;

$_dm = (!empty($_COOKIE['vpnlink_dark']) && $_COOKIE['vpnlink_dark'] === '1')
    || (isset($_g5_vpn_theme_adm) && $_g5_vpn_theme_adm === 'dark');
$_dm_cls = $_dm ? ' vpns-dm' : '';
?>
<style>
<?php if (!empty($_in_iframe)): ?>
/* iframe 모드: 본문 여백 */
body, #page-body { margin: 0 !important; padding: 14px 18px !important; min-height: auto !important; }
<?php endif; ?>
/* ── 로그 페이지 테마 변수 (myvpnservers와 동일한 vpns-wrap/vpns-dm 변수 재활용) ── */
.vpns-wrap {
    --vpns-card-bg   : #fff;
    --vpns-card-br   : #dee2e6;
    --vpns-th-bg     : #f4f5f7;
    --vpns-th-color  : #495057;
    --vpns-td-color  : #212529;
    --vpns-muted     : #6c757d;
    --vpns-row-hover : #f8f9fa;
    --vpns-well-bg   : #f8f9fa;
    --vpns-well-br   : #dee2e6;
    --vpns-well-color: #495057;
}
.vpns-dm {
    --vpns-card-bg   : #26262c;
    --vpns-card-br   : #3a3a44;
    --vpns-th-bg     : #1c1c22;
    --vpns-th-color  : #b0b0bc;
    --vpns-td-color  : #c8c8d2;
    --vpns-muted     : #8a8a96;
    --vpns-row-hover : #2e2e38;
    --vpns-well-bg   : #202026;
    --vpns-well-br   : #3a3a44;
    --vpns-well-color: #c0c0cc;
}

/* ── btn-default ── */
.vpns-dm .btn-default {
    background-image: none !important;
    background-color: #2c2c34 !important;
    border-color: #4a4a54 !important;
    color: #c8c8d2 !important;
}
.vpns-dm .btn-default:hover,
.vpns-dm .btn-default:focus {
    background-image: none !important;
    background-color: #38383e !important;
    border-color: #5a5a64 !important;
    color: #e4e4ec !important;
}
/* style.css:88의 .btn-default i { color:#444 } 를 다크모드에서 무력화 */
.vpns-dm .btn i { color: inherit !important; }

/* ── 필터 바 ── */
.vpns-log-filter {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    padding: 11px 14px;
    margin-bottom: 14px;
    border-radius: 6px;
    border: 1px solid var(--vpns-well-br);
    background: var(--vpns-well-bg);
}
.vpns-dm .form-control {
    background-color: #2c2c34 !important;
    border-color: #4a4a54 !important;
    color: #e0e0e8 !important;
}
.vpns-dm select.form-control option {
    background-color: #2c2c34;
    color: #e0e0e8;
}

/* ── 테이블 ── */
.vpns-table {
    font-size: 13px;
    margin-bottom: 0;
    color: var(--vpns-td-color);
}
.vpns-table thead th {
    background-color: var(--vpns-th-bg) !important;
    color: var(--vpns-th-color) !important;
    border-color: var(--vpns-card-br) !important;
    font-size: 0.85em;
    font-weight: 600;
    padding: 8px 10px;
    white-space: nowrap;
}
.vpns-table tbody td {
    border-color: var(--vpns-card-br) !important;
    color: var(--vpns-td-color) !important;
    vertical-align: middle;
    padding: 7px 10px;
}
.vpns-table tbody tr:hover td {
    background-color: var(--vpns-row-hover) !important;
}
.vpns-table .vpns-muted {
    color: var(--vpns-muted) !important;
}

/* ── 페이지네이션 ── */
.vpns-dm .pagination > li > a,
.vpns-dm .pagination > li > span {
    background-color: #26262c !important;
    border-color: #3a3a44 !important;
    color: #c8c8d2 !important;
}
.vpns-dm .pagination > .active > a,
.vpns-dm .pagination > .active > span {
    background-color: #3f65a8 !important;
    border-color: #3f65a8 !important;
    color: #fff !important;
}

/* ── 안내 박스 ── */
.vpns-dm .alert-info {
    background-color: #162030 !important;
    border-color: #1e3a5f !important;
    color: #7eafdf !important;
}
.vpns-dm .alert-warning {
    background-color: #2a1c0e !important;
    border-color: #4a3010 !important;
    color: #e8c87a !important;
}
</style>

<div class="vpns-wrap<?php echo $_dm_cls; ?>">

<?php if (empty($_vpns_frag)): ?>
<div style="display:flex; align-items:center; justify-content:space-between; flex-wrap:wrap; gap:8px; margin-bottom:14px;">
    <h1 style="margin:0;"><i class="fa fa-history"></i> 관리자 로그인 이력</h1>
    <a href="?ap=myvpnservers" class="btn btn-default btn-sm">
        <i class="fa fa-arrow-left"></i> 내 VPN서버관리로
    </a>
</div>
<?php endif; ?>

<div class="vpns-log-filter">
    <?php
    // 폼 action과 초기화 링크를 모드에 따라 결정
    if (!empty($_in_iframe)) {
        $__form_action = './';
        $__reset_href  = './?ap=vpnserver_sso_log&_iframe=1';
    } elseif (!empty($_vpns_frag)) {
        $__form_action = APMS_PARTNER_URL . '/vpnserver_log_frag.php';
        $__reset_href  = APMS_PARTNER_URL . '/vpnserver_log_frag.php';
    } else {
        $__form_action = '';
        $__reset_href  = '?ap=vpnserver_sso_log';
    }
    ?>
    <form method="get"
          action="<?php echo $__form_action; ?>"
          <?php if (!empty($_vpns_frag) && empty($_in_iframe)) echo 'data-vpns-frag="1"'; ?>
          style="display:contents;">
        <?php if (!empty($_in_iframe)): ?>
        <input type="hidden" name="ap" value="vpnserver_sso_log">
        <input type="hidden" name="_iframe" value="1">
        <?php elseif (empty($_vpns_frag)): ?>
        <input type="hidden" name="ap" value="vpnserver_sso_log">
        <?php endif; ?>
        <select name="vs_no" class="form-control input-sm" style="width:auto; min-width:120px;">
            <option value="">전체 서버</option>
            <?php foreach ($my_servers as $s): ?>
                <option value="<?php echo (int)$s['vs_no']; ?>"
                    <?php if ($filter_vs == $s['vs_no']) echo 'selected'; ?>>
                    <?php echo htmlspecialchars($s['vs_alias'] ?: $s['vs_hostname'], ENT_QUOTES); ?>
                </option>
            <?php endforeach; ?>
        </select>
        <select name="login_type" class="form-control input-sm" style="width:auto; min-width:130px;">
            <option value="">전체 유형</option>
            <option value="sso"    <?php if (($filter_type ?? '') === 'sso')    echo 'selected'; ?>>SSO 자동로그인</option>
            <option value="manual" <?php if (($filter_type ?? '') === 'manual') echo 'selected'; ?>>수동 로그인</option>
        </select>
        <button type="submit" class="btn btn-default btn-sm">
            <i class="fa fa-search"></i> 필터
        </button>
        <?php if ($filter_vs || ($filter_type ?? '')): ?>
            <a href="<?php echo $__reset_href; ?>" class="btn btn-link btn-sm" style="padding-left:4px;">초기화</a>
        <?php endif; ?>
        <span style="margin-left:auto; font-size:0.85em; color:var(--vpns-muted); white-space:nowrap;">
            총 <strong style="color:var(--vpns-td-color);"><?php echo number_format($total_count); ?></strong>건
        </span>
    </form>
</div>

<?php if (empty($list)): ?>
    <div class="alert alert-info">
        <i class="fa fa-info-circle"></i> 아직 관리자 로그인 이력이 없습니다.
    </div>
<?php else: ?>
    <div class="table-responsive">
        <table class="table table-hover vpns-table">
            <thead>
                <tr>
                    <th style="width:145px;">접속 일시</th>
                    <th style="width:110px;">유형</th>
                    <th>서버</th>
                    <th style="width:120px;">서버 IP</th>
                    <th style="width:80px;">계정</th>
                    <th style="width:130px;">접속 IP</th>
                    <th>브라우저</th>
                    <th style="width:70px;">추적 ID</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($list as $log):
                    $is_sso     = ($log['login_type'] === 'sso');
                    $type_label = $is_sso ? 'SSO 자동' : '수동';
                    $type_cls   = $is_sso ? 'success' : 'primary';
                ?>
                <tr>
                    <td style="white-space:nowrap; font-family:monospace; font-size:0.9em;">
                        <?php echo htmlspecialchars($log['sso_at'], ENT_QUOTES); ?>
                    </td>
                    <td>
                        <span class="label label-<?php echo $type_cls; ?>"><?php echo $type_label; ?></span>
                    </td>
                    <td>
                        <strong><?php echo htmlspecialchars($log['vs_alias'], ENT_QUOTES); ?></strong>
                    </td>
                    <td style="font-family:monospace; font-size:0.88em;">
                        <span class="vpns-muted"><?php echo htmlspecialchars($log['vs_ip'] ?: '—', ENT_QUOTES); ?></span>
                    </td>
                    <td style="font-family:monospace; font-size:0.88em;">
                        <?php echo htmlspecialchars($log['actor'] ?: '-', ENT_QUOTES); ?>
                    </td>
                    <td style="font-family:monospace; font-size:0.88em;">
                        <?php echo htmlspecialchars($log['client_ip'], ENT_QUOTES); ?>
                    </td>
                    <td style="max-width:260px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;"
                        title="<?php echo htmlspecialchars($log['user_agent'], ENT_QUOTES); ?>">
                        <span class="vpns-muted" style="font-size:0.88em;">
                            <?php echo htmlspecialchars($log['user_agent'], ENT_QUOTES); ?>
                        </span>
                    </td>
                    <td style="font-family:monospace; font-size:0.82em;">
                        <span class="vpns-muted">
                            <?php echo $log['nonce_prefix'] ? htmlspecialchars($log['nonce_prefix'], ENT_QUOTES) . '…' : '—'; ?>
                        </span>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <div class="text-center" style="margin-top:10px;">
        <ul class="pagination pagination-sm en">
            <?php echo apms_paging($write_pages, $page, $total_page, $list_page); ?>
        </ul>
    </div>
<?php endif; ?>

<div class="alert alert-warning" style="margin-top:16px; font-size:0.85em;">
    <i class="fa fa-info-circle"></i>
    <strong>SSO 자동로그인</strong> — 판매자센터 "서버 관리 열기" 버튼을 통한 자동 로그인 &nbsp;|&nbsp;
    <strong>수동 로그인</strong> — VPN 서버 패널에서 아이디/비밀번호로 직접 로그인.
    상세 접속 로그는 각 서버의 <code>logs/admin_actions.log</code>에서 확인할 수 있습니다.
</div>

</div><!-- /.vpns-wrap -->
