<?php
/**
 * Plugin settings form template.
 *
 * Variables in scope (passed by Plugin_Settings::render()):
 *
 * @var string   $form_action
 * @var int      $default_form_id
 * @var bool     $popup_dismissible
 * @var string   $popup_trigger
 * @var string   $popup_heading
 * @var bool     $geo_enabled
 * @var string   $geo_provider
 * @var bool     $pdf_enabled
 * @var bool     $acknowledged
 * @var \WP_Post[] $give_forms
 *
 * @since   1.0.0
 * @package DonationRouter
 */

declare( strict_types=1 );

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use DonationRouter\Plugin_Settings;
?>
<form method="post" action="<?php echo esc_url( $form_action ); ?>" class="ador-settings-form">
	<?php wp_nonce_field( Plugin_Settings::NONCE_ACTION, Plugin_Settings::NONCE_FIELD ); ?>

	<h2 class="ador-settings-section"><?php esc_html_e( 'General', 'alamin-donation-router' ); ?></h2>
	<table class="form-table" role="presentation">
		<tr>
			<th scope="row">
				<label for="ador_default_form_id"><?php esc_html_e( 'Default Donation Form', 'alamin-donation-router' ); ?></label>
			</th>
			<td>
				<select id="ador_default_form_id" name="ador_default_form_id" class="ador-select">
					<option value="0"><?php esc_html_e( '— None —', 'alamin-donation-router' ); ?></option>
					<?php foreach ( $give_forms as $ador_form ) : ?>
						<option value="<?php echo esc_attr( (string) $ador_form->ID ); ?>" <?php selected( $default_form_id, $ador_form->ID ); ?>>
							<?php echo esc_html( $ador_form->post_title ); ?>
						</option>
					<?php endforeach; ?>
				</select>
				<p class="description"><?php esc_html_e( 'Used as fallback when no routing rule matches.', 'alamin-donation-router' ); ?></p>
			</td>
		</tr>
	</table>

	<h2 class="ador-settings-section"><?php esc_html_e( 'Country Selector Popup', 'alamin-donation-router' ); ?></h2>
	<table class="form-table" role="presentation">
		<tr>
			<th scope="row"><?php esc_html_e( 'Trigger', 'alamin-donation-router' ); ?></th>
			<td>
				<fieldset>
					<label>
						<input type="radio" name="ador_popup_default_trigger" value="auto" <?php checked( $popup_trigger, 'auto' ); ?>>
						<?php esc_html_e( 'Automatic — show modal on page load', 'alamin-donation-router' ); ?>
					</label>
					<br>
					<label>
						<input type="radio" name="ador_popup_default_trigger" value="button" <?php checked( $popup_trigger, 'button' ); ?>>
						<?php esc_html_e( 'Button — donor opens modal manually', 'alamin-donation-router' ); ?>
					</label>
				</fieldset>
			</td>
		</tr>
		<tr>
			<th scope="row">
				<label for="ador_popup_heading_default"><?php esc_html_e( 'Popup Heading', 'alamin-donation-router' ); ?></label>
			</th>
			<td>
				<input type="text" id="ador_popup_heading_default" name="ador_popup_heading_default"
					value="<?php echo esc_attr( $popup_heading ); ?>" class="regular-text"
					placeholder="<?php esc_attr_e( 'Where are you donating from?', 'alamin-donation-router' ); ?>">
				<p class="description"><?php esc_html_e( 'Leave blank to use the default translated heading.', 'alamin-donation-router' ); ?></p>
			</td>
		</tr>
		<tr>
			<th scope="row"><?php esc_html_e( 'Dismissible', 'alamin-donation-router' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="ador_popup_dismissible" value="1" <?php checked( $popup_dismissible ); ?>>
					<?php esc_html_e( 'Allow donors to close the popup without selecting a country', 'alamin-donation-router' ); ?>
				</label>
			</td>
		</tr>
	</table>

	<h2 class="ador-settings-section"><?php esc_html_e( 'Geolocation', 'alamin-donation-router' ); ?></h2>
	<table class="form-table" role="presentation">
		<tr>
			<th scope="row"><?php esc_html_e( 'Enable Geolocation', 'alamin-donation-router' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="ador_geolocation_enabled" value="1" <?php checked( $geo_enabled ); ?>>
					<?php esc_html_e( 'Pre-fill the popup with the donor\'s detected country', 'alamin-donation-router' ); ?>
				</label>
			</td>
		</tr>
		<tr>
			<th scope="row">
				<label for="ador_geolocation_provider"><?php esc_html_e( 'Geolocation Provider', 'alamin-donation-router' ); ?></label>
			</th>
			<td>
				<select id="ador_geolocation_provider" name="ador_geolocation_provider" class="ador-select">
					<option value="cloudflare" <?php selected( $geo_provider, 'cloudflare' ); ?>><?php esc_html_e( 'Cloudflare (CF-IPCountry header)', 'alamin-donation-router' ); ?></option>
					<option value="maxmind" <?php selected( $geo_provider, 'maxmind' ); ?>><?php esc_html_e( 'MaxMind GeoIP Detect plugin', 'alamin-donation-router' ); ?></option>
					<option value="wp_filter" <?php selected( $geo_provider, 'wp_filter' ); ?>><?php esc_html_e( 'WordPress geoip_country_code filter', 'alamin-donation-router' ); ?></option>
				</select>
				<p class="description"><?php esc_html_e( 'All providers are tried in sequence when auto is active.', 'alamin-donation-router' ); ?></p>
			</td>
		</tr>
	</table>

	<h2 class="ador-settings-section"><?php esc_html_e( 'PDF Receipts', 'alamin-donation-router' ); ?></h2>
	<table class="form-table" role="presentation">
		<tr>
			<th scope="row"><?php esc_html_e( 'Enable PDF Receipts', 'alamin-donation-router' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="ador_pdf_receipts_enabled" value="1" <?php checked( $pdf_enabled ); ?>>
					<?php esc_html_e( 'Generate downloadable PDF receipts for donors (requires dompdf)', 'alamin-donation-router' ); ?>
				</label>
				<?php if ( ! class_exists( \Dompdf\Dompdf::class ) ) : ?>
				<p class="ador-notice-error">
					<?php echo ador_icon( 'alert-triangle' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					<?php esc_html_e( 'dompdf is not installed. Run: composer require dompdf/dompdf', 'alamin-donation-router' ); ?>
				</p>
				<?php endif; ?>
			</td>
		</tr>
	</table>

	<h2 class="ador-settings-section"><?php esc_html_e( 'Legal &amp; Compliance', 'alamin-donation-router' ); ?></h2>
	<table class="form-table" role="presentation">
		<tr>
			<th scope="row"><?php esc_html_e( 'Receipt Review', 'alamin-donation-router' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="ador_receipt_legal_review_acknowledged" value="1" <?php checked( $acknowledged ); ?>>
					<?php esc_html_e( 'I confirm that the receipt templates, tax statements, and legal entity information have been reviewed and approved by a qualified legal or tax adviser for each country in use.', 'alamin-donation-router' ); ?>
				</label>
				<?php if ( ! $acknowledged ) : ?>
				<p class="ador-notice-warning">
					<?php echo ador_icon( 'info' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					<?php esc_html_e( 'Recommended before processing live donations. This is a reminder for your records — the plugin does not block donations while it is unticked.', 'alamin-donation-router' ); ?>
				</p>
				<?php endif; ?>
			</td>
		</tr>
	</table>

	<?php submit_button( __( 'Save Settings', 'alamin-donation-router' ) ); ?>
</form>
