<?php if ( ! defined( 'ABSPATH' ) ) { exit; } ?>
<div class="wrap">
	<div class="zacls-action-bar">
		<div class="zacls-action-left">
			<h1 class="zacls-page-title"><?php esc_html_e( 'Vulnerability Scanner', 'za-creative-login-shield' ); ?></h1>
		</div>
		<div class="zacls-action-right">
			<button type="button" class="zacls-btn zacls-btn-primary" id="zacls-run-vuln-scan">
				<span class="dashicons dashicons-search"></span>
				<?php esc_html_e( 'Run Vulnerability Scan', 'za-creative-login-shield' ); ?>
			</button>
		</div>
	</div>
	<p class="zacls-page-description"><?php esc_html_e( 'Scan installed plugins and themes for known security vulnerabilities using public vulnerability feeds.', 'za-creative-login-shield' ); ?></p>

	<div id="zacls-vuln-scan-progress" style="display:none" class="zacls-card">
		<p><span class="spinner is-active" style="float:none"></span> <?php esc_html_e( 'Scanning for vulnerabilities...', 'za-creative-login-shield' ); ?></p>
	</div>

	<div id="zacls-vuln-scan-results">
		<?php
		$vuln_results = ZACLS_Vulnerability_Scanner::get_cached_results();
		$last_scan    = get_option( 'zacls_vuln_scan_last', 0 );
		$vuln_count   = 0;

		if ( ! empty( $vuln_results ) ) :
			foreach ( $vuln_results as $slug => $plugin ) {
				$vuln_count += $plugin['vuln_count'] ?? 0;
			}
			?>
			<div class="zacls-card">
				<h2 class="zacls-card-title">
					<?php esc_html_e( 'Scan Results', 'za-creative-login-shield' ); ?>
					<span class="zacls-status-badge <?php echo $vuln_count > 0 ? 'status-failed' : 'status-success'; ?>">
						<?php if ( $vuln_count > 0 ) : ?>
							<?php echo esc_html( sprintf( esc_html__( '%d Vulnerabilities', 'za-creative-login-shield' ), $vuln_count ) ); ?>
						<?php else : ?>
							<?php esc_html_e( 'No Vulnerabilities', 'za-creative-login-shield' ); ?>
						<?php endif; ?>
					</span>
				</h2>
				<?php if ( $last_scan ) : ?>
					<p class="description"><?php printf( esc_html__( 'Last scan: %s', 'za-creative-login-shield' ), esc_html( gmdate( 'Y-m-d H:i:s', $last_scan ) ) ); ?></p>
				<?php endif; ?>
			</div>

			<?php foreach ( $vuln_results as $slug => $plugin ) : ?>
				<?php if ( ! empty( $plugin['vulnerabilities'] ) ) : ?>
					<div class="zacls-section-card">
						<div class="zacls-section-card-header">
							<span class="dashicons dashicons-warning"></span>
							<h3>
								<?php echo esc_html( $plugin['name'] ); ?>
								<code><?php echo esc_html( $plugin['version'] ); ?></code>
								<span class="zacls-status-badge status-failed"><?php echo count( $plugin['vulnerabilities'] ); ?> <?php esc_html_e( 'vuln(s)', 'za-creative-login-shield' ); ?></span>
							</h3>
						</div>
						<div class="zacls-section-card-body">
							<table class="widefat striped">
								<thead><tr><th><?php esc_html_e( 'Title', 'za-creative-login-shield' ); ?></th><th><?php esc_html_e( 'Type', 'za-creative-login-shield' ); ?></th><th><?php esc_html_e( 'Fixed In', 'za-creative-login-shield' ); ?></th></tr></thead>
								<tbody>
								<?php foreach ( $plugin['vulnerabilities'] as $v ) : ?>
									<tr>
										<td><?php echo esc_html( $v['title'] ); ?></td>
										<td><span class="zacls-status-badge status-manual"><?php echo esc_html( $v['type'] ); ?></span></td>
										<td><code><?php echo esc_html( $v['fixed_in'] ?: 'N/A' ); ?></code></td>
									</tr>
								<?php endforeach; ?>
								</tbody>
							</table>
						</div>
					</div>
				<?php endif; ?>
			<?php endforeach; ?>
		<?php else : ?>
			<div class="zacls-empty-state">
				<span class="dashicons dashicons-shield"></span>
				<p><?php esc_html_e( 'No vulnerability data available. Run a scan to check your plugins and themes.', 'za-creative-login-shield' ); ?></p>
			</div>
		<?php endif; ?>
	</div>
</div>

<script>
jQuery(function($) {
	$('#zacls-run-vuln-scan').on('click', function() {
		var btn = $(this).prop('disabled', true);
		$('#zacls-vuln-scan-progress').show();
		$.post(zaclsData.ajaxUrl, { action: 'zacls_run_vuln_scan', nonce: zaclsData.nonce }, function(response) {
			$('#zacls-vuln-scan-progress').hide();
			btn.prop('disabled', false);
			if (response.success) { location.reload(); }
			else { alert(response.data.message || zaclsData.i18n.error); }
		});
	});
});
</script>
