<?php
/**
 * Admin View: Logs
 * @package GuardianShield
 */
if ( ! defined( 'ABSPATH' ) ) exit;
?>
<div class="wrap gs-wrap">
	<div class="gs-header">
		<div class="gs-header__logo">
			<span class="dashicons dashicons-list-view"></span>
			<h1><?php esc_html_e( 'Guardian Shield — Logs', 'guardian-shield' ); ?></h1>
		</div>
		<div class="gs-header__actions">
			<button class="gs-btn gs-btn--danger gs-btn--sm" id="gs-clear-logs">
				🗑 <?php esc_html_e( 'Clear Logs', 'guardian-shield' ); ?>
			</button>
		</div>
	</div>

	<!-- Tabs -->
	<div class="gs-tabs">
		<a href="?page=guardian-shield-logs&tab=attacks" class="gs-tab <?php echo $tab === 'attacks' ? 'gs-tab--active' : ''; ?>">
			⚔️ <?php esc_html_e( 'Attack Logs', 'guardian-shield' ); ?> <span class="gs-badge"><?php echo count( $attacks ); ?></span>
		</a>
		<a href="?page=guardian-shield-logs&tab=login" class="gs-tab <?php echo $tab === 'login' ? 'gs-tab--active' : ''; ?>">
			🔐 <?php esc_html_e( 'Login Attempts', 'guardian-shield' ); ?> <span class="gs-badge"><?php echo count( $logins ); ?></span>
		</a>
	</div>

	<?php if ( $tab === 'attacks' ) : ?>
	<div class="gs-card">
		<div class="gs-card__body">
			<table class="gs-table gs-table--striped">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Time', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'IP', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Type', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Severity', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Score', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Description', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Blocked', 'guardian-shield' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php if ( empty( $attacks ) ) : ?>
						<tr><td colspan="7" class="gs-empty"><?php esc_html_e( 'No attack logs found.', 'guardian-shield' ); ?></td></tr>
					<?php else : foreach ( $attacks as $row ) : ?>
					<tr>
						<td class="gs-td--time"><?php echo esc_html( gs_human_time_diff( $row['created_at'] ) ); ?></td>
						<td><code><?php echo esc_html( $row['ip_address'] ); ?></code></td>
						<td><span class="gs-type-badge"><?php echo esc_html( $row['attack_type'] ); ?></span></td>
						<td><span class="gs-severity <?php echo esc_attr( gs_severity_class( (int) $row['severity'] ) ); ?>"><?php echo esc_html( gs_severity_label( (int) $row['severity'] ) ); ?></span></td>
						<td><?php echo esc_html( $row['threat_score'] ); ?>/100</td>
						<td class="gs-td--desc"><?php echo esc_html( wp_trim_words( $row['description'], 12 ) ); ?></td>
						<td><?php echo $row['blocked'] ? '<span class="gs-badge gs-badge--red">✗</span>' : '<span class="gs-badge gs-badge--green">✓</span>'; ?></td>
					</tr>
					<?php endforeach; endif; ?>
				</tbody>
			</table>
		</div>
	</div>

	<?php else : ?>

	<div class="gs-card">
		<div class="gs-card__body">
			<table class="gs-table gs-table--striped">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Time', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'IP', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Username', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Result', 'guardian-shield' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php if ( empty( $logins ) ) : ?>
						<tr><td colspan="4" class="gs-empty"><?php esc_html_e( 'No login attempts recorded.', 'guardian-shield' ); ?></td></tr>
					<?php else : foreach ( $logins as $row ) : ?>
					<tr>
						<td class="gs-td--time"><?php echo esc_html( gs_human_time_diff( $row['created_at'] ) ); ?></td>
						<td><code><?php echo esc_html( $row['ip_address'] ); ?></code></td>
						<td><?php echo esc_html( $row['username'] ); ?></td>
						<td>
							<?php if ( $row['result'] === 'success' ) : ?>
								<span class="gs-badge gs-badge--green">✓ <?php esc_html_e( 'Success', 'guardian-shield' ); ?></span>
							<?php elseif ( $row['result'] === 'blocked' ) : ?>
								<span class="gs-badge gs-badge--red">✗ <?php esc_html_e( 'Blocked', 'guardian-shield' ); ?></span>
							<?php else : ?>
								<span class="gs-badge gs-badge--yellow">✗ <?php esc_html_e( 'Failed', 'guardian-shield' ); ?></span>
							<?php endif; ?>
						</td>
					</tr>
					<?php endforeach; endif; ?>
				</tbody>
			</table>
		</div>
	</div>
	<?php endif; ?>
</div>

<script>
(function($){
	$('#gs-clear-logs').on('click', function(){
		if (!confirm(gs_admin.strings.confirm_clear)) return;
		const tab = '<?php echo esc_js( $tab === 'login' ? 'logins' : 'attacks' ); ?>';
		$.post(gs_admin.ajax_url, {
			action: 'gs_clear_logs',
			nonce: gs_admin.nonce,
			log_type: tab
		}, function(res){
			if (res.success) location.reload();
		});
	});
})(jQuery);
</script>
