<?php
/**
 * Template for displaying custom warranty checkout fields.
 *
 * Warranty Fields Template
 *
 * @package WarrantyCart
 * @version 1.0.0
 */

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound,WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Email template variables and WooCommerce hooks are local scope and don't need prefixing

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

/**
 * Checkout fields configuration.
 *
 * @var array $fields Checkout fields configuration
 */

if ( empty( $fields ) ) {
	return;
}
?>
<div class="warranty-checkout-fields">
	<?php foreach ( $fields as $field ) : ?>
		<?php
		$field_name  = 'warranty_' . esc_attr( $field['id'] );
		$field_id    = 'warranty-field-' . esc_attr( $field['id'] );
		$field_class = 'warranty-field input-text';
		$required    = $field['required'] ? ' <span class="required">*</span>' : '';
		$value       = isset( $_POST[ $field_name ] ) ? sanitize_text_field( wp_unslash( $_POST[ $field_name ] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verification handled by WooCommerce checkout
		?>

		<p class="form-row <?php echo esc_attr( 'textarea' === $field['type'] ? 'form-row-wide' : 'form-row-first' ); ?>" id="<?php echo esc_attr( $field_id ); ?>_field">
			<label for="<?php echo esc_attr( $field_id ); ?>" class="warranty-field-label">
				<?php echo esc_html( $field['label'] ) . wp_kses_post( $required ); ?>
			</label>

			<?php if ( 'text' === $field['type'] || 'email' === $field['type'] || 'number' === $field['type'] ) : ?>
				<input
					type="<?php echo esc_attr( $field['type'] ); ?>"
					class="<?php echo esc_attr( $field_class ); ?>"
					name="<?php echo esc_attr( $field_name ); ?>"
					id="<?php echo esc_attr( $field_id ); ?>"
					placeholder="<?php echo esc_attr( $field['placeholder'] ?? '' ); ?>"
					value="<?php echo esc_attr( $value ); ?>"
					<?php echo $field['required'] ? 'required' : ''; ?>
				/>

			<?php elseif ( 'textarea' === $field['type'] ) : ?>
				<textarea
					class="<?php echo esc_attr( $field_class ); ?>"
					name="<?php echo esc_attr( $field_name ); ?>"
					id="<?php echo esc_attr( $field_id ); ?>"
					placeholder="<?php echo esc_attr( $field['placeholder'] ?? '' ); ?>"
					rows="3"
					<?php echo $field['required'] ? 'required' : ''; ?>
				><?php echo esc_textarea( $value ); ?></textarea>

			<?php elseif ( 'select' === $field['type'] && ! empty( $field['options'] ) ) : ?>
				<select
					class="<?php echo esc_attr( $field_class ); ?>"
					name="<?php echo esc_attr( $field_name ); ?>"
					id="<?php echo esc_attr( $field_id ); ?>"
					<?php echo $field['required'] ? 'required' : ''; ?>
				>
					<option value=""><?php esc_html_e( 'Select an option...', 'warranty-cart' ); ?></option>
					<?php foreach ( $field['options'] as $option ) : ?>
						<option value="<?php echo esc_attr( $option ); ?>" <?php selected( $value, $option ); ?>>
							<?php echo esc_html( $option ); ?>
						</option>
					<?php endforeach; ?>
				</select>

			<?php elseif ( 'checkbox' === $field['type'] ) : ?>
				<?php if ( ! empty( $field['options'] ) ) : ?>
					<?php foreach ( $field['options'] as $index => $option ) : ?>
						<label class="checkbox">
							<input
								type="checkbox"
								class="<?php echo esc_attr( $field_class ); ?>"
								name="<?php echo esc_attr( $field_name ); ?>[]"
								id="<?php echo esc_attr( $field_id . '_' . $index ); ?>"
								value="<?php echo esc_attr( $option ); ?>"
								<?php checked( in_array( $option, (array) $value, true ) ); ?>
								<?php echo $field['required'] ? 'required' : ''; ?>
							/>
							<?php echo esc_html( $option ); ?>
						</label>
					<?php endforeach; ?>
				<?php else : ?>
					<label class="checkbox">
						<input
							type="checkbox"
							class="<?php echo esc_attr( $field_class ); ?>"
							name="<?php echo esc_attr( $field_name ); ?>"
							id="<?php echo esc_attr( $field_id ); ?>"
							value="1"
							<?php checked( $value ); ?>
							<?php echo $field['required'] ? 'required' : ''; ?>
						/>
						<?php echo esc_html( $field['label'] ); ?>
					</label>
				<?php endif; ?>

			<?php elseif ( 'radio' === $field['type'] && ! empty( $field['options'] ) ) : ?>
				<?php foreach ( $field['options'] as $index => $option ) : ?>
					<label class="radio">
						<input
							type="radio"
							class="<?php echo esc_attr( $field_class ); ?>"
							name="<?php echo esc_attr( $field_name ); ?>"
							id="<?php echo esc_attr( $field_id . '_' . $index ); ?>"
							value="<?php echo esc_attr( $option ); ?>"
							<?php checked( $value, $option ); ?>
							<?php echo $field['required'] ? 'required' : ''; ?>
						/>
						<?php echo esc_html( $option ); ?>
					</label>
				<?php endforeach; ?>
			<?php endif; ?>
		</p>
	<?php endforeach; ?>
</div>


<!-- phpcs:enable -->
