<?php
defined( 'ABSPATH' ) || exit;
global $wpdb;

$lockdown_active = ZACLS_Emergency::is_lockdown_active();
$lockdown_data   = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}zacls_lockdown WHERE is_active = 1 LIMIT 1", ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery
?>
<div class="wrap">
	<div class="zacls-action-bar">
		<div class="zacls-action-left">
			<h1 class="zacls-page-title">
				<?php esc_html_e( 'Emergency Lockdown', 'za-creative-login-shield' ); ?>
				<?php if ( $lockdown_active ) : ?>
					<span class="zacls-status-badge status-failed"><?php esc_html_e( 'Active', 'za-creative-login-shield' ); ?></span>
				<?php endif; ?>
			</h1>
		</div>
	</div>

	<div class="zacls-card">
		<?php if ( $lockdown_active ) : ?>
			<div class="zacls-alert zacls-alert-error">
				<strong><?php esc_html_e( 'Lockdown is ACTIVE', 'za-creative-login-shield' ); ?></strong>
				<?php esc_html_e( 'All login attempts are blocked except whitelisted IPs.', 'za-creative-login-shield' ); ?>
			</div>

			<?php if ( $lockdown_data ) : ?>
				<table class="form-table">
					<tr><th><?php esc_html_e( 'Activated', 'za-creative-login-shield' ); ?></th><td><?php echo esc_html( $lockdown_data['activated_at'] ); ?></td></tr>
					<tr><th><?php esc_html_e( 'Reason', 'za-creative-login-shield' ); ?></th><td><?php echo esc_html( $lockdown_data['reason'] ); ?></td></tr>
					<tr><th><?php esc_html_e( 'Whitelisted IPs', 'za-creative-login-shield' ); ?></th><td><code><?php echo esc_html( ! empty( $lockdown_data['whitelist_ips'] ) ? $lockdown_data['whitelist_ips'] : __( 'None', 'za-creative-login-shield' ) ); ?></code></td></tr>
				</table>
			<?php endif; ?>

			<p><button type="button" id="zacls-deactivate-lockdown" class="zacls-btn zacls-btn-primary"><?php esc_html_e( 'Deactivate Lockdown', 'za-creative-login-shield' ); ?></button></p>

		<?php else : ?>

			<p><?php esc_html_e( 'Emergency lockdown is not active. Use this to block all login attempts except whitelisted IPs in case of an active security threat.', 'za-creative-login-shield' ); ?></p>

			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Reason', 'za-creative-login-shield' ); ?></th>
					<td><input type="text" id="zacls-lockdown-reason" class="regular-text" placeholder="<?php esc_attr_e( 'Reason for lockdown', 'za-creative-login-shield' ); ?>"></td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Whitelist IPs', 'za-creative-login-shield' ); ?></th>
					<td>
						<textarea id="zacls-lockdown-whitelist" rows="3" class="large-text code" placeholder="<?php esc_attr_e( 'One IP per line', 'za-creative-login-shield' ); ?>"></textarea>
						<p class="description"><?php esc_html_e( 'These IPs will still be able to log in during lockdown. Your current IP is automatically whitelisted.', 'za-creative-login-shield' ); ?></p>
					</td>
				</tr>
			</table>

			<p><button type="button" id="zacls-activate-lockdown" class="zacls-btn zacls-btn-primary"><?php esc_html_e( 'Activate Emergency Lockdown', 'za-creative-login-shield' ); ?></button></p>

		<?php endif; ?>
	</div>
</div>
