<?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;
$filter           = isset( $_GET['type'] ) ? sanitize_text_field( wp_unslash( $_GET['type'] ) ) : '';
$blocked_ips      = ZACLS_IP_Blocker::get_blocked_ips( $filter, $records_per_page, $offset );
$total            = ZACLS_IP_Blocker::get_blocked_count( $filter );
$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( 'Blocked IPs', 'za-creative-login-shield' ); ?></h1>
		</div>
	</div>

	<form id="zacls-block-ip-form" class="zacls-inline-form" method="post">
		<input type="text" id="zacls-new-block-ip" placeholder="<?php esc_attr_e( 'Enter IP address', 'za-creative-login-shield' ); ?>" pattern="[0-9\.\:a-fA-F\/]+" required>
		<input type="text" id="zacls-block-reason" placeholder="<?php esc_attr_e( 'Reason (optional)', 'za-creative-login-shield' ); ?>">
		<button type="submit" class="zacls-btn zacls-btn-primary"><?php esc_html_e( 'Block IP', 'za-creative-login-shield' ); ?></button>
	</form>

	<ul class="subsubsub">
		<li><a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=blocked-ips' ) ); ?>" class="<?php echo empty( $filter ) ? 'current' : ''; ?>"><?php esc_html_e( 'All', 'za-creative-login-shield' ); ?> <span class="count">(<?php echo esc_html( ZACLS_IP_Blocker::get_blocked_count() ); ?>)</span></a> |</li>
		<li><a href="<?php echo esc_url( add_query_arg( 'type', 'auto' ) ); ?>" class="<?php echo 'auto' === $filter ? 'current' : ''; ?>"><?php esc_html_e( 'Auto-Blocked', 'za-creative-login-shield' ); ?> <span class="count">(<?php echo esc_html( ZACLS_IP_Blocker::get_blocked_count( 'auto' ) ); ?>)</span></a> |</li>
		<li><a href="<?php echo esc_url( add_query_arg( 'type', 'manual' ) ); ?>" class="<?php echo 'manual' === $filter ? 'current' : ''; ?>"><?php esc_html_e( 'Manually Blocked', 'za-creative-login-shield' ); ?> <span class="count">(<?php echo esc_html( ZACLS_IP_Blocker::get_blocked_count( 'manual' ) ); ?>)</span></a> |</li>
		<li><a href="<?php echo esc_url( add_query_arg( 'type', 'whitelist' ) ); ?>" class="<?php echo 'whitelist' === $filter ? 'current' : ''; ?>"><?php esc_html_e( 'Whitelisted', 'za-creative-login-shield' ); ?> <span class="count">(<?php echo esc_html( ZACLS_IP_Blocker::get_blocked_count( 'whitelist' ) ); ?>)</span></a></li>
	</ul>

	<?php if ( empty( $blocked_ips ) ) : ?>
		<div class="zacls-empty-state">
			<span class="dashicons dashicons-shield-alt"></span>
			<p><?php esc_html_e( 'No IPs 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( '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( 'Type', 'za-creative-login-shield' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Reason', 'za-creative-login-shield' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Blocked At', 'za-creative-login-shield' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Expires', 'za-creative-login-shield' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Actions', 'za-creative-login-shield' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $blocked_ips as $ip_data ) : ?>
						<tr>
							<td><code><?php echo esc_html( $ip_data['ip'] ); ?></code></td>
							<td><?php echo ! empty( $ip_data['country_code'] ) ? esc_html( $ip_data['country_code'] ) : '-'; ?></td>
							<td><span class="zacls-status-badge status-<?php echo esc_attr( $ip_data['type'] ); ?>"><?php echo esc_html( ucfirst( $ip_data['type'] ) ); ?></span></td>
							<td><?php echo esc_html( $ip_data['reason'] ? $ip_data['reason'] : '-' ); ?></td>
							<td><?php echo esc_html( $ip_data['blocked_at'] ); ?></td>
							<td><?php echo esc_html( $ip_data['expires_at'] ? $ip_data['expires_at'] : ( 'whitelist' === $ip_data['type'] ? __( 'Never', 'za-creative-login-shield' ) : __( 'Permanent', 'za-creative-login-shield' ) ) ); ?></td>
							<td class="zacls-actions">
								<?php if ( 'whitelist' !== $ip_data['type'] ) : ?>
									<button type="button" class="zacls-btn button-small zacls-whitelist-ip" data-ip="<?php echo esc_attr( $ip_data['ip'] ); ?>"><?php esc_html_e( 'Whitelist', 'za-creative-login-shield' ); ?></button>
								<?php endif; ?>
								<button type="button" class="zacls-btn button-small zacls-unblock-ip" data-ip="<?php echo esc_attr( $ip_data['ip'] ); ?>"><?php esc_html_e( 'Unblock', '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>
