<?php
/**
 * Admin view: global dashboard.
 *
 * @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

$stats     = BFNET_Order::stats( 0 );
$upcoming  = BFNET_Event::query( array( 'statuses' => array( 'open', 'published', 'sold_out', 'closed' ), 'upcoming' => true, 'orderby' => 'starts_at', 'order' => 'ASC', 'limit' => 6 ) );
$recent    = BFNET_Order::query( array( 'limit' => 10 ) );
$pending   = BFNET_Order::count( array( 'payment_status' => 'pending' ) );
$templates = BFNET_Template::all();
$page_id   = BFNET_Util::registration_page_id();
?>
<div class="wrap bfnet-admin">

	<?php
	$bfnet_title    = __( 'BitForgeNet Guest Tickets control room', 'bitforgenet-guest-tickets' );
	$bfnet_subtitle = __( 'Everything that is selling, everything waiting on a payment, and who is at the door tonight.', '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>'
		. '<a href="' . esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-scanner' ) ) . '" class="button">' . esc_html__( 'Open scanner', 'bitforgenet-guest-tickets' ) . '</a>';
	include BFNET_PATH . 'admin/views/partial-masthead.php';
	?>

	<?php if ( $pending > 0 ) : ?>
		<div class="notice notice-warning" style="margin:0 0 18px">
			<p>
				<strong>
					<?php
					/* translators: %d: number of bookings awaiting payment. */
					echo esc_html( sprintf( _n( '%d booking is waiting for payment validation.', '%d bookings are waiting for payment validation.', $pending, 'bitforgenet-guest-tickets' ), $pending ) );
					?>
				</strong>
				<a href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-orders', array( 'status' => 'pending' ) ) ); ?>"><?php esc_html_e( 'Review them now', 'bitforgenet-guest-tickets' ); ?></a>
			</p>
		</div>
	<?php endif; ?>

	<div class="bfnet-stats">
		<div class="bfnet-stat bfnet-stat--good">
			<div class="bfnet-stat__label"><?php esc_html_e( 'Confirmed revenue', 'bitforgenet-guest-tickets' ); ?></div>
			<div class="bfnet-stat__value"><?php echo esc_html( BFNET_Util::money( $stats['revenue_cents'] ) ); ?></div>
			<div class="bfnet-stat__meta">
				<?php
				/* translators: %d: number of paid bookings. */
				echo esc_html( sprintf( _n( '%d paid booking', '%d paid bookings', $stats['orders_paid'], 'bitforgenet-guest-tickets' ), $stats['orders_paid'] ) );
				?>
			</div>
		</div>
		<div class="bfnet-stat bfnet-stat--warn">
			<div class="bfnet-stat__label"><?php esc_html_e( 'Awaiting payment', 'bitforgenet-guest-tickets' ); ?></div>
			<div class="bfnet-stat__value"><?php echo esc_html( BFNET_Util::money( $stats['pipeline_cents'] ) ); ?></div>
			<div class="bfnet-stat__meta">
				<?php
				/* translators: %d: number of seats reserved but unpaid. */
				echo esc_html( sprintf( _n( '%d seat held', '%d seats held', $stats['seats_pending'], 'bitforgenet-guest-tickets' ), $stats['seats_pending'] ) );
				?>
			</div>
		</div>
		<div class="bfnet-stat">
			<div class="bfnet-stat__label"><?php esc_html_e( 'Tickets issued', 'bitforgenet-guest-tickets' ); ?></div>
			<div class="bfnet-stat__value"><?php echo esc_html( number_format_i18n( $stats['tickets_total'] ) ); ?></div>
			<div class="bfnet-stat__meta">
				<?php
				/* translators: %d: number of voided tickets. */
				echo esc_html( sprintf( __( '%d void', 'bitforgenet-guest-tickets' ), $stats['tickets_void'] ) );
				?>
			</div>
		</div>
		<div class="bfnet-stat">
			<div class="bfnet-stat__label"><?php esc_html_e( 'Checked in', 'bitforgenet-guest-tickets' ); ?></div>
			<div class="bfnet-stat__value"><?php echo esc_html( number_format_i18n( $stats['tickets_checked_in'] ) ); ?></div>
			<div class="bfnet-stat__meta"><?php esc_html_e( 'across all events', 'bitforgenet-guest-tickets' ); ?></div>
		</div>
		<div class="bfnet-stat">
			<div class="bfnet-stat__label"><?php esc_html_e( 'Events', 'bitforgenet-guest-tickets' ); ?></div>
			<div class="bfnet-stat__value"><?php echo esc_html( number_format_i18n( BFNET_Event::count() ) ); ?></div>
			<div class="bfnet-stat__meta">
				<?php
				/* translators: %d: number of events open for registration. */
				echo esc_html( sprintf( __( '%d open for booking', 'bitforgenet-guest-tickets' ), BFNET_Event::count( 'open' ) ) );
				?>
			</div>
		</div>
	</div>

	<div class="bfnet-cols">
		<div>
			<div class="bfnet-panel">
				<div class="bfnet-panel__head">
					<h2><?php esc_html_e( 'Next events', 'bitforgenet-guest-tickets' ); ?></h2>
					<a class="button button-small" href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-events' ) ); ?>"><?php esc_html_e( 'All events', 'bitforgenet-guest-tickets' ); ?></a>
				</div>
				<div class="bfnet-panel__body bfnet-panel__body--flush">
					<?php if ( empty( $upcoming ) ) : ?>
						<div class="bfnet-empty-state">
							<h3><?php esc_html_e( 'No event scheduled yet', 'bitforgenet-guest-tickets' ); ?></h3>
							<p><?php esc_html_e( 'Create your first event.', '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( '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( 'Pending', 'bitforgenet-guest-tickets' ); ?></th>
									<th></th>
								</tr>
							</thead>
							<tbody>
							<?php foreach ( $upcoming as $row ) : ?>
								<?php
								$row_stats = BFNET_Order::stats( (int) $row->id );
								$row_left  = BFNET_Event::seats_left( $row );
								?>
								<tr>
									<td>
										<div class="bfnet-table__title"><?php echo esc_html( $row->title ); ?></div>
										<div class="bfnet-table__sub">
											<?php echo esc_html( BFNET_Util::date( $row->starts_at ) ); ?> · <?php echo esc_html( BFNET_Util::relative( $row->starts_at ) ); ?>
										</div>
									</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 if ( null !== $row_left ) : ?>
											<div class="bfnet-table__sub">
												<?php
												/* translators: %d: seats remaining. */
												echo esc_html( sprintf( __( '%d left', 'bitforgenet-guest-tickets' ), $row_left ) );
												?>
											</div>
										<?php endif; ?>
									</td>
									<td class="bfnet-num"><?php echo esc_html( number_format_i18n( $row_stats['seats_pending'] ) ); ?></td>
									<td class="bfnet-num">
										<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( 'Open', 'bitforgenet-guest-tickets' ); ?></a>
									</td>
								</tr>
							<?php endforeach; ?>
							</tbody>
						</table>
					<?php endif; ?>
				</div>
			</div>

			<div class="bfnet-panel">
				<div class="bfnet-panel__head">
					<h2><?php esc_html_e( 'Latest bookings', 'bitforgenet-guest-tickets' ); ?></h2>
					<a class="button button-small" href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-orders' ) ); ?>"><?php esc_html_e( 'All subscribers', 'bitforgenet-guest-tickets' ); ?></a>
				</div>
				<div class="bfnet-panel__body bfnet-panel__body--flush">
					<?php if ( empty( $recent ) ) : ?>
						<div class="bfnet-empty-state"><p><?php esc_html_e( 'No booking yet.', 'bitforgenet-guest-tickets' ); ?></p></div>
					<?php else : ?>
						<table class="bfnet-table">
							<tbody>
							<?php foreach ( $recent as $order ) : ?>
								<?php $order_event = BFNET_Event::get( $order->event_id ); ?>
								<tr>
									<td>
										<div class="bfnet-table__title"><?php echo esc_html( BFNET_Util::full_name( $order->first_name, $order->last_name ) ); ?></div>
										<div class="bfnet-table__sub"><?php echo esc_html( $order_event ? $order_event->title : '—' ); ?> · <?php echo esc_html( BFNET_Util::relative( $order->created_at ) ); ?></div>
									</td>
									<td class="bfnet-num"><?php echo esc_html( (int) $order->quantity ); ?>×</td>
									<td class="bfnet-num"><?php echo esc_html( BFNET_Util::money( $order->total_cents, $order->currency ) ); ?></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>
									</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( 'View', 'bitforgenet-guest-tickets' ); ?></a>
									</td>
								</tr>
							<?php endforeach; ?>
							</tbody>
						</table>
					<?php endif; ?>
				</div>
			</div>
		</div>

		<div>
			<div class="bfnet-panel">
				<div class="bfnet-panel__head"><h2><?php esc_html_e( 'Put the booking form on your site', 'bitforgenet-guest-tickets' ); ?></h2></div>
				<div class="bfnet-panel__body">
					<p style="margin-top:0"><?php esc_html_e( 'Paste this shortcode into the page where people should book. It shows the open event, or a chooser when several are running.', 'bitforgenet-guest-tickets' ); ?></p>
					<code class="bfnet-code">[bitforgenet_registration]</code>
					<p class="description" style="margin-top:10px"><?php esc_html_e( 'Useful variants:', 'bitforgenet-guest-tickets' ); ?></p>
					<code class="bfnet-code">[bitforgenet_registration event="opening-night"]</code>
					<code class="bfnet-code" style="margin-top:6px">[bitforgenet_events limit="6"]</code>
					<?php if ( $page_id ) : ?>
						<p style="margin-bottom:0"><a href="<?php echo esc_url( BFNET_Util::registration_page_url() ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Open the booking page', 'bitforgenet-guest-tickets' ); ?></a></p>
					<?php else : ?>
						<p style="margin-bottom:0;color:#b26b00"><strong><?php esc_html_e( 'No page carries this shortcode yet — direct event links will not work until one does.', 'bitforgenet-guest-tickets' ); ?></strong></p>
					<?php endif; ?>
				</div>
			</div>

			<div class="bfnet-panel">
				<div class="bfnet-panel__head"><h2><?php esc_html_e( 'How a booking becomes a ticket', 'bitforgenet-guest-tickets' ); ?></h2></div>
				<div class="bfnet-panel__body">
					<ol style="margin:0 0 0 18px;padding:0;font-size:13.5px;line-height:1.85">
						<li><?php esc_html_e( 'The guest books on your page and receives a reference.', 'bitforgenet-guest-tickets' ); ?></li>
						<li><?php esc_html_e( 'They contact you on WhatsApp and pay.', 'bitforgenet-guest-tickets' ); ?></li>
						<li><?php esc_html_e( 'You open Subscribers and switch them to Paid.', 'bitforgenet-guest-tickets' ); ?></li>
						<li><?php esc_html_e( 'One ticket per guest is generated, each with its own QR code, and emailed as PDF.', 'bitforgenet-guest-tickets' ); ?></li>
						<li><?php esc_html_e( 'At the door, scanning the QR checks that guest in — once.', 'bitforgenet-guest-tickets' ); ?></li>
					</ol>
				</div>
			</div>

			<div class="bfnet-panel">
				<div class="bfnet-panel__head">
					<h2><?php esc_html_e( 'Ticket design', 'bitforgenet-guest-tickets' ); ?></h2>
					<a class="button button-small" href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-templates' ) ); ?>"><?php esc_html_e( 'Manage', 'bitforgenet-guest-tickets' ); ?></a>
				</div>
				<div class="bfnet-panel__body">
					<?php $default_template = BFNET_Template::get_default(); ?>
					<?php if ( $default_template ) : ?>
						<div style="display:flex;gap:12px;align-items:flex-start">
							<img src="<?php echo esc_url( BFNET_Template::preview_url( $default_template ) ); ?>" alt="" style="width:84px;border-radius:6px;border:1px solid #dcdcde">
							<div>
								<strong><?php echo esc_html( $default_template->name ); ?></strong>
								<p class="description" style="margin:4px 0 0">
									<?php echo esc_html( sprintf( '%d × %d px @ %d dpi', (int) $default_template->width_px, (int) $default_template->height_px, (int) $default_template->dpi ) ); ?><br>
									<?php
									$mm = BFNET_Ticket_Renderer::page_mm( $default_template );
									/* translators: 1: width in mm, 2: height in mm. */
									echo esc_html( sprintf( __( '%1$s × %2$s mm printed', 'bitforgenet-guest-tickets' ), $mm[0], $mm[1] ) );
									?>
								</p>
							</div>
						</div>
					<?php else : ?>
						<p><?php esc_html_e( 'No ticket design yet.', 'bitforgenet-guest-tickets' ); ?></p>
					<?php endif; ?>
					<p class="description" style="margin-bottom:0">
						<?php
						/* translators: %d: number of designs. */
						echo esc_html( sprintf( _n( '%d design available', '%d designs available', count( $templates ), 'bitforgenet-guest-tickets' ), count( $templates ) ) );
						?>
					</p>
				</div>
			</div>
		</div>
	</div>

</div>
