<?php
/**
 * Admin view: the When and where tab of the event editor.
 *
 * Included by admin/views/event-edit.php inside that tab's own form, so the
 * $event row and the $bfnet_dt helper defined there are in scope here.
 *
 * @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
?>
<div class="bfnet-panel">
	<div class="bfnet-panel__head"><h2><?php esc_html_e( 'When and where', 'bitforgenet-guest-tickets' ); ?></h2></div>
	<div class="bfnet-panel__body">
		<div class="bfnet-row bfnet-row--3">
			<div class="bfnet-field">
				<label for="bfnet-starts"><?php esc_html_e( 'Starts', 'bitforgenet-guest-tickets' ); ?> *</label>
				<input type="datetime-local" id="bfnet-starts" name="starts_at" required value="<?php echo esc_attr( $bfnet_dt( $event ? $event->starts_at : '' ) ); ?>">
			</div>
			<div class="bfnet-field">
				<label for="bfnet-ends"><?php esc_html_e( 'Ends', 'bitforgenet-guest-tickets' ); ?></label>
				<input type="datetime-local" id="bfnet-ends" name="ends_at" value="<?php echo esc_attr( $bfnet_dt( $event ? $event->ends_at : '' ) ); ?>">
			</div>
			<div class="bfnet-field">
				<label for="bfnet-doors"><?php esc_html_e( 'Doors open', 'bitforgenet-guest-tickets' ); ?></label>
				<input type="datetime-local" id="bfnet-doors" name="doors_open_at" value="<?php echo esc_attr( $bfnet_dt( $event ? $event->doors_open_at : '' ) ); ?>">
			</div>
		</div>

		<div class="bfnet-field">
			<label for="bfnet-timezone"><?php esc_html_e( 'Event timezone', 'bitforgenet-guest-tickets' ); ?></label>
			<select id="bfnet-timezone" name="timezone">
				<?php foreach ( BFNET_Util::timezone_choices() as $bfnet_tz ) : ?>
					<option value="<?php echo esc_attr( $bfnet_tz ); ?>" <?php selected( $event && $event->timezone ? $event->timezone : BFNET_Util::site_timezone(), $bfnet_tz ); ?>><?php echo esc_html( $bfnet_tz ); ?></option>
				<?php endforeach; ?>
			</select>
			<p class="description"><?php esc_html_e( 'The times above are the wall clock at the venue. Countdowns and check-in windows are derived from this, so a guest in Spain sees the correct time remaining.', 'bitforgenet-guest-tickets' ); ?></p>
		</div>

		<div class="bfnet-row">
			<div class="bfnet-field">
				<label for="bfnet-venue"><?php esc_html_e( 'Venue', 'bitforgenet-guest-tickets' ); ?></label>
				<select id="bfnet-venue" name="venue_id">
					<option value="0"><?php esc_html_e( '— none —', 'bitforgenet-guest-tickets' ); ?></option>
					<?php foreach ( $venues as $venue ) : ?>
						<option value="<?php echo (int) $venue->id; ?>" <?php selected( $event ? (int) $event->venue_id : 0, (int) $venue->id ); ?>>
							<?php echo esc_html( $venue->name . ( $venue->city ? ' — ' . $venue->city : '' ) ); ?>
						</option>
					<?php endforeach; ?>
				</select>
				<p class="description"><a href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-venues' ) ); ?>"><?php esc_html_e( 'Manage venues and their Google Maps links', 'bitforgenet-guest-tickets' ); ?></a></p>
			</div>
			<div class="bfnet-field">
				<label for="bfnet-organizer"><?php esc_html_e( 'Organizer', 'bitforgenet-guest-tickets' ); ?></label>
				<select id="bfnet-organizer" name="organizer_id">
					<option value="0"><?php esc_html_e( '— none —', 'bitforgenet-guest-tickets' ); ?></option>
					<?php foreach ( $organizers as $organizer ) : ?>
						<option value="<?php echo (int) $organizer->id; ?>" <?php selected( $event ? (int) $event->organizer_id : ( BFNET_Organizer::primary() ? (int) BFNET_Organizer::primary()->id : 0 ), (int) $organizer->id ); ?>>
							<?php echo esc_html( $organizer->name ); ?>
						</option>
					<?php endforeach; ?>
				</select>
			</div>
		</div>

		<div class="bfnet-field">
			<label class="bfnet-label"><?php esc_html_e( 'Line-up', 'bitforgenet-guest-tickets' ); ?></label>
			<p class="description" style="margin-top:0"><?php esc_html_e( 'DJ, orchestra, band, host… printed on the ticket and shown on the booking page.', 'bitforgenet-guest-tickets' ); ?></p>
			<div class="bfnet-repeat" id="bfnet-lineup">
				<?php foreach ( $lineup as $index => $item ) : ?>
					<div class="bfnet-repeat__row">
						<input type="text" name="lineup[<?php echo (int) $index; ?>][role]" value="<?php echo esc_attr( $item['role'] ); ?>" placeholder="<?php esc_attr_e( 'DJ', 'bitforgenet-guest-tickets' ); ?>">
						<input type="text" name="lineup[<?php echo (int) $index; ?>][name]" value="<?php echo esc_attr( $item['name'] ); ?>" placeholder="<?php esc_attr_e( 'Name', 'bitforgenet-guest-tickets' ); ?>">
						<input type="text" name="lineup[<?php echo (int) $index; ?>][note]" value="<?php echo esc_attr( $item['note'] ); ?>" placeholder="<?php esc_attr_e( 'Note (optional)', 'bitforgenet-guest-tickets' ); ?>">
						<button type="button" class="bfnet-remove" aria-label="<?php esc_attr_e( 'Remove', 'bitforgenet-guest-tickets' ); ?>">&times;</button>
					</div>
				<?php endforeach; ?>
			</div>
			<template id="bfnet-lineup-template">
				<div class="bfnet-repeat__row">
					<input type="text" name="lineup[__INDEX__][role]" placeholder="<?php esc_attr_e( 'DJ', 'bitforgenet-guest-tickets' ); ?>">
					<input type="text" name="lineup[__INDEX__][name]" placeholder="<?php esc_attr_e( 'Name', 'bitforgenet-guest-tickets' ); ?>">
					<input type="text" name="lineup[__INDEX__][note]" placeholder="<?php esc_attr_e( 'Note (optional)', 'bitforgenet-guest-tickets' ); ?>">
					<button type="button" class="bfnet-remove" aria-label="<?php esc_attr_e( 'Remove', 'bitforgenet-guest-tickets' ); ?>">&times;</button>
				</div>
			</template>
			<p><button type="button" class="button" data-bfnet-add="#bfnet-lineup" data-bfnet-template="#bfnet-lineup-template"><?php esc_html_e( '+ Add a performer', 'bitforgenet-guest-tickets' ); ?></button></p>
		</div>

		<div class="bfnet-row">
			<div class="bfnet-field">
				<label for="bfnet-dress"><?php esc_html_e( 'Dress code', 'bitforgenet-guest-tickets' ); ?></label>
				<input type="text" id="bfnet-dress" name="dress_code" maxlength="180" value="<?php echo esc_attr( $event ? $event->dress_code : '' ); ?>">
			</div>
			<div class="bfnet-field">
				<label for="bfnet-age"><?php esc_html_e( 'Minimum age', 'bitforgenet-guest-tickets' ); ?></label>
				<input type="number" id="bfnet-age" name="min_age" class="bfnet-narrow" min="0" max="99" value="<?php echo (int) ( $event ? $event->min_age : 0 ); ?>">
				<p class="description"><?php esc_html_e( '0 = no restriction.', 'bitforgenet-guest-tickets' ); ?></p>
			</div>
		</div>

		<div class="bfnet-field">
			<label for="bfnet-age-mode"><?php esc_html_e( 'How the age is checked', 'bitforgenet-guest-tickets' ); ?></label>
			<select id="bfnet-age-mode" name="age_check_mode">
				<?php foreach ( BFNET_Event::age_modes() as $bfnet_mode => $bfnet_label ) : ?>
					<option value="<?php echo esc_attr( $bfnet_mode ); ?>" <?php selected( $event ? $event->age_check_mode : 'off', $bfnet_mode ); ?>><?php echo esc_html( $bfnet_label ); ?></option>
				<?php endforeach; ?>
			</select>
			<p class="description"><?php esc_html_e( 'Date of birth is the strict option: the booking is refused outright if any guest is under the minimum age.', 'bitforgenet-guest-tickets' ); ?></p>
		</div>
	</div>
</div>
