<?php
/**
 * Path: templates/tracking-lite.php
 * Purpose: Show "Booking Details" here and move payment to WooCommerce order-pay.
 */
if ( ! defined( 'ABSPATH' ) ) exit;

get_header();

/* ---------- input / guards ---------- */
$order_id = isset($_GET['order_id']) ? absint( wp_unslash($_GET['order_id']) ) : 0;
$nonce    = isset($_GET['cc_nonce']) ? sanitize_text_field( wp_unslash($_GET['cc_nonce']) ) : '';

if ( ! $order_id ) { echo '<div class="execvitr-wrap"><p>'.esc_html__('Invalid or missing order id.','executive-vip-transfer-service').'</p></div>'; get_footer(); return; }
if ( ! $nonce || ! wp_verify_nonce( $nonce, 'cc_track_order_' . $order_id ) ) { echo '<div class="execvitr-wrap"><p>'.esc_html__('Invalid or expired tracking link.','executive-vip-transfer-service').'</p></div>'; get_footer(); return; }
if ( ! function_exists('wc_get_order') ) { echo '<div class="execvitr-wrap"><p>'.esc_html__('WooCommerce is required.','executive-vip-transfer-service').'</p></div>'; get_footer(); return; }

$order = wc_get_order( $order_id );
if ( ! $order ) { echo '<div class="execvitr-wrap"><p>'.esc_html__('Order not found.','executive-vip-transfer-service').'</p></div>'; get_footer(); return; }

/* ---------- booking card helper (kept local to this template) ---------- */
if ( ! function_exists('cc_execvitr_render_booking_details_card') ) {
	function cc_execvitr_render_booking_details_card( WC_Order $order ): void {
		$esc = static fn($v) => $v === null ? '' : esc_html((string)$v);

		$ref_no        = $order->get_order_number();
		$total_html    = $order->get_formatted_order_total();
		$created       = $order->get_date_created() ? $order->get_date_created()->date_i18n( get_option('date_format').' '.get_option('time_format') ) : '';

		// Adjust keys if yours differ
		$origin        = (string) $order->get_meta('_cc_address_from');
		$destination   = (string) $order->get_meta('_cc_address_to');
		$distance_text = (string) $order->get_meta('_cc_distance_text');
		$duration_text = (string) $order->get_meta('_cc_duration');
		$passengers    = $order->get_meta('_cc_passenger_count');
		$luggage       = $order->get_meta('_cc_luggage_count');
		$pickup_at     = (string) $order->get_meta('_cc_pickup_datetime'); // if exists
		$vehicle       = (string) $order->get_meta('_cc_vehicle_name');    // optional

		?>
		<style>
			.execvitr-wrap { max-width: 900px; margin: 24px auto; padding: 6px 16px 24px; }
			.cc-bdet-card { border:1px solid #e5e7eb; border-radius:12px; background:#fff; margin:0 0 18px; padding:16px; }
			.cc-bdet-h { margin:0 0 10px; font-size:18px; }
			.cc-bdet-row { display:flex; justify-content:space-between; gap:8px; margin:6px 0; }
			.cc-bdet-k { color:#6b7280; }
		</style>
		<div class="cc-bdet-card">
			<h3 class="cc-bdet-h"><?php echo esc_html__('Reservation Summary','executive-vip-transfer-service'); ?></h3>
			<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Order','executive-vip-transfer-service'); ?></span><span>#<?php echo $esc($ref_no); ?></span></div>
			<?php if ($created): ?>
				<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Created','executive-vip-transfer-service'); ?></span><span><?php echo $esc($created); ?></span></div>
			<?php endif; ?>
			<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Total','executive-vip-transfer-service'); ?></span><span><?php echo wp_kses_post($total_html); ?></span></div>
			<?php if ($pickup_at): ?>
				<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Pickup Time','executive-vip-transfer-service'); ?></span><span><?php echo $esc($pickup_at); ?></span></div>
			<?php endif; ?>
			<?php if ($vehicle): ?>
				<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Vehicle','executive-vip-transfer-service'); ?></span><span><?php echo $esc($vehicle); ?></span></div>
			<?php endif; ?>
			<?php if ($origin || $destination): ?>
				<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Route','executive-vip-transfer-service'); ?></span><span><?php echo $esc($origin); ?> → <?php echo $esc($destination); ?></span></div>
			<?php endif; ?>
			<?php if ($distance_text): ?>
				<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Distance','executive-vip-transfer-service'); ?></span><span><?php echo $esc($distance_text); ?></span></div>
			<?php endif; ?>
			<?php if ($duration_text): ?>
				<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Duration','executive-vip-transfer-service'); ?></span><span><?php echo $esc($duration_text); ?></span></div>
			<?php endif; ?>
			<?php if ($passengers): ?>
				<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Passengers','executive-vip-transfer-service'); ?></span><span><?php echo (int) $passengers; ?></span></div>
			<?php endif; ?>
			<?php if ($luggage): ?>
				<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Luggage','executive-vip-transfer-service'); ?></span><span><?php echo (int) $luggage; ?></span></div>
			<?php endif; ?>
		</div>
		<?php
	}
}

/* ---------- page output ---------- */
$needs_payment = ( method_exists($order,'payment_needed') && $order->payment_needed() ) || $order->needs_payment();
$pay_url       = $order->get_checkout_payment_url();

?>
<div class="execvitr-wrap">
	<h1 style="margin:8px 0 16px"><?php echo esc_html__('Reservation Tracking','executive-vip-transfer-service'); ?></h1>

	<?php cc_execvitr_render_booking_details_card( $order ); ?>

	<?php if ( $needs_payment ): ?>
		<?php
		if ( ! headers_sent() ) {
			// WHY: Ödeme WooCommerce tarafında yönetilsin.
			wp_safe_redirect( $pay_url );
			exit;
		}
		?>
		<p><?php echo esc_html__('Payment is required. Continue to the secure checkout to complete your booking.','executive-vip-transfer-service'); ?></p>
		<p style="margin:18px 0">
			<a class="button" href="<?php echo esc_url($pay_url); ?>">
				<?php echo esc_html__('Go to secure payment','executive-vip-transfer-service'); ?>
			</a>
		</p>
	<?php else: ?>
		<?php $pdf_url = $order->get_meta('_cc_pdf_url') ?: ''; ?>
		<p style="margin:0 0 8px"><?php echo esc_html__('Your reservation is confirmed.','executive-vip-transfer-service'); ?></p>
		<?php if ( $pdf_url ): ?>
			<p style="margin:10px 0 0">
				<a class="button" href="<?php echo esc_url($pdf_url); ?>" target="_blank" rel="noopener">
					<?php echo esc_html__('Download Ticket (PDF)','executive-vip-transfer-service'); ?>
				</a>
			</p>
		<?php endif; ?>
	<?php endif; ?>
</div>
<?php
get_footer();
