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

$whitelisted = ZACLS_IP_Blocker::get_blocked_ips( 'whitelist', 100, 0 );
$current_ip  = ZACLS_Main::get_ip();
?>
<div class="wrap">
	<div class="zacls-action-bar">
		<div class="zacls-action-left">
			<h1 class="zacls-page-title"><?php esc_html_e( 'IP Whitelist', 'za-creative-login-shield' ); ?></h1>
		</div>
	</div>
	<p class="zacls-page-description"><?php esc_html_e( 'Whitelisted IP addresses bypass all security checks including rate limiting and IP blocking.', 'za-creative-login-shield' ); ?></p>

	<div class="zacls-section-card">
		<div class="zacls-section-card-header">
			<span class="dashicons dashicons-plus-alt"></span>
			<h3><?php esc_html_e( 'Add IP to Whitelist', 'za-creative-login-shield' ); ?></h3>
		</div>
		<div class="zacls-section-card-body">
			<form id="zacls-whitelist-form" class="zacls-inline-form">
				<input type="text" id="zacls-whitelist-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-whitelist-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( 'Add to Whitelist', 'za-creative-login-shield' ); ?></button>
				<button type="button" id="zacls-whitelist-current" class="zacls-btn" data-current-ip="<?php echo esc_attr( $current_ip ); ?>"><?php esc_html_e( 'Whitelist My IP', 'za-creative-login-shield' ); ?></button>
			</form>
		</div>
	</div>

	<?php if ( empty( $whitelisted ) ) : ?>
		<div class="zacls-empty-state">
			<span class="dashicons dashicons-yes-alt"></span>
			<p><?php esc_html_e( 'No whitelisted IPs. Add trusted IP addresses above.', '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( 'Reason', 'za-creative-login-shield' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Whitelisted At', 'za-creative-login-shield' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Actions', 'za-creative-login-shield' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $whitelisted as $ip ) : ?>
						<tr>
							<td><code><?php echo esc_html( $ip['ip'] ); ?></code></td>
							<td><?php echo esc_html( $ip['reason'] ? $ip['reason'] : '-' ); ?></td>
							<td><?php echo esc_html( $ip['blocked_at'] ); ?></td>
							<td><button type="button" class="zacls-btn button-small zacls-remove-whitelist" data-ip="<?php echo esc_attr( $ip['ip'] ); ?>"><?php esc_html_e( 'Remove', 'za-creative-login-shield' ); ?></button></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>
	<?php endif; ?>
</div>
