<?php
/**
 * Public view: post-submission confirmation.
 *
 * @package BitForgeNet_Guest_Tickets
 *
 * @var object      $order Order row.
 * @var object|null $event Event row.
 */

defined( 'ABSPATH' ) || exit;

// This file is a template: it is included from a method -- never at file
// scope -- so everything declared here belongs to that method's variable
// scope and not to the global one. PHPCS reads each file on its own and has
// no way to see where the include happens, so it reads these as globals.
// Stated once, for the whole file, per this plugin's suppression rule.
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

$bfnet_attendees = BFNET_Order::attendees( $order->id );
$bfnet_whatsapp  = ( $event && $event->whatsapp_number ) ? $event->whatsapp_number : (string) BFNET_Settings::get( 'whatsapp_number', '' );
$bfnet_wa_link   = '';

if ( $bfnet_whatsapp ) {
	$bfnet_wa_link = 'https://wa.me/' . preg_replace( '/\D/', '', $bfnet_whatsapp ) . '?text=' . rawurlencode(
		sprintf(
			/* translators: 1: order reference, 2: event title. */
			__( 'Hello, I would like to pay for reservation %1$s (%2$s).', 'bitforgenet-guest-tickets' ),
			$order->reference,
			$event ? $event->title : ''
		)
	);
}
?>
<div class="bfnet">
	<div class="bfnet-confirm">
		<div class="bfnet-confirm__mark" aria-hidden="true">
			<svg viewBox="0 0 52 52" width="52" height="52"><circle cx="26" cy="26" r="24" fill="none" stroke="currentColor" stroke-width="2"/><path fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" d="M15 27l8 8 15-16"/></svg>
		</div>

		<h2><?php esc_html_e( 'Your place is reserved', 'bitforgenet-guest-tickets' ); ?></h2>

		<p class="bfnet-confirm__lead"><?php echo esc_html( (string) BFNET_Settings::get( 'success_message', '' ) ); ?></p>

		<div class="bfnet-reference">
			<span><?php esc_html_e( 'Your reference', 'bitforgenet-guest-tickets' ); ?></span>
			<strong><?php echo esc_html( $order->reference ); ?></strong>
		</div>

		<dl class="bfnet-facts bfnet-facts--compact">
			<?php if ( $event ) : ?>
				<div class="bfnet-fact">
					<dt><?php esc_html_e( 'Event', 'bitforgenet-guest-tickets' ); ?></dt>
					<dd><?php echo esc_html( $event->title ); ?></dd>
				</div>
				<div class="bfnet-fact">
					<dt><?php esc_html_e( 'Date', 'bitforgenet-guest-tickets' ); ?></dt>
					<dd><?php echo esc_html( BFNET_Util::date( $event->starts_at, 'l j F Y — H:i' ) ); ?></dd>
				</div>
			<?php endif; ?>
			<div class="bfnet-fact">
				<dt><?php esc_html_e( 'Guests', 'bitforgenet-guest-tickets' ); ?></dt>
				<dd>
					<?php
					$bfnet_names = array();
					foreach ( $bfnet_attendees as $bfnet_attendee ) {
						$bfnet_names[] = BFNET_Util::full_name( $bfnet_attendee->first_name, $bfnet_attendee->last_name );
					}
					echo esc_html( implode( ', ', $bfnet_names ) );
					?>
				</dd>
			</div>
			<div class="bfnet-fact">
				<dt><?php esc_html_e( 'Total to pay', 'bitforgenet-guest-tickets' ); ?></dt>
				<dd><strong><?php echo esc_html( BFNET_Util::money( $order->total_cents, $order->currency ) ); ?></strong></dd>
			</div>
		</dl>

		<?php if ( $bfnet_wa_link ) : ?>
			<a class="bfnet-btn bfnet-btn--lg" href="<?php echo esc_url( $bfnet_wa_link ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Contact us on WhatsApp to pay', 'bitforgenet-guest-tickets' ); ?></a>
		<?php endif; ?>

		<p class="bfnet-fineprint"><?php esc_html_e( 'A confirmation email is on its way. Your tickets — one QR code per guest — arrive by email the moment your payment is validated.', 'bitforgenet-guest-tickets' ); ?></p>
	</div>
</div>
