<?php
/**
 * Country routing add/edit form template.
 *
 * Variables in scope (passed by Country_Routing_Form::render()):
 *
 * @var string                                                    $heading
 * @var string                                                    $back_url
 * @var bool                                                      $is_edit
 * @var \DonationRouter\Country_Routing|null    $routing
 * @var string                                                    $country_code
 * @var int                                                       $form_id
 * @var int                                                       $account_id
 * @var string                                                    $display_label
 * @var string                                                    $display_description
 * @var int                                                       $sort_order
 * @var bool                                                      $is_active
 * @var string                                                    $action
 * @var string                                                    $nonce_action
 * @var string                                                    $account_select_label  Pre-resolved label for account select.
 * @var array<string,string>                                      $countries
 *
 * @since   1.0.0
 * @package DonationRouter
 */

declare( strict_types=1 );

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

?>
<div class="ador-form-wrap">
	<h2><?php echo esc_html( $heading ); ?></h2>
	<p><a href="<?php echo esc_url( $back_url ); ?>">&larr; <?php esc_html_e( 'Back to Country Routing', 'alamin-donation-router' ); ?></a></p>

	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="ador-form" id="ador-routing-form">
		<input type="hidden" name="action" value="<?php echo esc_attr( $action ); ?>" />
		<?php if ( $is_edit && null !== $routing ) : ?>
			<input type="hidden" name="routing_id" value="<?php echo absint( $routing->id ); ?>" />
		<?php endif; ?>
		<?php wp_nonce_field( $nonce_action, 'ador_nonce' ); ?>

		<table class="ador-form-table" role="presentation">
			<tr>
				<th scope="row">
					<label for="ador_country_code">
						<?php esc_html_e( 'Country', 'alamin-donation-router' ); ?>
						<span class="required" aria-hidden="true">*</span>
					</label>
				</th>
				<td>
					<select id="ador_country_code" name="country_code" required class="ador-select">
						<option value=""><?php esc_html_e( '— Select Country —', 'alamin-donation-router' ); ?></option>
						<?php foreach ( $countries as $ador_code => $ador_label ) : ?>
							<option value="<?php echo esc_attr( $ador_code ); ?>" <?php selected( $country_code, $ador_code ); ?>>
								<?php echo esc_html( $ador_label ); ?>
							</option>
						<?php endforeach; ?>
					</select>
					<p class="description"><?php esc_html_e( 'Donors from this country will use the form and account selected below.', 'alamin-donation-router' ); ?></p>
				</td>
			</tr>

			<tr>
				<th scope="row">
					<label for="ador_form_id">
						<?php esc_html_e( 'Donation Form', 'alamin-donation-router' ); ?>
						<span class="required" aria-hidden="true">*</span>
					</label>
				</th>
				<td>
					<select id="ador_form_id" name="form_id" required class="ador-select">
						<?php if ( $form_id > 0 ) : ?>
							<option value="<?php echo absint( $form_id ); ?>" selected="selected">
								<?php echo esc_html( (string) get_the_title( $form_id ) ); ?>
							</option>
						<?php else : ?>
							<option value=""><?php esc_html_e( '— Loading forms… —', 'alamin-donation-router' ); ?></option>
						<?php endif; ?>
					</select>
					<p class="description"><?php esc_html_e( 'Donation form shown to donors from the selected country.', 'alamin-donation-router' ); ?></p>
				</td>
			</tr>

			<tr>
				<th scope="row">
					<label for="ador_account_id">
						<?php esc_html_e( 'PayPal Account', 'alamin-donation-router' ); ?>
						<span class="required" aria-hidden="true">*</span>
					</label>
				</th>
				<td>
					<select id="ador_account_id" name="account_id" required class="ador-select">
						<?php if ( $account_id > 0 ) : ?>
							<option value="<?php echo absint( $account_id ); ?>" selected="selected">
								<?php echo esc_html( $account_select_label ); ?>
							</option>
						<?php else : ?>
							<option value=""><?php esc_html_e( '— Loading accounts… —', 'alamin-donation-router' ); ?></option>
						<?php endif; ?>
					</select>
					<p class="description"><?php esc_html_e( 'PayPal Business account to charge for donations from this country.', 'alamin-donation-router' ); ?></p>
				</td>
			</tr>

			<tr>
				<th scope="row">
					<label for="ador_display_label">
						<?php esc_html_e( 'Display Label', 'alamin-donation-router' ); ?>
						<span class="required" aria-hidden="true">*</span>
					</label>
				</th>
				<td>
					<input
						type="text"
						id="ador_display_label"
						name="display_label"
						value="<?php echo esc_attr( wp_strip_all_tags( $display_label ) ); ?>"
						maxlength="191"
						required
						class="regular-text"
					/>
					<p class="description"><?php esc_html_e( 'Label shown to donors when this country is selected (e.g. "Give to our US program"). Basic HTML allowed.', 'alamin-donation-router' ); ?></p>
				</td>
			</tr>

			<tr>
				<th scope="row">
					<label for="ador_display_description">
						<?php esc_html_e( 'Display Description', 'alamin-donation-router' ); ?>
					</label>
				</th>
				<td>
					<textarea
						id="ador_display_description"
						name="display_description"
						rows="3"
						class="large-text"
					><?php echo wp_kses_post( $display_description ); ?></textarea>
					<p class="description"><?php esc_html_e( 'Optional supplementary text (e.g. tax-deductibility note). Basic HTML allowed.', 'alamin-donation-router' ); ?></p>
				</td>
			</tr>

			<tr>
				<th scope="row">
					<label for="ador_sort_order">
						<?php esc_html_e( 'Sort Order', 'alamin-donation-router' ); ?>
					</label>
				</th>
				<td>
					<input
						type="number"
						id="ador_sort_order"
						name="sort_order"
						value="<?php echo absint( $sort_order ); ?>"
						min="0"
						class="small-text"
					/>
					<p class="description"><?php esc_html_e( 'Lower numbers display first in the country selector. Default: 0.', 'alamin-donation-router' ); ?></p>
				</td>
			</tr>

			<?php if ( $is_edit ) : ?>
			<tr>
				<th scope="row">
					<?php esc_html_e( 'Active', 'alamin-donation-router' ); ?>
				</th>
				<td>
					<label for="ador_is_active">
						<input
							type="checkbox"
							id="ador_is_active"
							name="is_active"
							value="1"
							<?php checked( $is_active ); ?>
						/>
						<?php esc_html_e( 'This routing rule is active', 'alamin-donation-router' ); ?>
					</label>
				</td>
			</tr>
			<?php endif; ?>
		</table>

		<p class="submit">
			<button type="submit" class="button button-primary"><?php esc_html_e( 'Save Routing Rule', 'alamin-donation-router' ); ?></button>
			<a href="<?php echo esc_url( $back_url ); ?>" class="button"><?php esc_html_e( 'Cancel', 'alamin-donation-router' ); ?></a>
		</p>
	</form>
</div>
