<?php
/**
 * Admin View: Malware Scanner
 * @package GuardianShield
 */
if ( ! defined( 'ABSPATH' ) ) exit;
?>
<div class="wrap gs-wrap">
	<div class="gs-header">
		<div class="gs-header__logo">
			<span class="dashicons dashicons-search"></span>
			<h1><?php esc_html_e( 'Malware Scanner', 'guardian-shield' ); ?></h1>
		</div>
		<div class="gs-header__actions">
			<button class="gs-btn gs-btn--primary" id="gs-run-scan">
				🔍 <?php esc_html_e( 'Run Scan Now', 'guardian-shield' ); ?>
			</button>
		</div>
	</div>

	<div id="gs-scan-progress" class="gs-progress-bar" style="display:none">
		<div class="gs-progress-bar__fill" id="gs-progress-fill"></div>
		<span id="gs-progress-text"><?php esc_html_e( 'Scanning…', 'guardian-shield' ); ?></span>
	</div>

	<div class="gs-card">
		<div class="gs-card__header">
			<h2><?php esc_html_e( 'Scan Results', 'guardian-shield' ); ?></h2>
			<?php if ( ! empty( $scan_results ) ) : ?>
				<span class="gs-badge gs-badge--<?php echo count( array_filter( $scan_results, fn($r) => $r['status'] === 'detected' ) ) > 0 ? 'red' : 'green'; ?>">
					<?php echo count( $scan_results ); ?> <?php esc_html_e( 'entries', 'guardian-shield' ); ?>
				</span>
			<?php endif; ?>
		</div>
		<div class="gs-card__body">
			<table class="gs-table gs-table--striped">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Scanned', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'File', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Threat', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Severity', 'guardian-shield' ); ?></th>
						<th><?php esc_html_e( 'Status', 'guardian-shield' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php if ( empty( $scan_results ) ) : ?>
						<tr><td colspan="5" class="gs-empty">
							<?php esc_html_e( 'No scan results. Run a scan to get started.', 'guardian-shield' ); ?>
						</td></tr>
					<?php else : foreach ( $scan_results as $r ) : ?>
					<tr class="<?php echo $r['status'] === 'detected' ? 'gs-row--danger' : ''; ?>">
						<td class="gs-td--time"><?php echo esc_html( gs_human_time_diff( $r['scan_at'] ) ); ?></td>
						<td class="gs-td--file"><code><?php echo esc_html( basename( $r['file_path'] ) ); ?></code>
							<small class="gs-path"><?php echo esc_html( dirname( $r['file_path'] ) ); ?></small>
						</td>
						<td><?php echo esc_html( $r['threat_type'] ); ?></td>
						<td><span class="gs-severity <?php echo esc_attr( gs_severity_class( (int) $r['severity'] ) ); ?>"><?php echo esc_html( gs_severity_label( (int) $r['severity'] ) ); ?></span></td>
						<td>
							<?php if ( $r['status'] === 'quarantined' ) : ?>
								<span class="gs-badge gs-badge--green">🔒 <?php esc_html_e( 'Quarantined', 'guardian-shield' ); ?></span>
							<?php elseif ( $r['status'] === 'detected' ) : ?>
								<span class="gs-badge gs-badge--red">⚠ <?php esc_html_e( 'Detected', 'guardian-shield' ); ?></span>
							<?php else : ?>
								<span class="gs-badge"><?php echo esc_html( ucfirst( $r['status'] ) ); ?></span>
							<?php endif; ?>
						</td>
					</tr>
					<?php endforeach; endif; ?>
				</tbody>
			</table>
		</div>
	</div>
</div>

<script>
(function($){
	$('#gs-run-scan').on('click', function(){
		if (!confirm(gs_admin.strings.confirm_scan)) return;
		const $btn = $(this).prop('disabled', true).text('⏳ ' + gs_admin.strings.scanning);
		$('#gs-scan-progress').show();

		// Animate progress bar (indeterminate).
		let w = 0;
		const interval = setInterval(() => {
			w = Math.min(90, w + Math.random() * 5);
			$('#gs-progress-fill').css('width', w + '%');
		}, 300);

		$.post(gs_admin.ajax_url, {
			action: 'gs_run_scan',
			nonce: gs_admin.nonce
		}, function(res){
			clearInterval(interval);
			$('#gs-progress-fill').css('width', '100%');
			setTimeout(() => {
				$('#gs-scan-progress').hide();
				$btn.prop('disabled', false).text('🔍 <?php echo esc_js( __( 'Run Scan Now', 'guardian-shield' ) ); ?>');
				if (res.success) {
					alert('✅ ' + res.data.count + ' <?php echo esc_js( __( 'threat(s) found.', 'guardian-shield' ) ); ?>');
					location.reload();
				}
			}, 500);
		});
	});
})(jQuery);
</script>
