<?php
/**
 * @package BlaminhorEssentials
 * @var Blaminhor_Essentials_Module_Cookies $module
 * @var Blaminhor_Essentials_Cookies_Service_Templates $templates
 * @var array $settings
 */

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

$grouped = $templates->group_by_category();
$active  = $settings['active_services'];
?>
<p>
	<?php esc_html_e( 'Choose which third-party services to block until the visitor consents.', 'blaminhor-essentials' ); ?>
</p>

<div class="blaminhor-essentials-services-list">
		<?php foreach ( array( 'essential', 'preferences', 'statistics', 'marketing' ) as $cat ) : ?>
			<?php
			if ( empty( $grouped[ $cat ] ) ) {
				continue;
			}
			?>
			<h3><?php echo esc_html( ucfirst( $cat ) ); ?></h3>
			<table class="widefat striped blaminhor-essentials-services-table">
				<thead>
					<tr>
						<th class="check-column">
							<label class="screen-reader-text" for="bla-toggle-all-<?php echo esc_attr( $cat ); ?>">
								<?php esc_html_e( 'Toggle all services in this category', 'blaminhor-essentials' ); ?>
							</label>
							<input type="checkbox" id="bla-toggle-all-<?php echo esc_attr( $cat ); ?>"
								class="blaminhor-essentials-services-toggle-all"
								aria-label="<?php esc_attr_e( 'Toggle all services in this category', 'blaminhor-essentials' ); ?>">
						</th>
						<th><?php esc_html_e( 'Service', 'blaminhor-essentials' ); ?></th>
						<th><?php esc_html_e( 'Provider', 'blaminhor-essentials' ); ?></th>
						<th><?php esc_html_e( 'Privacy policy', 'blaminhor-essentials' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $grouped[ $cat ] as $handle => $svc ) : ?>
						<tr>
							<td class="check-column">
								<input type="checkbox" name="active_services[]" value="<?php echo esc_attr( $handle ); ?>"
									<?php checked( empty( $active ) || in_array( $handle, $active, true ) ); ?>>
							</td>
							<td><strong><?php echo esc_html( $svc['name'] ); ?></strong></td>
							<td><?php echo esc_html( $svc['provider'] ); ?></td>
							<td>
								<?php if ( ! empty( $svc['privacy_url'] ) ) : ?>
									<a href="<?php echo esc_url( $svc['privacy_url'] ); ?>" target="_blank" rel="noopener noreferrer">
										<?php esc_html_e( 'View', 'blaminhor-essentials' ); ?>
									</a>
								<?php endif; ?>
							</td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		<?php endforeach; ?>
</div>
