<?php
/**
 * @category Mageants HyvaSimpleConfigurable
 * @package Mageants_HyvaSimpleConfigurable
 * @copyright Copyright (c) 2023 Mageants
 * @author Mageants Team <info@mageants.com>
 */

declare(strict_types=1);

use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\ProductAttributes;
use Hyva\Theme\ViewModel\ProductCompare;
use Hyva\Theme\ViewModel\ProductPage;
use Hyva\Theme\ViewModel\Wishlist;
use Magento\Catalog\Model\Product;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;

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

/** @var ProductPage $productViewModel */
$productViewModel = $viewModels->require(ProductPage::class);
/** @var ProductAttributes $attributesViewModel */
$attributesViewModel = $viewModels->require(ProductAttributes::class);
/** @var Product $product */
$product = $productViewModel->getProduct();

?>
<div class="w-full mb-6">

    <div class="my-2 flex">
        <?= $block->getChildHtml('product.info.review') ?>
    </div>

    <div class="flex flex-col sm:flex-row justify-between my-4">
        <?= $block->getChildHtml("product.info.stockstatus") ?>
        <?= $block->getChildHtml("alert.urls") ?>
    </div>

    <div id="product-details">
        <div class="flex border-t border-gray-300 py-2 last:mb-6 last:border-b product-qty-div hidden">
            <span class="w-1/2 text-left text-gray-700 product-detail-label">
                <?= 'QTY' ?>
            </span>
            <span class="w-1/2 ml-2 text-left text-gray-900 product-detail-value product-qty" >
                <?= $escaper->escapeHtml($product->getQty()) ?>
            </span>
        </div>
        <?php foreach ($block->getAttributes() as $attributeConfig) {
            $attribute = $attributesViewModel->getAttributeFromLayoutConfig($attributeConfig); ?>
            <?php if ($value = $attribute['value'] ?? null) { ?>
                <div class="flex border-t border-gray-300 py-2 last:mb-6 
                    last:border-b <?=/* @noEscape */ $attribute['value'] ?: "" ?>
                    attribute-<?=/* @noEscape */ $attribute['code'] ?: "" ?>">
                    <span class="w-1/2 text-left text-gray-700 product-detail-label">
                        <?= $escaper->escapeHtml($attribute['label']) ?>
                    </span>
                    <span class="<?= $attribute['code'] == 'sku' ? 'product-sku' : '';?> 
                    w-1/2 ml-2 text-left text-gray-900 product-detail-value" >
                        <?= $escaper->escapeHtml($value) ?>
                    </span>
                </div>
            <?php } ?>
        <?php } ?>
    </div>

    <?= $block->getChildHtml('product.info.form') ?>

    <div class="flex flex-col sm:flex-row items-end my-4">

        <div>
            <?= $block->getChildHtml("product.info.price") ?>
        </div>

        <div class="flex mt-4 ml-auto sm:mt-0">
            <?php if ($product->isSaleable()): ?>
                <?= $block->getChildHtml("product.info.quantity") ?>
                <?= $block->getChildHtml("product.info.addtocart") ?>
            <?php endif; ?>
        </div>

    </div>

    <?php if ($product->isSaleable()): ?>
        <div class="flex mt-4 justify-end">
            <?= $block->getChildHtml('addtocart.shortcut.buttons') ?>
        </div>
    <?php endif; ?>

    <div class="flex mt-4 justify-end">
        <?= $block->getChildHtml('product.info.addtowishlist'); ?>
        <?= $block->getChildHtml('product.info.addtocompare'); ?>
        <?= $block->getChildHtml('product.info.emailtofriend'); ?>
        <?= $block->getChildHtml('product.info.additional.actions'); ?>
    </div>

    <?php if ($tierPriceBlock = $block->getChildHtml("product.price.tier")): ?>
        <div class="py-4 my-2 tier-price-container">
            <?= /** @noEscape */ $tierPriceBlock ?>
        </div>
    <?php endif; ?>

    <?= $block->getChildHtml("product.info.additional") ?>
</div>
