<?php
/**
 * Safely convert CakePHP ResultSet, Collection, Traversable,
 * array or null value into a PHP array.
 */
$convertToArray = static function ($value) {
    if (is_array($value)) {
        return $value;
    }

    if (is_object($value) && method_exists($value, 'toArray')) {
        $convertedValue = $value->toArray();

        return is_array($convertedValue) ? $convertedValue : [];
    }

    if ($value instanceof \Traversable) {
        return iterator_to_array($value);
    }

    return [];
};

/*
 * Normalize all database variables.
 * This prevents foreach() and array-related errors when data is null.
 */
$currencyrate       = $convertToArray($currencyrate ?? null);
$queryslider        = $convertToArray($queryslider ?? null);
$querynew           = $convertToArray($querynew ?? null);
$querywishlists     = $convertToArray($querywishlists ?? null);
$queryhomecategories = $convertToArray($queryhomecategories ?? null);
$querysignature     = $convertToArray($querysignature ?? null);

/*
 * Create wishlist lookup.
 * This replaces array_column(), which was receiving null.
 */
$wishlistLookup = [];

foreach ($querywishlists as $wishadded) {
    $wishlistKey = null;

    if (is_array($wishadded)) {
        $wishlistKey = $wishadded['mykey'] ?? null;
    } elseif (is_object($wishadded)) {
        $wishlistKey = $wishadded->mykey ?? null;
    }

    if ($wishlistKey !== null && $wishlistKey !== '') {
        $wishlistLookup[(string) $wishlistKey] = true;
    }
}

/*
 * Safely read currency information.
 */
$session = $this->request->getSession();

$currencyCode = $session->read('currency');
$currencySymbol = $session->read('currencysymbol');

if (empty($currencySymbol)) {
    $currencySymbol = 'Rs. ';
}

$currencyMultiplier = 1;

if (
    isset($currencyrate[0]) &&
    is_array($currencyrate[0]) &&
    isset($currencyrate[0][$currencyCode]) &&
    is_numeric($currencyrate[0][$currencyCode])
) {
    $currencyMultiplier = (float) $currencyrate[0][$currencyCode];
} elseif (
    isset($currencyrate[$currencyCode]) &&
    is_numeric($currencyrate[$currencyCode])
) {
    $currencyMultiplier = (float) $currencyrate[$currencyCode];
}
?>

<div class="banner owl-carousel owl-theme">
    <?php foreach ($queryslider as $slider) : ?>
        <?php
        $sliderUrl = $slider['url'] ?? '';
        $sliderPicture = $slider['picture'] ?? '';
        ?>

        <?php if (!empty($sliderPicture)) : ?>
            <div class="item">
                <?php if (!empty($sliderUrl)) : ?>
                    <?php
                    echo $this->Html->link(
                        $this->Html->image(
                            $sliderPicture,
                            [
                                'title' => $sliderPicture,
                                'alt' => $sliderPicture
                            ]
                        ),
                        $sliderUrl,
                        ['escape' => false]
                    );
                    ?>
                <?php else : ?>
                    <?php
                    echo $this->Html->image(
                        $sliderPicture,
                        [
                            'title' => $sliderPicture,
                            'alt' => $sliderPicture
                        ]
                    );
                    ?>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    <?php endforeach; ?>
</div>

<div class="bannermobile">
    <?php foreach ($queryslider as $slider) : ?>
        <?php
        $sliderUrl = $slider['url'] ?? '';
        $mobileBanner = $slider['bannermobile'] ?? '';
        ?>

        <?php if (!empty($mobileBanner)) : ?>
            <div class="item">
                <?php if (!empty($sliderUrl)) : ?>
                    <?php
                    echo $this->Html->link(
                        $this->Html->image(
                            $mobileBanner,
                            [
                                'title' => $mobileBanner,
                                'alt' => $mobileBanner
                            ]
                        ),
                        $sliderUrl,
                        ['escape' => false]
                    );
                    ?>
                <?php else : ?>
                    <?php
                    echo $this->Html->image(
                        $mobileBanner,
                        [
                            'title' => $mobileBanner,
                            'alt' => $mobileBanner
                        ]
                    );
                    ?>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    <?php endforeach; ?>
</div>

<section class="new-collections visible-effect">
    <div class="container">
        <div class="row">
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <h6 class="heading-animation">New Arrivals</h6>
            </div>

            <div class="category">
                <?php foreach ($querynew as $product) : ?>
                    <?php
                    $productKey = $product['mykey'] ?? '';
                    $productUrl = $product['url'] ?? '';
                    $productName = $product['name'] ?? '';
                    $productPicture = $product['picture1'] ?? '';
                    $productPrice = $product['price'] ?? 0;
                    $productMrp = $product['mrp'] ?? 0;
                    $productDiscount = $product['discount'] ?? 0;

                    $isWishlisted = (
                        $productKey !== '' &&
                        isset($wishlistLookup[(string) $productKey])
                    );
                    ?>

                    <div class="product-box">
                        <div class="img-box">
                            <?php
                            echo $this->Html->link(
                                $this->Html->image(
                                    $productPicture,
                                    ['alt' => $productName]
                                ),
                                [
                                    'controller' => 'details',
                                    'action' => 'detail',
                                    $productUrl
                                ],
                                ['escape' => false]
                            );
                            ?>

                            <div
                                class="add-to-wishlist"
                                id="<?php echo h($productKey); ?>"
                            >
                                <?php
                                echo $this->Html->image(
                                    $isWishlisted
                                        ? 'wishlist-fill.png'
                                        : 'wishlist-empty.png',
                                    ['alt' => 'Wishlist']
                                );
                                ?>
                            </div>
                        </div>

                        <div class="product-summary">
                            <div class="name">
                                <?php
                                echo $this->Html->link(
                                    h($productName),
                                    [
                                        'controller' => 'details',
                                        'action' => 'detail',
                                        $productUrl
                                    ],
                                    ['escape' => false]
                                );
                                ?>
                            </div>

                            <div class="price">
                                <div class="new-price">
                                    Rs. <?php echo h($productPrice); ?>
                                </div>

                                <?php if ((string) $productDiscount !== '0') : ?>
                                    <div class="old-price">
                                        Rs. <?php echo h($productMrp); ?>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
</section>

<section class="design-led visible-effect">
    <div class="container">
        <div class="row">
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <h2 class="heading-animation">Design-Led Categories</h2>

                <div class="category">
                    <?php foreach ($queryhomecategories as $product) : ?>
                        <?php
                        $categoryName = $product['name'] ?? '';
                        $categoryPicture = $product['picture'] ?? '';

                        $categorySlug = strtolower(
                            str_replace(' ', '-', trim($categoryName))
                        );
                        ?>

                        <div class="box">
                            <?php
                            echo $this->Html->link(
                                $this->Html->image(
                                    $categoryPicture,
                                    ['alt' => $categoryName]
                                ) .
                                '<span>' . h($categoryName) . '</span>',
                                [
                                    'controller' => 'products',
                                    'action' => 'category',
                                    $categorySlug
                                ],
                                ['escape' => false]
                            );
                            ?>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    </div>
</section>

<section class="expert-guidance visible-effect">
    <div class="container">
        <div class="row">
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <div class="box">
                    <h5>For Offline Sales and Dealership Enquiry</h5>

                    <div class="whatsapp-button">
                        <?php
                        echo $this->Html->link(
                            '+91 9351190805',
                            'https://wa.me/919351190805/?text=Hello%20Meridio%20Luxury%20Fans',
                            [
                                'escape' => false,
                                'target' => '_blank',
                                'rel' => 'noopener'
                            ]
                        );
                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<section class="new-collections visible-effect">
    <div class="container">
        <div class="row">
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <h3 class="heading-animation">The Signature Choice</h3>
            </div>

            <div class="category">
                <?php foreach ($querysignature as $product) : ?>
                    <?php
                    $productKey = $product['mykey'] ?? '';
                    $productUrl = $product['url'] ?? '';
                    $productName = $product['name'] ?? '';
                    $productPicture = $product['picture1'] ?? '';
                    $productPrice = (float) ($product['price'] ?? 0);
                    $productMrp = (float) ($product['mrp'] ?? 0);
                    $productDiscount = $product['discount'] ?? 0;

                    $isWishlisted = (
                        $productKey !== '' &&
                        isset($wishlistLookup[(string) $productKey])
                    );

                    $convertedPrice = intval(
                        $productPrice * $currencyMultiplier
                    );

                    $convertedMrp = intval(
                        $productMrp * $currencyMultiplier
                    );
                    ?>

                    <div class="product-box">
                        <div class="img-box">
                            <?php
                            echo $this->Html->link(
                                $this->Html->image(
                                    $productPicture,
                                    ['alt' => $productName]
                                ),
                                [
                                    'controller' => 'details',
                                    'action' => 'detail',
                                    $productUrl
                                ],
                                ['escape' => false]
                            );
                            ?>

                            <?php if ((string) $productDiscount !== '0') : ?>
                                <span>
                                    <?php echo h($productDiscount); ?>% Off
                                </span>
                            <?php endif; ?>

                            <div
                                class="add-to-wishlist<?php echo $isWishlisted ? ' red' : ''; ?>"
                                id="<?php echo h($productKey); ?>"
                            >
                                <i class="icon icon-valentines-heart"></i>
                            </div>
                        </div>

                        <div class="product-summary">
                            <div class="name">
                                <?php
                                echo $this->Html->link(
                                    h($productName),
                                    [
                                        'controller' => 'details',
                                        'action' => 'detail',
                                        $productUrl
                                    ],
                                    ['escape' => false]
                                );
                                ?>
                            </div>

                            <?php if ((string) $productDiscount !== '0') : ?>
                                <div class="price">
                                    <div class="new-price">
                                        <?php
                                        echo h($currencySymbol);
                                        echo h($convertedPrice);
                                        ?>
                                    </div>

                                    <div class="old-price">
                                        <?php
                                        echo h($currencySymbol);
                                        echo h($convertedMrp);
                                        ?>
                                    </div>
                                </div>
                            <?php else : ?>
                                <div class="price">
                                    <div class="new-price">
                                        <?php
                                        echo h($currencySymbol);
                                        echo h($convertedPrice);
                                        ?>
                                    </div>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
</section>

<section class="standy visible-effect">
    <div class="container">
        <div class="box">
            <div class="row">
                <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                    <h4 class="heading-animation">
                        Benefits for Direct Buyers
                    </h4>

                    <p>
                        Become a Meridio Fans Priority Customer by purchasing
                        directly from our official website and unlock premium
                        privileges designed for your peace of mind and
                        satisfaction:
                    </p>

                    <ul class="list">
                        <li>
                            <strong>100% Transit Insurance:</strong>
                            Full protection until your product reaches you.
                        </li>

                        <li>
                            <strong>Priority Dispatch:</strong>
                            Your order gets shipped faster than on any other
                            platform.
                        </li>

                        <li>
                            <strong>Extended Warranty:</strong>
                            Extra coverage beyond standard warranty terms.
                        </li>

                        <li>
                            <strong>Dedicated Support Team:</strong>
                            Get direct access to our expert customer-care
                            specialists.
                        </li>
                    </ul>

                    <div class="home-icon-carousle">
                        <div class="box">
                            <?php
                            echo $this->Html->image(
                                'free-shipping.png',
                                ['alt' => 'Free Shipping']
                            );
                            ?>
                            <span>Free Shipping</span>
                        </div>

                        <div class="box">
                            <?php
                            echo $this->Html->image(
                                'free-gift.png',
                                ['alt' => 'Free Gift']
                            );
                            ?>
                            <span>Free Gift</span>
                        </div>

                        <div class="box">
                            <?php
                            echo $this->Html->image(
                                'customer-support.png',
                                ['alt' => 'Customer Support']
                            );
                            ?>
                            <span>Customer Support</span>
                        </div>

                        <div class="box">
                            <?php
                            echo $this->Html->image(
                                'safe-payment.png',
                                ['alt' => 'Safe Payment']
                            );
                            ?>
                            <span>Safe Payment</span>
                        </div>
                    </div>
                </div>

                <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                    <video
                        style="width: 100%;"
                        src="img/home-video.mp4"
                        autoplay
                        loop
                        muted
                        playsinline
                        controlslist="nodownload"
                    ></video>
                </div>

                <div class="corners top-left"></div>
                <div class="corners top-right"></div>
                <div class="corners bottom-left"></div>
                <div class="corners bottom-right"></div>
            </div>
        </div>
    </div>
</section>