<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

$payment_options     = SftcyWordformBase::wordform_settings_page_payment_config();
$payment_currencies  = SftcyWordformBase::wordform_get_supported_payment_currencies();
$is_basic_tier       = class_exists( 'SftcyWordformFeatures' ) && 'basic' === SftcyWordformFeatures::wordform_get_product_tier();
$wordform_upgrade_url = class_exists( 'SftcyWordformFeatures' ) ? SftcyWordformFeatures::wordform_get_upgrade_url() : 'https://softcoy.com/products/wordform-pro/checkout';

$stripe_ready       = ! empty( $payment_options['stripe_publishable_key'] ) && ! empty( $payment_options['stripe_secret_key'] );
$paypal_ready       = ! empty( $payment_options['paypal_client_id'] ) && ! empty( $payment_options['paypal_client_secret'] );
$square_ready       = ! empty( $payment_options['square_application_id'] ) && ! empty( $payment_options['square_access_token'] ) && ! empty( $payment_options['square_location_id'] );
$authorizenet_ready = ! empty( $payment_options['authorizenet_api_login_id'] ) && ! empty( $payment_options['authorizenet_client_key'] ) && ! empty( $payment_options['authorizenet_transaction_key'] );

$stripe_complete_count = 0;
$stripe_complete_count += ! empty( $payment_options['stripe_publishable_key'] ) ? 1 : 0;
$stripe_complete_count += ! empty( $payment_options['stripe_secret_key'] ) ? 1 : 0;
$stripe_webhook_ready = ! empty( $payment_options['stripe_webhook_secret'] );

$paypal_complete_count = 0;
$paypal_complete_count += ! empty( $payment_options['paypal_client_id'] ) ? 1 : 0;
$paypal_complete_count += ! empty( $payment_options['paypal_client_secret'] ) ? 1 : 0;
$paypal_webhook_ready = ! empty( $payment_options['paypal_webhook_id'] );

$square_complete_count = 0;
$square_complete_count += ! empty( $payment_options['square_application_id'] ) ? 1 : 0;
$square_complete_count += ! empty( $payment_options['square_location_id'] ) ? 1 : 0;
$square_complete_count += ! empty( $payment_options['square_access_token'] ) ? 1 : 0;

$authorizenet_complete_count = 0;
$authorizenet_complete_count += ! empty( $payment_options['authorizenet_api_login_id'] ) ? 1 : 0;
$authorizenet_complete_count += ! empty( $payment_options['authorizenet_client_key'] ) ? 1 : 0;
$authorizenet_complete_count += ! empty( $payment_options['authorizenet_transaction_key'] ) ? 1 : 0;

$render_gateway_status = static function( $is_ready = false, $mode = '', $locked = false ) {
	if ( $locked ) {
		return '<span class="wordform-payment-gateway-status-pill is-pro">' . esc_html__( 'Pro only', 'wordform' ) . '</span>';
	}

	if ( $is_ready ) {
		$mode_label = ! empty( $mode ) ? ' · ' . strtoupper( sanitize_text_field( $mode ) ) : '';
		return '<span class="wordform-payment-gateway-status-pill is-ready">' . esc_html__( 'Configured', 'wordform' ) . esc_html( $mode_label ) . '</span>';
	}

	return '<span class="wordform-payment-gateway-status-pill is-pending">' . esc_html__( 'Needs setup', 'wordform' ) . '</span>';
};

$render_gateway_check_item = static function( $is_complete, $label, $data_key ) {
	$state_class = $is_complete ? ' is-done' : '';
	$icon_class  = $is_complete ? 'fa-solid fa-circle-check' : 'fa-regular fa-circle';
	return '<li class="wordform-payment-check-item' . esc_attr( $state_class ) . '" data-payment-check="' . esc_attr( $data_key ) . '"><span class="wordform-payment-check-icon"><i class="' . esc_attr( $icon_class ) . '" aria-hidden="true"></i></span><span>' . esc_html( $label ) . '</span></li>';
};

$render_gateway_setup_tip = static function( $badge, $title, $items = array() ) {
	$markup = '<div class="wordform-payment-setup-tip-card">';
	$markup .= '<div class="wordform-payment-setup-tip-head"><span class="wordform-payment-setup-tip-badge">' . esc_html( $badge ) . '</span><strong>' . esc_html( $title ) . '</strong></div>';

	if ( ! empty( $items ) ) {
		$markup .= '<ul class="wordform-payment-setup-tip-list">';

		foreach ( $items as $item ) {
			if ( empty( $item ) ) {
				continue;
			}

			$markup .= '<li><span class="wordform-payment-setup-tip-icon"><i class="fa-solid fa-circle-info" aria-hidden="true"></i></span><span>' . esc_html( $item ) . '</span></li>';
		}

		$markup .= '</ul>';
	}

	$markup .= '</div>';

	return $markup;
};
?>
<div class="sc-wordform-setting-page-payment-tab-table-wrapper">
	<div class="wordform-payment-settings-overview">
		<div class="wordform-payment-settings-help">
			<strong><?php esc_html_e( 'Guided payment setup', 'wordform' ); ?></strong>
			<p><?php esc_html_e( 'Choose a gateway from the left, review the current status, paste the required keys, then save once. Use test or sandbox mode first and switch to live mode only after a full checkout test.', 'wordform' ); ?></p>
			<small><?php esc_html_e( 'Setup buttons open the related fields and checklist below. They do not sign in to the gateway or generate credentials automatically.', 'wordform' ); ?></small>
			<?php if ( $is_basic_tier ) { ?>
			<p><strong><?php esc_html_e( 'Free version scope:', 'wordform' ); ?></strong> <?php esc_html_e( 'Stripe and PayPal are active. Square, Authorize.Net, coupons, and payment reporting stay visible as Pro previews.', 'wordform' ); ?></p>
			<?php } ?>
		</div>
	</div>

	<form method="post" id="scWordformSettingsPaymentTabForm">
		<div class="wordform-payment-settings-shell">
			<aside class="wordform-payment-settings-sidebar">
				<button type="button" class="wordform-payment-settings-nav-button is-active" data-payment-panel="general">
					<span class="wordform-payment-settings-nav-icon"><i class="fa-solid fa-sliders"></i></span>
					<span class="wordform-payment-settings-nav-copy">
						<strong><?php esc_html_e( 'General', 'wordform' ); ?></strong>
						<small><?php esc_html_e( 'Currency and symbols', 'wordform' ); ?></small>
					</span>
				</button>

				<button type="button" class="wordform-payment-settings-nav-button" data-payment-panel="stripe">
					<span class="wordform-payment-settings-nav-icon"><i class="fa-brands fa-stripe"></i></span>
					<span class="wordform-payment-settings-nav-copy">
						<strong>Stripe</strong>
						<small><?php echo $stripe_ready ? esc_html__( 'Configured', 'wordform' ) : esc_html__( 'Needs setup', 'wordform' ); ?></small>
					</span>
				</button>

				<button type="button" class="wordform-payment-settings-nav-button" data-payment-panel="paypal">
					<span class="wordform-payment-settings-nav-icon"><i class="fa-brands fa-paypal"></i></span>
					<span class="wordform-payment-settings-nav-copy">
						<strong>PayPal</strong>
						<small><?php echo $paypal_ready ? esc_html__( 'Configured', 'wordform' ) : esc_html__( 'Needs setup', 'wordform' ); ?></small>
					</span>
				</button>

				<button type="button" class="wordform-payment-settings-nav-button<?php echo $is_basic_tier ? ' is-locked' : ''; ?>" data-payment-panel="square">
					<span class="wordform-payment-settings-nav-icon"><i class="fa-solid fa-square"></i></span>
					<span class="wordform-payment-settings-nav-copy">
						<strong>Square</strong>
						<small><?php echo $is_basic_tier ? esc_html__( 'Pro preview', 'wordform' ) : ( $square_ready ? esc_html__( 'Configured', 'wordform' ) : esc_html__( 'Needs setup', 'wordform' ) ); ?></small>
					</span>
				</button>

				<button type="button" class="wordform-payment-settings-nav-button<?php echo $is_basic_tier ? ' is-locked' : ''; ?>" data-payment-panel="authorizenet">
					<span class="wordform-payment-settings-nav-icon"><i class="fa-solid fa-building-columns"></i></span>
					<span class="wordform-payment-settings-nav-copy">
						<strong><?php esc_html_e( 'Authorize.Net', 'wordform' ); ?></strong>
						<small><?php echo $is_basic_tier ? esc_html__( 'Pro preview', 'wordform' ) : ( $authorizenet_ready ? esc_html__( 'Configured', 'wordform' ) : esc_html__( 'Needs setup', 'wordform' ) ); ?></small>
					</span>
				</button>
			</aside>

			<div class="wordform-payment-settings-main">
				<section class="wordform-payment-settings-pane is-active" data-payment-panel-content="general">
					<div class="wordform-payment-settings-pane-head">
						<div>
							<h3><?php esc_html_e( 'General payment display', 'wordform' ); ?></h3>
							<p><?php esc_html_e( 'These settings affect how prices and totals appear across all payment-enabled forms.', 'wordform' ); ?></p>
						</div>
					</div>

					<div class="wordform-payment-settings-grid">
						<div class="wordform-payment-settings-field">
							<label for="wordformPaymentCurrency"><?php esc_html_e( 'Payment Currency', 'wordform' ); ?></label>
							<select id="wordformPaymentCurrency" name="wordform-payment-currency" class="wordform-payment-currency">
								<?php foreach ( $payment_currencies as $currency_code => $currency_label ) : ?>
								<option value="<?php echo esc_attr( $currency_code ); ?>" <?php selected( $payment_options['payment_currency'], $currency_code ); ?>><?php echo esc_html( $currency_label ); ?></option>
								<?php endforeach; ?>
							</select>
							<small><?php esc_html_e( 'Choose the currency used in payment summaries and gateway charges.', 'wordform' ); ?></small>
						</div>

						<div class="wordform-payment-settings-field">
							<label for="wordformPaymentCurrencyPosition"><?php esc_html_e( 'Currency Symbol Position', 'wordform' ); ?></label>
							<select id="wordformPaymentCurrencyPosition" name="wordform-payment-currency-symbol-position" class="wordform-payment-currency-symbol-position">
								<option value="before" <?php selected( $payment_options['payment_currency_symbol_position'], 'before' ); ?>><?php esc_html_e( 'Before amount', 'wordform' ); ?></option>
								<option value="after" <?php selected( $payment_options['payment_currency_symbol_position'], 'after' ); ?>><?php esc_html_e( 'After amount', 'wordform' ); ?></option>
							</select>
						</div>
					</div>
				</section>

				<section class="wordform-payment-settings-pane" data-payment-panel-content="stripe">
					<div class="wordform-payment-gateway-hero">
						<div>
							<div class="wordform-payment-gateway-kicker"><?php esc_html_e( 'Card payments', 'wordform' ); ?></div>
							<h3><i class="fa-brands fa-stripe"></i> Stripe</h3>
							<p><?php esc_html_e( 'Paste your Stripe publishable key, secret key, and optional webhook secret. Keep test mode on until you finish a full checkout test.', 'wordform' ); ?></p>
						</div>
						<div class="wordform-payment-gateway-actions">
							<?php echo wp_kses_post( $render_gateway_status( $stripe_ready, $payment_options['stripe_mode'] ) ); ?>
							<button type="button" class="button button-primary wordform-payment-connect-trigger" data-payment-panel="stripe" data-focus-target="#wordformStripePublishableKey"><?php esc_html_e( 'Open Stripe setup', 'wordform' ); ?></button>
							<a class="button button-secondary" href="https://docs.stripe.com/keys" target="_blank" rel="noopener"><?php esc_html_e( 'Stripe docs', 'wordform' ); ?></a>
						</div>
					</div>

					<?php
					echo wp_kses_post(
						$render_gateway_setup_tip(
							__( 'Guided manual setup', 'wordform' ),
							__( 'What this setup button does', 'wordform' ),
							array(
								__( 'It jumps to the Stripe fields below so you can enter the required publishable key, secret key, and optional webhook secret.', 'wordform' ),
								__( 'Use values from the same Stripe mode at the same time: Test with test keys first, then swap every value to Live after a successful checkout test.', 'wordform' ),
								__( 'This flow does not sign in to Stripe or create keys automatically. Create the keys in Stripe, paste them here, save once, then run a real test payment.', 'wordform' ),
							)
						)
					);
					?>

					<div class="wordform-payment-gateway-steps">
						<div class="wordform-payment-step-card"><strong>1</strong><span><?php esc_html_e( 'Enable Stripe and choose Test or Live mode.', 'wordform' ); ?></span></div>
						<div class="wordform-payment-step-card"><strong>2</strong><span><?php esc_html_e( 'Paste the publishable and secret keys from your Stripe dashboard.', 'wordform' ); ?></span></div>
						<div class="wordform-payment-step-card"><strong>3</strong><span><?php esc_html_e( 'Optional: add the webhook secret for future verification workflows.', 'wordform' ); ?></span></div>
					</div>

					<div class="wordform-payment-gateway-overview" data-payment-overview="stripe">
						<div class="wordform-payment-gateway-summary-grid">
							<div class="wordform-payment-gateway-summary-card">
								<span class="wordform-payment-gateway-summary-label"><?php esc_html_e( 'Connection status', 'wordform' ); ?></span>
								<strong class="wordform-payment-gateway-summary-value" data-payment-summary-status><?php echo esc_html( $stripe_ready ? __( 'Configured', 'wordform' ) : __( 'Needs setup', 'wordform' ) ); ?></strong>
							</div>
							<div class="wordform-payment-gateway-summary-card">
								<span class="wordform-payment-gateway-summary-label"><?php esc_html_e( 'Mode', 'wordform' ); ?></span>
								<strong class="wordform-payment-gateway-summary-value" data-payment-summary-mode><?php echo esc_html( strtoupper( $payment_options['stripe_mode'] ) ); ?></strong>
							</div>
							<div class="wordform-payment-gateway-summary-card">
								<span class="wordform-payment-gateway-summary-label"><?php esc_html_e( 'Required keys added', 'wordform' ); ?></span>
								<strong class="wordform-payment-gateway-summary-value" data-payment-summary-progress><?php echo esc_html( $stripe_complete_count . ' / 2' ); ?></strong>
							</div>
							<div class="wordform-payment-gateway-summary-card">
								<span class="wordform-payment-gateway-summary-label"><?php esc_html_e( 'Webhook', 'wordform' ); ?></span>
								<strong class="wordform-payment-gateway-summary-value" data-payment-summary-optional><?php echo esc_html( $stripe_webhook_ready ? __( 'Added', 'wordform' ) : __( 'Optional', 'wordform' ) ); ?></strong>
							</div>
						</div>

						<div class="wordform-payment-gateway-checklist-shell">
							<div class="wordform-payment-gateway-checklist-copy">
								<strong><?php esc_html_e( 'Checklist before saving', 'wordform' ); ?></strong>
								<p data-payment-next-step>
									<?php
									if ( ! $stripe_ready ) {
										esc_html_e( 'Add the required Stripe keys, then save once. Keep Test mode enabled until a full checkout succeeds.', 'wordform' );
									} elseif ( ! $stripe_webhook_ready ) {
										esc_html_e( 'Stripe is ready for a first pass. Add the webhook later if you need server-side event verification.', 'wordform' );
									} else {
										esc_html_e( 'Stripe is configured. Run a complete test checkout before switching the gateway to Live mode.', 'wordform' );
									}
									?>
								</p>
							</div>
							<ul class="wordform-payment-checklist">
								<?php
								echo wp_kses_post( $render_gateway_check_item( 'yes' === $payment_options['stripe_enabled'], __( 'Gateway enabled', 'wordform' ), 'enabled' ) );
								echo wp_kses_post( $render_gateway_check_item( ! empty( $payment_options['stripe_publishable_key'] ), __( 'Publishable key added', 'wordform' ), 'primary' ) );
								echo wp_kses_post( $render_gateway_check_item( ! empty( $payment_options['stripe_secret_key'] ), __( 'Secret key added', 'wordform' ), 'secondary' ) );
								echo wp_kses_post( $render_gateway_check_item( $stripe_webhook_ready, __( 'Webhook secret added', 'wordform' ), 'optional' ) );
								?>
							</ul>
						</div>
					</div>

					<div class="wordform-payment-settings-grid">
						<div class="wordform-payment-settings-field">
							<label for="wordformStripeEnabled"><?php esc_html_e( 'Enable Stripe', 'wordform' ); ?></label>
							<div class="wordform-payment-settings-inline">
								<input type="checkbox" id="wordformStripeEnabled" name="wordform-stripe-enabled" <?php checked( 'yes', $payment_options['stripe_enabled'] ); ?> />
								<span><?php esc_html_e( 'Allow Stripe payment elements to process payments.', 'wordform' ); ?></span>
							</div>
						</div>

						<div class="wordform-payment-settings-field">
							<label for="wordformStripeMode"><?php esc_html_e( 'Stripe Mode', 'wordform' ); ?></label>
							<select id="wordformStripeMode" name="wordform-stripe-mode">
								<option value="test" <?php selected( $payment_options['stripe_mode'], 'test' ); ?>><?php esc_html_e( 'Test', 'wordform' ); ?></option>
								<option value="live" <?php selected( $payment_options['stripe_mode'], 'live' ); ?>><?php esc_html_e( 'Live', 'wordform' ); ?></option>
							</select>
						</div>

						<div class="wordform-payment-settings-field">
							<label for="wordformStripePublishableKey"><?php esc_html_e( 'Publishable Key', 'wordform' ); ?></label>
							<input type="text" id="wordformStripePublishableKey" name="wordform-stripe-publishable-key" value="<?php echo esc_attr( $payment_options['stripe_publishable_key'] ); ?>" placeholder="pk_test_..." />
						</div>

						<div class="wordform-payment-settings-field">
							<label for="wordformStripeSecretKey"><?php esc_html_e( 'Secret Key', 'wordform' ); ?></label>
							<input type="text" id="wordformStripeSecretKey" name="wordform-stripe-secret-key" value="<?php echo esc_attr( $payment_options['stripe_secret_key'] ); ?>" placeholder="sk_test_..." />
						</div>

						<div class="wordform-payment-settings-field wordform-payment-settings-field-full">
							<label for="wordformStripeWebhookSecret"><?php esc_html_e( 'Webhook Signing Secret', 'wordform' ); ?></label>
							<input type="text" id="wordformStripeWebhookSecret" name="wordform-stripe-webhook-secret" value="<?php echo esc_attr( $payment_options['stripe_webhook_secret'] ); ?>" placeholder="whsec_..." />
							<small><?php esc_html_e( 'Add this after you create a Stripe webhook endpoint for your site.', 'wordform' ); ?></small>
						</div>
					</div>
				</section>

				<section class="wordform-payment-settings-pane" data-payment-panel-content="paypal">
					<div class="wordform-payment-gateway-hero">
						<div>
							<div class="wordform-payment-gateway-kicker"><?php esc_html_e( 'PayPal checkout', 'wordform' ); ?></div>
							<h3><i class="fa-brands fa-paypal"></i> PayPal</h3>
							<p><?php esc_html_e( 'Paste the client ID and client secret from your PayPal developer app. Use Sandbox first, then switch to Live after verification.', 'wordform' ); ?></p>
						</div>
						<div class="wordform-payment-gateway-actions">
							<?php echo wp_kses_post( $render_gateway_status( $paypal_ready, $payment_options['paypal_mode'] ) ); ?>
							<button type="button" class="button button-primary wordform-payment-connect-trigger" data-payment-panel="paypal" data-focus-target="#wordformPaypalClientId"><?php esc_html_e( 'Open PayPal setup', 'wordform' ); ?></button>
							<a class="button button-secondary" href="https://developer.paypal.com/api/rest/#link-getcredentials" target="_blank" rel="noopener"><?php esc_html_e( 'PayPal docs', 'wordform' ); ?></a>
						</div>
					</div>

					<?php
					echo wp_kses_post(
						$render_gateway_setup_tip(
							__( 'Guided manual setup', 'wordform' ),
							__( 'What this setup button does', 'wordform' ),
							array(
								__( 'It jumps to the PayPal fields below so you can enter the REST app Client ID, Client Secret, and optional Webhook ID.', 'wordform' ),
								__( 'Keep Sandbox and Live values separate. The mode switch and every saved credential must come from the same PayPal app environment.', 'wordform' ),
								__( 'This flow does not sign in to PayPal or create app credentials automatically. Copy the values from the PayPal developer dashboard, save them here, then test a real checkout.', 'wordform' ),
							)
						)
					);
					?>

					<div class="wordform-payment-gateway-steps">
						<div class="wordform-payment-step-card"><strong>1</strong><span><?php esc_html_e( 'Enable PayPal and keep Sandbox mode on for the first test.', 'wordform' ); ?></span></div>
						<div class="wordform-payment-step-card"><strong>2</strong><span><?php esc_html_e( 'Paste the client ID and client secret from your PayPal app.', 'wordform' ); ?></span></div>
						<div class="wordform-payment-step-card"><strong>3</strong><span><?php esc_html_e( 'Optional: add a webhook ID after you configure the event listener in PayPal.', 'wordform' ); ?></span></div>
					</div>

					<div class="wordform-payment-gateway-overview" data-payment-overview="paypal">
						<div class="wordform-payment-gateway-summary-grid">
							<div class="wordform-payment-gateway-summary-card">
								<span class="wordform-payment-gateway-summary-label"><?php esc_html_e( 'Connection status', 'wordform' ); ?></span>
								<strong class="wordform-payment-gateway-summary-value" data-payment-summary-status><?php echo esc_html( $paypal_ready ? __( 'Configured', 'wordform' ) : __( 'Needs setup', 'wordform' ) ); ?></strong>
							</div>
							<div class="wordform-payment-gateway-summary-card">
								<span class="wordform-payment-gateway-summary-label"><?php esc_html_e( 'Mode', 'wordform' ); ?></span>
								<strong class="wordform-payment-gateway-summary-value" data-payment-summary-mode><?php echo esc_html( strtoupper( $payment_options['paypal_mode'] ) ); ?></strong>
							</div>
							<div class="wordform-payment-gateway-summary-card">
								<span class="wordform-payment-gateway-summary-label"><?php esc_html_e( 'Required keys added', 'wordform' ); ?></span>
								<strong class="wordform-payment-gateway-summary-value" data-payment-summary-progress><?php echo esc_html( $paypal_complete_count . ' / 2' ); ?></strong>
							</div>
							<div class="wordform-payment-gateway-summary-card">
								<span class="wordform-payment-gateway-summary-label"><?php esc_html_e( 'Webhook', 'wordform' ); ?></span>
								<strong class="wordform-payment-gateway-summary-value" data-payment-summary-optional><?php echo esc_html( $paypal_webhook_ready ? __( 'Added', 'wordform' ) : __( 'Optional', 'wordform' ) ); ?></strong>
							</div>
						</div>

						<div class="wordform-payment-gateway-checklist-shell">
							<div class="wordform-payment-gateway-checklist-copy">
								<strong><?php esc_html_e( 'Checklist before saving', 'wordform' ); ?></strong>
								<p data-payment-next-step>
									<?php
									if ( ! $paypal_ready ) {
										esc_html_e( 'Add the required PayPal app credentials, then save once. Keep Sandbox enabled until a full checkout succeeds.', 'wordform' );
									} elseif ( ! $paypal_webhook_ready ) {
										esc_html_e( 'PayPal is ready for a first pass. Add the webhook ID later if you need event-level verification.', 'wordform' );
									} else {
										esc_html_e( 'PayPal is configured. Run a complete checkout test before switching the gateway to Live mode.', 'wordform' );
									}
									?>
								</p>
							</div>
							<ul class="wordform-payment-checklist">
								<?php
								echo wp_kses_post( $render_gateway_check_item( 'yes' === $payment_options['paypal_enabled'], __( 'Gateway enabled', 'wordform' ), 'enabled' ) );
								echo wp_kses_post( $render_gateway_check_item( ! empty( $payment_options['paypal_client_id'] ), __( 'Client ID added', 'wordform' ), 'primary' ) );
								echo wp_kses_post( $render_gateway_check_item( ! empty( $payment_options['paypal_client_secret'] ), __( 'Client secret added', 'wordform' ), 'secondary' ) );
								echo wp_kses_post( $render_gateway_check_item( $paypal_webhook_ready, __( 'Webhook ID added', 'wordform' ), 'optional' ) );
								?>
							</ul>
						</div>
					</div>

					<div class="wordform-payment-settings-grid">
						<div class="wordform-payment-settings-field">
							<label for="wordformPaypalEnabled"><?php esc_html_e( 'Enable PayPal', 'wordform' ); ?></label>
							<div class="wordform-payment-settings-inline">
								<input type="checkbox" id="wordformPaypalEnabled" name="wordform-paypal-enabled" <?php checked( 'yes', $payment_options['paypal_enabled'] ); ?> />
								<span><?php esc_html_e( 'Allow PayPal payment elements to process payments.', 'wordform' ); ?></span>
							</div>
						</div>

						<div class="wordform-payment-settings-field">
							<label for="wordformPaypalMode"><?php esc_html_e( 'PayPal Mode', 'wordform' ); ?></label>
							<select id="wordformPaypalMode" name="wordform-paypal-mode">
								<option value="sandbox" <?php selected( $payment_options['paypal_mode'], 'sandbox' ); ?>><?php esc_html_e( 'Sandbox', 'wordform' ); ?></option>
								<option value="live" <?php selected( $payment_options['paypal_mode'], 'live' ); ?>><?php esc_html_e( 'Live', 'wordform' ); ?></option>
							</select>
						</div>

						<div class="wordform-payment-settings-field">
							<label for="wordformPaypalClientId"><?php esc_html_e( 'Client ID', 'wordform' ); ?></label>
							<input type="text" id="wordformPaypalClientId" name="wordform-paypal-client-id" value="<?php echo esc_attr( $payment_options['paypal_client_id'] ); ?>" />
						</div>

						<div class="wordform-payment-settings-field">
							<label for="wordformPaypalClientSecret"><?php esc_html_e( 'Client Secret', 'wordform' ); ?></label>
							<input type="text" id="wordformPaypalClientSecret" name="wordform-paypal-client-secret" value="<?php echo esc_attr( $payment_options['paypal_client_secret'] ); ?>" />
						</div>

						<div class="wordform-payment-settings-field wordform-payment-settings-field-full">
							<label for="wordformPaypalWebhookId"><?php esc_html_e( 'Webhook ID', 'wordform' ); ?></label>
							<input type="text" id="wordformPaypalWebhookId" name="wordform-paypal-webhook-id" value="<?php echo esc_attr( $payment_options['paypal_webhook_id'] ); ?>" />
						</div>
					</div>
				</section>

				<section class="wordform-payment-settings-pane<?php echo $is_basic_tier ? ' is-locked' : ''; ?>" data-payment-panel-content="square">
					<div class="wordform-payment-gateway-hero">
						<div>
							<div class="wordform-payment-gateway-kicker"><?php esc_html_e( 'Square Web Payments', 'wordform' ); ?></div>
							<h3><i class="fa-solid fa-square"></i> Square</h3>
							<p><?php esc_html_e( 'Use Square Web Payments with application ID, location ID, and access token controls from a guided setup page.', 'wordform' ); ?></p>
						</div>
						<div class="wordform-payment-gateway-actions">
							<?php echo wp_kses_post( $render_gateway_status( $square_ready, $payment_options['square_mode'], $is_basic_tier ) ); ?>
							<?php if ( $is_basic_tier ) { ?>
							<a class="button button-primary wordform-pro-locked-button" href="<?php echo esc_url( $wordform_upgrade_url ); ?>" target="_blank" rel="noopener" data-upgrade-url="<?php echo esc_url( $wordform_upgrade_url ); ?>" title="<?php esc_attr_e( 'Get Pro', 'wordform' ); ?>"><?php esc_html_e( 'Unlock Square setup', 'wordform' ); ?></a>
							<?php } else { ?>
							<button type="button" class="button button-primary wordform-payment-connect-trigger" data-payment-panel="square" data-focus-target="#wordformSquareApplicationId"><?php esc_html_e( 'Open Square setup', 'wordform' ); ?></button>
							<?php } ?>
							<a class="button button-secondary" href="https://developer.squareup.com/docs/web-payments/overview" target="_blank" rel="noopener"><?php esc_html_e( 'Square docs', 'wordform' ); ?></a>
						</div>
					</div>

					<?php
					echo wp_kses_post(
						$render_gateway_setup_tip(
							$is_basic_tier ? __( 'Pro preview', 'wordform' ) : __( 'Guided manual setup', 'wordform' ),
							__( 'What this setup button does', 'wordform' ),
							$is_basic_tier
								? array(
									__( 'WordForm Free shows the real Square setup structure here so you can see the full workflow before upgrading.', 'wordform' ),
									__( 'Upgrade unlocks the same fields below for real Application ID, Location ID, and Access Token values.', 'wordform' ),
									__( 'The setup button in Pro still opens fields only. It does not sign in to Square or generate credentials automatically.', 'wordform' ),
								)
								: array(
									__( 'It jumps to the Square fields below so you can enter the Application ID, Location ID, and Access Token from your developer account.', 'wordform' ),
									__( 'Application ID, Location ID, Access Token, and the selected mode must all come from the same Square environment. Do not mix Sandbox and Production values.', 'wordform' ),
									__( 'If Square returns unauthorized, check the mode first, then refresh the Access Token and confirm the Location ID belongs to that same account and environment.', 'wordform' ),
									__( 'This flow does not sign in to Square or generate credentials automatically. Create or copy the values in Square, save here, then run a live card-entry sandbox test.', 'wordform' ),
								)
						)
					);
					?>

					<div class="wordform-payment-gateway-steps">
						<div class="wordform-payment-step-card"><strong>1</strong><span><?php esc_html_e( 'Enable Square and keep Sandbox mode selected for the first test pass.', 'wordform' ); ?></span></div>
						<div class="wordform-payment-step-card"><strong>2</strong><span><?php esc_html_e( 'Paste the application ID, location ID, and access token from your Square developer dashboard.', 'wordform' ); ?></span></div>
						<div class="wordform-payment-step-card"><strong>3</strong><span><?php esc_html_e( 'Save once, then run a real card-entry checkout test from a payment-enabled form.', 'wordform' ); ?></span></div>
					</div>

					<div class="wordform-payment-gateway-overview" data-payment-overview="square">
						<div class="wordform-payment-gateway-summary-grid">
							<div class="wordform-payment-gateway-summary-card">
								<span class="wordform-payment-gateway-summary-label"><?php esc_html_e( 'Connection status', 'wordform' ); ?></span>
								<strong class="wordform-payment-gateway-summary-value" data-payment-summary-status><?php echo esc_html( $is_basic_tier ? __( 'Pro preview', 'wordform' ) : ( $square_ready ? __( 'Configured', 'wordform' ) : __( 'Needs setup', 'wordform' ) ) ); ?></strong>
							</div>
							<div class="wordform-payment-gateway-summary-card">
								<span class="wordform-payment-gateway-summary-label"><?php esc_html_e( 'Mode', 'wordform' ); ?></span>
								<strong class="wordform-payment-gateway-summary-value" data-payment-summary-mode><?php echo esc_html( ! empty( $payment_options['square_mode'] ) ? strtoupper( $payment_options['square_mode'] ) : __( 'SANDBOX', 'wordform' ) ); ?></strong>
							</div>
							<div class="wordform-payment-gateway-summary-card">
								<span class="wordform-payment-gateway-summary-label"><?php esc_html_e( 'Required keys added', 'wordform' ); ?></span>
								<strong class="wordform-payment-gateway-summary-value" data-payment-summary-progress><?php echo esc_html( $square_complete_count . ' / 3' ); ?></strong>
							</div>
							<div class="wordform-payment-gateway-summary-card">
								<span class="wordform-payment-gateway-summary-label"><?php esc_html_e( 'Upgrade path', 'wordform' ); ?></span>
								<strong class="wordform-payment-gateway-summary-value" data-payment-summary-optional><?php echo esc_html( $is_basic_tier ? __( 'Unlock in Pro', 'wordform' ) : __( 'Ready for test', 'wordform' ) ); ?></strong>
							</div>
						</div>

						<div class="wordform-payment-gateway-checklist-shell">
							<div class="wordform-payment-gateway-checklist-copy">
								<strong><?php esc_html_e( 'Checklist before saving', 'wordform' ); ?></strong>
								<p data-payment-next-step>
									<?php
									if ( $is_basic_tier ) {
										esc_html_e( 'Square is available in WordForm Pro. Upgrade to unlock the same guided connection flow and save real sandbox or live credentials.', 'wordform' );
									} elseif ( ! $square_ready ) {
										esc_html_e( 'Add the three required Square credentials, then save once and test the Web Payments card field from a real form.', 'wordform' );
									} else {
										esc_html_e( 'Square is configured. Run a complete sandbox checkout before moving the gateway to live traffic.', 'wordform' );
									}
									?>
								</p>
							</div>
							<ul class="wordform-payment-checklist">
								<?php
								echo wp_kses_post( $render_gateway_check_item( 'yes' === $payment_options['square_enabled'], __( 'Gateway enabled', 'wordform' ), 'enabled' ) );
								echo wp_kses_post( $render_gateway_check_item( ! empty( $payment_options['square_application_id'] ), __( 'Application ID added', 'wordform' ), 'primary' ) );
								echo wp_kses_post( $render_gateway_check_item( ! empty( $payment_options['square_location_id'] ), __( 'Location ID added', 'wordform' ), 'secondary' ) );
								echo wp_kses_post( $render_gateway_check_item( ! empty( $payment_options['square_access_token'] ), __( 'Access token added', 'wordform' ), 'tertiary' ) );
								?>
							</ul>
						</div>
					</div>

					<div class="wordform-payment-settings-grid">
						<div class="wordform-payment-settings-field">
							<label for="wordformSquareEnabled"><?php esc_html_e( 'Enable Square', 'wordform' ); ?></label>
							<div class="wordform-payment-settings-inline">
								<input type="checkbox" id="wordformSquareEnabled" name="wordform-square-enabled" <?php checked( 'yes', $payment_options['square_enabled'] ); ?> <?php disabled( $is_basic_tier ); ?> />
								<span><?php esc_html_e( 'Allow Square payment elements to process payments.', 'wordform' ); ?></span>
							</div>
						</div>
						<div class="wordform-payment-settings-field">
							<label for="wordformSquareMode"><?php esc_html_e( 'Square Mode', 'wordform' ); ?></label>
							<select id="wordformSquareMode" name="wordform-square-mode" <?php disabled( $is_basic_tier ); ?>>
								<option value="sandbox" <?php selected( $payment_options['square_mode'], 'sandbox' ); ?>><?php esc_html_e( 'Sandbox', 'wordform' ); ?></option>
								<option value="live" <?php selected( $payment_options['square_mode'], 'live' ); ?>><?php esc_html_e( 'Live', 'wordform' ); ?></option>
							</select>
						</div>
						<div class="wordform-payment-settings-field">
							<label for="wordformSquareApplicationId"><?php esc_html_e( 'Application ID', 'wordform' ); ?></label>
							<input type="text" id="wordformSquareApplicationId" name="wordform-square-application-id" value="<?php echo esc_attr( $payment_options['square_application_id'] ); ?>" placeholder="sq0idp-..." <?php disabled( $is_basic_tier ); ?> />
						</div>
						<div class="wordform-payment-settings-field">
							<label for="wordformSquareLocationId"><?php esc_html_e( 'Location ID', 'wordform' ); ?></label>
							<input type="text" id="wordformSquareLocationId" name="wordform-square-location-id" value="<?php echo esc_attr( $payment_options['square_location_id'] ); ?>" placeholder="L..." <?php disabled( $is_basic_tier ); ?> />
						</div>
						<div class="wordform-payment-settings-field wordform-payment-settings-field-full">
							<label for="wordformSquareAccessToken"><?php esc_html_e( 'Access Token', 'wordform' ); ?></label>
							<input type="text" id="wordformSquareAccessToken" name="wordform-square-access-token" value="<?php echo esc_attr( $payment_options['square_access_token'] ); ?>" placeholder="EAAA..." <?php disabled( $is_basic_tier ); ?> />
						</div>
					</div>
				</section>

				<section class="wordform-payment-settings-pane<?php echo $is_basic_tier ? ' is-locked' : ''; ?>" data-payment-panel-content="authorizenet">
					<div class="wordform-payment-gateway-hero">
						<div>
							<div class="wordform-payment-gateway-kicker"><?php esc_html_e( 'Accept.js checkout', 'wordform' ); ?></div>
							<h3><i class="fa-solid fa-building-columns"></i> <?php esc_html_e( 'Authorize.Net', 'wordform' ); ?></h3>
							<p><?php esc_html_e( 'Use Accept.js tokenization with API Login ID, Client Key, and Transaction Key from a guided setup panel.', 'wordform' ); ?></p>
						</div>
						<div class="wordform-payment-gateway-actions">
							<?php echo wp_kses_post( $render_gateway_status( $authorizenet_ready, $payment_options['authorizenet_mode'], $is_basic_tier ) ); ?>
							<?php if ( $is_basic_tier ) { ?>
							<a class="button button-primary wordform-pro-locked-button" href="<?php echo esc_url( $wordform_upgrade_url ); ?>" target="_blank" rel="noopener" data-upgrade-url="<?php echo esc_url( $wordform_upgrade_url ); ?>" title="<?php esc_attr_e( 'Get Pro', 'wordform' ); ?>"><?php esc_html_e( 'Unlock Authorize.Net setup', 'wordform' ); ?></a>
							<?php } else { ?>
							<button type="button" class="button button-primary wordform-payment-connect-trigger" data-payment-panel="authorizenet" data-focus-target="#wordformAuthorizeNetApiLoginId"><?php esc_html_e( 'Open Authorize.Net setup', 'wordform' ); ?></button>
							<?php } ?>
							<a class="button button-secondary" href="https://developer.authorize.net/api/reference/features/acceptjs.html" target="_blank" rel="noopener"><?php esc_html_e( 'Authorize.Net docs', 'wordform' ); ?></a>
						</div>
					</div>

					<?php
					echo wp_kses_post(
						$render_gateway_setup_tip(
							$is_basic_tier ? __( 'Pro preview', 'wordform' ) : __( 'Guided manual setup', 'wordform' ),
							__( 'What this setup button does', 'wordform' ),
							$is_basic_tier
								? array(
									__( 'WordForm Free shows the real Authorize.Net setup structure here so you can review the full flow before upgrading.', 'wordform' ),
									__( 'Upgrade unlocks the same fields below for API Login ID, Client Key, and Transaction Key values.', 'wordform' ),
									__( 'The setup button in Pro still opens fields only. It does not sign in to Authorize.Net or generate credentials automatically.', 'wordform' ),
								)
								: array(
									__( 'It jumps to the Authorize.Net fields below so you can enter the API Login ID, Client Key, and Transaction Key needed for Accept.js.', 'wordform' ),
									__( 'Keep Sandbox and Live credentials separate. All three values and the selected mode must come from the same merchant account environment.', 'wordform' ),
									__( 'Accept.js front-end card tokenization must run on HTTPS. Plan the final checkout test on HTTPS even if admin setup is done on localhost first.', 'wordform' ),
									__( 'This flow does not sign in to Authorize.Net or create credentials automatically. Generate the values in the merchant dashboard, save here, then run the final checkout test.', 'wordform' ),
								)
						)
					);
					?>

					<div class="wordform-payment-gateway-steps">
						<div class="wordform-payment-step-card"><strong>1</strong><span><?php esc_html_e( 'Enable Authorize.Net and keep Sandbox mode active while validating the setup.', 'wordform' ); ?></span></div>
						<div class="wordform-payment-step-card"><strong>2</strong><span><?php esc_html_e( 'Paste the API Login ID, Client Key, and Transaction Key from your merchant account.', 'wordform' ); ?></span></div>
						<div class="wordform-payment-step-card"><strong>3</strong><span><?php esc_html_e( 'Use an HTTPS front-end form for the final Accept.js checkout test because the browser tokenization step requires it.', 'wordform' ); ?></span></div>
					</div>

					<div class="wordform-payment-gateway-overview" data-payment-overview="authorizenet">
						<div class="wordform-payment-gateway-summary-grid">
							<div class="wordform-payment-gateway-summary-card">
								<span class="wordform-payment-gateway-summary-label"><?php esc_html_e( 'Connection status', 'wordform' ); ?></span>
								<strong class="wordform-payment-gateway-summary-value" data-payment-summary-status><?php echo esc_html( $is_basic_tier ? __( 'Pro preview', 'wordform' ) : ( $authorizenet_ready ? __( 'Configured', 'wordform' ) : __( 'Needs setup', 'wordform' ) ) ); ?></strong>
							</div>
							<div class="wordform-payment-gateway-summary-card">
								<span class="wordform-payment-gateway-summary-label"><?php esc_html_e( 'Mode', 'wordform' ); ?></span>
								<strong class="wordform-payment-gateway-summary-value" data-payment-summary-mode><?php echo esc_html( ! empty( $payment_options['authorizenet_mode'] ) ? strtoupper( $payment_options['authorizenet_mode'] ) : __( 'SANDBOX', 'wordform' ) ); ?></strong>
							</div>
							<div class="wordform-payment-gateway-summary-card">
								<span class="wordform-payment-gateway-summary-label"><?php esc_html_e( 'Required keys added', 'wordform' ); ?></span>
								<strong class="wordform-payment-gateway-summary-value" data-payment-summary-progress><?php echo esc_html( $authorizenet_complete_count . ' / 3' ); ?></strong>
							</div>
							<div class="wordform-payment-gateway-summary-card">
								<span class="wordform-payment-gateway-summary-label"><?php esc_html_e( 'Checkout surface', 'wordform' ); ?></span>
								<strong class="wordform-payment-gateway-summary-value" data-payment-summary-optional><?php echo esc_html( $is_basic_tier ? __( 'Unlock in Pro', 'wordform' ) : __( 'HTTPS required', 'wordform' ) ); ?></strong>
							</div>
						</div>

						<div class="wordform-payment-gateway-checklist-shell">
							<div class="wordform-payment-gateway-checklist-copy">
								<strong><?php esc_html_e( 'Checklist before saving', 'wordform' ); ?></strong>
								<p data-payment-next-step>
									<?php
									if ( $is_basic_tier ) {
										esc_html_e( 'Authorize.Net is available in WordForm Pro. Upgrade to unlock the same guided setup, then complete the final checkout test on HTTPS.', 'wordform' );
									} elseif ( ! $authorizenet_ready ) {
										esc_html_e( 'Add the three required Authorize.Net credentials, then save once. Final browser payment tests must run on HTTPS because Accept.js will reject HTTP.', 'wordform' );
									} else {
										esc_html_e( 'Authorize.Net credentials are configured. Finish the final sandbox checkout test from an HTTPS form URL before using it in production.', 'wordform' );
									}
									?>
								</p>
							</div>
							<ul class="wordform-payment-checklist">
								<?php
								echo wp_kses_post( $render_gateway_check_item( 'yes' === $payment_options['authorizenet_enabled'], __( 'Gateway enabled', 'wordform' ), 'enabled' ) );
								echo wp_kses_post( $render_gateway_check_item( ! empty( $payment_options['authorizenet_api_login_id'] ), __( 'API Login ID added', 'wordform' ), 'primary' ) );
								echo wp_kses_post( $render_gateway_check_item( ! empty( $payment_options['authorizenet_client_key'] ), __( 'Client Key added', 'wordform' ), 'secondary' ) );
								echo wp_kses_post( $render_gateway_check_item( ! empty( $payment_options['authorizenet_transaction_key'] ), __( 'Transaction Key added', 'wordform' ), 'tertiary' ) );
								?>
							</ul>
						</div>
					</div>

					<div class="wordform-payment-settings-grid">
						<div class="wordform-payment-settings-field">
							<label for="wordformAuthorizeNetEnabled"><?php esc_html_e( 'Enable Authorize.Net', 'wordform' ); ?></label>
							<div class="wordform-payment-settings-inline">
								<input type="checkbox" id="wordformAuthorizeNetEnabled" name="wordform-authorizenet-enabled" <?php checked( 'yes', $payment_options['authorizenet_enabled'] ); ?> <?php disabled( $is_basic_tier ); ?> />
								<span><?php esc_html_e( 'Allow Authorize.Net payment elements to process payments.', 'wordform' ); ?></span>
							</div>
						</div>
						<div class="wordform-payment-settings-field">
							<label for="wordformAuthorizeNetMode"><?php esc_html_e( 'Authorize.Net Mode', 'wordform' ); ?></label>
							<select id="wordformAuthorizeNetMode" name="wordform-authorizenet-mode" <?php disabled( $is_basic_tier ); ?>>
								<option value="sandbox" <?php selected( $payment_options['authorizenet_mode'], 'sandbox' ); ?>><?php esc_html_e( 'Sandbox', 'wordform' ); ?></option>
								<option value="live" <?php selected( $payment_options['authorizenet_mode'], 'live' ); ?>><?php esc_html_e( 'Live', 'wordform' ); ?></option>
							</select>
						</div>
						<div class="wordform-payment-settings-field">
							<label for="wordformAuthorizeNetApiLoginId"><?php esc_html_e( 'API Login ID', 'wordform' ); ?></label>
							<input type="text" id="wordformAuthorizeNetApiLoginId" name="wordform-authorizenet-api-login-id" value="<?php echo esc_attr( $payment_options['authorizenet_api_login_id'] ); ?>" <?php disabled( $is_basic_tier ); ?> />
						</div>
						<div class="wordform-payment-settings-field">
							<label for="wordformAuthorizeNetClientKey"><?php esc_html_e( 'Client Key', 'wordform' ); ?></label>
							<input type="text" id="wordformAuthorizeNetClientKey" name="wordform-authorizenet-client-key" value="<?php echo esc_attr( $payment_options['authorizenet_client_key'] ); ?>" <?php disabled( $is_basic_tier ); ?> />
						</div>
						<div class="wordform-payment-settings-field wordform-payment-settings-field-full">
							<label for="wordformAuthorizeNetTransactionKey"><?php esc_html_e( 'Transaction Key', 'wordform' ); ?></label>
							<input type="text" id="wordformAuthorizeNetTransactionKey" name="wordform-authorizenet-transaction-key" value="<?php echo esc_attr( $payment_options['authorizenet_transaction_key'] ); ?>" <?php disabled( $is_basic_tier ); ?> />
						</div>
					</div>
				</section>
			</div>
		</div>

		<div class="sc-wordform-general-actions sc-wordform-payment-settings-actions">
			<button type="submit" class="button button-primary button-large"><?php esc_html_e( 'Save Payment Settings', 'wordform' ); ?></button>
			<div class="sc-wordform-payment-tab-update-info"></div>
		</div>
	</form>
</div>
