<?php
/**
 * Widgets settings tab.
 *
 * @package Rsmspark_Content_Widgets
 *
 * @var array<string, mixed> $settings Plugin settings.
 */

defined( 'ABSPATH' ) || exit;

$definitions   = Rsmspark_Content_Widgets_Registry::definitions();
$enabled_map   = is_array( $settings['widgets_enabled'] ?? null ) ? $settings['widgets_enabled'] : array();

?>
<form method="post" action="<?php echo esc_url( Rsmspark_Content_Widgets_Admin::settings_url( 'widgets' ) ); ?>">
	<?php wp_nonce_field( 'rsmspark_content_widgets_save_settings', 'rsmspark_content_widgets_settings_nonce' ); ?>
	<input type="hidden" name="rsm_settings_tab" value="widgets" />

	<p class="description"><?php esc_html_e( 'Enable or disable individual widgets for this site.', 'rsm-content-widgets' ); ?></p>

	<table class="widefat striped rsmspark-cw-widget-table" style="max-width: 900px; margin-top: 1em;">
		<thead>
			<tr>
				<th scope="col" style="width: 48px;"><?php esc_html_e( 'On', 'rsm-content-widgets' ); ?></th>
				<th scope="col"><?php esc_html_e( 'Widget', 'rsm-content-widgets' ); ?></th>
				<th scope="col"><?php esc_html_e( 'Description', 'rsm-content-widgets' ); ?></th>
			</tr>
		</thead>
		<tbody>
			<?php foreach ( $definitions as $widget_id => $widget ) : ?>
				<?php
				$is_on = ! empty( $enabled_map[ $widget_id ] );
				$field = 'rsmspark_content_widgets_widget_' . $widget_id;
				?>
				<tr>
					<td>
						<input type="checkbox" name="<?php echo esc_attr( $field ); ?>" value="1" <?php checked( $is_on ); ?> />
					</td>
					<td>
						<strong><?php echo esc_html( (string) ( $widget['label'] ?? $widget_id ) ); ?></strong>
					</td>
					<td><?php echo esc_html( (string) ( $widget['description'] ?? '' ) ); ?></td>
				</tr>
			<?php endforeach; ?>
		</tbody>
	</table>

	<?php submit_button(); ?>
</form>
