<?php
// Получаем корзину WooCommerce
$cart = WC()->cart->get_cart();
$address = carbon_get_theme_option('contact_address');
$address_map_link = carbon_get_theme_option('contact_address_map_link');
// Проверяем, есть ли товары в корзине
if (!empty($cart)) {
    // Берём последний добавленный товар (предполагается, что это последний элемент массива)
    $last_cart_item = end($cart);
    
    // Получаем данные товара
    $product_id = $last_cart_item['product_id'];
    $product = wc_get_product($product_id);

    $product_custom = get_post_meta($product_id, '_custom_product_full_price', true);
    
    // Имя товара
    $product_name = $product->get_name();
    
    // Цена товара
    $product_price = $product->get_price();
    
    // Мета информация товара
    $product_meta_data = $product->get_meta_data();

    $product_info = (object) [
        'name' => $product_name,
        'price' => $product_price,
        'meta' => $product_meta_data
    ];

    $first_name = WC()->session->get('billing_first_name');
    $last_name = WC()->session->get('billing_last_name');
    $phone = WC()->session->get('billing_phone');
    $email = WC()->session->get('billing_email');
    $reserved_time = WC()->session->get('reserved_time');
    // Ensure the Intl extension is enabled in your PHP configuration
    $date = new DateTime($reserved_time); // Or however you get your DateTime object
	$date->modify('-2 hours');
    $locale = 'pl_PL'; // Polish locale
    $formatter = new IntlDateFormatter(
        $locale,
        IntlDateFormatter::LONG, // or IntlDateFormatter::MEDIUM for shorter formats
        IntlDateFormatter::SHORT,
        'Europe/Warsaw', // Set the timezone if necessary
        IntlDateFormatter::GREGORIAN,
        'd MMMM yyyy, HH:mm' // Use 'd' for day of month, 'MMMM' for full month name, 'yyyy' for year, 'HH:mm' for 24-hour time
    );

    $readable_date = $formatter->format($date);
}

get_header();
?>
<main id="checkout">
    <div class="checkout__wrapper">
        <div class="checkout__inner">
            <h1>Potwierdzenie informacji o wizycie:</h1>
            <div class="product__info">
                <div>Nazwa: </div>
                <div><?=carbon_get_post_meta('11', 'homepage_title')?></div>
                <div>Adres: </div>
                <div><a href="<?=$address_map_link?>" target="_blank"><?=$address?></a></div>
                <div>Diagnostyka<br/>USG:</div>
                <div><?=$product_name?></div>
                <div>Cena: </div>
                <div><?=$product_custom?> zł</div>
                <div>Data i godzina: </div>
                <div><?=$readable_date?></div>
                <div>Imie i nazwisko pacjenta: </div>
                <div><?=$first_name?></div>
            </div>

            <div class="payment__wrapper">
                <p>Aby potwierdzić rejestrację na badanie należy opłacić<br/>częściowo wizytę</p>
                <div class="to-pay">PŁACISZ: <span class="highlighted-text"><?=$product_price?> zł</span></div>
                <a id="order_front">WPŁAĆ ZALICZKĘ</a>
                <div class="payment-info">Pozostałą kwotę <span class="highlighted-text"><?=$product_custom - $product_info->price?> zł</span> zapłacisz w placówce</div>
            </div>
        </div>
    </div>
    <div class="back">
        <a href="<?=home_url() ?>"><img src="<?=get_template_directory_uri()?>/assets/images/arrow-checkout.svg" alt="arrow"> <span>WRÓC</span></a>
    </div>
</main>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const order_front = document.getElementById('order_front');
    const address = document.getElementById('billing_address_1');
	const city = document.getElementById('billing_city');
	const country = document.getElementById('billing_country');
	const postcode = document.getElementById('billing_postcode');

	

    // Check if elements exist before trying to set their values
    const firstNameInput = document.querySelector('input[name="billing_first_name"]');
    if (firstNameInput) {
        firstNameInput.value = '<?=$first_name?>';
    }

    const phoneInput = document.querySelector('input[name="billing_phone"]');
    if (phoneInput) {
        phoneInput.value = '<?=$phone?>';
    }

    const emailInput = document.querySelector('input[name="billing_email"]');
    if (emailInput) {
        emailInput.value = '<?=$email?>';
    }

    const reservedTimeInput = document.querySelector('input[name="reserved_time"]');
    if (reservedTimeInput) {
        reservedTimeInput.value = '<?=$reserved_time?>';
    }

    if (order_front) {
        order_front.onclick = (e) => {
            const order = document.getElementById('place_order');
            e.preventDefault();
            // Only click the order button if it exists
            address.value = 'ul. Młynarska 42';
			country.value = 'PL'
			postcode.value = '01-171';
			order_front.innerText = 'W trakcie realizacji...';
            if (order) {
                order.click();
            }
        };
    }
});

</script>

<?php
the_content();
get_footer();