<?php
include_once('./_common.php');

if ($is_guest) {
    alert('판매자센터만 이용가능합니다.', APMS_PARTNER_URL . '/login.php');
}
if ($is_admin !== 'super') {
    alert('최고관리자만 열람할 수 있습니다.', APMS_PARTNER_URL);
}

$table = $g5['apms_vpn_server_log'];

$f_ip = isset($_GET['ip']) ? trim((string)$_GET['ip']) : '';
$f_pt = isset($_GET['pt_id']) ? trim((string)$_GET['pt_id']) : '';
$f_result = isset($_GET['result']) ? trim((string)$_GET['result']) : '';
$page = isset($_GET['page']) && (int)$_GET['page'] > 0 ? (int)$_GET['page'] : 1;
$rows_per_page = 50;

$where = array();
if ($f_ip !== '') {
    $where[] = "vs_ip = '" . sql_real_escape_string($f_ip) . "'";
}
if ($f_pt !== '') {
    $where[] = "(pt_id_attempted = '" . sql_real_escape_string($f_pt) . "' or previous_pt_id = '" . sql_real_escape_string($f_pt) . "')";
}
if ($f_result !== '') {
    $where[] = "result = '" . sql_real_escape_string($f_result) . "'";
}
$where_sql = count($where) > 0 ? ' where ' . implode(' and ', $where) : '';

$cnt_row = sql_fetch("select count(*) as cnt from {$table}{$where_sql}");
$total = (int)($cnt_row['cnt'] ?? 0);
$total_page = max(1, (int)ceil($total / $rows_per_page));
if ($page > $total_page) $page = $total_page;
$offset = ($page - 1) * $rows_per_page;

$list = array();
$result = sql_query("select * from {$table}{$where_sql} order by log_no desc limit {$offset}, {$rows_per_page}");
while ($row = sql_fetch_array($result)) {
    $list[] = $row;
}

$qs = function ($overrides) use ($f_ip, $f_pt, $f_result) {
    $params = array_merge(array('ip' => $f_ip, 'pt_id' => $f_pt, 'result' => $f_result), $overrides);
    return http_build_query(array_filter($params, function ($v) { return $v !== ''; }));
};

include_once(G5_ADMIN_PATH . '/admin.head.sub.php');
include_once($skin_path . '/_head.php');
?>

<h1><i class="fa fa-shield"></i> VPN 서버 등록/소유권 변경 감사 로그</h1>

<div class="well" style="padding:12px 15px;">
	VPN 서버가 판매자센터에 등록되거나 소유권이 이전될 때마다(성공/실패 모두) 기록됩니다.
	비정상적인 반복 시도나 예상치 못한 계정 변경을 추적하는 데 사용하세요. 총 <strong><?php echo number_format($total); ?></strong>건.
</div>

<form class="form-inline" method="get" style="margin-bottom:14px;">
	<input type="text" name="ip" value="<?php echo htmlspecialchars($f_ip, ENT_QUOTES); ?>" placeholder="서버 IP" class="form-control input-sm" style="margin-right:6px;">
	<input type="text" name="pt_id" value="<?php echo htmlspecialchars($f_pt, ENT_QUOTES); ?>" placeholder="판매자 ID" class="form-control input-sm" style="margin-right:6px;">
	<select name="result" class="form-control input-sm" style="margin-right:6px;">
		<option value="">전체 결과</option>
		<?php foreach (array('success', 'invalid_credentials', 'not_approved_seller', 'missing_server_ip', 'missing_credentials') as $r) { ?>
			<option value="<?php echo $r; ?>" <?php echo ($f_result === $r) ? 'selected' : ''; ?>><?php echo $r; ?></option>
		<?php } ?>
	</select>
	<button type="submit" class="btn btn-primary btn-sm">필터</button>
	<a href="?" class="btn btn-default btn-sm">초기화</a>
</form>

<div class="table-responsive">
<table class="table table-bordered table-condensed" style="font-size:12px;">
	<tr style="background:#f5f5f5;">
		<th>시간</th><th>결과</th><th>시도 판매자</th><th>이전 판매자</th><th>서버 IP/호스트</th><th>요청 IP</th><th>등급/점수/권장</th><th>메시지</th>
	</tr>
	<?php if (empty($list)) { ?>
		<tr><td colspan="8" class="text-center text-muted">기록이 없습니다.</td></tr>
	<?php } ?>
	<?php foreach ($list as $r) {
		$is_success = ($r['result'] === 'success');
		$row_style = $is_success ? '' : "background:#fff5f5;";
	?>
	<tr style="<?php echo $row_style; ?>">
		<td style="white-space:nowrap;"><?php echo htmlspecialchars($r['created_at'], ENT_QUOTES); ?></td>
		<td>
			<?php if ($is_success) { ?>
				<span style="color:#28a745; font-weight:700;"><?php echo htmlspecialchars($r['result'], ENT_QUOTES); ?></span>
			<?php } else { ?>
				<span style="color:#dc3545; font-weight:700;"><?php echo htmlspecialchars($r['result'], ENT_QUOTES); ?></span>
			<?php } ?>
		</td>
		<td><?php echo htmlspecialchars($r['pt_id_attempted'], ENT_QUOTES); ?></td>
		<td><?php echo $r['previous_pt_id'] !== '' ? htmlspecialchars($r['previous_pt_id'], ENT_QUOTES) : '-'; ?></td>
		<td><?php echo htmlspecialchars($r['vs_ip'], ENT_QUOTES) . ' / ' . htmlspecialchars($r['vs_hostname'], ENT_QUOTES); ?></td>
		<td><?php echo htmlspecialchars($r['request_ip'], ENT_QUOTES); ?></td>
		<td><?php echo htmlspecialchars($r['grade'], ENT_QUOTES) . ' / ' . (int)$r['grade_score'] . ' / ' . number_format((int)$r['recommended_vpn_id_count']); ?></td>
		<td><?php echo htmlspecialchars($r['message'], ENT_QUOTES); ?></td>
	</tr>
	<?php } ?>
</table>
</div>

<div class="text-center">
	<?php if ($page > 1) { ?><a href="?<?php echo $qs(array('page' => $page - 1)); ?>" class="btn btn-default btn-sm">이전</a><?php } ?>
	<span style="margin:0 10px;"><?php echo $page; ?> / <?php echo $total_page; ?></span>
	<?php if ($page < $total_page) { ?><a href="?<?php echo $qs(array('page' => $page + 1)); ?>" class="btn btn-default btn-sm">다음</a><?php } ?>
</div>

<?php
include_once($skin_path . '/_tail.php');
include_once(G5_PATH . '/tail.sub.php');
?>
