<?php
/**
 * Modules Overview View.
 *
 * Displays a table with all registered modules, allowing enable/disable actions and settings access.
 *
 * @package Ilabs\Firevert\Core\Views\Layouts
 *
 * @var array $groups Registered settings groups.
 */

use Ilabs\Firevert\Core\Settings\Registrar\CoreSettingsRegistrar;
use Ilabs\Firevert\Core\Settings\UI\SettingsFieldRenderer;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['module'] ) ) {
	return;
}
?>

<form method="post" action="options.php" class="firevert-modules-overview-form">
	<?php settings_fields( 'firevert_settings' ); ?>
	<?php do_settings_sections( 'firevert_modules' ); ?>

	<table class="widefat striped">
		<thead>
		<tr>
			<th><?php esc_html_e( 'Module', 'firevert' ); ?></th>
			<th><?php esc_html_e( 'Enabled', 'firevert' ); ?></th>
			<th><?php esc_html_e( 'Actions', 'firevert' ); ?></th>
		</tr>
		</thead>
		<tbody>
		<?php foreach ( $groups as $slug => $group ) : ?>
			<?php
			$field = array(
				'id'    => 'enabled',
				'label' => $group['title'] ?? ucfirst( $slug ),
				'type'  => 'checkbox',
				'slug'  => $slug,
			);
			?>
			<tr>
				<td><?php echo esc_html( $field['label'] ); ?></td>
				<td><?php SettingsFieldRenderer::render( $field ); ?></td>
				<td>
					<?php if ( $group['is_enabled'] ) { ?>
						<a href="?page=firevert&tab=modules&module=<?php echo esc_attr( $slug ); ?>" class="button">
							<?php esc_html_e( 'Settings', 'firevert' ); ?>
						</a>
					<?php } ?>
				</td>
			</tr>
		<?php endforeach; ?>
		</tbody>
	</table>

	<?php submit_button( __( 'Save', 'firevert' ) ); ?>
</form>
