<?php
/**
 * templates/form-wrapper.php
 * Front-end transfer form template (EXECVITR only)
 */
if (!defined('ABSPATH'))
	exit;

$min_price = (float) get_option('execvitr_minimum_price', 0);
$plugin_url = plugins_url('../assets/img/', __FILE__);
$selected_country = get_option('execvitr_country', 'TR');

$dial_codes = [
  'TR' => '90',
  'US' => '1',
  'CA' => '1',
  'GB' => '44',
  'IE' => '353',

  'DE' => '49',
  'FR' => '33',
  'IT' => '39',
  'ES' => '34',
  'PT' => '351',
  'NL' => '31',
  'BE' => '32',
  'CH' => '41',
  'AT' => '43',
  'GR' => '30',
  'PL' => '48',
  'SE' => '46',
  'NO' => '47',
  'FI' => '358',
  'DK' => '45',
  'RO' => '40',
  'UA' => '380',
  'CZ' => '420',
  'HU' => '36',
  'BG' => '359',
  'HR' => '385',
  'RS' => '381',

  'AE' => '971',
  'SA' => '966',
  'QA' => '974',
  'KW' => '965',
  'OM' => '968',
  'BH' => '973',
  'JO' => '962',
  'LB' => '961',
  'IQ' => '964',
  'IL' => '972',
  'IR' => '98',

  'RU' => '7',

  'IN' => '91',
  'CN' => '86',
  'JP' => '81',
  'KR' => '82',
  'SG' => '65',
  'TH' => '66',
  'VN' => '84',
  'PH' => '63',
  'ID' => '62',
  'MY' => '60',
  'PK' => '92',

  'AU' => '61',
  'NZ' => '64',

  'BR' => '55',
  'MX' => '52',
  'AR' => '54',

  'EG' => '20',
  'ZA' => '27',
  'MA' => '212',
  'NG' => '234',
];

$dial_code = $dial_codes[$selected_country] ?? '90';

$currency_code = get_option('execvitr_currency', 'USD');
$symbols = [
	'USD' => '$',
	'EUR' => '€',
	'GBP' => '£',
	'TRY' => '₺',
	'CAD' => 'C$',
	'AUD' => 'A$',
	'CHF' => 'CHF',
	'JPY' => '¥',
	'CNY' => '¥',
	'AED' => 'د.إ',
	'INR' => '₹',
	'BRL' => 'R$',
	'ZAR' => 'R',
	'KRW' => '₩',
	'MXN' => '$'
];
$currency_symbol = $symbols[$currency_code] ?? '$';


$execvitr_form_uid = function_exists('wp_unique_id') ? wp_unique_id('execvitr_') : uniqid('execvitr_');
$form_id = $execvitr_form_uid . '_form';
$pickup_time_id = $execvitr_form_uid . '_pickup_time';
$time_slots_id = $execvitr_form_uid . '_time_slots';
$return_toggle_id = $execvitr_form_uid . '_is_return';
$return_fields_id = $execvitr_form_uid . '_return_fields';
$dial_code_id = $execvitr_form_uid . '_dial_code';
$phone_e164_id = $execvitr_form_uid . '_phone_e164';

/** Cars from settings */
$cars_option = get_option('execvitr_cars', []);
$cars_option = is_array($cars_option) ? $cars_option : [];
$car_localize = [];
foreach ($cars_option as $index => $car) {
	$brand = isset($car['brand']) ? (string) $car['brand'] : '';
	$image_id = isset($car['image_id']) ? (int) $car['image_id'] : 0;
	$passenger = isset($car['passenger']) ? (int) $car['passenger'] : 1;
	$luggage = isset($car['luggage']) ? (int) $car['luggage'] : 0;
	$km_price = isset($car['km_price']) ? (float) $car['km_price'] : 0;
	$desc = isset($car['description']) ? (string) $car['description'] : '';
	$img_url = $image_id ? wp_get_attachment_image_url($image_id, 'medium') : '';

	$car_localize[] = [
		'i' => (int) $index,
		'brand' => $brand,
		'passengers' => max(1, $passenger),
		'luggage' => max(0, $luggage),
		'image' => $img_url,
		'km_price' => $km_price,
		'description' => $desc,
	];
}

$is_pro = function_exists('execvitr_is_pro_active') && execvitr_is_pro_active();
$pricing_mode = $is_pro ? 'pro' : 'free';
$has_cars = !empty($car_localize);

wp_localize_script('execvitr-js', 'ExecvitrData', [
	'km_price' => (float) get_option('execvitr_km_price', 0), // FREE: global km
	'minimum_price' => (float) get_option('execvitr_minimum_price', 0),
	'currency_symbol' => $currency_symbol,
	'currency_code' => $currency_code,
	'max_distance_km' => (float) get_option('execvitr_max_distance', 0),
	'country' => $selected_country,
	  'dial_code' => $dial_code,
  'dial_codes' => $dial_codes,
	'is_pro' => (bool) $is_pro,
	'pricing_mode' => $pricing_mode, // 'free' | 'pro'
	'has_cars' => (bool) $has_cars,
]);

wp_localize_script(
	'execvitr-js',
	'ExecvitrCars',
	['items' => $is_pro ? $car_localize : []]
);
?>
<form id="<?php echo esc_attr($form_id); ?>" class="vts-form execvitr-transfer-form" data-execvitr-form="1" data-execvitr-instance="<?php echo esc_attr($execvitr_form_uid); ?>" method="post" action="" novalidate>
	<?php wp_nonce_field('execvitr_form_submit', 'cc_nonce'); ?>

	<div class="form-wrapper">
		<div class="step-indicator" id="vts-steps">
			<div class="circle" data-step="1">1</div>
			<div class="line"></div>
			<div class="circle" data-step="2">2</div>
		</div>

		<div class="form-sections vts-grid-2">
			<div class="address-section">
				<h3><?php esc_html_e('Pick up Location', 'executive-vip-transfer-service'); ?></h3>
				<input type="text" id="address_from" name="address_from"
					placeholder="<?php echo esc_attr__('Street name & Locality Name', 'executive-vip-transfer-service'); ?>"
					required autocomplete="off">
				<input type="hidden" id="execvitr_origin_place_id" name="origin_place_id" value="">
			</div>

			<div class="address-section-2">
				<h3><?php esc_html_e('Drop Off Location', 'executive-vip-transfer-service'); ?></h3>
				<input type="text" id="address_to" name="address_to"
					placeholder="<?php echo esc_attr__('Street name & Locality Name', 'executive-vip-transfer-service'); ?>"
					required autocomplete="off">
				<input type="hidden" id="execvitr_origin_place_id" name="origin_place_id" value="">
			</div>
		</div>
	</div>

	<div class="execvitr-side-col">
		<?php if (function_exists('execvitr_is_pro_active') && execvitr_is_pro_active()): ?>
			<div id="execvitr-route-map" class="execvitr-map"></div>
		<?php endif; ?>
		<div id="execvitr-summary" class="execvitr-card">
			<div class="execvitr-card__bd">
				<div class="execvitr-metrics">
					<div class="execvitr-metric" aria-live="polite">
						<div class="execvitr-metric__icon" aria-hidden="true">↔︎</div>
						<div class="execvitr-metric__body">
							<div class="execvitr-metric__label">
								<?php echo esc_html__('Distance', 'executive-vip-transfer-service'); ?>
							</div>
							<div class="execvitr-metric__value"><span id="metric-distance">—</span></div>
						</div>
					</div>
					<div class="execvitr-metric" aria-live="polite">
						<div class="execvitr-metric__icon" aria-hidden="true">⏱</div>
						<div class="execvitr-metric__body">
							<div class="execvitr-metric__label">
								<?php echo esc_html__('Duration', 'executive-vip-transfer-service'); ?>
							</div>
							<div class="execvitr-metric__value"><span id="metric-duration">—</span></div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<?php if (function_exists('execvitr_is_pro_active') && execvitr_is_pro_active()): ?>
		<div class="package-section city-column1">
			<h3><?php esc_html_e('Cars', 'executive-vip-transfer-service'); ?></h3>
			<div class="cars-options">
				<?php if (!empty($car_localize)):
					foreach ($car_localize as $idx => $car):
						$img = $car['image'] ? $car['image'] : $plugin_url . 'car.png';
						$brand = $car['brand'] !== '' ? (string) $car['brand'] : __('Vehicle', 'executive-vip-transfer-service');
						$pid = 'vts_car_' . (int) $car['i']; ?>
						<label class="vts-car-card" for="<?php echo esc_attr($pid); ?>">
							<input type="radio" id="<?php echo esc_attr($pid); ?>" name="car_choice"
								value="<?php echo (int) $car['i']; ?>" required data-brand="<?php echo esc_attr($brand); ?>"
								data-passengers="<?php echo (int) $car['passengers']; ?>"
								data-luggage="<?php echo (int) $car['luggage']; ?>"
								data-kmprice="<?php echo esc_attr($car['km_price'] ?? 0); ?>">
							<div class="radio-card-inner">
								<img class="vts-car-thumb" src="<?php echo esc_url($img); ?>"
									alt="<?php echo esc_attr($brand); ?>" />
								<div class="vts-car-meta">
									<h4><?php echo esc_html($brand); ?></h4>
									<div class="vts-car-price">
										<span
											class="vts-car-price-label"><?php esc_html_e('Price', 'executive-vip-transfer-service'); ?>:</span>
										<span class="vts-car-price-amount"
											aria-live="polite"><?php echo esc_html($currency_symbol); ?> —</span>
									</div>

									<div class="vts-badges">
										<?php
										$passengers_label = sprintf(__('Passengers: %d', 'executive-vip-transfer-service'), (int) $car['passengers']);
										echo '<span class="vts-badge">' . esc_html($passengers_label) . '</span>';
										$luggage_label = sprintf(__('Luggage: %d', 'executive-vip-transfer-service'), (int) $car['luggage']);
										echo '<span class="vts-badge">' . esc_html($luggage_label) . '</span>';
										?>
									</div>
									<?php if (!empty($car['description'])): ?>
										<div class="vts-car-desc"><?php
$desc_raw = trim((string) ($car['description'] ?? ''));
if ($desc_raw !== '') {
    echo '<div class="execvitr-car-desc">'.esc_html($desc_raw).'</div>';
}
?>

</div>
									<?php endif; ?>
									<div class="vts-services">
										<span
											class="vts-service"><?php esc_html_e('Including Meet & Greet', 'executive-vip-transfer-service'); ?></span>
										<span
											class="vts-service"><?php esc_html_e('Free waiting time', 'executive-vip-transfer-service'); ?></span>
										<span
											class="vts-service"><?php esc_html_e('Door to Door', 'executive-vip-transfer-service'); ?></span>
									</div>
								</div>
							</div>
						</label>
					<?php endforeach; else: ?>
					<p>&mdash;
						<?php esc_html_e('No cars defined yet. Please add cars in the admin.', 'executive-vip-transfer-service'); ?>
					</p>
				<?php endif; ?>
			</div>
		</div>
	<?php endif; ?>

	<div class="city-column1">
		<h3><?php esc_html_e('Info', 'executive-vip-transfer-service'); ?></h3>
		<div class="info-grid">
			<div class="info-row info-row--name">
				<input type="text" id="sender_name" name="sender_name"
					placeholder="<?php echo esc_attr__('Name', 'executive-vip-transfer-service'); ?>" required>
				<input type="text" id="sender_surname" name="sender_surname"
					placeholder="<?php echo esc_attr__('Surname', 'executive-vip-transfer-service'); ?>" required>
			</div>
			<div class="phone_container col-span-2">
  <select class="phone_dial_select" id="<?php echo esc_attr($dial_code_id); ?>" name="dial_code" aria-label="Dial code">
    <?php foreach ($dial_codes as $cc => $dc): ?>
      <option value="<?php echo esc_attr($dc); ?>" <?php selected($dc, $dial_code); ?>>
        <?php echo esc_html($cc . ' +' . $dc); ?>
      </option>
    <?php endforeach; ?>
  </select>

  <input
    class="phone_input"
    autocomplete="off"
    placeholder="<?php echo esc_attr__('Phone number', 'executive-vip-transfer-service'); ?>"
    type="tel"
    name="pickup_phone"
    required
    inputmode="tel"
    pattern="[0-9\s\-\(\)]*"
  >

  <input type="hidden" name="pickup_phone_e164" id="<?php echo esc_attr($phone_e164_id); ?>" value="">
</div>

			<input type="email" class="col-span-2" name="user_email" autocomplete="on"
				placeholder="<?php echo esc_attr__('E-Mail', 'executive-vip-transfer-service'); ?>" required>
			<!-- Billing address fields are captured from Google Places (pickup) and sent as hidden inputs -->
			<input type="hidden" name="billing_address_1" id="execvitr_billing_address_1" value="">
			<input type="hidden" name="billing_city" id="execvitr_billing_city" value="">
			<input type="hidden" name="billing_postcode" id="execvitr_billing_postcode" value="">
			<input type="hidden" name="billing_country" id="execvitr_billing_country" value="">
			<div class="info-row info-row--counts">
				<input type="number" name="passenger_count" min="1"
					placeholder="<?php echo esc_attr__('Passengers', 'executive-vip-transfer-service'); ?>"
					inputmode="numeric" required>
				<input type="number" id="luggage_count" name="luggage_count" min="0" placeholder="<?php echo esc_attr__('Bag', 'executive-vip-transfer-service'); ?>" required inputmode="numeric" pattern="\d*">
			</div>
			<?php if (function_exists('execvitr_is_pro_active') && execvitr_is_pro_active()): ?>
				<div class="info-row info-row--datetime">
					<input type="date" name="pickup_date" required
						aria-label="<?php echo esc_attr__('Pickup date', 'executive-vip-transfer-service'); ?>">
					<input type="time" name="pickup_time" id="<?php echo esc_attr($pickup_time_id); ?>" required list="<?php echo esc_attr($time_slots_id); ?>"
						aria-label="<?php echo esc_attr__('Pickup time', 'executive-vip-transfer-service'); ?>">
				</div>
				<datalist id="<?php echo esc_attr($time_slots_id); ?>">
					<?php for ($h = 0; $h < 24; $h++) {
						foreach ([0, 30] as $m) {
							$val = sprintf('%02d:%02d', (int) $h, (int) $m);
							printf('<option value="%s"></option>', esc_attr($val));
						}
					} ?>
				</datalist>
				<div class="execvitr-return-toggle">
					<label class="execvitr-return-label" for="<?php echo esc_attr($return_toggle_id); ?>">
						<input type="checkbox" id="<?php echo esc_attr($return_toggle_id); ?>" name="is_return" value="1">
						<span class="execvitr-return-text">
							<span class="execvitr-return-title"><?php echo esc_html__( 'Return booking', 'executive-vip-transfer-service' ); ?></span>
							<span class="execvitr-return-sub"><?php echo esc_html__( 'Add a return date & time for a round trip.', 'executive-vip-transfer-service' ); ?></span>
						</span>
					</label>
				</div>

				<div class="execvitr-return-datetime info-row info-row--return" id="<?php echo esc_attr($return_fields_id); ?>" style="display:none;">
					<input type="date" name="return_date" aria-label="<?php echo esc_attr__( 'Return date', 'executive-vip-transfer-service' ); ?>">
					<input type="time" name="return_time" list="<?php echo esc_attr($time_slots_id); ?>" aria-label="<?php echo esc_attr__( 'Return time', 'executive-vip-transfer-service' ); ?>">
				</div>

			<?php endif; ?>
			<div class="execvitr-notes-box">
  <textarea id="execvitr_pickup_details" name="pickup_details" rows="2"
    placeholder="Flight number, terminal, hotel/lobby, child seat request, special instructions..."></textarea>
  <small class="execvitr-help">Optional. Add any details that help the driver.</small>
</div>

		</div>
	</div>

	<div class="city-column1">
		<h3><?php esc_html_e('Payment Type', 'executive-vip-transfer-service'); ?></h3>
		<div class="payment-type">
			<?php
				$cash_enabled = ( get_option('execvitr_enable_cash', 'yes') !== 'no' );
				$card_enabled = ( get_option('execvitr_enable_card', 'yes') !== 'no' );

				// Safety: if both disabled, keep card enabled to avoid empty form state.
				if ( ! $cash_enabled && ! $card_enabled ) {
					$card_enabled = true;
				}

				$default_payment = $cash_enabled ? 'cash' : 'card';
			?>

			<?php if ( $cash_enabled ) : ?>
				<label class="radio-card-footer">
					<input type="radio" name="payment" value="cash" <?php checked( $default_payment, 'cash' ); ?> >
					<div class="radio-card-inner">
						<img src="<?php echo esc_url( $plugin_url . 'banknotes.png' ); ?>"
							alt="<?php echo esc_attr__( 'Cash', 'executive-vip-transfer-service' ); ?>">
						<span class="radio-card-label"><?php esc_html_e( 'Cash', 'executive-vip-transfer-service' ); ?></span>
					</div>
				</label>
			<?php endif; ?>

			<?php if ( $card_enabled ) : ?>
				<label class="radio-card-footer">
					<input type="radio" name="payment" value="card" <?php checked( $default_payment, 'card' ); ?> >
					<div class="radio-card-inner">
						<img src="<?php echo esc_url( $plugin_url . 'debit-card.png' ); ?>"
							alt="<?php echo esc_attr__( 'Card', 'executive-vip-transfer-service' ); ?>">
						<span class="radio-card-label"><?php esc_html_e( 'Card', 'executive-vip-transfer-service' ); ?></span>
					</div>
				</label>
			<?php endif; ?>
</div>
	</div>

	<div class="form-footer-block city-column1">
		<div class="order-total-block">
			<div class="dv-order-total-price__label">
				<?php esc_html_e('Total: from', 'executive-vip-transfer-service'); ?>
			</div>
			<div class="dv-order-total-price__value">
				<span class="total-amount"
					id="vts-total-amount"><?php echo esc_html($currency_symbol . $min_price); ?></span>
				<span class="brand-note" id="vts-brand-note" aria-live="polite"></span>
			</div>
			<div class="order-total-tooltip">
				<span class="tooltip-icon" tabindex="0">?</span>
				<div class="tooltip-content">
					<?php esc_html_e('Price may vary depending on the selected service.', 'executive-vip-transfer-service'); ?>
				</div>
			</div>
		</div>

		<div class="distance-warning" id="vts-distance-warning" aria-live="polite"></div>

		<input type="hidden" name="distance_text" id="execvitr-distance-text" value="">
		<input type="hidden" name="distance_km" id="execvitr-distance-km" value="">
		<input type="hidden" name="duration_text" id="execvitr-duration-text" value="">

		<input type="hidden" name="form_type" value="delivery">
		<input type="hidden" name="cc_nonce" value="<?php echo esc_attr(wp_create_nonce('execvitr_form_submit')); ?>">

		<div class="form-submit-block">
			<button type="submit" class="form-submit-btn"
				id="vts-submit-btn"><?php esc_html_e('Submit Order', 'executive-vip-transfer-service'); ?></button>
		</div>
		<button type="button" class="action-link"
			onclick="window.scrollTo({top:0,behavior:'smooth'});"><?php esc_html_e('Go to top', 'executive-vip-transfer-service'); ?></button>
	</div>
</form>

<div id="cc-response" aria-live="polite"></div>