<?php
/**
 * Registration Form Template
 * 
 * This template renders the main registration form container and structure.
 * It includes the form fields, honeypot field (if enabled), and submit button.
 * 
 * @package WPEventGenius
 * @since 1.0.0
 * 
 * @var \WPEventGenius\Event\Post $event_post The event post object
 */

use WPEventGenius\Common\Utils\Settings;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
// In edit mode (registration has id), keep the passed registration_data; otherwise apply prefill filter.
$registration_data = isset( $registration_data ) && is_array( $registration_data ) ? $registration_data : array();
if ( empty( $registration_data['id'] ) ) {
	$registration_data = apply_filters( 'evge_registration_data', $registration_data, $event_post );
}
$flags = apply_filters( 'evge_registration_form_flags', array(), $event_post );

$is_modal_context = isset( $is_modal_context ) && $is_modal_context === true;
$form_id          = EVGE()->registration_form_service()->registration_form_dom_id(
	$is_modal_context,
	isset( $form_id ) ? $form_id : null
);
$submit_button_id = $form_id . '-submit';
?>

<div class="evge-registration-form-fields" 
	role="region" 
	aria-label="<?php esc_attr_e( 'Event Registration Form', 'event-genius' ); ?>">
	
	<div class="evge-registration-form-inner">
		<form id="<?php echo esc_attr( $form_id ); ?>" 
			method="post" 
			action="" 
			class="evge-registration-form" 
			aria-label="<?php esc_attr_e( 'Event Registration', 'event-genius' ); ?>">
			
			<?php 
			// Use EventPost method to get the appropriate event ID input(s)
			// This handles both single and bulk registration automatically
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			echo $event_post->get_event_id_input();
			?>
			<input type="hidden" 
				name="action" 
				value="evge_registration_form_submit">

			<?php do_action( 'evge_registration_form_before_fields', $event_post, $registration_data ); ?>

			<div class="evge-form-fields-information" 
				role="group" 
				aria-label="<?php esc_attr_e( 'Registration Information', 'event-genius' ); ?>">
				<div class="evge-main-registration evge-form-fields" data-guest-number="main">
					<?php
					// Pass bulk registration types into flags so dynamic_fields callbacks can output hidden inputs (per event: one or more type_id/quantity rows for simple_count).
					$flags['bulk_registration_types'] = isset( $bulk_registration_types ) && is_array( $bulk_registration_types ) ? $bulk_registration_types : array();
					$flags['bulk_registration_type_rows'] = isset( $bulk_registration_type_rows ) && is_array( $bulk_registration_type_rows ) ? $bulk_registration_type_rows : array();
					/**
					 * Generic hook for dynamic fields rendered alongside the main registration fields.
					 * 
					 * Moving this inside the main-registration wrapper makes it easy to show/hide
					 * dynamic UI (like the registration type selector) together with the main fields.
					 *
					 * @param \WPEventGenius\Event\Post $event_post        Event post object.
					 * @param array                     $registration_data Prefill/registration data.
					 * @param array                     $flags             Flags from evge_registration_form_flags; may include bulk_registration_types for bulk form.
					 */
					do_action( 'evge_registration_form_dynamic_fields', $event_post, $registration_data, $flags );

					// Disable wrapper output since form.php already provides the wrapper
					$flags['output_wrapper'] = false;
					$event_post->display_form_fields( $registration_data, $flags ); 
					?>
					<?php $event_post->maybe_display_honeypot(); ?>
				</div>
				<?php do_action( 'evge_registration_form_after_fields', $event_post, $registration_data ); ?>
			</div>

			<?php 
			// Only show submit button in standalone forms, not in modal context
			if ( ! $is_modal_context ) : ?>
			<div class="evge-form-button-wrapper">
				<button id="<?php echo esc_attr( $submit_button_id ); ?>" 
					class="evge-form-button evge-button evge-green-button" 
					type="submit"
					aria-label="<?php esc_attr_e( 'Submit Registration', 'event-genius' ); ?>"
					<?php 
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
					echo $event_post->submit_button_style_att(); ?>>
					<?php 
					$submit_button_text = apply_filters( 'evge_registration_form_submit_button_text', esc_html( Settings::get( 'form_submit_button_text' ) ), $event_post, $registration_data );
					echo esc_html( $submit_button_text ); 
					?>
				</button>
			</div>
			<?php endif; ?>
		</form>
	</div>

</div>
