<?php
/**
 * Admin view: single booking — validate the payment and manage its tickets.
 *
 * @package BitForgeNet_Guest_Tickets
 *
 * @var object $order Order 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

$event     = BFNET_Event::get( $order->event_id );
$attendees = BFNET_Order::attendees( (int) $order->id );
$tickets   = BFNET_Order::tickets( (int) $order->id );
$is_paid   = 'paid' === $order->payment_status;

$tickets_by_attendee = array();
foreach ( $tickets as $ticket ) {
	$tickets_by_attendee[ (int) $ticket->attendee_id ] = $ticket;
}

$whatsapp = preg_replace( '/\D/', '', $order->whatsapp ? $order->whatsapp : $order->phone );
?>
<div class="wrap bfnet-admin">

	<?php
	$bfnet_title    = BFNET_Util::full_name( $order->first_name, $order->last_name );
	$bfnet_subtitle = sprintf( '%s · %s', $order->reference, $event ? $event->title : __( 'event deleted', 'bitforgenet-guest-tickets' ) );
	$bfnet_actions  = '<a class="button" href="' . esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-orders', array( 'event' => (int) $order->event_id ) ) ) . '">&larr; ' . esc_html__( 'Back to subscribers', 'bitforgenet-guest-tickets' ) . '</a>';
	include BFNET_PATH . 'admin/views/partial-masthead.php';
	?>

	<div class="bfnet-cols">
		<div>
			<!-- Payment -->
			<div class="bfnet-panel">
				<div class="bfnet-panel__head">
					<h2><?php esc_html_e( 'Payment', 'bitforgenet-guest-tickets' ); ?></h2>
					<span class="bfnet-badge" style="background:<?php echo esc_attr( BFNET_Util::status_color( $order->payment_status ) ); ?>">
						<?php echo esc_html( BFNET_Util::payment_statuses()[ $order->payment_status ] ?? $order->payment_status ); ?>
					</span>
				</div>
				<div class="bfnet-panel__body">
					<?php
					/**
					 * What a module needs to say or ask about one booking.
					 *
					 * @param object $order The booking.
					 */
					do_action( 'bfnet_order_panels', $order );
					?>


					<?php if ( ! $is_paid ) : ?>
						<div class="bfnet-note-box" style="margin-bottom:16px">
							<?php esc_html_e( 'When you confirm the payment, one ticket per guest is generated with its own QR code, rendered as a PDF and emailed to the buyer. This happens immediately.', 'bitforgenet-guest-tickets' ); ?>
						</div>

						<form method="post" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>">
							<?php wp_nonce_field( 'bfnet_order_status', '_bfnet_nonce' ); ?>
							<input type="hidden" name="page" value="bitforgenet-guest-tickets-orders">
							<input type="hidden" name="bfnet_do" value="order_status">
							<input type="hidden" name="order_id" value="<?php echo (int) $order->id; ?>">
							<input type="hidden" name="status" value="paid">

							<div class="bfnet-row">
								<div class="bfnet-field">
									<label for="bfnet-payref"><?php esc_html_e( 'Payment reference', 'bitforgenet-guest-tickets' ); ?></label>
									<input type="text" id="bfnet-payref" name="payment_reference" placeholder="<?php esc_attr_e( 'Transfer ID, receipt number…', 'bitforgenet-guest-tickets' ); ?>">
								</div>
								<div class="bfnet-field">
									<label for="bfnet-paymethod"><?php esc_html_e( 'Method', 'bitforgenet-guest-tickets' ); ?></label>
									<select id="bfnet-paymethod" name="payment_method">
										<option value="manual"><?php esc_html_e( 'Manual / cash', 'bitforgenet-guest-tickets' ); ?></option>
										<option value="transfer"><?php esc_html_e( 'Bank transfer', 'bitforgenet-guest-tickets' ); ?></option>
										<option value="interac"><?php esc_html_e( 'Interac', 'bitforgenet-guest-tickets' ); ?></option>
										<option value="card_link"><?php esc_html_e( 'Card payment link', 'bitforgenet-guest-tickets' ); ?></option>
										<option value="paypal"><?php esc_html_e( 'PayPal', 'bitforgenet-guest-tickets' ); ?></option>
										<option value="other"><?php esc_html_e( 'Other', 'bitforgenet-guest-tickets' ); ?></option>
									</select>
								</div>
							</div>

							<?php
							/*
							 * The same gate the fulfilment itself uses, asked here so the
							 * button can say why rather than fail after the click. A build
							 * where nothing holds a booking gets null, and the button is
							 * simply live.
							 */
							$bfnet_held = BFNET_Fulfilment::held( (int) $order->id, BFNET_Event::get( (int) $order->event_id ) );
							?>
							<button class="button button-primary button-large" <?php disabled( is_wp_error( $bfnet_held ) ); ?>>
								<?php esc_html_e( 'Confirm payment and send the tickets', 'bitforgenet-guest-tickets' ); ?>
							</button>
							<?php if ( is_wp_error( $bfnet_held ) ) : ?>
								<p class="description" style="margin-top:8px">
									<?php echo esc_html( $bfnet_held->get_error_message() ); ?>
								</p>
							<?php endif; ?>
						</form>
					<?php else : ?>
						<dl class="bfnet-kv">
							<dt><?php esc_html_e( 'Paid at', 'bitforgenet-guest-tickets' ); ?></dt>
							<dd><?php echo esc_html( BFNET_Util::date( $order->paid_at ) ); ?></dd>
							<dt><?php esc_html_e( 'Method', 'bitforgenet-guest-tickets' ); ?></dt>
							<dd><?php echo esc_html( $order->payment_method ); ?></dd>
							<?php if ( $order->payment_reference ) : ?>
								<dt><?php esc_html_e( 'Reference', 'bitforgenet-guest-tickets' ); ?></dt>
								<dd class="bfnet-mono"><?php echo esc_html( $order->payment_reference ); ?></dd>
							<?php endif; ?>
							<dt><?php esc_html_e( 'Tickets emailed', 'bitforgenet-guest-tickets' ); ?></dt>
							<dd><?php echo $order->tickets_sent_at ? esc_html( BFNET_Util::date( $order->tickets_sent_at ) ) : esc_html__( 'not yet', 'bitforgenet-guest-tickets' ); ?></dd>
						</dl>

						<div class="bfnet-inline" style="margin-top:16px">
							<a class="button" data-bfnet-confirm="<?php esc_attr_e( 'This creates brand-new QR codes and emails them. The tickets already in the guest\'s inbox stop working. Continue?', 'bitforgenet-guest-tickets' ); ?>" href="<?php echo esc_url( wp_nonce_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-orders', array( 'bfnet_do' => 'order_resend', 'order' => (int) $order->id ) ), 'bfnet_order_resend', '_bfnet_nonce' ) ); ?>">
								<?php esc_html_e( 'Regenerate and resend tickets', 'bitforgenet-guest-tickets' ); ?>
							</a>

							<form method="post" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" style="display:inline">
								<?php wp_nonce_field( 'bfnet_order_status', '_bfnet_nonce' ); ?>
								<input type="hidden" name="page" value="bitforgenet-guest-tickets-orders">
								<input type="hidden" name="bfnet_do" value="order_status">
								<input type="hidden" name="order_id" value="<?php echo (int) $order->id; ?>">
								<input type="hidden" name="status" value="pending">
								<button class="button" data-bfnet-confirm="<?php esc_attr_e( 'This voids every ticket on this booking. Continue?', 'bitforgenet-guest-tickets' ); ?>"><?php esc_html_e( 'Revert to awaiting payment', 'bitforgenet-guest-tickets' ); ?></button>
							</form>
						</div>
					<?php endif; ?>
				</div>
			</div>

			<!-- Guests & tickets -->
			<div class="bfnet-panel">
				<div class="bfnet-panel__head">
					<h2><?php esc_html_e( 'Guests and their tickets', 'bitforgenet-guest-tickets' ); ?></h2>
					<span class="description">
						<?php
						/* translators: 1: number of guests, 2: number of separate QR codes. */
						echo esc_html( sprintf( _n( '%1$d guest, %2$d separate QR code', '%1$d guests, %2$d separate QR codes', count( $attendees ), 'bitforgenet-guest-tickets' ), count( $attendees ), count( $attendees ) ) );
						?>
					</span>
				</div>
				<div class="bfnet-panel__body bfnet-panel__body--flush">
					<table class="bfnet-table">
						<thead>
							<tr>
								<th>#</th>
								<th><?php esc_html_e( 'Guest', 'bitforgenet-guest-tickets' ); ?></th>
								<?php
								/**
								 * Columns a module adds to the guest list.
								 *
								 * A category and a table number are worth a column only when
								 * something can fill one in. Otherwise the screen is two columns
								 * of dashes, which is worse than no column at all.
								 *
								 * @param array $columns key => heading.
								 */
								$bfnet_guest_cols = (array) apply_filters( 'bfnet_guest_columns', array() );
								foreach ( $bfnet_guest_cols as $bfnet_gh ) :
									?>
									<th><?php echo esc_html( $bfnet_gh ); ?></th>
								<?php endforeach; ?>
								<th><?php esc_html_e( 'Ticket', 'bitforgenet-guest-tickets' ); ?></th>
								<th><?php esc_html_e( 'At the door', 'bitforgenet-guest-tickets' ); ?></th>
								<th></th>
							</tr>
						</thead>
						<tbody>
						<?php foreach ( $attendees as $attendee ) : ?>
							<?php
							$ticket = isset( $tickets_by_attendee[ (int) $attendee->id ] ) ? $tickets_by_attendee[ (int) $attendee->id ] : null;
							?>
							<tr>
								<td><?php echo (int) $attendee->position; ?></td>
								<td class="bfnet-table__title"><?php echo esc_html( BFNET_Util::full_name( $attendee->first_name, $attendee->last_name ) ); ?></td>
								<?php foreach ( $bfnet_guest_cols as $bfnet_gc => $bfnet_gh ) : ?>
									<td>
										<?php
										/**
										 * One cell of a module's column.
										 *
										 * @param string      $column   Which column.
										 * @param object      $attendee The guest.
										 * @param object|null $ticket   Their ticket, if issued.
										 */
										do_action( 'bfnet_guest_column', $bfnet_gc, $attendee, $ticket );
										?>
									</td>
								<?php endforeach; ?>
								<td>
									<?php if ( $ticket ) : ?>
										<span class="bfnet-mono"><?php echo esc_html( $ticket->serial ); ?></span>
										<div class="bfnet-table__sub">
											<span class="bfnet-badge" style="background:<?php echo esc_attr( BFNET_Util::status_color( $ticket->status ) ); ?>">
												<?php echo esc_html( BFNET_Util::ticket_statuses()[ $ticket->status ] ?? $ticket->status ); ?>
											</span>
										</div>
									<?php else : ?>
										<span class="description"><?php esc_html_e( 'not issued yet', 'bitforgenet-guest-tickets' ); ?></span>
									<?php endif; ?>
								</td>
								<td>
									<?php if ( $ticket && $ticket->checked_in ) : ?>
										<strong style="color:#1e8e3e"><?php esc_html_e( 'Checked in', 'bitforgenet-guest-tickets' ); ?></strong>
										<div class="bfnet-table__sub"><?php echo esc_html( BFNET_Util::date( $ticket->checked_in_at, 'j M H:i' ) ); ?><?php echo $ticket->gate ? ' · ' . esc_html( $ticket->gate ) : ''; ?></div>
									<?php else : ?>
										<span class="description">—</span>
									<?php endif; ?>
								</td>
								<td class="bfnet-num">
									<?php if ( $ticket ) : ?>
										<div class="bfnet-actions">
											<?php if ( ! empty( $ticket->pdf_file ) ) : ?>
												<a class="button button-small" target="_blank" rel="noopener" href="<?php echo esc_url( BFNET_Ticket::download_url( $ticket ) ); ?>"><?php esc_html_e( 'PDF', 'bitforgenet-guest-tickets' ); ?></a>
											<?php endif; ?>
											<?php if ( $ticket->checked_in ) : ?>
												<a class="button button-small" href="<?php echo esc_url( wp_nonce_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-orders', array( 'bfnet_do' => 'ticket_action', 'what' => 'undo_checkin', 'ticket' => (int) $ticket->id, 'back' => 'order' ) ), 'bfnet_ticket_action', '_bfnet_nonce' ) ); ?>"><?php esc_html_e( 'Undo', 'bitforgenet-guest-tickets' ); ?></a>
											<?php else : ?>
												<a class="button button-small" href="<?php echo esc_url( wp_nonce_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-orders', array( 'bfnet_do' => 'ticket_action', 'what' => 'checkin', 'ticket' => (int) $ticket->id, 'back' => 'order' ) ), 'bfnet_ticket_action', '_bfnet_nonce' ) ); ?>"><?php esc_html_e( 'Check in', 'bitforgenet-guest-tickets' ); ?></a>
											<?php endif; ?>
											<a class="button button-small" style="color:#c5221f" data-bfnet-confirm="<?php esc_attr_e( 'Void this single ticket?', 'bitforgenet-guest-tickets' ); ?>" href="<?php echo esc_url( wp_nonce_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-orders', array( 'bfnet_do' => 'ticket_action', 'what' => 'void', 'ticket' => (int) $ticket->id, 'back' => 'order' ) ), 'bfnet_ticket_action', '_bfnet_nonce' ) ); ?>"><?php esc_html_e( 'Void', 'bitforgenet-guest-tickets' ); ?></a>
										</div>
									<?php endif; ?>
								</td>
							</tr>
						<?php endforeach; ?>
						</tbody>
					</table>
				</div>
			</div>

			<!-- Internal note -->
			<div class="bfnet-panel">
				<div class="bfnet-panel__head"><h2><?php esc_html_e( 'Internal note', 'bitforgenet-guest-tickets' ); ?></h2></div>
				<div class="bfnet-panel__body">
					<form method="post" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>">
						<?php wp_nonce_field( 'bfnet_order_note', '_bfnet_nonce' ); ?>
						<input type="hidden" name="page" value="bitforgenet-guest-tickets-orders">
						<input type="hidden" name="bfnet_do" value="order_note">
						<input type="hidden" name="order_id" value="<?php echo (int) $order->id; ?>">
						<div class="bfnet-field">
							<textarea name="admin_note" rows="3" placeholder="<?php esc_attr_e( 'Only your team sees this.', 'bitforgenet-guest-tickets' ); ?>"><?php echo esc_textarea( $order->admin_note ); ?></textarea>
						</div>
						<button class="button"><?php esc_html_e( 'Save note', 'bitforgenet-guest-tickets' ); ?></button>
					</form>
				</div>
			</div>
		</div>

		<div>
			<div class="bfnet-panel">
				<div class="bfnet-panel__head"><h2><?php esc_html_e( 'Contact', 'bitforgenet-guest-tickets' ); ?></h2></div>
				<div class="bfnet-panel__body">
					<dl class="bfnet-kv">
						<dt><?php esc_html_e( 'Email', 'bitforgenet-guest-tickets' ); ?></dt>
						<dd><a href="mailto:<?php echo esc_attr( $order->email ); ?>"><?php echo esc_html( $order->email ); ?></a></dd>
						<dt><?php esc_html_e( 'Phone', 'bitforgenet-guest-tickets' ); ?></dt>
						<dd><?php echo esc_html( $order->phone ); ?></dd>
						<?php if ( $order->city ) : ?>
							<dt><?php esc_html_e( 'City', 'bitforgenet-guest-tickets' ); ?></dt>
							<dd><?php echo esc_html( $order->city ); ?></dd>
						<?php endif; ?>
					</dl>

					<?php if ( $whatsapp ) : ?>
						<p style="margin-bottom:0">
							<a class="button button-bfnet" target="_blank" rel="noopener" href="https://wa.me/<?php echo esc_attr( $whatsapp ); ?>?text=<?php echo rawurlencode( sprintf( /* translators: 1: guest name, 2: order reference. */ __( 'Hello %1$s, about your reservation %2$s…', 'bitforgenet-guest-tickets' ), $order->first_name, $order->reference ) ); ?>">
								<?php esc_html_e( 'Message on WhatsApp', 'bitforgenet-guest-tickets' ); ?>
							</a>
						</p>
					<?php endif; ?>
				</div>
			</div>

			<div class="bfnet-panel">
				<div class="bfnet-panel__head"><h2><?php esc_html_e( 'Booking', 'bitforgenet-guest-tickets' ); ?></h2></div>
				<div class="bfnet-panel__body">
					<dl class="bfnet-kv">
						<dt><?php esc_html_e( 'Reference', 'bitforgenet-guest-tickets' ); ?></dt>
						<dd class="bfnet-mono"><?php echo esc_html( $order->reference ); ?></dd>
						<dt><?php esc_html_e( 'N° Tickets', 'bitforgenet-guest-tickets' ); ?></dt>
						<dd><?php echo (int) $order->quantity; ?></dd>
						<dt><?php esc_html_e( 'Total', 'bitforgenet-guest-tickets' ); ?></dt>
						<dd><strong><?php echo esc_html( BFNET_Util::money( $order->total_cents, $order->currency ) ); ?></strong></dd>
						<dt><?php esc_html_e( 'Booked', 'bitforgenet-guest-tickets' ); ?></dt>
						<dd><?php echo esc_html( BFNET_Util::date( $order->created_at ) ); ?></dd>
						<dt><?php esc_html_e( 'Source', 'bitforgenet-guest-tickets' ); ?></dt>
						<dd><?php echo esc_html( $order->source ); ?></dd>
					</dl>

					<?php if ( $order->customer_note ) : ?>
						<div class="bfnet-note-box" style="margin-top:14px">
							<strong><?php esc_html_e( 'Guest note', 'bitforgenet-guest-tickets' ); ?></strong><br>
							<?php echo esc_html( $order->customer_note ); ?>
						</div>
					<?php endif; ?>
				</div>
			</div>

			<div class="bfnet-panel">
				<div class="bfnet-panel__head"><h2><?php esc_html_e( 'Danger zone', 'bitforgenet-guest-tickets' ); ?></h2></div>
				<div class="bfnet-panel__body">
					<p class="description" style="margin-top:0"><?php esc_html_e( 'Deleting removes the booking, its guests, its tickets and its scan history.', 'bitforgenet-guest-tickets' ); ?></p>
					<a class="button" style="color:#c5221f;border-color:#c5221f" data-bfnet-confirm="<?php esc_attr_e( 'Delete this booking permanently?', 'bitforgenet-guest-tickets' ); ?>" href="<?php echo esc_url( wp_nonce_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-orders', array( 'bfnet_do' => 'order_delete', 'order' => (int) $order->id ) ), 'bfnet_order_delete', '_bfnet_nonce' ) ); ?>">
						<?php esc_html_e( 'Delete booking', 'bitforgenet-guest-tickets' ); ?>
					</a>
				</div>
			</div>
		</div>
	</div>
</div>
