<?php
defined( 'ABSPATH' ) || exit;

$current_page     = isset( $_GET['paged'] ) ? max( 1, intval( wp_unslash( $_GET['paged'] ) ) ) : 1;
$records_per_page = 30;
$offset           = ( $current_page - 1 ) * $records_per_page;
$total            = ZACLS_Login_Limiter::get_total_attempts( 'failed', 365 );
$total_pages      = ceil( $total / $records_per_page );
$logs             = ZACLS_Login_Limiter::get_recent_attempts( $records_per_page, $offset );
?>
<div class="wrap">
	<div class="zacls-action-bar">
		<div class="zacls-action-left">
			<h1 class="zacls-page-title"><?php esc_html_e( 'Login Attempts Log', 'za-creative-login-shield' ); ?></h1>
		</div>
		<div class="zacls-action-right">
			<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'zacls_export', 'login_attempts', admin_url( 'admin.php?page=zacls&tab=logs' ) ), 'zacls_export_nonce' ) ); ?>" class="zacls-btn zacls-btn-outline"><span class="dashicons dashicons-download"></span> <?php esc_html_e( 'Export CSV', 'za-creative-login-shield' ); ?></a>
			<button type="button" class="zacls-btn zacls-clear-logs"><?php esc_html_e( 'Clear Logs', 'za-creative-login-shield' ); ?></button>
		</div>
	</div>

	<?php if ( empty( $logs ) ) : ?>
		<div class="zacls-empty-state">
			<span class="dashicons dashicons-yes-alt"></span>
			<p><?php esc_html_e( 'No failed login attempts recorded. Your site is secure!', 'za-creative-login-shield' ); ?></p>
		</div>
	<?php else : ?>
		<div class="zacls-table-wrap">
			<table class="wp-list-table widefat fixed striped">
				<thead>
					<tr>
						<th scope="col"><?php esc_html_e( 'IP Address', 'za-creative-login-shield' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Country', 'za-creative-login-shield' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Username', 'za-creative-login-shield' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Status', 'za-creative-login-shield' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Date/Time', 'za-creative-login-shield' ); ?></th>
						<th scope="col"><?php esc_html_e( 'User Agent', 'za-creative-login-shield' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Actions', 'za-creative-login-shield' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $logs as $log ) : ?>
						<tr>
							<td><code><?php echo esc_html( $log['ip'] ); ?></code></td>
							<td><?php echo ! empty( $log['country_code'] ) ? esc_html( $log['country_code'] ) : '-'; ?></td>
							<td><?php echo esc_html( $log['username'] ? $log['username'] : '-' ); ?></td>
							<td><span class="zacls-status-badge status-<?php echo esc_attr( $log['status'] ); ?>"><?php echo esc_html( ucfirst( $log['status'] ) ); ?></span></td>
							<td><?php echo esc_html( $log['attempt_time'] ); ?></td>
							<td class="zacls-user-agent" title="<?php echo esc_attr( $log['user_agent'] ); ?>"><?php echo esc_html( wp_trim_words( $log['user_agent'], 10 ) ); ?></td>
							<td><button type="button" class="zacls-btn button-small zacls-block-ip-btn" data-ip="<?php echo esc_attr( $log['ip'] ); ?>"><?php esc_html_e( 'Block', 'za-creative-login-shield' ); ?></button></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
			<?php if ( $total_pages > 1 ) : ?>
				<div class="tablenav bottom">
					<div class="tablenav-pages">
						<span class="displaying-num"><?php printf( esc_html__( '%d items', 'za-creative-login-shield' ), esc_html( $total ) ); ?></span>
						<?php echo wp_kses_post( paginate_links( array( 'base' => add_query_arg( 'paged', '%#%' ), 'format' => '', 'prev_text' => '&laquo;', 'next_text' => '&raquo;', 'total' => $total_pages, 'current' => $current_page, 'type' => 'list' ) ) ); ?>
					</div>
				</div>
			<?php endif; ?>
		</div>
	<?php endif; ?>
</div>
