<?php
defined( 'ABSPATH' ) || exit;
/**
 * @var array  $cfg
 * @var array  $counts
 * @var array  $available
 * @var array  $failures
 * @var bool   $htaccess
 * @var ?string $active
 */
?>
<div class="wrap bw-webp-wrap">
	<h1><?php esc_html_e( 'BW WebP', 'bw-webp' ); ?></h1>

	<?php settings_errors( 'bw_webp' ); ?>

	<div class="bw-webp-status card">
		<h2><?php esc_html_e( 'Status', 'bw-webp' ); ?></h2>
		<table class="form-table" role="presentation">
			<tr>
				<th scope="row"><?php esc_html_e( 'Active converter', 'bw-webp' ); ?></th>
				<td>
					<?php if ( $active ) : ?>
						<code><?php echo esc_html( $active ); ?></code>
					<?php else : ?>
						<span style="color:#b32d2e;"><?php esc_html_e( 'No converter available — install cwebp, Imagick, or GD with WebP support.', 'bw-webp' ); ?></span>
					<?php endif; ?>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php esc_html_e( 'Available converters', 'bw-webp' ); ?></th>
				<td>
					<?php foreach ( $available as $name => $ok ) : ?>
						<span><code><?php echo esc_html( $name ); ?></code>: <?php echo $ok ? '✅' : '❌'; ?></span>&nbsp;
					<?php endforeach; ?>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php esc_html_e( '.htaccess', 'bw-webp' ); ?></th>
				<td>
					<?php if ( $htaccess ) : ?>
						<?php esc_html_e( 'Writable — rewrite rules installed automatically.', 'bw-webp' ); ?>
					<?php else : ?>
						<?php esc_html_e( 'Not writable. On Nginx, paste this into your server block:', 'bw-webp' ); ?>
						<pre><?php echo esc_html( ( new BW_WebP_Rewrite() )->nginx_snippet() ); ?></pre>
					<?php endif; ?>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php esc_html_e( 'Counts', 'bw-webp' ); ?></th>
				<td id="bw-webp-counts">
					<span><?php esc_html_e( 'Total:', 'bw-webp' ); ?> <b data-c="total"><?php echo (int) $counts['total']; ?></b></span> ·
					<span><?php esc_html_e( 'Done:', 'bw-webp' ); ?> <b data-c="done"><?php echo (int) $counts['done']; ?></b></span> ·
					<span><?php esc_html_e( 'Pending:', 'bw-webp' ); ?> <b data-c="pending"><?php echo (int) $counts['pending']; ?></b></span> ·
					<span><?php esc_html_e( 'Converting:', 'bw-webp' ); ?> <b data-c="converting"><?php echo (int) $counts['converting']; ?></b></span> ·
					<span><?php esc_html_e( 'Failed:', 'bw-webp' ); ?> <b data-c="failed"><?php echo (int) $counts['failed']; ?></b></span>
				</td>
			</tr>
		</table>

		<p>
			<button type="button" class="button" id="bw-webp-scan"><?php esc_html_e( 'Scan now', 'bw-webp' ); ?></button>
			<button type="button" class="button button-primary" id="bw-webp-convert"><?php esc_html_e( 'Convert all', 'bw-webp' ); ?></button>
			<button type="button" class="button" id="bw-webp-clear"><?php esc_html_e( 'Clear conversions', 'bw-webp' ); ?></button>
			<span id="bw-webp-msg" style="margin-left:8px;"></span>
		</p>

		<div id="bw-webp-progress" style="display:none;">
			<progress id="bw-webp-bar" max="100" value="0" style="width:100%;"></progress>
		</div>
	</div>

	<form method="post" action="" class="card">
		<h2><?php esc_html_e( 'Settings', 'bw-webp' ); ?></h2>
		<?php wp_nonce_field( 'bw_webp_save_settings' ); ?>

		<table class="form-table" role="presentation">
			<tr>
				<th scope="row"><label for="bw_webp_quality"><?php esc_html_e( 'Quality', 'bw-webp' ); ?></label></th>
				<td>
					<input type="number" min="1" max="100" id="bw_webp_quality" name="bw_webp[quality]" value="<?php echo (int) $cfg['quality']; ?>">
					<p class="description"><?php esc_html_e( '1–100. Default 82.', 'bw-webp' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="bw_webp_workers"><?php esc_html_e( 'Workers', 'bw-webp' ); ?></label></th>
				<td>
					<input type="number" min="1" max="8" id="bw_webp_workers" name="bw_webp[workers]" value="<?php echo (int) $cfg['workers']; ?>">
					<p class="description"><?php esc_html_e( 'Parallel conversion workers (1–8).', 'bw-webp' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="bw_webp_converter"><?php esc_html_e( 'Converter', 'bw-webp' ); ?></label></th>
				<td>
					<select id="bw_webp_converter" name="bw_webp[converter]">
						<?php foreach ( array( 'auto', 'cwebp', 'imagick', 'gd' ) as $opt ) : ?>
							<option value="<?php echo esc_attr( $opt ); ?>" <?php selected( $cfg['converter'], $opt ); ?>><?php echo esc_html( $opt ); ?></option>
						<?php endforeach; ?>
					</select>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php esc_html_e( 'Options', 'bw-webp' ); ?></th>
				<td>
					<label>
						<input type="checkbox" name="bw_webp[skip_thumbnails]" value="1" <?php checked( ! empty( $cfg['skip_thumbnails'] ) ); ?>>
						<?php esc_html_e( 'Skip thumbnail sizes (originals only)', 'bw-webp' ); ?>
					</label><br>
					<label>
						<input type="checkbox" name="bw_webp[enable_rewrite]" value="1" <?php checked( ! empty( $cfg['enable_rewrite'] ) ); ?>>
						<?php esc_html_e( 'Install .htaccess rewrite rules (Apache/LiteSpeed)', 'bw-webp' ); ?>
					</label><br>
					<label>
						<input type="checkbox" name="bw_webp[delete_on_uninstall]" value="1" <?php checked( ! empty( $cfg['delete_on_uninstall'] ) ); ?>>
						<?php esc_html_e( 'Delete generated .webp files on uninstall', 'bw-webp' ); ?>
					</label>
				</td>
			</tr>
		</table>

		<p>
			<button type="submit" name="bw_webp_save" class="button button-primary"><?php esc_html_e( 'Save settings', 'bw-webp' ); ?></button>
		</p>
	</form>

	<?php if ( ! empty( $failures ) ) : ?>
		<div class="card">
			<h2><?php esc_html_e( 'Recent failures', 'bw-webp' ); ?></h2>
			<table class="widefat striped">
				<thead><tr><th><?php esc_html_e( 'Source', 'bw-webp' ); ?></th><th><?php esc_html_e( 'Error', 'bw-webp' ); ?></th></tr></thead>
				<tbody>
					<?php foreach ( $failures as $f ) : ?>
						<tr>
							<td><code><?php echo esc_html( (string) $f['src_path'] ); ?></code></td>
							<td><?php echo esc_html( (string) $f['error'] ); ?></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>
	<?php endif; ?>
</div>
