<?php
/**
 * Create Fundraiser Form — root fundraiser selection step.
 *
 * Shown only when no root fundraiser context could be resolved from the current
 * page. The visitor picks an open root fundraiser to join, or — if allowed —
 * creates a new one.
 *
 * @var array $context  Resolved context from ContextResolver::resolve().
 *
 * @package AmbassadorHub\Fundraisers\Blocks\CreateFundraiserForm
 */

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

$ambh_roots = get_posts(
	array(
		'post_type'      => 'ambh_fundraiser',
		'post_status'    => 'publish',
		'post_parent'    => 0,
		'posts_per_page' => 50,
		'orderby'        => 'title',
		'order'          => 'ASC',
	)
);
?>

<h3 class="ambh-cff-step-title"><?php esc_html_e( 'Choose a fundraiser to join', 'ambassadorhub' ); ?></h3>

<?php if ( empty( $ambh_roots ) && ! $context['can_create_root'] ) : ?>
	<p class="ambh-cff-notice"><?php esc_html_e( 'No fundraisers are currently available.', 'ambassadorhub' ); ?></p>
<?php else : ?>

	<?php if ( ! empty( $ambh_roots ) ) : ?>
		<div class="ambh-cff-root-grid" role="list">
			<?php
			foreach ( $ambh_roots as $ambh_root ) :
				$ambh_thumb_url_raw   = get_the_post_thumbnail_url( $ambh_root->ID, 'medium' );
				$ambh_thumb_url       = $ambh_thumb_url_raw ? $ambh_thumb_url_raw : '';
				$ambh_nesting_value   = get_post_meta( $ambh_root->ID, '_ambh_teams_enabled', true ) === '1' ? '1' : '0';
				$ambh_show_fundraiser = \AmbassadorHub\Fundraisers\FundraisersHelpers::sub_fundraiser_creation_allowed( $ambh_root->ID );
				?>
				<article class="ambh-cff-root-card" role="listitem">

					<div class="ambh-cff-root-card__image">
						<?php if ( $ambh_thumb_url ) : ?>
							<img src="<?php echo esc_url( $ambh_thumb_url ); ?>"
								alt="<?php echo esc_attr( $ambh_root->post_title ); ?>"
								loading="lazy">
						<?php else : ?>
							<div class="ambh-cff-root-card__placeholder" aria-hidden="true">
								<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
									<path d="M21 19V5c0-1.1-.9-2-2-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2zM8.5 13.5l2.5 3.01L14.5 12l4.5 6H5l3.5-4.5z"/>
								</svg>
							</div>
						<?php endif; ?>
					</div>

					<div class="ambh-cff-root-card__body">
						<h4 class="ambh-cff-root-card__title"><?php echo esc_html( $ambh_root->post_title ); ?></h4>
						<div class="ambh-cff-root-card__actions">
							<button
								type="button"
								class="ambh-cff-root-option ambh-cff-root-card__btn"
								data-root-id="<?php echo esc_attr( $ambh_root->ID ); ?>"
								data-root-title="<?php echo esc_attr( $ambh_root->post_title ); ?>"
								data-nesting-enabled="<?php echo esc_attr( $ambh_nesting_value ); ?>"
								data-action="individual">
								<?php esc_html_e( 'Join', 'ambassadorhub' ); ?>
							</button>
							<?php if ( '1' === $ambh_nesting_value ) : ?>
							<button
								type="button"
								class="ambh-cff-root-option ambh-cff-root-card__btn ambh-cff-root-card__btn--secondary"
								data-root-id="<?php echo esc_attr( $ambh_root->ID ); ?>"
								data-root-title="<?php echo esc_attr( $ambh_root->post_title ); ?>"
								data-nesting-enabled="<?php echo esc_attr( $ambh_nesting_value ); ?>"
								data-action="team">
								<?php esc_html_e( 'Create a team', 'ambassadorhub' ); ?>
							</button>
							<?php endif; ?>
							<?php if ( $ambh_show_fundraiser ) : ?>
							<button
								type="button"
								class="ambh-cff-root-option ambh-cff-root-card__btn ambh-cff-root-card__btn--tertiary"
								data-root-id="<?php echo esc_attr( $ambh_root->ID ); ?>"
								data-root-title="<?php echo esc_attr( $ambh_root->post_title ); ?>"
								data-nesting-enabled="<?php echo esc_attr( $ambh_nesting_value ); ?>"
								data-action="fundraiser">
								<?php esc_html_e( 'Start a fundraiser', 'ambassadorhub' ); ?>
							</button>
							<?php endif; ?>
						</div>
					</div>

				</article>
			<?php endforeach; ?>
		</div>
	<?php endif; ?>

	<?php if ( $context['can_create_root'] ) : ?>
		<div class="ambh-cff-create-root-wrap">
			<button
				type="button"
				class="ambh-cff-root-option ambh-cff-create-root-btn"
				data-root-id="0"
				data-root-title=""
				data-create-root="1"
				data-nesting-enabled="0"
				data-action="fundraiser">
				<?php esc_html_e( '+ Create new fundraiser', 'ambassadorhub' ); ?>
			</button>
		</div>
	<?php endif; ?>

<?php endif; ?>
