<?php if ( ! defined( 'ABSPATH' ) ) { exit; } ?>
<div class="wrap">
	<div class="zacls-action-bar">
		<div class="zacls-action-left">
			<h1 class="zacls-page-title"><?php esc_html_e( 'Session Management', 'za-creative-login-shield' ); ?></h1>
		</div>
	</div>
	<p class="zacls-page-description"><?php esc_html_e( 'View and manage active user sessions. You can terminate individual sessions or force logout all sessions for a user.', 'za-creative-login-shield' ); ?></p>

	<?php
	global $wpdb;
	$sessions = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
		"SELECT s.*, u.display_name, u.user_email
		FROM {$wpdb->prefix}zacls_sessions s
		LEFT JOIN {$wpdb->users} u ON s.user_id = u.ID
		WHERE s.is_active = 1
		ORDER BY s.last_activity DESC
		LIMIT 100",
		ARRAY_A
	); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
	?>

	<?php if ( empty( $sessions ) ) : ?>
		<div class="zacls-empty-state">
			<span class="dashicons dashicons-id"></span>
			<p><?php esc_html_e( 'No active sessions found.', 'za-creative-login-shield' ); ?></p>
		</div>
	<?php else : ?>
		<div class="zacls-table-wrap">
			<table class="wp-list-table widefat fixed striped">
				<thead>
					<tr>
						<th><?php esc_html_e( 'User', 'za-creative-login-shield' ); ?></th>
						<th><?php esc_html_e( 'IP', 'za-creative-login-shield' ); ?></th>
						<th><?php esc_html_e( 'Device', 'za-creative-login-shield' ); ?></th>
						<th><?php esc_html_e( 'Login Time', 'za-creative-login-shield' ); ?></th>
						<th><?php esc_html_e( 'Last Activity', 'za-creative-login-shield' ); ?></th>
						<th><?php esc_html_e( 'Expires', 'za-creative-login-shield' ); ?></th>
						<th><?php esc_html_e( 'Actions', 'za-creative-login-shield' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $sessions as $s ) : ?>
						<tr>
							<td>
								<strong><?php echo esc_html( $s['display_name'] ?: '#' . $s['user_id'] ); ?></strong>
								<?php if ( $s['user_email'] ) : ?><br><small><?php echo esc_html( $s['user_email'] ); ?></small><?php endif; ?>
							</td>
							<td><code><?php echo esc_html( $s['ip'] ); ?></code></td>
							<td><small><?php echo esc_html( substr( $s['device_id'] ?: '-', 0, 16 ) ); ?></small></td>
							<td><?php echo esc_html( $s['login_time'] ); ?></td>
							<td><?php echo esc_html( $s['last_activity'] ); ?></td>
							<td><?php echo esc_html( $s['expires_at'] ); ?></td>
							<td class="zacls-actions">
								<button type="button" class="zacls-btn button-small zacls-terminate-session" data-session="<?php echo esc_attr( $s['session_id'] ); ?>"><?php esc_html_e( 'Terminate', 'za-creative-login-shield' ); ?></button>
							</td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>
	<?php endif; ?>
</div>

<script>
jQuery(function($) {
	$('.zacls-terminate-session').on('click', function() {
		if (!confirm('<?php esc_html_e( 'Terminate this session? The user will be logged out.', 'za-creative-login-shield' ); ?>')) return;
		var btn = $(this).prop('disabled', true);
		$.post(zaclsData.ajaxUrl, { action: 'zacls_terminate_session', nonce: zaclsData.nonce, session_id: btn.data('session') }, function(response) {
			if (response.success) { btn.closest('tr').fadeOut(300, function() { $(this).remove(); }); }
			else { alert(response.data.message || zaclsData.i18n.error); btn.prop('disabled', false); }
		});
	});
});
</script>
