<?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( 'File Integrity Monitor', 'za-creative-login-shield' ); ?></h1>
		</div>
		<div class="zacls-action-right">
			<button type="button" class="zacls-btn zacls-btn-primary" id="zacls-run-file-scan">
				<span class="dashicons dashicons-update"></span>
				<?php esc_html_e( 'Run Full File Scan', 'za-creative-login-shield' ); ?>
			</button>
		</div>
	</div>
	<p class="zacls-page-description"><?php esc_html_e( 'Monitor WordPress core, theme, plugin files, wp-config.php, and .htaccess for unauthorized changes.', 'za-creative-login-shield' ); ?></p>

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

	<div id="zacls-file-scan-results">
		<?php
		$scan_results = get_option( 'zacls_file_scan_results', array() );
		$scan_time    = get_option( 'zacls_file_scan_time', '' );

		if ( ! empty( $scan_results ) ) :
			$core = $scan_results['core'] ?? array();
			?>
			<div class="zacls-section-card">
				<div class="zacls-section-card-header">
					<span class="dashicons dashicons-media-default"></span>
					<h3>
						<?php esc_html_e( 'Core Files', 'za-creative-login-shield' ); ?>
						<span class="zacls-status-badge <?php echo ( $core['modified'] ?? 0 ) > 0 ? 'status-failed' : 'status-success'; ?>">
							<?php echo ( $core['modified'] ?? 0 ) > 0 ? esc_html__( 'Issues Found', 'za-creative-login-shield' ) : esc_html__( 'Clean', 'za-creative-login-shield' ); ?>
						</span>
					</h3>
				</div>
				<div class="zacls-section-card-body">
					<p><?php printf( esc_html__( 'Checked: %d | Modified: %d | Missing: %d', 'za-creative-login-shield' ), (int) ( $core['total_checked'] ?? 0 ), (int) ( $core['modified'] ?? 0 ), (int) ( $core['missing'] ?? 0 ) ); ?></p>
					<?php if ( ! empty( $core['results'] ) ) : ?>
						<div class="zacls-table-wrap">
							<table class="widefat striped">
								<thead><tr><th><?php esc_html_e( 'File', 'za-creative-login-shield' ); ?></th><th><?php esc_html_e( 'Status', 'za-creative-login-shield' ); ?></th></tr></thead>
								<tbody>
								<?php foreach ( array_slice( $core['results'], 0, 50 ) as $r ) : ?>
									<tr>
										<td><code><?php echo esc_html( $r['file'] ); ?></code></td>
										<td><span class="zacls-status-badge <?php echo 'modified' === $r['status'] ? 'status-failed' : 'status-inactive'; ?>"><?php echo esc_html( $r['status'] ); ?></span></td>
									</tr>
								<?php endforeach; ?>
								</tbody>
							</table>
						</div>
					<?php endif; ?>
				</div>
			</div>

			<div class="zacls-section-card">
				<div class="zacls-section-card-header">
					<span class="dashicons dashicons-admin-generic"></span>
					<h3><?php esc_html_e( 'wp-config.php', 'za-creative-login-shield' ); ?></h3>
				</div>
				<div class="zacls-section-card-body">
					<?php foreach ( $scan_results['config'] ?? array() as $c ) : ?>
						<p><span class="zacls-status-badge <?php echo 'exists' === $c['status'] ? 'status-success' : 'status-failed'; ?>"><?php echo esc_html( $c['status'] ); ?></span> <code><?php echo esc_html( $c['file'] ); ?></code></p>
					<?php endforeach; ?>
				</div>
			</div>

			<div class="zacls-section-card">
				<div class="zacls-section-card-header">
					<span class="dashicons dashicons-hidden"></span>
					<h3><?php esc_html_e( '.htaccess', 'za-creative-login-shield' ); ?></h3>
				</div>
				<div class="zacls-section-card-body">
					<?php foreach ( $scan_results['htaccess'] ?? array() as $h ) : ?>
						<p><span class="zacls-status-badge <?php echo 'exists' === $h['status'] ? 'status-success' : 'status-manual'; ?>"><?php echo esc_html( $h['status'] ); ?></span> <code><?php echo esc_html( $h['file'] ); ?></code> (<?php echo esc_html( size_format( $h['size'] ?? 0 ) ); ?>)</p>
					<?php endforeach; ?>
				</div>
			</div>

			<?php if ( $scan_time ) : ?>
				<p class="description"><?php printf( esc_html__( 'Last scan: %s', 'za-creative-login-shield' ), esc_html( $scan_time ) ); ?></p>
			<?php endif; ?>
		<?php else : ?>
			<div class="zacls-empty-state">
				<span class="dashicons dashicons-media-document"></span>
				<p><?php esc_html_e( 'No scan results yet. Run a file scan to check file integrity.', 'za-creative-login-shield' ); ?></p>
			</div>
		<?php endif; ?>
	</div>
</div>

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