<?php
/**
 * Admin view: events 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

$filter_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 = 20;
$events   = BFNET_Event::query(
	array(
		'status'  => $filter_status,
		'search'  => $search,
		'orderby' => 'starts_at',
		'order'   => 'DESC',
		'limit'   => $per_page,
		'offset'  => ( $paged - 1 ) * $per_page,
	)
);
?>
<div class="wrap bfnet-admin">

	<?php
	$bfnet_title    = __( 'Events', 'bitforgenet-guest-tickets' );
	$bfnet_subtitle = __( 'Each event has its own registration, its own capacity, its own ticket design and its own door list.', 'bitforgenet-guest-tickets' );
	$bfnet_actions  = '<a href="' . esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-events', array( 'action' => 'new' ) ) ) . '" class="button button-bfnet">' . esc_html__( '+ New event', '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-events">
				<div class="bfnet-field">
					<label class="bfnet-label" for="bfnet-filter-status"><?php esc_html_e( 'Status', 'bitforgenet-guest-tickets' ); ?></label>
					<select id="bfnet-filter-status" name="status">
						<option value=""><?php esc_html_e( 'All statuses', 'bitforgenet-guest-tickets' ); ?></option>
						<?php foreach ( BFNET_Util::event_statuses() as $key => $label ) : ?>
							<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $filter_status, $key ); ?>><?php echo esc_html( $label ); ?></option>
						<?php endforeach; ?>
					</select>
				</div>
				<div class="bfnet-field">
					<label class="bfnet-label" for="bfnet-search"><?php esc_html_e( 'Search', 'bitforgenet-guest-tickets' ); ?></label>
					<input type="search" id="bfnet-search" name="s" value="<?php echo esc_attr( $search ); ?>" placeholder="<?php esc_attr_e( 'Title or series…', '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( $events ) ) : ?>
				<div class="bfnet-empty-state">
					<h3><?php esc_html_e( 'Nothing here yet', 'bitforgenet-guest-tickets' ); ?></h3>
					<p><?php esc_html_e( 'Create an event to open registration for it.', 'bitforgenet-guest-tickets' ); ?></p>
					<a class="button button-bfnet" href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-events', array( 'action' => 'new' ) ) ); ?>"><?php esc_html_e( 'Create an event', 'bitforgenet-guest-tickets' ); ?></a>
				</div>
			<?php else : ?>
				<table class="bfnet-table">
					<thead>
						<tr>
							<th><?php esc_html_e( 'Event', 'bitforgenet-guest-tickets' ); ?></th>
							<th><?php esc_html_e( 'When', 'bitforgenet-guest-tickets' ); ?></th>
							<th><?php esc_html_e( 'Venue', 'bitforgenet-guest-tickets' ); ?></th>
							<th><?php esc_html_e( 'Status', 'bitforgenet-guest-tickets' ); ?></th>
							<th class="bfnet-num"><?php esc_html_e( 'Sold', 'bitforgenet-guest-tickets' ); ?></th>
							<th class="bfnet-num"><?php esc_html_e( 'Revenue', 'bitforgenet-guest-tickets' ); ?></th>
							<th></th>
						</tr>
					</thead>
					<tbody>
					<?php foreach ( $events as $row ) : ?>
						<?php
						$row_venue = BFNET_Venue::get( $row->venue_id );
						$row_stats = BFNET_Order::stats( (int) $row->id );
						$row_left  = BFNET_Event::seats_left( $row );
						$capacity  = (int) $row->capacity;
						$percent   = $capacity > 0 ? min( 100, round( ( $row_stats['seats_paid'] / $capacity ) * 100 ) ) : 0;
						?>
						<tr>
							<td>
								<div class="bfnet-table__title">
									<a href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-events', array( 'action' => 'dashboard', 'event' => (int) $row->id ) ) ); ?>"><?php echo esc_html( $row->title ); ?></a>
								</div>
								<div class="bfnet-table__sub">
									<?php echo $row->series ? esc_html( $row->series ) : esc_html__( 'Standalone', 'bitforgenet-guest-tickets' ); ?>
									<?php echo $row->edition ? ' · ' . esc_html( $row->edition ) : ''; ?>
								</div>
							</td>
							<td>
								<?php echo esc_html( BFNET_Util::date( $row->starts_at ) ); ?>
								<div class="bfnet-table__sub"><?php echo esc_html( BFNET_Util::relative( $row->starts_at ) ); ?></div>
							</td>
							<td><?php echo esc_html( $row_venue ? $row_venue->name : '—' ); ?></td>
							<td>
								<span class="bfnet-badge" style="background:<?php echo esc_attr( BFNET_Util::status_color( $row->status ) ); ?>">
									<?php echo esc_html( BFNET_Util::event_statuses()[ $row->status ] ?? $row->status ); ?>
								</span>
							</td>
							<td class="bfnet-num">
								<?php echo esc_html( number_format_i18n( $row_stats['seats_paid'] ) ); ?><?php echo $capacity > 0 ? ' / ' . esc_html( number_format_i18n( $capacity ) ) : ''; ?>
								<?php if ( $capacity > 0 ) : ?>
									<div class="bfnet-progress"><span style="width:<?php echo (int) $percent; ?>%"></span></div>
								<?php endif; ?>
								<?php if ( $row_stats['seats_pending'] > 0 ) : ?>
									<div class="bfnet-table__sub">
										<?php
										/* translators: %d: seats awaiting payment. */
										echo esc_html( sprintf( __( '+%d awaiting', 'bitforgenet-guest-tickets' ), $row_stats['seats_pending'] ) );
										?>
									</div>
								<?php endif; ?>
							</td>
							<td class="bfnet-num"><?php echo esc_html( BFNET_Util::money( $row_stats['revenue_cents'], $row->currency ) ); ?></td>
							<td>
								<div class="bfnet-actions">
									<a class="button button-small" href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-events', array( 'action' => 'dashboard', 'event' => (int) $row->id ) ) ); ?>"><?php esc_html_e( 'Dashboard', 'bitforgenet-guest-tickets' ); ?></a>
									<a class="button button-small" href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-events', array( 'action' => 'edit', 'event' => (int) $row->id ) ) ); ?>"><?php esc_html_e( 'Edit', 'bitforgenet-guest-tickets' ); ?></a>
									<a class="button button-small" href="<?php echo esc_url( wp_nonce_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-events', array( 'bfnet_do' => 'duplicate_event', 'event' => (int) $row->id ) ), 'bfnet_duplicate_event', '_bfnet_nonce' ) ); ?>"><?php esc_html_e( 'Duplicate', 'bitforgenet-guest-tickets' ); ?></a>
									<a class="button button-small" style="color:#c5221f" data-bfnet-confirm="<?php esc_attr_e( 'Deleting this event also deletes its bookings and tickets. Continue?', 'bitforgenet-guest-tickets' ); ?>" href="<?php echo esc_url( wp_nonce_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-events', array( 'bfnet_do' => 'delete_event', 'event' => (int) $row->id ) ), 'bfnet_delete_event', '_bfnet_nonce' ) ); ?>"><?php esc_html_e( 'Delete', 'bitforgenet-guest-tickets' ); ?></a>
								</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; <?php esc_html_e( 'Previous', 'bitforgenet-guest-tickets' ); ?></a>
					<?php endif; ?>
					<span><?php echo esc_html( sprintf( '%d', $paged ) ); ?></span>
					<?php if ( count( $events ) === $per_page ) : ?>
						<a class="button button-small" href="<?php echo esc_url( add_query_arg( 'paged', $paged + 1 ) ); ?>"><?php esc_html_e( 'Next', 'bitforgenet-guest-tickets' ); ?> &raquo;</a>
					<?php endif; ?>
				</div>
			<?php endif; ?>
		</div>
	</div>
</div>
