<?php
/**
 * The B2B registration form.
 *
 * Rendered by both the shortcode and the block, so there is one form to keep
 * correct. Every value is escaped on output; the POST handler sanitises on the
 * way in.
 *
 * @var string $formAction
 */

namespace SoftminalB2B\Registration;

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

// This file is a template partial included in method scope; its locals are not
// global variables.
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

$errors = RegistrationForm::errors();
$posted = static function (string $key): string {
    // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Redisplaying the submitted value; the handler verifies the nonce.
    return isset($_POST['smb2b_' . $key]) ? esc_attr(sanitize_text_field(wp_unslash($_POST['smb2b_' . $key]))) : '';
};
?>
<form class="smb2b-register" method="post">
	<?php if (!empty($errors)) : ?>
		<div class="smb2b-notice smb2b-notice--error">
			<ul>
				<?php foreach ($errors as $error) : ?>
					<li><?php echo esc_html($error); ?></li>
				<?php endforeach; ?>
			</ul>
		</div>
	<?php endif; ?>

	<div class="smb2b-register__row">
		<p class="smb2b-register__field">
			<label for="smb2b_first_name"><?php esc_html_e('First name', 'softminal-b2b-wholesale-for-woocommerce'); ?> <span aria-hidden="true">*</span></label>
			<input type="text" id="smb2b_first_name" name="smb2b_first_name" value="<?php echo $posted('first_name'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in $posted. ?>" required>
		</p>
		<p class="smb2b-register__field">
			<label for="smb2b_last_name"><?php esc_html_e('Last name', 'softminal-b2b-wholesale-for-woocommerce'); ?></label>
			<input type="text" id="smb2b_last_name" name="smb2b_last_name" value="<?php echo $posted('last_name'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in $posted. ?>">
		</p>
	</div>

	<p class="smb2b-register__field">
		<label for="smb2b_email"><?php esc_html_e('Work email', 'softminal-b2b-wholesale-for-woocommerce'); ?> <span aria-hidden="true">*</span></label>
		<input type="email" id="smb2b_email" name="smb2b_email" value="<?php echo $posted('email'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in $posted. ?>" required>
		<span class="smb2b-register__help"><?php esc_html_e('We will send your account details here once approved.', 'softminal-b2b-wholesale-for-woocommerce'); ?></span>
	</p>

	<?php foreach (Applicant::fields() as $key => $field) : ?>
		<p class="smb2b-register__field">
			<label for="smb2b_<?php echo esc_attr($key); ?>">
				<?php echo esc_html($field['label']); ?>
				<?php if ($field['required']) : ?><span aria-hidden="true">*</span><?php endif; ?>
			</label>

			<?php if ($field['type'] === 'textarea') : ?>
				<textarea
					id="smb2b_<?php echo esc_attr($key); ?>"
					name="smb2b_<?php echo esc_attr($key); ?>"
					rows="3"
					<?php echo $field['required'] ? 'required' : ''; ?>
				><?php echo $posted($key); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in $posted. ?></textarea>
			<?php else : ?>
				<input
					type="<?php echo esc_attr($field['type']); ?>"
					id="smb2b_<?php echo esc_attr($key); ?>"
					name="smb2b_<?php echo esc_attr($key); ?>"
					value="<?php echo $posted($key); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in $posted. ?>"
					<?php echo $field['required'] ? 'required' : ''; ?>
				>
			<?php endif; ?>
		</p>
	<?php endforeach; ?>

	<?php
	// Honeypot. Hidden from people and from screen readers, but left in the
	// tab order's reach for bots that fill everything in.
	?>
	<div class="smb2b-register__hp" aria-hidden="true">
		<label for="<?php echo esc_attr(RegistrationForm::honeypotName()); ?>"><?php esc_html_e('Leave this field empty', 'softminal-b2b-wholesale-for-woocommerce'); ?></label>
		<input
			type="text"
			id="<?php echo esc_attr(RegistrationForm::honeypotName()); ?>"
			name="<?php echo esc_attr(RegistrationForm::honeypotName()); ?>"
			value=""
			tabindex="-1"
			autocomplete="off"
		>
	</div>

	<input type="hidden" name="<?php echo esc_attr(RegistrationForm::timestampName()); ?>" value="<?php echo esc_attr((string) time()); ?>">
	<?php echo RegistrationForm::nonceField(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_nonce_field returns escaped markup. ?>

	<p class="smb2b-register__actions">
		<button type="submit" name="smb2b_register_submit" value="1" class="button">
			<?php esc_html_e('Apply for a trade account', 'softminal-b2b-wholesale-for-woocommerce'); ?>
		</button>
	</p>
</form>
