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

global $wpdb;

$current_page     = isset( $_GET['paged'] ) ? max( 1, absint( $_GET['paged'] ) ) : 1;
$records_per_page = 30;
$offset           = ( $current_page - 1 ) * $records_per_page;
$action_filter    = isset( $_GET['action_filter'] ) ? sanitize_key( $_GET['action_filter'] ) : '';

$logs    = ZACLS_Audit_Trail::get_logs( $records_per_page, $offset, $action_filter );
$total   = ZACLS_Audit_Trail::count_logs( $action_filter );
$actions = ZACLS_Audit_Trail::get_unique_actions();
$total_pages = ceil( $total / $records_per_page );
?>
<div class="wrap">
	<div class="zacls-action-bar">
		<div class="zacls-action-left">
			<h1 class="zacls-page-title"><?php esc_html_e( 'Audit Trail', 'za-creative-login-shield' ); ?></h1>
		</div>
		<div class="zacls-action-right">
			<button type="button" class="zacls-btn zacls-clear-audit-logs"><?php esc_html_e( 'Clear Audit Logs', 'za-creative-login-shield' ); ?></button>
			<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'zacls_export', 'audit', admin_url( 'admin.php?page=zacls&tab=audit-trail' ) ), '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>
		</div>
	</div>
	<p class="zacls-page-description"><?php esc_html_e( 'Track all security-related actions performed by administrators.', 'za-creative-login-shield' ); ?></p>

	<form method="get" class="zacls-inline-form">
		<input type="hidden" name="page" value="zacls">
		<input type="hidden" name="tab" value="audit-trail">
		<select name="action_filter">
			<option value=""><?php esc_html_e( 'All Actions', 'za-creative-login-shield' ); ?></option>
			<?php foreach ( $actions as $a ) : ?>
				<option value="<?php echo esc_attr( $a ); ?>" <?php selected( $action_filter, $a ); ?>><?php echo esc_html( $a ); ?></option>
			<?php endforeach; ?>
		</select>
		<button type="submit" class="zacls-btn"><?php esc_html_e( 'Filter', 'za-creative-login-shield' ); ?></button>
	</form>

	<?php if ( empty( $logs ) ) : ?>
		<div class="zacls-empty-state">
			<span class="dashicons dashicons-yes-alt"></span>
			<p><?php esc_html_e( 'No audit log entries 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 scope="col"><?php esc_html_e( 'Time', 'za-creative-login-shield' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Action', 'za-creative-login-shield' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Details', 'za-creative-login-shield' ); ?></th>
						<th scope="col"><?php esc_html_e( 'User', 'za-creative-login-shield' ); ?></th>
						<th scope="col"><?php esc_html_e( 'IP', 'za-creative-login-shield' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $logs as $entry ) : ?>
						<?php $user = $entry['user_id'] ? get_userdata( $entry['user_id'] ) : false; ?>
						<tr>
							<td><?php echo esc_html( $entry['created_at'] ); ?></td>
							<td><code><?php echo esc_html( $entry['action'] ); ?></code></td>
							<td><?php echo esc_html( $entry['details'] ? $entry['details'] : '-' ); ?></td>
							<td><?php echo $user ? esc_html( $user->display_name ) : esc_html__( 'System', 'za-creative-login-shield' ); ?></td>
							<td><code><?php echo esc_html( $entry['ip'] ? $entry['ip'] : '-' ); ?></code></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
			<?php if ( $total_pages > 1 ) : ?>
				<div class="tablenav bottom">
					<div class="tablenav-pages">
						<?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 ) ) ); ?>
					</div>
				</div>
			<?php endif; ?>
		</div>
	<?php endif; ?>
</div>
