<?php
/**
 * Admin view: create an event, one question at a time.
 *
 * The edit screen has sixty-six fields, and confronting somebody with all of
 * them before the event has a name is how a plugin gets abandoned on the first
 * evening. The wizard asks the four things an event cannot exist without, in
 * the order somebody actually knows them, and hands over to the full editor
 * afterwards.
 *
 * Two decisions worth stating. The draft is created at the end of step one, so
 * nothing typed is ever lost to a closed tab or a wandering attention — every
 * later step edits a real row. And every step after the first can be skipped:
 * a price and a form can be decided tomorrow, a date cannot.
 *
 * @package BitForgeNet_Guest_Tickets
 *
 * @var object|null $event Draft being built, or null on the first step.
 * @var int         $step  Which step to show.
 */

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

$venues     = BFNET_Venue::all( '' );
$templates  = BFNET_Template::all();
$currencies = BFNET_Util::currencies();
$step       = max( 1, min( 4, (int) $step ) );

$bfnet_dt = static function ( $value ) {
	if ( empty( $value ) || '0000-00-00 00:00:00' === $value ) {
		return '';
	}
	return gmdate( 'Y-m-d\TH:i', strtotime( $value ) );
};

$bfnet_steps = array(
	1 => __( 'The event', 'bitforgenet-guest-tickets' ),
	2 => __( 'Tickets', 'bitforgenet-guest-tickets' ),
	3 => __( 'The form', 'bitforgenet-guest-tickets' ),
	4 => __( 'Ready', 'bitforgenet-guest-tickets' ),
);

$bfnet_id = $event ? (int) $event->id : 0;
?>
<div class="wrap bfnet-admin">

	<?php
	$bfnet_title    = __( 'New event', 'bitforgenet-guest-tickets' );
	$bfnet_subtitle = __( 'Four questions. Everything else can wait — and can be changed afterwards.', 'bitforgenet-guest-tickets' );
	$bfnet_actions  = '<a class="button" href="' . esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-events' ) ) . '">' . esc_html__( 'Cancel', 'bitforgenet-guest-tickets' ) . '</a>';
	include BFNET_PATH . 'admin/views/partial-masthead.php';
	?>

	<ol class="bfnet-steps">
		<?php foreach ( $bfnet_steps as $bfnet_n => $bfnet_label ) : ?>
			<li class="bfnet-steps__item <?php echo $bfnet_n === $step ? 'is-here' : ( $bfnet_n < $step ? 'is-done' : '' ); ?>">
				<span class="bfnet-steps__dot"><?php echo $bfnet_n < $step ? '&#10003;' : (int) $bfnet_n; ?></span>
				<span class="bfnet-steps__label"><?php echo esc_html( $bfnet_label ); ?></span>
			</li>
		<?php endforeach; ?>
	</ol>

	<form method="post" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>">
		<?php wp_nonce_field( 'bfnet_wizard', '_bfnet_nonce' ); ?>
		<input type="hidden" name="page" value="bitforgenet-guest-tickets-events">
		<input type="hidden" name="bfnet_do" value="wizard">
		<input type="hidden" name="event_id" value="<?php echo (int) $bfnet_id; ?>">
		<input type="hidden" name="step" value="<?php echo (int) $step; ?>">

		<?php if ( 1 === $step ) : ?>

			<div class="bfnet-panel bfnet-panel--wizard">
				<div class="bfnet-panel__body">
					<div class="bfnet-field">
						<label for="w-title"><?php esc_html_e( 'What is the event called?', 'bitforgenet-guest-tickets' ); ?></label>
						<input type="text" id="w-title" name="title" required autofocus
							value="<?php echo esc_attr( $event ? $event->title : '' ); ?>"
							placeholder="<?php esc_attr_e( 'Opening Night', 'bitforgenet-guest-tickets' ); ?>">
						<p class="description"><?php esc_html_e( 'This is what guests read on the booking page and on their ticket.', 'bitforgenet-guest-tickets' ); ?></p>
					</div>

					<div class="bfnet-row">
						<div class="bfnet-field">
							<label for="w-starts"><?php esc_html_e( 'When does it start?', 'bitforgenet-guest-tickets' ); ?></label>
							<input type="datetime-local" id="w-starts" name="starts_at" required
								value="<?php echo esc_attr( $bfnet_dt( $event ? $event->starts_at : '' ) ); ?>">
						</div>
						<div class="bfnet-field">
							<label for="w-doors"><?php esc_html_e( 'And the doors?', 'bitforgenet-guest-tickets' ); ?></label>
							<input type="datetime-local" id="w-doors" name="doors_open_at"
								value="<?php echo esc_attr( $bfnet_dt( $event ? $event->doors_open_at : '' ) ); ?>">
							<p class="description"><?php esc_html_e( 'Optional. The door station uses it to decide when to start letting people in.', 'bitforgenet-guest-tickets' ); ?></p>
						</div>
					</div>

					<div class="bfnet-field">
						<label for="w-venue"><?php esc_html_e( 'Where?', 'bitforgenet-guest-tickets' ); ?></label>
						<select id="w-venue" name="venue_id">
							<option value="0"><?php esc_html_e( '— decide later —', 'bitforgenet-guest-tickets' ); ?></option>
							<?php foreach ( $venues as $bfnet_venue ) : ?>
								<option value="<?php echo (int) $bfnet_venue->id; ?>" <?php selected( $event ? (int) $event->venue_id : 0, (int) $bfnet_venue->id ); ?>>
									<?php echo esc_html( $bfnet_venue->name ); ?>
								</option>
							<?php endforeach; ?>
						</select>
						<?php if ( ! $venues ) : ?>
							<p class="description">
								<?php esc_html_e( 'No venue yet.', 'bitforgenet-guest-tickets' ); ?>
								<a href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-venues' ) ); ?>" target="_blank"><?php esc_html_e( 'Add one', 'bitforgenet-guest-tickets' ); ?></a>
								<?php esc_html_e( '— or carry on and choose it later.', 'bitforgenet-guest-tickets' ); ?>
							</p>
						<?php endif; ?>
					</div>
				</div>
			</div>

		<?php elseif ( 2 === $step ) : ?>

			<div class="bfnet-panel bfnet-panel--wizard">
				<div class="bfnet-panel__body">
					<div class="bfnet-row">
						<div class="bfnet-field">
							<label for="w-price"><?php esc_html_e( 'How much is a ticket?', 'bitforgenet-guest-tickets' ); ?></label>
							<input type="text" id="w-price" name="price" inputmode="decimal" autofocus
								value="<?php echo esc_attr( $event ? number_format( (int) $event->price_cents / 100, 2, '.', '' ) : '' ); ?>"
								placeholder="0.00">
							<p class="description"><?php esc_html_e( 'Leave it at zero for a free night. Ticket categories come later, on the Pricing tab.', 'bitforgenet-guest-tickets' ); ?></p>
						</div>
						<div class="bfnet-field">
							<label for="w-currency"><?php esc_html_e( 'Currency', 'bitforgenet-guest-tickets' ); ?></label>
							<select id="w-currency" name="currency">
								<?php
								$bfnet_cur = $event && $event->currency ? $event->currency : (string) BFNET_Settings::get( 'currency', 'CAD' );
								foreach ( $currencies as $bfnet_code => $bfnet_meta ) :
									?>
									<option value="<?php echo esc_attr( $bfnet_code ); ?>" <?php selected( $bfnet_cur, $bfnet_code ); ?>><?php echo esc_html( $bfnet_code ); ?></option>
								<?php endforeach; ?>
							</select>
						</div>
					</div>

					<div class="bfnet-row">
						<div class="bfnet-field">
							<label for="w-capacity"><?php esc_html_e( 'How many people fit?', 'bitforgenet-guest-tickets' ); ?></label>
							<input type="number" id="w-capacity" name="capacity" min="0" class="bfnet-narrow"
								value="<?php echo (int) ( $event ? $event->capacity : 0 ); ?>">
							<p class="description"><?php esc_html_e( 'Zero means no limit. The form closes itself when the last place goes.', 'bitforgenet-guest-tickets' ); ?></p>
						</div>
						<div class="bfnet-field">
							<label for="w-max"><?php esc_html_e( 'Most tickets in one booking', 'bitforgenet-guest-tickets' ); ?></label>
							<input type="number" id="w-max" name="max_per_order" min="1" max="50" class="bfnet-narrow"
								value="<?php echo (int) ( $event ? $event->max_per_order : (int) BFNET_Settings::get( 'default_max_per_order', 10 ) ); ?>">
						</div>
					</div>
				</div>
			</div>

		<?php elseif ( 3 === $step ) : ?>

			<div class="bfnet-panel bfnet-panel--wizard">
				<div class="bfnet-panel__body">
					<div class="bfnet-field">
						<label>
							<input type="checkbox" name="collect_guest_names" value="1" <?php checked( $event ? (int) $event->collect_guest_names : 1, 1 ); ?>>
							<?php esc_html_e( 'Ask for the name of every guest', 'bitforgenet-guest-tickets' ); ?>
						</label>
						<p class="description"><?php esc_html_e( 'Recommended. Each guest then gets a personal ticket with its own QR code, and one code cannot let five people in.', 'bitforgenet-guest-tickets' ); ?></p>
					</div>

					<div class="bfnet-field">
						<label for="w-template"><?php esc_html_e( 'Which ticket design?', 'bitforgenet-guest-tickets' ); ?></label>
						<select id="w-template" name="template_id">
							<option value="0"><?php esc_html_e( 'Use the default design', 'bitforgenet-guest-tickets' ); ?></option>
							<?php foreach ( $templates as $bfnet_template ) : ?>
								<option value="<?php echo (int) $bfnet_template->id; ?>" <?php selected( $event ? (int) $event->template_id : 0, (int) $bfnet_template->id ); ?>>
									<?php echo esc_html( $bfnet_template->name ); ?>
								</option>
							<?php endforeach; ?>
						</select>
					</div>

					<div class="bfnet-field">
						<label for="w-whatsapp"><?php esc_html_e( 'WhatsApp number for payments', 'bitforgenet-guest-tickets' ); ?></label>
						<input type="tel" id="w-whatsapp" name="whatsapp_number"
							value="<?php echo esc_attr( $event && $event->whatsapp_number ? $event->whatsapp_number : (string) BFNET_Settings::get( 'whatsapp_number', '' ) ); ?>"
							placeholder="+44 7700 900123">
						<p class="description"><?php esc_html_e( 'Leave it empty to use the number in your settings.', 'bitforgenet-guest-tickets' ); ?></p>
					</div>
				</div>
			</div>

		<?php else : ?>

			<div class="bfnet-panel bfnet-panel--wizard">
				<div class="bfnet-panel__body">
					<h2 class="bfnet-wizard__done"><?php echo esc_html( $event->title ); ?></h2>

					<dl class="bfnet-summary">
						<dt><?php esc_html_e( 'Starts', 'bitforgenet-guest-tickets' ); ?></dt>
						<dd><?php echo esc_html( BFNET_Util::event_date( $event, 'starts_at', 'l j F Y, H:i' ) ); ?></dd>

						<dt><?php esc_html_e( 'Venue', 'bitforgenet-guest-tickets' ); ?></dt>
						<dd>
							<?php
							$bfnet_venue = $event->venue_id ? BFNET_Venue::get( (int) $event->venue_id ) : null;
							echo esc_html( $bfnet_venue ? $bfnet_venue->name : __( 'not chosen yet', 'bitforgenet-guest-tickets' ) );
							?>
						</dd>

						<dt><?php esc_html_e( 'Ticket', 'bitforgenet-guest-tickets' ); ?></dt>
						<dd><?php echo esc_html( BFNET_Util::money( $event->price_cents, $event->currency ) ); ?></dd>

						<dt><?php esc_html_e( 'Capacity', 'bitforgenet-guest-tickets' ); ?></dt>
						<dd><?php echo esc_html( $event->capacity ? (string) (int) $event->capacity : __( 'no limit', 'bitforgenet-guest-tickets' ) ); ?></dd>

						<dt><?php esc_html_e( 'Booking link', 'bitforgenet-guest-tickets' ); ?></dt>
						<dd><code class="bfnet-code"><?php echo esc_html( BFNET_Util::event_url( $event ) ); ?></code></dd>
					</dl>

					<div class="bfnet-field">
						<label for="w-status"><?php esc_html_e( 'Open it now, or keep it as a draft?', 'bitforgenet-guest-tickets' ); ?></label>
						<select id="w-status" name="status">
							<?php foreach ( BFNET_Util::event_statuses() as $bfnet_key => $bfnet_label ) : ?>
								<option value="<?php echo esc_attr( $bfnet_key ); ?>" <?php selected( 'draft', $bfnet_key ); ?>><?php echo esc_html( $bfnet_label ); ?></option>
							<?php endforeach; ?>
						</select>
						<p class="description"><?php esc_html_e( 'Only "Registration open" accepts bookings. Everything else on the event can still be changed afterwards.', 'bitforgenet-guest-tickets' ); ?></p>
					</div>
				</div>
			</div>

		<?php endif; ?>

		<div class="bfnet-savebar bfnet-savebar--wizard">
			<?php if ( $step > 1 ) : ?>
				<button class="button" name="back" value="1" formnovalidate><?php esc_html_e( 'Back', 'bitforgenet-guest-tickets' ); ?></button>
			<?php endif; ?>

			<button class="button button-primary button-large">
				<?php echo esc_html( 4 === $step ? __( 'Finish', 'bitforgenet-guest-tickets' ) : __( 'Continue', 'bitforgenet-guest-tickets' ) ); ?>
			</button>

			<?php if ( $step > 1 && $step < 4 ) : ?>
				<button class="button button-link" name="skip" value="1" formnovalidate><?php esc_html_e( 'Skip this step', 'bitforgenet-guest-tickets' ); ?></button>
			<?php endif; ?>

			<span class="bfnet-savebar__note">
				<?php
				echo esc_html(
					1 === $step
						? __( 'Saved as a draft the moment you continue — nothing is lost if you stop here.', 'bitforgenet-guest-tickets' )
						: __( 'Already saved as a draft. You can leave and come back.', 'bitforgenet-guest-tickets' )
				);
				?>
			</span>
		</div>
	</form>
</div>
