<?php
/**
 * Account add/edit form template.
 *
 * Variables in scope (passed by Account_Form::render()):
 *
 * @var string                                               $heading
 * @var string                                               $back_url
 * @var bool                                                 $is_edit
 * @var \DonationRouter\PayPal_Account|null $account
 *
 * Sub-templates included directly (variables already in scope):
 *   templates/admin/account-form-fields.php
 *   templates/admin/account-form-receipt-section.php  (edit mode only)
 *
 * @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 Accounts', 'alamin-donation-router' ); ?></a></p>

	<?php if ( $is_edit && null !== $account ) : ?>
		<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="ador-form" id="ador-edit-account-form">
			<input type="hidden" name="action" value="ador_save_account" />
			<input type="hidden" name="account_id" value="<?php echo absint( $account->id ); ?>" />
			<?php wp_nonce_field( 'ador_save_account_' . $account->id, 'ador_nonce' ); ?>
			<?php include ADOR_PATH . 'templates/admin/account-form-fields.php'; ?>
			<?php include ADOR_PATH . 'templates/admin/account-form-receipt-section.php'; ?>
			<p class="submit">
				<button type="submit" class="button button-primary"><?php esc_html_e( 'Save Changes', '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 class="ador-connection-section">
			<h3><?php esc_html_e( 'PayPal Connection', 'alamin-donation-router' ); ?></h3>
			<p>
				<strong><?php esc_html_e( 'Merchant ID:', 'alamin-donation-router' ); ?></strong>
				<?php echo esc_html( $account->merchant_id ); ?>
			</p>
			<?php if ( null !== $account->webhook_id ) : ?>
				<p>
					<strong><?php esc_html_e( 'Webhook ID:', 'alamin-donation-router' ); ?></strong>
					<?php echo esc_html( $account->webhook_id ); ?>
				</p>
			<?php endif; ?>
		</div>

	<?php else : ?>
		<div class="ador-form" id="ador-add-account-form">
			<?php include ADOR_PATH . 'templates/admin/account-form-fields.php'; ?>

			<div class="ador-connect-section">
				<h3><?php esc_html_e( 'Connect PayPal Account', 'alamin-donation-router' ); ?></h3>
				<p class="description">
					<?php esc_html_e( 'Enter the account details above, choose the environment, then click Connect. A PayPal window opens to finish onboarding; the account is created here once it completes. Connect as many accounts as you need — one per country.', 'alamin-donation-router' ); ?>
				</p>
				<p>
					<label for="ador_account_mode">
						<strong><?php esc_html_e( 'Environment', 'alamin-donation-router' ); ?></strong>
					</label>
					<br />
					<select id="ador_account_mode" name="account_mode" class="ador-select">
						<option value="live"><?php esc_html_e( 'Live', 'alamin-donation-router' ); ?></option>
						<option value="sandbox"><?php esc_html_e( 'Sandbox', 'alamin-donation-router' ); ?></option>
					</select>
					<span class="description">
						<?php esc_html_e( 'Which PayPal environment to connect this account in. Sandbox is for testing.', 'alamin-donation-router' ); ?>
					</span>
				</p>
				<p>
					<button type="button" class="button button-primary" id="ador-connect-paypal">
						<?php esc_html_e( 'Connect with PayPal', 'alamin-donation-router' ); ?>
					</button>
					<a href="<?php echo esc_url( $back_url ); ?>" class="button"><?php esc_html_e( 'Cancel', 'alamin-donation-router' ); ?></a>
				</p>
				<?php // PayPal's partner SDK turns this hidden anchor into the onboarding launcher. ?>
				<a class="ador-hidden" id="ador-paypal-sdk-anchor" target="PPFrame" data-paypal-button="true" rel="noopener noreferrer">
					<?php esc_html_e( 'Sign up for PayPal', 'alamin-donation-router' ); ?>
				</a>
				<p class="ador-connect-status" id="ador-connect-status" role="status" aria-live="polite"></p>
			</div>
		</div>
	<?php endif; ?>
</div>
