<?php
/**
 * @package BlaminhorEssentials
 * @var Blaminhor_Essentials_Module_Cookies                $module
 * @var Blaminhor_Essentials_Cookies_Regional_Presets       $presets
 * @var Blaminhor_Essentials_Cookies_Consent_Log            $consent_log
 * @var array                                               $settings
 */

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

$preset_labels = $presets->get_preset_labels();
?>
<h3><?php esc_html_e( 'Main jurisdiction', 'blaminhor-essentials' ); ?></h3>
<table class="form-table" role="presentation">
	<tr>
		<th><label for="force_preset"><?php esc_html_e( 'Rules to apply to all visitors', 'blaminhor-essentials' ); ?></label></th>
		<td>
			<select id="force_preset" name="force_preset">
				<option value=""><?php esc_html_e( 'Strictest rules (safe default — recommended)', 'blaminhor-essentials' ); ?></option>
				<?php foreach ( $preset_labels as $key => $label ) : ?>
					<?php if ( 'default' === $key ) { continue; } ?>
					<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $settings['force_preset'], $key ); ?>>
						<?php echo esc_html( $label ); ?>
					</option>
				<?php endforeach; ?>
			</select>
			<p class="description">
				<?php esc_html_e( 'Pick the jurisdiction your site mainly targets. Every visitor sees the same banner. To tailor the banner per visitor country, enable visitor geolocation in the Advanced tab.', 'blaminhor-essentials' ); ?>
			</p>
		</td>
	</tr>
</table>

<hr style="margin: 30px 0;">

<h3><?php esc_html_e( 'Layout & visibility', 'blaminhor-essentials' ); ?></h3>
<table class="form-table" role="presentation">
	<tr>
		<th><label for="layout"><?php esc_html_e( 'Banner layout', 'blaminhor-essentials' ); ?></label></th>
		<td>
			<select id="layout" name="layout">
				<option value="banner-bottom" <?php selected( $settings['layout'], 'banner-bottom' ); ?>><?php esc_html_e( 'Bottom banner (full width)', 'blaminhor-essentials' ); ?></option>
				<option value="banner-top" <?php selected( $settings['layout'], 'banner-top' ); ?>><?php esc_html_e( 'Top banner (full width)', 'blaminhor-essentials' ); ?></option>
				<option value="popup-center" <?php selected( $settings['layout'], 'popup-center' ); ?>><?php esc_html_e( 'Centered popup', 'blaminhor-essentials' ); ?></option>
				<option value="box-bottom-right" <?php selected( $settings['layout'], 'box-bottom-right' ); ?>><?php esc_html_e( 'Compact box (bottom right)', 'blaminhor-essentials' ); ?></option>
			</select>
		</td>
	</tr>
	<tr>
		<th><label for="dark_mode"><?php esc_html_e( 'Dark mode', 'blaminhor-essentials' ); ?></label></th>
		<td>
			<select id="dark_mode" name="dark_mode">
				<option value="auto" <?php selected( $settings['dark_mode'], 'auto' ); ?>><?php esc_html_e( 'Match the visitor\'s system theme', 'blaminhor-essentials' ); ?></option>
				<option value="light" <?php selected( $settings['dark_mode'], 'light' ); ?>><?php esc_html_e( 'Always light', 'blaminhor-essentials' ); ?></option>
				<option value="dark" <?php selected( $settings['dark_mode'], 'dark' ); ?>><?php esc_html_e( 'Always dark', 'blaminhor-essentials' ); ?></option>
			</select>
		</td>
	</tr>
	<tr>
		<th><?php esc_html_e( 'Visibility', 'blaminhor-essentials' ); ?></th>
		<td>
			<label><input type="checkbox" name="hide_for_admins" <?php checked( ! empty( $settings['hide_for_admins'] ) ); ?>>
				<?php esc_html_e( 'Hide the banner for logged-in administrators', 'blaminhor-essentials' ); ?></label><br>
			<label><input type="checkbox" name="show_revoke_icon" <?php checked( ! empty( $settings['show_revoke_icon'] ) ); ?>>
				<?php esc_html_e( 'Show a small icon on the site so visitors can change their choice later', 'blaminhor-essentials' ); ?></label><br>
			<label><input type="checkbox" name="symmetry_lock" <?php checked( ! empty( $settings['symmetry_lock'] ) ); ?>>
				<?php esc_html_e( 'Accept and Reject must have the same visual weight (recommended — required by EU law)', 'blaminhor-essentials' ); ?></label>
		</td>
	</tr>
</table>

<hr style="margin: 30px 0;">

<h3><?php esc_html_e( 'Colors', 'blaminhor-essentials' ); ?></h3>
<table class="form-table" role="presentation">
	<tr>
		<th><?php esc_html_e( 'Light theme', 'blaminhor-essentials' ); ?></th>
		<td>
			<?php
			$bla_cookie_colors_light = array(
				'color_bg'            => __( 'Background', 'blaminhor-essentials' ),
				'color_text'          => __( 'Text', 'blaminhor-essentials' ),
				'color_button_bg'     => __( 'Button background', 'blaminhor-essentials' ),
				'color_button_text'   => __( 'Button text', 'blaminhor-essentials' ),
				'color_button_border' => __( 'Button border', 'blaminhor-essentials' ),
			);
			?>
			<div class="blaminhor-essentials-color-grid">
				<?php foreach ( $bla_cookie_colors_light as $field => $label ) :
					$val = isset( $settings[ $field ] ) ? $settings[ $field ] : '';
					?>
					<div class="blaminhor-essentials-color-row">
						<span class="blaminhor-essentials-color-label-text"><?php echo esc_html( $label ); ?></span>
						<input type="text" name="<?php echo esc_attr( $field ); ?>" value="<?php echo esc_attr( $val ); ?>" class="blaminhor-essentials-color-picker" data-default-color="<?php echo esc_attr( $val ); ?>">
					</div>
				<?php endforeach; ?>
			</div>
		</td>
	</tr>
	<tr>
		<th><?php esc_html_e( 'Dark theme', 'blaminhor-essentials' ); ?></th>
		<td>
			<?php
			$bla_cookie_colors_dark = array(
				'color_dark_bg'   => __( 'Background', 'blaminhor-essentials' ),
				'color_dark_text' => __( 'Text', 'blaminhor-essentials' ),
			);
			?>
			<div class="blaminhor-essentials-color-grid">
				<?php foreach ( $bla_cookie_colors_dark as $field => $label ) :
					$val = isset( $settings[ $field ] ) ? $settings[ $field ] : '';
					?>
					<div class="blaminhor-essentials-color-row">
						<span class="blaminhor-essentials-color-label-text"><?php echo esc_html( $label ); ?></span>
						<input type="text" name="<?php echo esc_attr( $field ); ?>" value="<?php echo esc_attr( $val ); ?>" class="blaminhor-essentials-color-picker" data-default-color="<?php echo esc_attr( $val ); ?>">
					</div>
				<?php endforeach; ?>
			</div>
		</td>
	</tr>
</table>

<hr style="margin: 30px 0;">

<h3><?php esc_html_e( 'Preview the banner', 'blaminhor-essentials' ); ?></h3>
<p class="description">
	<?php esc_html_e( 'Open the front-end with the banner forced visible, even if you are logged in. Your choices are not saved and no other visitor is affected.', 'blaminhor-essentials' ); ?>
</p>
<p>
	<?php
	$bla_cookie_preview_url = wp_nonce_url(
		add_query_arg( 'blaminhor_preview_banner', '1', home_url( '/' ) ),
		'blaminhor_essentials_cookies_preview'
	);
	?>
	<a class="button" href="<?php echo esc_url( $bla_cookie_preview_url ); ?>" target="_blank" rel="noopener">
		<?php esc_html_e( 'Preview the banner', 'blaminhor-essentials' ); ?>
	</a>
</p>

<hr style="margin: 30px 0;">

<h3><?php esc_html_e( 'Integrations', 'blaminhor-essentials' ); ?></h3>
<table class="form-table" role="presentation">
	<tr>
		<th><?php esc_html_e( 'Google Consent Mode v2', 'blaminhor-essentials' ); ?></th>
		<td>
			<label><input type="checkbox" name="gcm_enabled" <?php checked( ! empty( $settings['gcm_enabled'] ) ); ?>>
				<?php esc_html_e( 'Send the visitor\'s choice to Google Ads, Analytics and Tag Manager. Enable if you use Google Ads with visitors in the EU, UK or Switzerland.', 'blaminhor-essentials' ); ?></label>
		</td>
	</tr>
	<tr>
		<th><?php esc_html_e( 'WP Consent API', 'blaminhor-essentials' ); ?></th>
		<td>
			<label><input type="checkbox" name="wp_consent_api" <?php checked( ! empty( $settings['wp_consent_api'] ) ); ?>>
				<?php esc_html_e( 'Share the visitor\'s choice with other compatible plugins (WP Statistics, Matomo, WooCommerce, etc.) so they adapt automatically.', 'blaminhor-essentials' ); ?></label>
		</td>
	</tr>
	<tr>
		<th><?php esc_html_e( 'Scripts in the theme', 'blaminhor-essentials' ); ?></th>
		<td>
			<label><input type="checkbox" name="intercept_inline" <?php checked( ! empty( $settings['intercept_inline'] ) ); ?>>
				<?php esc_html_e( 'Also block scripts inserted directly in the theme header or via an injection plugin (Google Tag Manager, Meta Pixel, etc.). Recommended in most cases.', 'blaminhor-essentials' ); ?></label>
		</td>
	</tr>
	<tr>
		<th><label for="privacy_policy_url"><?php esc_html_e( 'Privacy policy URL', 'blaminhor-essentials' ); ?></label></th>
		<td><input type="url" id="privacy_policy_url" name="privacy_policy_url" class="regular-text" value="<?php echo esc_attr( $settings['privacy_policy_url'] ); ?>" placeholder="<?php echo esc_attr( home_url( '/privacy-policy/' ) ); ?>"></td>
	</tr>
</table>

<hr style="margin: 30px 0;">

<h3><?php esc_html_e( 'Consent log', 'blaminhor-essentials' ); ?></h3>
<?php $bla_cookie_log_count = $consent_log->count(); ?>
<p class="description">
	<?php esc_html_e( 'Every visitor choice is recorded in a private table to satisfy RGPD Art.7 (proof of consent). Records older than 13 months are deleted automatically.', 'blaminhor-essentials' ); ?>
</p>
<p>
	<?php
	printf(
		/* translators: %d: total consent records */
		esc_html( _n( '%d record stored.', '%d records stored.', $bla_cookie_log_count, 'blaminhor-essentials' ) ),
		absint( $bla_cookie_log_count )
	);
	?>
	&nbsp;
	<a class="button" href="<?php echo esc_url( rest_url( 'blaminhor-essentials/cookies/v1/export' ) . '?_wpnonce=' . wp_create_nonce( 'wp_rest' ) ); ?>">
		<?php esc_html_e( 'Export as CSV', 'blaminhor-essentials' ); ?>
	</a>
</p>
