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

$venues = BFNET_Venue::all( '' );

// phpcs:ignore WordPress.Security.NonceVerification
$edit_id = isset( $_GET['venue'] ) ? (int) $_GET['venue'] : 0;
$editing = $edit_id ? BFNET_Venue::get( $edit_id ) : null;
?>
<div class="wrap bfnet-admin">

	<?php
	$bfnet_title    = __( 'Venues', 'bitforgenet-guest-tickets' );
	$bfnet_subtitle = __( 'Where the party happens. The Google Maps link is shown on the booking page and in the confirmation email.', 'bitforgenet-guest-tickets' );
	include BFNET_PATH . 'admin/views/partial-masthead.php';
	?>

	<div class="bfnet-cols">
		<div>
			<div class="bfnet-panel">
				<div class="bfnet-panel__head"><h2><?php esc_html_e( 'All venues', 'bitforgenet-guest-tickets' ); ?></h2></div>
				<div class="bfnet-panel__body bfnet-panel__body--flush">
					<?php if ( empty( $venues ) ) : ?>
						<div class="bfnet-empty-state"><p><?php esc_html_e( 'No venue yet. Add the first one on the right.', 'bitforgenet-guest-tickets' ); ?></p></div>
					<?php else : ?>
						<table class="bfnet-table">
							<thead>
								<tr>
									<th><?php esc_html_e( 'Venue', 'bitforgenet-guest-tickets' ); ?></th>
									<th><?php esc_html_e( 'Address', 'bitforgenet-guest-tickets' ); ?></th>
									<th class="bfnet-num"><?php esc_html_e( 'Capacity', 'bitforgenet-guest-tickets' ); ?></th>
									<th><?php esc_html_e( 'Map', 'bitforgenet-guest-tickets' ); ?></th>
									<th></th>
								</tr>
							</thead>
							<tbody>
							<?php foreach ( $venues as $venue ) : ?>
								<tr>
									<td>
										<div class="bfnet-table__title"><?php echo esc_html( $venue->name ); ?></div>
										<div class="bfnet-table__sub"><?php echo esc_html( $venue->city ); ?></div>
									</td>
									<td><?php echo esc_html( BFNET_Venue::address_line( $venue ) ); ?></td>
									<td class="bfnet-num"><?php echo $venue->capacity ? esc_html( number_format_i18n( $venue->capacity ) ) : '—'; ?></td>
									<td>
										<?php $map = BFNET_Venue::map_url( $venue ); ?>
										<?php if ( $map ) : ?>
											<div class="bfnet-actions">
												<a class="button button-small" href="<?php echo esc_url( $map ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Open', 'bitforgenet-guest-tickets' ); ?></a>
												<button type="button" class="button button-small" data-bfnet-copy="<?php echo esc_attr( $map ); ?>"><?php esc_html_e( 'Copy', 'bitforgenet-guest-tickets' ); ?></button>
											</div>
										<?php else : ?>
											&mdash;
										<?php endif; ?>
									</td>
									<td class="bfnet-num">
										<div class="bfnet-actions">
											<a class="button button-small" href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-venues', array( 'venue' => (int) $venue->id ) ) ); ?>"><?php esc_html_e( 'Edit', 'bitforgenet-guest-tickets' ); ?></a>
											<a class="button button-small" style="color:#c5221f" data-bfnet-confirm="<?php esc_attr_e( 'Delete this venue?', 'bitforgenet-guest-tickets' ); ?>" href="<?php echo esc_url( wp_nonce_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-venues', array( 'bfnet_do' => 'delete_venue', 'venue' => (int) $venue->id ) ), 'bfnet_delete_venue', '_bfnet_nonce' ) ); ?>"><?php esc_html_e( 'Delete', 'bitforgenet-guest-tickets' ); ?></a>
										</div>
									</td>
								</tr>
							<?php endforeach; ?>
							</tbody>
						</table>
					<?php endif; ?>
				</div>
			</div>
		</div>

		<div>
			<div class="bfnet-panel">
				<div class="bfnet-panel__head">
					<h2><?php echo esc_html( $editing ? __( 'Edit venue', 'bitforgenet-guest-tickets' ) : __( 'New venue', 'bitforgenet-guest-tickets' ) ); ?></h2>
					<?php if ( $editing ) : ?>
						<a class="button button-small" href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-venues' ) ); ?>"><?php esc_html_e( 'New', 'bitforgenet-guest-tickets' ); ?></a>
					<?php endif; ?>
				</div>
				<div class="bfnet-panel__body">
					<form method="post" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>">
						<?php wp_nonce_field( 'bfnet_save_venue', '_bfnet_nonce' ); ?>
						<input type="hidden" name="page" value="bitforgenet-guest-tickets-venues">
						<input type="hidden" name="bfnet_do" value="save_venue">
						<input type="hidden" name="venue_id" value="<?php echo $editing ? (int) $editing->id : 0; ?>">

						<div class="bfnet-field">
							<label for="bfnet-v-name"><?php esc_html_e( 'Name', 'bitforgenet-guest-tickets' ); ?> *</label>
							<input type="text" id="bfnet-v-name" name="name" data-bfnet-map-part="key" required value="<?php echo esc_attr( $editing ? $editing->name : '' ); ?>">
						</div>

						<div class="bfnet-field">
							<label for="bfnet-v-address"><?php esc_html_e( 'Street address', 'bitforgenet-guest-tickets' ); ?></label>
							<input type="text" id="bfnet-v-address" name="address" data-bfnet-map-part="key" value="<?php echo esc_attr( $editing ? $editing->address : '' ); ?>">
						</div>

						<div class="bfnet-row bfnet-row--3">
							<div class="bfnet-field">
								<label for="bfnet-v-city"><?php esc_html_e( 'City', 'bitforgenet-guest-tickets' ); ?></label>
								<input type="text" id="bfnet-v-city" name="city" data-bfnet-map-part="key" value="<?php echo esc_attr( $editing ? $editing->city : '' ); ?>">
							</div>
							<div class="bfnet-field">
								<label for="bfnet-v-state"><?php esc_html_e( 'Province or state', 'bitforgenet-guest-tickets' ); ?></label>
								<input type="text" id="bfnet-v-state" name="state" list="bfnet-v-regions"
									data-bfnet-map-part="extra" maxlength="80"
									value="<?php echo esc_attr( $editing ? $editing->state : '' ); ?>">
								<datalist id="bfnet-v-regions">
									<?php foreach ( BFNET_Venue::regions() as $bfnet_country => $bfnet_list ) : ?>
										<?php foreach ( $bfnet_list as $bfnet_code => $bfnet_name ) : ?>
											<option value="<?php echo esc_attr( $bfnet_code ); ?>"><?php echo esc_html( $bfnet_name . ' — ' . $bfnet_country ); ?></option>
										<?php endforeach; ?>
									<?php endforeach; ?>
								</datalist>
								<p class="description"><?php esc_html_e( 'Optional. Type yours, or pick a US state or Canadian province from the list.', 'bitforgenet-guest-tickets' ); ?></p>
							</div>
							<div class="bfnet-field">
								<label for="bfnet-v-post"><?php esc_html_e( 'Postcode', 'bitforgenet-guest-tickets' ); ?></label>
								<input type="text" id="bfnet-v-post" name="postcode" data-bfnet-map-part="extra" value="<?php echo esc_attr( $editing ? $editing->postcode : '' ); ?>" >
							</div>
							<div class="bfnet-field">
								<label for="bfnet-v-country"><?php esc_html_e( 'Country', 'bitforgenet-guest-tickets' ); ?></label>
								<input type="text" id="bfnet-v-country" name="country" data-bfnet-map-part="extra" value="<?php echo esc_attr( $editing ? $editing->country : '' ); ?>">
							</div>
						</div>

						<div class="bfnet-row">
							<div class="bfnet-field">
								<label for="bfnet-v-lat"><?php esc_html_e( 'Latitude', 'bitforgenet-guest-tickets' ); ?></label>
								<input type="text" id="bfnet-v-lat" name="latitude" data-bfnet-map-lat value="<?php echo esc_attr( $editing && null !== $editing->latitude ? $editing->latitude : '' ); ?>" placeholder="51.0447">
							</div>
							<div class="bfnet-field">
								<label for="bfnet-v-lng"><?php esc_html_e( 'Longitude', 'bitforgenet-guest-tickets' ); ?></label>
								<input type="text" id="bfnet-v-lng" name="longitude" data-bfnet-map-lng value="<?php echo esc_attr( $editing && null !== $editing->longitude ? $editing->longitude : '' ); ?>" placeholder="-114.0719">
							</div>
						</div>

						<div class="bfnet-field" data-bfnet-map>
							<label for="bfnet-v-map"><?php esc_html_e( 'Google Maps link', 'bitforgenet-guest-tickets' ); ?></label>
							<input type="url" id="bfnet-v-map" name="google_maps_url" data-bfnet-map-url value="<?php echo esc_attr( $editing ? $editing->google_maps_url : '' ); ?>" placeholder="https://maps.app.goo.gl/…">
							<p class="description"><?php esc_html_e( 'Paste the link Google Maps gives you under Share. Only Google, Apple and OpenStreetMap links are accepted. Leave it empty and the link below is used instead.', 'bitforgenet-guest-tickets' ); ?></p>

							<?php /* Built live from whatever is typed above: coordinates first, the written address as a fallback. Nothing here is saved unless the manager chooses it. */ ?>
							<div class="bfnet-maplink" data-bfnet-map-box hidden>
								<div class="bfnet-maplink__head">
									<strong><?php esc_html_e( 'Suggested link', 'bitforgenet-guest-tickets' ); ?></strong>
									<span data-bfnet-map-from></span>
								</div>
								<code data-bfnet-map-suggest></code>
								<div class="bfnet-maplink__actions">
									<button type="button" class="button button-small" data-bfnet-map-copy><?php esc_html_e( 'Copy link', 'bitforgenet-guest-tickets' ); ?></button>
									<button type="button" class="button button-small" data-bfnet-map-use><?php esc_html_e( 'Use this link', 'bitforgenet-guest-tickets' ); ?></button>
									<a class="button button-small" target="_blank" rel="noopener" data-bfnet-map-open href="#"><?php esc_html_e( 'Open in Maps', 'bitforgenet-guest-tickets' ); ?></a>
								</div>
							</div>
						</div>

						<div class="bfnet-row">
							<div class="bfnet-field">
								<label for="bfnet-v-cap"><?php esc_html_e( 'Capacity', 'bitforgenet-guest-tickets' ); ?></label>
								<input type="number" id="bfnet-v-cap" name="capacity" min="0" value="<?php echo (int) ( $editing ? $editing->capacity : 0 ); ?>">
							</div>
							<div class="bfnet-field">
								<label for="bfnet-v-phone"><?php esc_html_e( 'Phone', 'bitforgenet-guest-tickets' ); ?></label>
								<input type="tel" id="bfnet-v-phone" name="phone" value="<?php echo esc_attr( $editing ? $editing->phone : '' ); ?>">
							</div>
						</div>

						<div class="bfnet-field">
							<label for="bfnet-v-notes"><?php esc_html_e( 'Notes', 'bitforgenet-guest-tickets' ); ?></label>
							<textarea id="bfnet-v-notes" name="notes" rows="2"><?php echo esc_textarea( $editing ? $editing->notes : '' ); ?></textarea>
						</div>

						<div class="bfnet-field">
							<label for="bfnet-v-status"><?php esc_html_e( 'Status', 'bitforgenet-guest-tickets' ); ?></label>
							<select id="bfnet-v-status" name="status">
								<option value="active" <?php selected( $editing ? $editing->status : 'active', 'active' ); ?>><?php esc_html_e( 'Active', 'bitforgenet-guest-tickets' ); ?></option>
								<option value="inactive" <?php selected( $editing ? $editing->status : '', 'inactive' ); ?>><?php esc_html_e( 'Inactive', 'bitforgenet-guest-tickets' ); ?></option>
							</select>
						</div>

						<button class="button button-primary" style="width:100%"><?php echo esc_html( $editing ? __( 'Save venue', 'bitforgenet-guest-tickets' ) : __( 'Create venue', 'bitforgenet-guest-tickets' ) ); ?></button>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
