<?php
/**
 * The door station markup.
 *
 * Rendered by BFNET_Door::render_station() inside its own bare document. Nothing
 * from the theme or from wp-admin is present, which is the entire point.
 *
 * @package BitForgeNet_Guest_Tickets
 * @since   1.2.0
 *
 * @var object $event Event row.
 * @var array  $stats Live counters.
 * @var array  $gates Gate names.
 */

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
?>
<header class="d-bar">
	<div class="d-bar__id">
		<strong><?php echo esc_html( $event->title ); ?></strong>
		<span><?php echo esc_html( BFNET_Util::event_date( $event, 'starts_at', 'j M · H:i' ) ); ?></span>
	</div>

	<div class="d-bar__count">
		<b data-d-inside><?php echo esc_html( number_format_i18n( $stats['tickets_checked_in'] ) ); ?></b>
		<span>/ <i data-d-expected><?php echo esc_html( number_format_i18n( $stats['seats_paid'] ) ); ?></i> <?php esc_html_e( 'in', 'bitforgenet-guest-tickets' ); ?></span>
	</div>

	<?php if ( count( $gates ) > 1 ) : ?>
		<select class="d-gate" data-d-gate aria-label="<?php esc_attr_e( 'Gate', 'bitforgenet-guest-tickets' ); ?>">
			<?php foreach ( $gates as $gate ) : ?>
				<option value="<?php echo esc_attr( $gate ); ?>"><?php echo esc_html( $gate ); ?></option>
			<?php endforeach; ?>
		</select>
	<?php else : ?>
		<input type="hidden" data-d-gate value="<?php echo esc_attr( $gates ? reset( $gates ) : '' ); ?>">
	<?php endif; ?>

	<button class="d-icon" data-d-menu aria-label="<?php esc_attr_e( 'Menu', 'bitforgenet-guest-tickets' ); ?>">⋯</button>
</header>

<nav class="d-modes" role="tablist">
	<button class="d-mode is-on" data-d-tab="camera"><?php esc_html_e( 'Camera', 'bitforgenet-guest-tickets' ); ?></button>
	<button class="d-mode" data-d-tab="photo"><?php esc_html_e( 'Photo', 'bitforgenet-guest-tickets' ); ?></button>
	<button class="d-mode" data-d-tab="keypad"><?php esc_html_e( 'Keypad', 'bitforgenet-guest-tickets' ); ?></button>
</nav>

<main class="d-main">

	<section class="d-pane is-on" data-d-pane="camera">
		<div class="d-stage">
			<!--
				The preview is a canvas we paint ourselves, not a <video> element.
				A stream can be live while the video element never paints — that is
				the black rectangle. Drawing the frames by hand means what the
				decoder sees is exactly what the agent sees.
			-->
			<canvas class="d-view" data-d-view width="720" height="960"></canvas>
			<video class="d-hidden-video" data-d-video playsinline muted autoplay webkit-playsinline></video>
			<div class="d-reticle" aria-hidden="true"></div>
			<p class="d-status" data-d-status><?php esc_html_e( 'Starting the camera…', 'bitforgenet-guest-tickets' ); ?></p>
		</div>

		<div class="d-actions">
			<button class="d-btn d-btn--primary" data-d-start hidden><?php esc_html_e( 'Start the camera', 'bitforgenet-guest-tickets' ); ?></button>
			<button class="d-btn" data-d-torch hidden><?php esc_html_e( 'Light', 'bitforgenet-guest-tickets' ); ?></button>
			<button class="d-btn" data-d-flip hidden><?php esc_html_e( 'Flip', 'bitforgenet-guest-tickets' ); ?></button>
		</div>

		<!--
			Phones are unreliable about which lens "environment" means, especially
			with three rear cameras. Once permission is granted the labels become
			readable, so the agent gets the list and the final say.
		-->
		<select class="d-picker" data-d-camera hidden aria-label="<?php esc_attr_e( 'Which camera', 'bitforgenet-guest-tickets' ); ?>"></select>

		<p class="d-note" data-d-note></p>
	</section>

	<section class="d-pane" data-d-pane="photo">
		<div class="d-photo">
			<p><?php esc_html_e( 'Opens the phone’s own camera app and reads the QR from the picture.', 'bitforgenet-guest-tickets' ); ?></p>
			<p class="d-note"><?php esc_html_e( 'Nothing can block this route — no permission, no policy, no driver. Use it if the live camera will not start.', 'bitforgenet-guest-tickets' ); ?></p>
			<button class="d-btn d-btn--primary d-btn--big" data-d-photo><?php esc_html_e( 'Take a photo', 'bitforgenet-guest-tickets' ); ?></button>
			<input type="file" accept="image/*" capture="environment" data-d-photo-input hidden>
			<canvas data-d-photo-view class="d-photo__view" hidden></canvas>
		</div>
	</section>

	<section class="d-pane" data-d-pane="keypad">
		<form class="d-keypad" data-d-keyform>
			<label for="d-code"><?php esc_html_e( 'Ticket number', 'bitforgenet-guest-tickets' ); ?></label>
			<input type="text" id="d-code" data-d-code autocomplete="off" autocapitalize="characters"
				autocorrect="off" spellcheck="false" maxlength="14"
				placeholder="<?php esc_attr_e( 'K7Q2X9', 'bitforgenet-guest-tickets' ); ?>">
			<p class="d-note">
				<?php
				printf(
					/* translators: %s: event name. */
					esc_html__( 'Just the number printed on the ticket — nothing in front of it. This station is signed in to %s, so the number alone is enough. A USB or Bluetooth barcode reader types straight in here.', 'bitforgenet-guest-tickets' ),
					esc_html( $event->title )
				);
				?>
			</p>

			<?php
			// The alphabet a ticket number is drawn from, laid out as a pad. At a
			// door this beats the phone keyboard: nothing slides up over the
			// screen, and there is no character on it that cannot appear.
			$bfnet_keys = str_split( BFNET_Ticket::NUMBER_ALPHABET );
			?>
			<div class="d-pad d-pad--wide">
				<?php foreach ( $bfnet_keys as $bfnet_key ) : ?>
					<button type="button" class="d-key" data-d-key="<?php echo esc_attr( $bfnet_key ); ?>"><?php echo esc_html( $bfnet_key ); ?></button>
				<?php endforeach; ?>
				<button type="button" class="d-key d-key--wide" data-d-key="⌫">⌫</button>
			</div>

			<button type="submit" class="d-btn d-btn--primary d-btn--big"><?php esc_html_e( 'Check this ticket', 'bitforgenet-guest-tickets' ); ?></button>
		</form>
	</section>

	<section class="d-feed">
		<h2><?php esc_html_e( 'Tonight', 'bitforgenet-guest-tickets' ); ?></h2>
		<ul data-d-feed></ul>
	</section>
</main>

<div class="d-verdict" data-d-verdict hidden>
	<div class="d-verdict__inner">
		<div class="d-verdict__mark" data-d-mark></div>
		<p class="d-verdict__title" data-d-title></p>
		<p class="d-verdict__name" data-d-name></p>
		<p class="d-verdict__meta" data-d-meta></p>
		<button class="d-btn" data-d-override hidden><?php esc_html_e( 'Let them in anyway', 'bitforgenet-guest-tickets' ); ?></button>
		<button class="d-btn d-btn--ghost" data-d-dismiss><?php esc_html_e( 'Next guest', 'bitforgenet-guest-tickets' ); ?></button>
	</div>
</div>

<div class="d-sheet" data-d-sheet hidden>
	<div class="d-sheet__inner">
		<h2><?php esc_html_e( 'Station', 'bitforgenet-guest-tickets' ); ?></h2>
		<button class="d-btn" data-d-selftest><?php esc_html_e( 'Run the self-test', 'bitforgenet-guest-tickets' ); ?></button>
		<button class="d-btn" data-d-retry><?php esc_html_e( 'Send queued scans now', 'bitforgenet-guest-tickets' ); ?></button>
		<a class="d-btn" href="<?php echo esc_url( BFNET_Door::url( array( 'reload' => time() ) ) ); ?>"><?php esc_html_e( 'Reload the station', 'bitforgenet-guest-tickets' ); ?></a>
		<a class="d-btn d-btn--quiet" href="<?php echo esc_url( BFNET_Door::url_sub( 'out' ) ); ?>"><?php esc_html_e( 'Sign this station out', 'bitforgenet-guest-tickets' ); ?></a>
		<pre class="d-report" data-d-report hidden></pre>
		<button class="d-btn d-btn--ghost" data-d-sheet-close><?php esc_html_e( 'Close', 'bitforgenet-guest-tickets' ); ?></button>
	</div>
</div>

<div class="d-offline" data-d-offline hidden>
	<?php esc_html_e( 'Offline — scans are being saved and will be sent automatically.', 'bitforgenet-guest-tickets' ); ?>
	<b data-d-queued>0</b>
</div>
