<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes 2020-present. All rights reserved.
 * This product is licensed per Magento install
 * See https://hyva.io/license
 */

declare(strict_types=1);

use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\CurrentProduct;
use Hyva\Theme\ViewModel\Modal;
use Magento\Catalog\Model\Product;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;

// phpcs:disable Magento2.Templates.ThisInTemplate.FoundThis

/** @var Template $block */
/** @var Escaper $escaper */
/** @var ViewModelRegistry $viewModels */

/** @var CurrentProduct $currentProduct */
$currentProduct = $viewModels->require(CurrentProduct::class);

$product = $block->hasData('product')
    ? $block->getData('product')
    : $currentProduct->get();

if (!$product || !$product->getId()) {
    return;
}

$delivText = $product->getData('delivery_text');

?>

<?php if (!empty($delivText) && strtolower($delivText) !== 'no'): ?>

<div class="border-b border-gray-300 pb-4 hover:decoration-blue-400 flex items-center gap-4">
    <svg width="22px" height="22px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M3 9H21M7 3V5M17 3V5M6 13H8M6 17H8M11 13H13M11 17H13M16 13H18M16 17H18M6.2 21H17.8C18.9201 21 19.4802 21 19.908 20.782C20.2843 20.5903 20.5903 20.2843 20.782 19.908C21 19.4802 21 18.9201 21 17.8V8.2C21 7.07989 21 6.51984 20.782 6.09202C20.5903 5.71569 20.2843 5.40973 19.908 5.21799C19.4802 5 18.9201 5 17.8 5H6.2C5.0799 5 4.51984 5 4.09202 5.21799C3.71569 5.40973 3.40973 5.71569 3.21799 6.09202C3 6.51984 3 7.07989 3 8.2V17.8C3 18.9201 3 19.4802 3.21799 19.908C3.40973 20.2843 3.71569 20.5903 4.09202 20.782C4.51984 21 5.07989 21 6.2 21Z" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> </g>
    </svg>
    <div onclick="openDelivDialog()" class="cursor-pointer hover:text-gray-800 hover:underline font-sen"><?php echo $delivText; ?></div>
</div>

<?php endif;?>
<?php

$deliveryDetails = $product->getData('deliverytimedetails') ?? '';
//$deliveryDetails= $escaper->escapeHtml($deliveryDetails);
$deliveryDetails= html_entity_decode($deliveryDetails);

$modal = $viewModels->require(Modal::class)
    ->createModal()
    ->withDialogRefName('deliv-cost')
    ->withContent(
        '
   <div class="relative w-full h-full max-w-2xl md:h-auto">
     <!-- Modal header with close button -->
     <div class="flex items-center justify-between pb-4 border-b rounded-t dark:border-gray-600">

         <h3 class="text-xl font-semibold text-gray-900  text-center w-full">
        Delivery Time Details
    </h3>
        <button @click="hide" type="button"
    class="text-gray-400 hover:text-gray-500 border-none outline-none focus:outline-none p-1">
    <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
    </svg>
       </button>
     </div>
     <!-- Modal body -->
     <div class="p-6 space-y-6 prose max-w-none">
        ' . $deliveryDetails . '
     </div>
   </div>
<script>
  const handleModal = () => {
       return{
           iAccept(){
               this.hide();
           }
       }
   }
</script>'
    );
?>
<div x-data="{
    ...hyva.modal(),
    init() {
        window.openDelivDialog = function (event) {
            this.show('deliv-cost', event);
        }.bind(this);
    }
}" x-init="init()">
    <?= $modal ?>
</div>




