<?php
/**
 * Admin view: tickets list.
 *
 * @package BitForgeNet_Guest_Tickets
 */

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_id = isset( $_GET['event'] ) ? (int) $_GET['event'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only filters.
$status   = isset( $_GET['status'] ) ? sanitize_key( wp_unslash( $_GET['status'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only filters.
$door     = isset( $_GET['door'] ) ? sanitize_key( wp_unslash( $_GET['door'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only filters.
$search   = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only filters.
$paged    = isset( $_GET['paged'] ) ? max( 1, (int) $_GET['paged'] ) : 1; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only filters.

$per_page = 30;
$tickets  = BFNET_Ticket::query(
	array(
		'event_id'   => $event_id,
		'status'     => $status,
		'checked_in' => '' === $door ? null : ( 'in' === $door ),
		'search'     => $search,
		'limit'      => $per_page,
		'offset'     => ( $paged - 1 ) * $per_page,
		'orderby'    => 'id',
		'order'      => 'DESC',
	)
);
$events = BFNET_Event::query( array( 'limit' => 100, 'orderby' => 'starts_at', 'order' => 'DESC' ) );
?>
<div class="wrap bfnet-admin">

	<?php
	$bfnet_title    = __( 'Tickets', 'bitforgenet-guest-tickets' );
	$bfnet_subtitle = __( 'Every issued ticket, its QR status and whether the guest has walked through the door.', 'bitforgenet-guest-tickets' );
	$bfnet_actions  = '<a class="button" href="' . esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-orders', array( 'action' => 'export', 'what' => 'guests', 'event' => $event_id ) ) ) . '">' . esc_html__( 'Export guest list', 'bitforgenet-guest-tickets' ) . '</a>';
	include BFNET_PATH . 'admin/views/partial-masthead.php';
	?>

	<div class="bfnet-panel">
		<div class="bfnet-panel__head">
			<form method="get" class="bfnet-filters">
				<input type="hidden" name="page" value="bitforgenet-guest-tickets-tickets">
				<div class="bfnet-field">
					<label class="bfnet-label" for="bfnet-t-event"><?php esc_html_e( 'Event', 'bitforgenet-guest-tickets' ); ?></label>
					<select id="bfnet-t-event" name="event">
						<option value="0"><?php esc_html_e( 'All events', 'bitforgenet-guest-tickets' ); ?></option>
						<?php foreach ( $events as $row ) : ?>
							<option value="<?php echo (int) $row->id; ?>" <?php selected( $event_id, (int) $row->id ); ?>><?php echo esc_html( $row->title ); ?></option>
						<?php endforeach; ?>
					</select>
				</div>
				<div class="bfnet-field">
					<label class="bfnet-label" for="bfnet-t-status"><?php esc_html_e( 'Ticket status', 'bitforgenet-guest-tickets' ); ?></label>
					<select id="bfnet-t-status" name="status">
						<option value=""><?php esc_html_e( 'Any', 'bitforgenet-guest-tickets' ); ?></option>
						<?php foreach ( BFNET_Util::ticket_statuses() as $key => $label ) : ?>
							<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $status, $key ); ?>><?php echo esc_html( $label ); ?></option>
						<?php endforeach; ?>
					</select>
				</div>
				<div class="bfnet-field">
					<label class="bfnet-label" for="bfnet-t-door"><?php esc_html_e( 'Door', 'bitforgenet-guest-tickets' ); ?></label>
					<select id="bfnet-t-door" name="door">
						<option value=""><?php esc_html_e( 'Everyone', 'bitforgenet-guest-tickets' ); ?></option>
						<option value="in" <?php selected( $door, 'in' ); ?>><?php esc_html_e( 'Checked in', 'bitforgenet-guest-tickets' ); ?></option>
						<option value="out" <?php selected( $door, 'out' ); ?>><?php esc_html_e( 'Not arrived', 'bitforgenet-guest-tickets' ); ?></option>
					</select>
				</div>
				<div class="bfnet-field">
					<label class="bfnet-label" for="bfnet-t-search"><?php esc_html_e( 'Search', 'bitforgenet-guest-tickets' ); ?></label>
					<input type="search" id="bfnet-t-search" name="s" value="<?php echo esc_attr( $search ); ?>" placeholder="<?php esc_attr_e( 'Name or serial…', 'bitforgenet-guest-tickets' ); ?>">
				</div>
				<div class="bfnet-field"><button class="button"><?php esc_html_e( 'Filter', 'bitforgenet-guest-tickets' ); ?></button></div>
			</form>
		</div>

		<div class="bfnet-panel__body bfnet-panel__body--flush">
			<?php if ( empty( $tickets ) ) : ?>
				<div class="bfnet-empty-state">
					<h3><?php esc_html_e( 'No ticket matches', 'bitforgenet-guest-tickets' ); ?></h3>
					<p><?php esc_html_e( 'Tickets appear here as soon as a booking is marked paid.', 'bitforgenet-guest-tickets' ); ?></p>
				</div>
			<?php else : ?>
				<table class="bfnet-table">
					<thead>
						<tr>
							<th><?php esc_html_e( 'Serial', 'bitforgenet-guest-tickets' ); ?></th>
							<th><?php esc_html_e( 'Guest', 'bitforgenet-guest-tickets' ); ?></th>
							<th><?php esc_html_e( 'Event', 'bitforgenet-guest-tickets' ); ?></th>
							<th><?php esc_html_e( 'Status', 'bitforgenet-guest-tickets' ); ?></th>
							<th><?php esc_html_e( 'Table', 'bitforgenet-guest-tickets' ); ?></th>
							<th><?php esc_html_e( 'At the door', 'bitforgenet-guest-tickets' ); ?></th>
							<th class="bfnet-num"><?php esc_html_e( 'Scans', 'bitforgenet-guest-tickets' ); ?></th>
							<th></th>
						</tr>
					</thead>
					<tbody>
					<?php foreach ( $tickets as $ticket ) : ?>
						<?php $ticket_event = BFNET_Event::get( $ticket->event_id ); ?>
						<tr>
							<td class="bfnet-mono"><?php echo esc_html( $ticket->serial ); ?></td>
							<td>
								<div class="bfnet-table__title"><?php echo esc_html( BFNET_Util::full_name( $ticket->first_name, $ticket->last_name ) ); ?></div>
								<div class="bfnet-table__sub">
									<a href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-orders', array( 'action' => 'view', 'order' => (int) $ticket->order_id ) ) ); ?>"><?php esc_html_e( 'open booking', 'bitforgenet-guest-tickets' ); ?></a>
								</div>
							</td>
							<td><?php echo esc_html( $ticket_event ? $ticket_event->title : '—' ); ?></td>
							<td>
								<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>
							</td>
							<td>
								<?php if ( ! empty( $ticket->table_number ) ) : ?>
									<strong style="font-size:15px"><?php echo esc_html( $ticket->table_number ); ?></strong>
								<?php else : ?>
									<span class="description">—</span>
								<?php endif; ?>
							</td>
							<td>
								<?php if ( $ticket->checked_in ) : ?>
									<strong style="color:#1e8e3e"><?php echo esc_html( BFNET_Util::date( $ticket->checked_in_at, 'j M H:i' ) ); ?></strong>
									<?php if ( $ticket->gate ) : ?><div class="bfnet-table__sub"><?php echo esc_html( $ticket->gate ); ?></div><?php endif; ?>
								<?php else : ?>
									<span class="description">—</span>
								<?php endif; ?>
							</td>
							<td class="bfnet-num"><?php echo (int) $ticket->scan_count; ?></td>
							<td class="bfnet-num">
								<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; ?>
									<a class="button button-small" data-bfnet-confirm="<?php esc_attr_e( 'Generate a new QR code for this guest and email it? The old ticket stops working.', 'bitforgenet-guest-tickets' ); ?>" href="<?php echo esc_url( wp_nonce_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-tickets', array( 'bfnet_do' => 'ticket_action', 'what' => 'resend', 'ticket' => (int) $ticket->id ) ), 'bfnet_ticket_action', '_bfnet_nonce' ) ); ?>"><?php esc_html_e( 'Resend', 'bitforgenet-guest-tickets' ); ?></a>
									<?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-tickets', array( 'bfnet_do' => 'ticket_action', 'what' => 'undo_checkin', 'ticket' => (int) $ticket->id ) ), '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-tickets', array( 'bfnet_do' => 'ticket_action', 'what' => 'checkin', 'ticket' => (int) $ticket->id ) ), 'bfnet_ticket_action', '_bfnet_nonce' ) ); ?>"><?php esc_html_e( 'Check in', 'bitforgenet-guest-tickets' ); ?></a>
									<?php endif; ?>
								</div>
							</td>
						</tr>
					<?php endforeach; ?>
					</tbody>
				</table>

				<div class="bfnet-pager">
					<?php if ( $paged > 1 ) : ?>
						<a class="button button-small" href="<?php echo esc_url( add_query_arg( 'paged', $paged - 1 ) ); ?>">&laquo;</a>
					<?php endif; ?>
					<span><?php echo esc_html( sprintf( '%d', $paged ) ); ?></span>
					<?php if ( count( $tickets ) === $per_page ) : ?>
						<a class="button button-small" href="<?php echo esc_url( add_query_arg( 'paged', $paged + 1 ) ); ?>">&raquo;</a>
					<?php endif; ?>
				</div>
			<?php endif; ?>
		</div>
	</div>
</div>
