<?php
/**
 * Activity Log Admin Page View
 *
 * @package AdvancedUserActivityLog
 * @since 1.0.0
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use WP_PowerSuite\Admin\PageHero;

$wpps_al_stats      = isset( $stats ) && is_array( $stats ) ? $stats : [];
$wpps_al_export     = isset( $export_url ) ? $export_url : '';
$wpps_al_roles      = isset( $roles ) && is_array( $roles ) ? $roles : [];
$wpps_al_total_all  = isset( $total_all_logs ) ? (int) $total_all_logs : 0;
$wpps_al_has_filters = ! empty( $search )
	|| ! empty( $filter_action )
	|| ! empty( $filter_severity )
	|| ! empty( $filter_user )
	|| ! empty( $filter_role )
	|| ! empty( $filter_object_type )
	|| ! empty( $date_from )
	|| ! empty( $date_to )
	|| ! empty( $filter_ip );
$wpps_al_severities = [
	''         => __( 'All severities', 'wppowersuite-lite' ),
	'critical' => __( 'Critical', 'wppowersuite-lite' ),
	'warning'  => __( 'Warning', 'wppowersuite-lite' ),
	'success'  => __( 'Success', 'wppowersuite-lite' ),
	'info'     => __( 'Info', 'wppowersuite-lite' ),
];
$wpps_al_active_filters = [];

if ( ! empty( $search ) ) {
	$wpps_al_active_filters[] = [
		'label' => __( 'Search', 'wppowersuite-lite' ),
		'value' => $search,
	];
}
if ( ! empty( $filter_action ) ) {
	$wpps_al_active_filters[] = [
		'label' => __( 'Action', 'wppowersuite-lite' ),
		'value' => ucwords( str_replace( '_', ' ', $filter_action ) ),
	];
}
if ( ! empty( $filter_severity ) ) {
	$wpps_al_active_filters[] = [
		'label' => __( 'Severity', 'wppowersuite-lite' ),
		'value' => $wpps_al_severities[ $filter_severity ] ?? $filter_severity,
	];
}
if ( ! empty( $filter_user ) ) {
	$wpps_filter_user = get_userdata( (int) $filter_user );
	$wpps_al_active_filters[] = [
		'label' => __( 'User', 'wppowersuite-lite' ),
		'value' => $wpps_filter_user ? $wpps_filter_user->user_login : (string) $filter_user,
	];
}
if ( ! empty( $filter_role ) ) {
	$wpps_al_active_filters[] = [
		'label' => __( 'Role', 'wppowersuite-lite' ),
		'value' => $filter_role,
	];
}
if ( ! empty( $filter_object_type ) ) {
	$wpps_al_active_filters[] = [
		'label' => __( 'Object type', 'wppowersuite-lite' ),
		'value' => ucwords( str_replace( '_', ' ', $filter_object_type ) ),
	];
}
if ( ! empty( $date_from ) ) {
	$wpps_al_active_filters[] = [
		'label' => __( 'From', 'wppowersuite-lite' ),
		'value' => $date_from,
	];
}
if ( ! empty( $date_to ) ) {
	$wpps_al_active_filters[] = [
		'label' => __( 'To', 'wppowersuite-lite' ),
		'value' => $date_to,
	];
}
if ( ! empty( $filter_ip ) ) {
	$wpps_al_active_filters[] = [
		'label' => __( 'IP', 'wppowersuite-lite' ),
		'value' => $filter_ip,
	];
}

$wpps_al_meta_html = sprintf(
	'<a href="%1$s" class="button button-secondary"><span class="dashicons dashicons-admin-plugins" aria-hidden="true"></span> %2$s</a>',
	esc_url( admin_url( 'admin.php?page=powersuite' ) ),
	esc_html__( 'WP PowerSuite', 'wppowersuite-lite' )
);

$wpps_al_hero_stats = [];
if ( ! empty( $wpps_al_stats ) ) {
	$wpps_al_hero_stats = [
		[
			'value' => number_format_i18n( $wpps_al_stats['total'] ?? 0 ),
			'label' => __( 'Total entries', 'wppowersuite-lite' ),
		],
		[
			'value' => number_format_i18n( $wpps_al_stats['today'] ?? 0 ),
			'label' => __( 'Today', 'wppowersuite-lite' ),
		],
		[
			'value' => number_format_i18n( $wpps_al_stats['failed_30d'] ?? 0 ),
			'label' => __( 'Failed logins (30d)', 'wppowersuite-lite' ),
		],
		[
			'value' => number_format_i18n( $wpps_al_stats['unique_users'] ?? 0 ),
			'label' => __( 'Active users', 'wppowersuite-lite' ),
		],
	];
}
?>

<div class="wrap wpps-al-wrap">
	<hr class="wp-header-end" style="margin:0;padding:0;border:none;height:0;overflow:hidden;" />

	<?php if ( isset( $_GET['cleared'] ) && $_GET['cleared'] === '1' ) : ?>
		<div class="notice notice-success is-dismissible">
			<p><?php esc_html_e( 'Logs cleared successfully.', 'wppowersuite-lite' ); ?></p>
		</div>
	<?php endif; ?>

	<?php if ( isset( $_GET['deleted'] ) && $_GET['deleted'] > 0 ) : ?>
		<div class="notice notice-success is-dismissible">
			<p>
				<?php
				printf(
					/* translators: %d: number of deleted logs */
					esc_html( _n( '%d log entry deleted.', '%d log entries deleted.', absint( $_GET['deleted'] ), 'wppowersuite-lite' ) ),
					absint( $_GET['deleted'] )
				);
				?>
			</p>
		</div>
	<?php endif; ?>

	<?php
	PageHero::render_for_module(
		[
			'icon'      => 'dashicons-clipboard',
			'title'     => __( 'Activity Log', 'wppowersuite-lite' ),
			'tagline'   => __( 'Track logins, content changes, plugins, themes, and settings for security auditing and troubleshooting.', 'wppowersuite-lite' ),
			'stats'     => $wpps_al_hero_stats,
			'meta_html' => $wpps_al_meta_html,
		]
	);
	?>

	<div class="wpps-al-card">
		<h2 class="wpps-al-card-title"><?php esc_html_e( 'Filters', 'wppowersuite-lite' ); ?></h2>
		<div class="wpps-al-card-body">
			<?php if ( ! empty( $wpps_al_active_filters ) ) : ?>
				<div class="wpps-al-active-filters" aria-label="<?php esc_attr_e( 'Active filters', 'wppowersuite-lite' ); ?>">
					<span class="wpps-al-active-filters__label"><?php esc_html_e( 'Active', 'wppowersuite-lite' ); ?></span>
					<?php foreach ( $wpps_al_active_filters as $wpps_al_active_filter ) : ?>
						<span class="wpps-al-filter-chip">
							<span class="wpps-al-filter-chip__label"><?php echo esc_html( $wpps_al_active_filter['label'] ); ?></span>
							<span class="wpps-al-filter-chip__value"><?php echo esc_html( $wpps_al_active_filter['value'] ); ?></span>
						</span>
					<?php endforeach; ?>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=wpps-activity-log' ) ); ?>" class="wpps-al-filter-clear"><?php esc_html_e( 'Reset all', 'wppowersuite-lite' ); ?></a>
				</div>
			<?php endif; ?>
			<form method="get" action="" class="wpps-al-filters-form">
				<input type="hidden" name="page" value="wpps-activity-log" />

				<div class="wpps-al-filter-row wpps-al-filter-row-primary">
					<div class="wpps-al-filter-group">
						<label for="s"><?php esc_html_e( 'Search', 'wppowersuite-lite' ); ?></label>
						<input type="search" id="s" name="s" value="<?php echo esc_attr( $search ); ?>" placeholder="<?php esc_attr_e( 'Description, user, object, IP…', 'wppowersuite-lite' ); ?>" />
					</div>

					<div class="wpps-al-filter-group">
						<label for="filter_action"><?php esc_html_e( 'Action', 'wppowersuite-lite' ); ?></label>
						<select id="filter_action" name="filter_action">
							<option value=""><?php esc_html_e( 'All actions', 'wppowersuite-lite' ); ?></option>
							<?php foreach ( $actions as $action ) : ?>
								<option value="<?php echo esc_attr( $action ); ?>" <?php selected( $filter_action, $action ); ?>>
									<?php echo esc_html( ucwords( str_replace( '_', ' ', $action ) ) ); ?>
								</option>
							<?php endforeach; ?>
						</select>
					</div>

					<div class="wpps-al-filter-group">
						<label for="filter_severity"><?php esc_html_e( 'Severity', 'wppowersuite-lite' ); ?></label>
						<select id="filter_severity" name="filter_severity">
							<?php foreach ( $wpps_al_severities as $sev_key => $sev_label ) : ?>
								<option value="<?php echo esc_attr( $sev_key ); ?>" <?php selected( isset( $filter_severity ) ? $filter_severity : '', $sev_key ); ?>>
									<?php echo esc_html( $sev_label ); ?>
								</option>
							<?php endforeach; ?>
						</select>
						<p class="description wpps-al-filter-note"><?php esc_html_e( 'Severity is ignored when a specific action is selected.', 'wppowersuite-lite' ); ?></p>
					</div>

					<div class="wpps-al-filter-group">
						<label for="filter_user"><?php esc_html_e( 'User', 'wppowersuite-lite' ); ?></label>
						<select id="filter_user" name="filter_user">
							<option value="0"><?php esc_html_e( 'All users', 'wppowersuite-lite' ); ?></option>
							<?php foreach ( $users as $user ) : ?>
								<option value="<?php echo esc_attr( $user->ID ); ?>" <?php selected( $filter_user, $user->ID ); ?>>
									<?php echo esc_html( $user->user_login ); ?>
								</option>
							<?php endforeach; ?>
						</select>
					</div>

					<div class="wpps-al-filter-group">
						<label for="filter_role"><?php esc_html_e( 'Role', 'wppowersuite-lite' ); ?></label>
						<select id="filter_role" name="filter_role">
							<option value=""><?php esc_html_e( 'All roles', 'wppowersuite-lite' ); ?></option>
							<?php foreach ( $wpps_al_roles as $role_slug ) : ?>
								<option value="<?php echo esc_attr( $role_slug ); ?>" <?php selected( isset( $filter_role ) ? $filter_role : '', $role_slug ); ?>>
									<?php echo esc_html( $role_slug ); ?>
								</option>
							<?php endforeach; ?>
						</select>
					</div>
				</div>

				<div class="wpps-al-filter-row wpps-al-filter-row-secondary">
					<div class="wpps-al-filter-group">
						<label for="date_from"><?php esc_html_e( 'From date', 'wppowersuite-lite' ); ?></label>
						<input type="date" id="date_from" name="date_from" value="<?php echo esc_attr( isset( $date_from ) ? $date_from : '' ); ?>" />
					</div>
					<div class="wpps-al-filter-group">
						<label for="date_to"><?php esc_html_e( 'To date', 'wppowersuite-lite' ); ?></label>
						<input type="date" id="date_to" name="date_to" value="<?php echo esc_attr( isset( $date_to ) ? $date_to : '' ); ?>" />
					</div>
					<div class="wpps-al-filter-group">
						<label for="filter_ip"><?php esc_html_e( 'IP contains', 'wppowersuite-lite' ); ?></label>
						<input type="text" id="filter_ip" name="filter_ip" value="<?php echo esc_attr( isset( $filter_ip ) ? $filter_ip : '' ); ?>" placeholder="<?php esc_attr_e( 'e.g. 192.168', 'wppowersuite-lite' ); ?>" />
					</div>
					<div class="wpps-al-filter-group">
						<label for="filter_object_type"><?php esc_html_e( 'Object type', 'wppowersuite-lite' ); ?></label>
						<select id="filter_object_type" name="filter_object_type">
							<option value=""><?php esc_html_e( 'All types', 'wppowersuite-lite' ); ?></option>
							<?php foreach ( $object_types as $type ) : ?>
								<option value="<?php echo esc_attr( $type ); ?>" <?php selected( $filter_object_type, $type ); ?>>
									<?php echo esc_html( ucwords( str_replace( '_', ' ', $type ) ) ); ?>
								</option>
							<?php endforeach; ?>
						</select>
					</div>

					<div class="wpps-al-filter-group wpps-al-filter-group--actions" role="group" aria-labelledby="wpps-al-filter-actions-label">
						<span id="wpps-al-filter-actions-label" class="wpps-al-filter-group-label"><?php esc_html_e( 'Actions', 'wppowersuite-lite' ); ?></span>
						<div class="wpps-al-filter-actions">
							<button type="submit" class="wpps-al-btn wpps-al-btn-primary"><?php esc_html_e( 'Apply filters', 'wppowersuite-lite' ); ?></button>
							<a href="<?php echo esc_url( admin_url( 'admin.php?page=wpps-activity-log' ) ); ?>" class="wpps-al-btn wpps-al-btn-secondary"><?php esc_html_e( 'Reset', 'wppowersuite-lite' ); ?></a>
						</div>
					</div>
				</div>
			</form>
		</div>
	</div>

	<?php if ( ! empty( $logs ) ) : ?>
		<?php
		$pagination_args = [ 'page' => 'wpps-activity-log' ];
		if ( ! empty( $search ) ) {
			$pagination_args['s'] = $search;
		}
		if ( ! empty( $filter_action ) ) {
			$pagination_args['filter_action'] = $filter_action;
		}
		if ( ! empty( $filter_severity ) ) {
			$pagination_args['filter_severity'] = $filter_severity;
		}
		if ( ! empty( $filter_user ) ) {
			$pagination_args['filter_user'] = $filter_user;
		}
		if ( ! empty( $filter_role ) ) {
			$pagination_args['filter_role'] = $filter_role;
		}
		if ( ! empty( $filter_object_type ) ) {
			$pagination_args['filter_object_type'] = $filter_object_type;
		}
		if ( ! empty( $date_from ) ) {
			$pagination_args['date_from'] = $date_from;
		}
		if ( ! empty( $date_to ) ) {
			$pagination_args['date_to'] = $date_to;
		}
		if ( ! empty( $filter_ip ) ) {
			$pagination_args['filter_ip'] = $filter_ip;
		}
		
		$pagination_base = add_query_arg( $pagination_args, admin_url( 'admin.php' ) );
		$pagination_base = esc_url_raw( add_query_arg( 'paged', '%#%', $pagination_base ) );

		$per_page        = isset( $per_page ) ? (int) $per_page : 50;
		$pagination_from = $total_logs > 0 ? (int) ( ( $current_page - 1 ) * $per_page + 1 ) : 0;
		$pagination_to   = $total_logs > 0 ? (int) min( $current_page * $per_page, $total_logs ) : 0;
		?>

		<div class="wpps-al-list">
			<div class="wpps-al-toolbar">
				<div class="wpps-al-toolbar-main">
					<span class="wpps-al-toolbar-count">
						<?php
						printf(
							/* translators: %d: number of logs */
							esc_html( _n( '%d log entry', '%d log entries', $total_logs, 'wppowersuite-lite' ) ),
							esc_html( number_format_i18n( $total_logs ) )
						);
						?>
					</span>
				</div>
				<div class="wpps-al-toolbar-actions">
					<button type="button" class="wpps-al-btn wpps-al-btn-secondary wpps-clear-logs" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wpps_activity_log_clear' ) ); ?>">
						<?php esc_html_e( 'Clear all logs', 'wppowersuite-lite' ); ?>
					</button>
					<a href="<?php echo esc_url( $wpps_al_export ? $wpps_al_export : wp_nonce_url( admin_url( 'admin-ajax.php?action=wpps_activity_log_export' ), 'wpps_activity_log_export', 'nonce' ) ); ?>" class="wpps-al-btn wpps-al-btn-primary">
						<span class="dashicons dashicons-download" aria-hidden="true"></span>
						<?php esc_html_e( 'Export CSV', 'wppowersuite-lite' ); ?>
					</a>
				</div>
			</div>
		<form id="posts-filter" class="wpps-al-list-form" method="post" action="<?php echo esc_url( admin_url( 'admin.php?page=wpps-activity-log' ) ); ?>">
			<?php wp_nonce_field( 'bulk-logs' ); ?>
			<input type="hidden" name="page" value="wpps-activity-log" />
			<?php if ( ! empty( $search ) ) : ?>
				<input type="hidden" name="s" value="<?php echo esc_attr( $search ); ?>" />
			<?php endif; ?>
			<?php if ( ! empty( $filter_action ) ) : ?>
				<input type="hidden" name="filter_action" value="<?php echo esc_attr( $filter_action ); ?>" />
			<?php endif; ?>
			<?php if ( ! empty( $filter_user ) ) : ?>
				<input type="hidden" name="filter_user" value="<?php echo esc_attr( $filter_user ); ?>" />
			<?php endif; ?>
			<?php if ( ! empty( $filter_object_type ) ) : ?>
				<input type="hidden" name="filter_object_type" value="<?php echo esc_attr( $filter_object_type ); ?>" />
			<?php endif; ?>
			<?php if ( ! empty( $filter_severity ) ) : ?>
				<input type="hidden" name="filter_severity" value="<?php echo esc_attr( $filter_severity ); ?>" />
			<?php endif; ?>
			<?php if ( ! empty( $filter_role ) ) : ?>
				<input type="hidden" name="filter_role" value="<?php echo esc_attr( $filter_role ); ?>" />
			<?php endif; ?>
			<?php if ( ! empty( $date_from ) ) : ?>
				<input type="hidden" name="date_from" value="<?php echo esc_attr( $date_from ); ?>" />
			<?php endif; ?>
			<?php if ( ! empty( $date_to ) ) : ?>
				<input type="hidden" name="date_to" value="<?php echo esc_attr( $date_to ); ?>" />
			<?php endif; ?>
			<?php if ( ! empty( $filter_ip ) ) : ?>
				<input type="hidden" name="filter_ip" value="<?php echo esc_attr( $filter_ip ); ?>" />
			<?php endif; ?>

		<div class="tablenav top">
			<div class="alignleft actions bulkactions">
				<label for="bulk-action-selector-top" class="screen-reader-text"><?php esc_html_e( 'Select bulk action', 'wppowersuite-lite' ); ?></label>
				<select name="action" id="bulk-action-selector-top">
					<option value="-1"><?php esc_html_e( 'Bulk Actions', 'wppowersuite-lite' ); ?></option>
					<option value="delete"><?php esc_html_e( 'Delete', 'wppowersuite-lite' ); ?></option>
				</select>
				<button type="button" id="doaction" class="button action"><?php esc_html_e( 'Apply', 'wppowersuite-lite' ); ?></button>
			</div>
			<?php if ( $total_pages > 1 ) : ?>
				<div class="tablenav-pages">
					<span class="displaying-num">
						<?php
						printf(
							/* translators: 1: First visible row number, 2: last visible row number, 3: total rows (Posts/Pages style). */
							esc_html__( '%1$s–%2$s of %3$s items', 'wppowersuite-lite' ),
							esc_html( number_format_i18n( $pagination_from ) ),
							esc_html( number_format_i18n( $pagination_to ) ),
							esc_html( number_format_i18n( $total_logs ) )
						);
						?>
					</span>
					<span class="pagination-links">
						<?php
						echo paginate_links(
							[
								'base'      => $pagination_base,
								'format'    => '',
								'prev_text' => __( '&laquo;', 'default' ),
								'next_text' => __( '&raquo;', 'default' ),
								'total'     => $total_pages,
								'current'   => $current_page,
								'end_size'  => 1,
								'mid_size'  => 2,
								'type'      => 'plain',
							]
						);
						?>
					</span>
				</div>
			<?php else : ?>
				<div class="tablenav-pages one-page">
					<?php /* translators: %s: string value. */ ?>
					<span class="displaying-num"><?php printf( esc_html__( '%s items', 'wppowersuite-lite' ), number_format_i18n( $total_logs ) ); ?></span>
				</div>
			<?php endif; ?>
		</div>

			<table class="wp-list-table widefat striped table-view-list">
				<thead>
					<tr>
						<td id="cb" class="manage-column column-cb check-column">
							<label for="cb-select-all-1" class="screen-reader-text"><?php esc_html_e( 'Select all', 'wppowersuite-lite' ); ?></label>
							<input id="cb-select-all-1" type="checkbox">
						</td>
						<th scope="col" id="wpps-al-col-date" class="manage-column column-date"><?php esc_html_e( 'Date', 'wppowersuite-lite' ); ?></th>
						<th scope="col" class="manage-column column-author"><?php esc_html_e( 'User', 'wppowersuite-lite' ); ?></th>
						<th scope="col" class="manage-column column-role"><?php esc_html_e( 'Role', 'wppowersuite-lite' ); ?></th>
						<th scope="col" class="manage-column column-action"><?php esc_html_e( 'Action', 'wppowersuite-lite' ); ?></th>
						<th scope="col" class="manage-column column-primary column-description"><?php esc_html_e( 'Description', 'wppowersuite-lite' ); ?></th>
						<th scope="col" class="manage-column column-ip"><?php esc_html_e( 'IP / location', 'wppowersuite-lite' ); ?></th>
					</tr>
				</thead>
				<tbody id="the-list">
					<?php foreach ( $logs as $log ) : ?>
						<?php
						$wpps_sev = \AdvancedUserActivityLog\Module::getSeverityForAction( (string) $log->action );
						?>
						<tr>
							<th scope="row" class="check-column">
								<label class="screen-reader-text" for="cb-select-<?php echo esc_attr( $log->id ); ?>"><?php esc_html_e( 'Select log entry', 'wppowersuite-lite' ); ?></label>
								<input id="cb-select-<?php echo esc_attr( $log->id ); ?>" type="checkbox" name="log[]" value="<?php echo esc_attr( $log->id ); ?>">
							</th>
							<td class="column-date" data-label="<?php esc_attr_e( 'Date', 'wppowersuite-lite' ); ?>">
							<time class="wpps-al-time" datetime="<?php echo esc_attr( mysql2date( 'c', $log->created_at ) ); ?>">
								<?php echo esc_html( date_i18n( get_option( 'date_format' ), strtotime( $log->created_at ) ) ); ?>
								<span><?php echo esc_html( date_i18n( get_option( 'time_format' ), strtotime( $log->created_at ) ) ); ?></span>
							</time>
						</td>
						<td class="author column-author" data-label="<?php esc_attr_e( 'User', 'wppowersuite-lite' ); ?>">
							<?php if ( $log->user_id ) : ?>
								<?php
								$user = get_userdata( $log->user_id );
								if ( $user ) {
									?>
									<span class="wpps-al-user-name"><?php echo esc_html( $user->user_login ); ?></span>
									<span class="wpps-al-user-id"><?php echo esc_html( sprintf( /* translators: %d: User ID. */ __( 'ID %d', 'wppowersuite-lite' ), (int) $log->user_id ) ); ?></span>
									<?php
								} else {
									?>
									<span class="wpps-al-user-name"><?php echo esc_html( $log->user_login ?? __( 'Unknown', 'wppowersuite-lite' ) ); ?></span>
									<?php
								}
								?>
							<?php else : ?>
								<span class="wpps-system-user"><?php esc_html_e( 'System', 'wppowersuite-lite' ); ?></span>
							<?php endif; ?>
						</td>
						<td class="column-role" data-label="<?php esc_attr_e( 'Role', 'wppowersuite-lite' ); ?>">
							<code class="wpps-role-pill"><?php echo esc_html( isset( $log->user_role ) && $log->user_role !== '' ? $log->user_role : '—' ); ?></code>
						</td>
						<td class="column-action" data-label="<?php esc_attr_e( 'Action', 'wppowersuite-lite' ); ?>">
							<span class="wpps-action-badge wpps-severity-<?php echo esc_attr( $wpps_sev ); ?> wpps-action-<?php echo esc_attr( $log->action ); ?>">
								<?php echo esc_html( ucwords( str_replace( '_', ' ', $log->action ) ) ); ?>
							</span>
						</td>
						<td class="column-description" data-label="<?php esc_attr_e( 'Description', 'wppowersuite-lite' ); ?>">
							<span class="wpps-al-description-text"><?php echo esc_html( $log->description ); ?></span>
						</td>
						<td class="column-ip" data-label="<?php esc_attr_e( 'IP Details', 'wppowersuite-lite' ); ?>">
							<div class="wpps-ip-details">
								<div class="wpps-ip-address">
									<code><?php echo esc_html( $log->ip_address ?? 'N/A' ); ?></code>
								</div>
								<?php if ( ! empty( $log->country ) || ! empty( $log->country_code ) ) : ?>
									<div class="wpps-ip-country">
										<span class="dashicons dashicons-admin-site"></span>
										<span class="wpps-country-name"><?php echo esc_html( $log->country ?? $log->country_code ?? '' ); ?></span>
									</div>
								<?php endif; ?>
							</div>
						</td>
					</tr>
				<?php endforeach; ?>
				</tbody>
			</table>

			<!-- Pagination Bottom -->
			<div class="tablenav bottom">
				<div class="alignleft actions wpps-al-footer-select">
					<label for="cb-select-all-2" class="screen-reader-text"><?php esc_html_e( 'Select all', 'wppowersuite-lite' ); ?></label>
					<input id="cb-select-all-2" type="checkbox">
				</div>
				<div class="alignleft actions bulkactions">
					<label for="bulk-action-selector-bottom" class="screen-reader-text"><?php esc_html_e( 'Select bulk action', 'wppowersuite-lite' ); ?></label>
					<select id="bulk-action-selector-bottom" name="action2">
						<option value="-1"><?php esc_html_e( 'Bulk Actions', 'wppowersuite-lite' ); ?></option>
						<option value="delete"><?php esc_html_e( 'Delete', 'wppowersuite-lite' ); ?></option>
					</select>
					<input type="button" id="doaction2" class="button action" value="<?php esc_attr_e( 'Apply', 'wppowersuite-lite' ); ?>">
				</div>
				<?php if ( $total_pages > 1 ) : ?>
					<div class="tablenav-pages">
						<span class="displaying-num">
							<?php
							printf(
								/* translators: 1: First visible row number, 2: last visible row number, 3: total rows. */
								esc_html__( '%1$s–%2$s of %3$s items', 'wppowersuite-lite' ),
								esc_html( number_format_i18n( $pagination_from ) ),
								esc_html( number_format_i18n( $pagination_to ) ),
								esc_html( number_format_i18n( $total_logs ) )
							);
							?>
						</span>
						<span class="pagination-links">
							<?php
							echo paginate_links(
								[
									'base'      => $pagination_base,
									'format'    => '',
									'prev_text' => __( '&laquo;', 'default' ),
									'next_text' => __( '&raquo;', 'default' ),
									'total'     => $total_pages,
									'current'   => $current_page,
									'end_size'  => 1,
									'mid_size'  => 2,
									'type'      => 'plain',
								]
							);
							?>
						</span>
					</div>
				<?php else : ?>
					<div class="tablenav-pages one-page">
						<?php /* translators: %s: string value. */ ?>
						<span class="displaying-num"><?php printf( esc_html__( '%s items', 'wppowersuite-lite' ), number_format_i18n( $total_logs ) ); ?></span>
					</div>
				<?php endif; ?>
			</div>
		</form>
		</div>
	<?php else : ?>
		<div class="wpps-al-empty-state">
			<div class="wpps-al-empty-icon" aria-hidden="true">
				<span class="dashicons dashicons-clipboard"></span>
			</div>
			<?php if ( $wpps_al_total_all === 0 && ! $wpps_al_has_filters ) : ?>
				<h2 class="wpps-al-empty-title"><?php esc_html_e( 'No activity has been recorded yet', 'wppowersuite-lite' ); ?></h2>
				<p class="wpps-al-empty-desc"><?php esc_html_e( 'Logins, edits, and setting changes will appear here after they happen. Export and clear actions appear once logs exist.', 'wppowersuite-lite' ); ?></p>
				<a href="<?php echo esc_url( admin_url( 'admin.php?page=powersuite' ) ); ?>" class="wpps-al-btn wpps-al-btn-primary"><?php esc_html_e( 'Open module settings', 'wppowersuite-lite' ); ?></a>
			<?php else : ?>
				<h2 class="wpps-al-empty-title"><?php esc_html_e( 'No activity logs match your filters', 'wppowersuite-lite' ); ?></h2>
				<p class="wpps-al-empty-desc"><?php esc_html_e( 'Reset filters or use the site to generate events—logins, edits, and setting changes will appear here.', 'wppowersuite-lite' ); ?></p>
				<a href="<?php echo esc_url( admin_url( 'admin.php?page=wpps-activity-log' ) ); ?>" class="wpps-al-btn wpps-al-btn-primary"><?php esc_html_e( 'View all logs', 'wppowersuite-lite' ); ?></a>
			<?php endif; ?>
		</div>
	<?php endif; ?>
</div>

