<?php
/**
 * Admin view: subscribers (orders) 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.
$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 = 25;
$args     = array(
	'event_id'       => $event_id,
	'payment_status' => $status,
	'search'         => $search,
);

$total  = BFNET_Order::count( $args );
$orders = BFNET_Order::query( array_merge( $args, array( 'limit' => $per_page, 'offset' => ( $paged - 1 ) * $per_page ) ) );
$events = BFNET_Event::query( array( 'limit' => 100, 'orderby' => 'starts_at', 'order' => 'DESC' ) );
$pages  = max( 1, (int) ceil( $total / $per_page ) );

/**
 * The bookings about to be listed.
 *
 * A module that adds a column gets one chance to fetch what it needs for the
 * whole page — one query rather than twenty-five, one per row.
 *
 * @param array $orders The bookings on this page.
 */
do_action( 'bfnet_orders_listed', $orders );

/**
 * Columns a module adds to the bookings table.
 *
 * @param array $columns key => heading.
 */
$bfnet_extra = (array) apply_filters( 'bfnet_order_columns', array() );
?>
<div class="wrap bfnet-admin">

	<?php
	$bfnet_title    = __( 'Subscribers', 'bitforgenet-guest-tickets' );
	$bfnet_subtitle = __( 'Everyone who booked. Switch a booking to Paid and the tickets are generated and emailed automatically.', 'bitforgenet-guest-tickets' );
	$bfnet_actions  = '<a class="button button-primary" href="' . esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-orders', array( 'action' => 'export', 'what' => 'orders', 'event' => $event_id ) ) ) . '">' . esc_html__( 'Export CSV', '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-orders">
				<div class="bfnet-field">
					<label class="bfnet-label" for="bfnet-f-event"><?php esc_html_e( 'Event', 'bitforgenet-guest-tickets' ); ?></label>
					<select id="bfnet-f-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-f-status"><?php esc_html_e( 'Payment', 'bitforgenet-guest-tickets' ); ?></label>
					<select id="bfnet-f-status" name="status">
						<option value=""><?php esc_html_e( 'Any status', 'bitforgenet-guest-tickets' ); ?></option>
						<?php foreach ( BFNET_Util::payment_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-f-search"><?php esc_html_e( 'Search', 'bitforgenet-guest-tickets' ); ?></label>
					<input type="search" id="bfnet-f-search" name="s" value="<?php echo esc_attr( $search ); ?>" placeholder="<?php esc_attr_e( 'Name, email, phone, reference…', 'bitforgenet-guest-tickets' ); ?>">
				</div>
				<div class="bfnet-field"><button class="button"><?php esc_html_e( 'Filter', 'bitforgenet-guest-tickets' ); ?></button></div>
			</form>
		</div>

		<?php if ( empty( $orders ) ) : ?>
			<div class="bfnet-empty-state">
				<h3><?php esc_html_e( 'No booking matches', 'bitforgenet-guest-tickets' ); ?></h3>
				<p><?php esc_html_e( 'Try clearing the filters, or share your booking link to get the first reservation in.', 'bitforgenet-guest-tickets' ); ?></p>
			</div>
		<?php else : ?>
			<form method="post" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>">
				<?php wp_nonce_field( 'bfnet_order_bulk', '_bfnet_nonce' ); ?>
				<input type="hidden" name="page" value="bitforgenet-guest-tickets-orders">
				<input type="hidden" name="bfnet_do" value="order_bulk">
				<input type="hidden" name="event_filter" value="<?php echo (int) $event_id; ?>">

				<div class="bfnet-panel__body bfnet-panel__body--flush">
					<table class="bfnet-table">
						<thead>
							<tr>
								<th style="width:28px"><input type="checkbox" onclick="this.closest('table').querySelectorAll('tbody input[type=checkbox]').forEach(function(c){c.checked=this.checked}.bind(this))"></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( 'Contact', 'bitforgenet-guest-tickets' ); ?></th>
								<th class="bfnet-num"><?php esc_html_e( 'N° Tickets', 'bitforgenet-guest-tickets' ); ?></th>
								<?php foreach ( $bfnet_extra as $bfnet_col => $bfnet_head ) : ?>
									<th><?php echo esc_html( $bfnet_head ); ?></th>
								<?php endforeach; ?>
								<th class="bfnet-num"><?php esc_html_e( 'Total', 'bitforgenet-guest-tickets' ); ?></th>
								<th><?php esc_html_e( 'Payment', 'bitforgenet-guest-tickets' ); ?></th>
								<th><?php esc_html_e( 'Booked', 'bitforgenet-guest-tickets' ); ?></th>
								<th></th>
							</tr>
						</thead>
						<tbody>
						<?php foreach ( $orders as $order ) : ?>
							<?php $order_event = BFNET_Event::get( $order->event_id ); ?>
							<tr>
								<td><input type="checkbox" name="orders[]" value="<?php echo (int) $order->id; ?>"></td>
								<td>
									<div class="bfnet-table__title">
										<a href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-orders', array( 'action' => 'view', 'order' => (int) $order->id ) ) ); ?>">
											<?php echo esc_html( BFNET_Util::full_name( $order->first_name, $order->last_name ) ); ?>
										</a>
									</div>
									<div class="bfnet-table__sub bfnet-mono"><?php echo esc_html( $order->reference ); ?></div>
								</td>
								<td><?php echo esc_html( $order_event ? $order_event->title : '—' ); ?></td>
								<td>
									<div><?php echo esc_html( $order->email ); ?></div>
									<?php if ( $order->phone ) : ?>
										<div class="bfnet-table__sub">
											<a href="https://wa.me/<?php echo esc_attr( preg_replace( '/\D/', '', $order->phone ) ); ?>" target="_blank" rel="noopener"><?php echo esc_html( $order->phone ); ?></a>
										</div>
									<?php endif; ?>
								</td>
								<td class="bfnet-num"><?php echo esc_html( (int) $order->quantity ); ?></td>
								<?php foreach ( $bfnet_extra as $bfnet_col => $bfnet_head ) : ?>
									<td>
										<?php
										/**
										 * One cell of a module's column.
										 *
										 * @param string      $column      Which column.
										 * @param object      $order       The booking.
										 * @param object|null $order_event Its event.
										 */
										do_action( 'bfnet_order_column', $bfnet_col, $order, $order_event );
										?>
									</td>
								<?php endforeach; ?>
								<td class="bfnet-num"><strong><?php echo esc_html( BFNET_Util::money( $order->total_cents, $order->currency ) ); ?></strong></td>
								<td>
									<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>
									<?php if ( $order->tickets_sent_at ) : ?>
										<div class="bfnet-table__sub"><?php esc_html_e( 'tickets sent', 'bitforgenet-guest-tickets' ); ?></div>
									<?php endif; ?>
								</td>
								<td>
									<?php echo esc_html( BFNET_Util::relative( $order->created_at ) ); ?>
									<div class="bfnet-table__sub"><?php echo esc_html( BFNET_Util::date( $order->created_at, 'j M, H:i' ) ); ?></div>
								</td>
								<td class="bfnet-num">
									<a class="button button-small" href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-orders', array( 'action' => 'view', 'order' => (int) $order->id ) ) ); ?>"><?php esc_html_e( 'Open', 'bitforgenet-guest-tickets' ); ?></a>
								</td>
							</tr>
						<?php endforeach; ?>
						</tbody>
					</table>
				</div>

				<div class="bfnet-panel__head" style="border-top:1px solid #dcdcde;border-bottom:0">
					<div class="bfnet-inline">
						<select name="bulk_action">
							<option value=""><?php esc_html_e( 'Bulk action…', 'bitforgenet-guest-tickets' ); ?></option>
							<option value="mark_paid"><?php esc_html_e( 'Mark as paid (issues and emails tickets)', 'bitforgenet-guest-tickets' ); ?></option>
							<option value="cancel"><?php esc_html_e( 'Cancel', 'bitforgenet-guest-tickets' ); ?></option>
							<option value="delete"><?php esc_html_e( 'Delete permanently', 'bitforgenet-guest-tickets' ); ?></option>
						</select>
						<button class="button" data-bfnet-confirm="<?php esc_attr_e( 'Apply this action to the selected bookings?', 'bitforgenet-guest-tickets' ); ?>"><?php esc_html_e( 'Apply', 'bitforgenet-guest-tickets' ); ?></button>
					</div>

					<div class="bfnet-inline">
						<?php
						/* translators: 1: current page, 2: total pages, 3: total bookings. */
						echo esc_html( sprintf( __( 'Page %1$d of %2$d — %3$d bookings', 'bitforgenet-guest-tickets' ), $paged, $pages, $total ) );
						?>
						<?php if ( $paged > 1 ) : ?>
							<a class="button button-small" href="<?php echo esc_url( add_query_arg( 'paged', $paged - 1 ) ); ?>">&laquo;</a>
						<?php endif; ?>
						<?php if ( $paged < $pages ) : ?>
							<a class="button button-small" href="<?php echo esc_url( add_query_arg( 'paged', $paged + 1 ) ); ?>">&raquo;</a>
						<?php endif; ?>
					</div>
				</div>
			</form>
		<?php endif; ?>
	</div>
</div>
