<?php

use rentsyst\admin\components\Rentsyst_Language;
use rentsyst\includes\Rentsyst_PluginSettings;
use rentsyst\site\Rentsyst_BookingWidget;
use rentsyst\site\Rentsyst_ResourceLoader;

add_shortcode( 'rentsyst_booking', function ($params)
{
	$script = '';
	if ( isset( $_GET['rentsyst-book-by-vehicle-id'] ) ) {
		$vehicleId = wp_json_encode( sanitize_text_field( $_GET['rentsyst-book-by-vehicle-id'] ) );
		$script = <<<JS
		document.addEventListener("rentsyst:search_ready", function () {
    		jQuery(document).ready(function() { bookCar($vehicleId) }); 		 
		});
JS;
	}
	if ( isset( $_GET['rentsyst-book-by-search-params'] ) ) {
		$location_from = sanitize_text_field( $_GET['locationFrom'] );
		$location_to = isset($_GET['locationTo']) ? sanitize_text_field( $_GET['locationTo'] ) : $location_from;
		$date_from = sanitize_text_field( $_GET['dateFrom'] );
		$date_to = sanitize_text_field( $_GET['dateTo'] );
		$script = <<<JS
		document.addEventListener("rentsyst:search_ready", function () {
			window.searchCars({
				dateFrom: "$date_from",
  				dateTo: "$date_to",
  				locationFrom: "$location_from",
  				locationTo: "$location_to",
			})		 
		});
JS;
	}

	if ((!is_main_query() || !in_the_loop()) && !isset($params['force_output'])) {
		return 'Booking page';
	}

	$widgetOutput = Rentsyst_BookingWidget::widget($params);

	wp_add_inline_script(Rentsyst_ResourceLoader::PREFIX . 'bundle_front', $script);

	return $widgetOutput;
} );

add_shortcode( 'rentsyst_search_form', function ($params) {
	$params = shortcode_atts( [
		'orientation'  => 'horizontal',
		'color_main'   => '',
		'color_second' => '',
		'text_color'   => '',
		'bg_color'     => '',
		'margin_top'   => 0,
	], $params );

	$settings = [
		'disabledBlocks' => ['header_text'],
		'tabs'           => false,
		'steps'          => [
			0 => [
				'view' => $params['orientation']
			]
		],
	];

	if ( ! empty( $params['color_main'] ) ) {
		$settings['colorMain'] = [
			'custom' => true,
			'value'  => sanitize_hex_color( $params['color_main'] )
		];
	}

	if ( ! empty( $params['color_second'] ) ) {
		$settings['colorSecond'] = [
			'custom' => true,
			'value'  => sanitize_hex_color( $params['color_second'] )
		];
	}

	if ( ! empty( $params['text_color'] ) ) {
		$settings['textColor'] = [
			'custom' => true,
			'value'  => sanitize_hex_color( $params['text_color'] )
		];
	}

	if ( ! empty( $params['bg_color'] ) ) {
		$settings['bgColor'] = [
			'custom' => true,
			'value'  => sanitize_hex_color( $params['bg_color'] )
		];
	}

	$marginTop = intval( $params['margin_top'] );

	// JavaScript handler for search button redirect
	if ( $page_id = get_option( Rentsyst_PluginSettings::BOOKING_PAGE_ID ) ) {
		$page_id = Rentsyst_Language::getConnectedPagesId( $page_id )[ Rentsyst_Language::getLanguageCode() ] ?? $page_id;
		$booking_page_url = get_permalink( $page_id );
		$script = <<<JS
document.addEventListener("rentsyst:search", function (event) {
	let rsBokingPageUrl = "$booking_page_url";
	let params = new URLSearchParams(event.detail.order).toString();
	params += '&' + 'rentsyst-book-by-search-params=1';
	if(rsBokingPageUrl.indexOf('?') === -1) {
		window.location.href = rsBokingPageUrl + '?' + params;
	} else {
		window.location.href = rsBokingPageUrl + '&' + params;
	}
	return false;
});
JS;
		wp_register_script( Rentsyst_ResourceLoader::PREFIX . 'search_form_shortcode', '' );
		wp_enqueue_script( Rentsyst_ResourceLoader::PREFIX . 'search_form_shortcode' );
		wp_add_inline_script( Rentsyst_ResourceLoader::PREFIX . 'search_form_shortcode', $script );
	}

	$widgetOutput = Rentsyst_BookingWidget::widget( $settings );

	return '<div style="position: relative; z-index: 10; margin-top: ' . $marginTop . 'px" class="rentsyst-booking-widget-wrapper">' . $widgetOutput . '</div>';
} );

