<?php
/* @var Tangibledesign\MyHome\Widgets\General\CustomCardListWidget $mhCurrentWidget */
global $mhCurrentWidget;
?>
<div class="myhome-term-list">
    <?php foreach ($mhCurrentWidget->getCards() as $mhCardData) :
        $mhImageSize = $mhCurrentWidget->getImageSizeSetting();
        $mhImageSrcset = !empty($mhCardData['image']['id']) ? wp_get_attachment_image_srcset($mhCardData['image']['id'], $mhImageSize) : '';
        $mhImageUrl = '';
        if (!empty($mhCardData['image']['id'])) {
            $mhImageUrl = wp_get_attachment_image_url($mhCardData['image']['id'], $mhImageSize);
        }

        if (empty($mhImageUrl) && !empty($mhCardData['image']['url'])) {
            $mhImageUrl = $mhCardData['image']['url'];
        }

        $mhRepeaterClass = '';
        if (!empty($mhCardData['_id'])) {
            $mhRepeaterClass = ' elementor-repeater-item-' . sanitize_html_class($mhCardData['_id']);
        }

        $mhUrl = !empty($mhCardData['url']['url']) ? $mhCardData['url']['url'] : '#';
        $mhLinkTarget = !empty($mhCardData['url']['is_external']) ? '_blank' : '_self';
        $mhLinkRel = !empty($mhCardData['url']['nofollow']) ? 'nofollow' : '';
        ?>
        <a
                class="myhome-term-list__item myhome-term-card<?php echo esc_attr($mhRepeaterClass); ?>"
                href="<?php echo esc_url($mhUrl); ?>"
                target="<?php echo esc_attr($mhLinkTarget); ?>"
                <?php if (!empty($mhLinkRel)) : ?>rel="<?php echo esc_attr($mhLinkRel); ?>"<?php endif; ?>
        >
            <?php if (!empty($mhImageUrl)) : ?>
                <div class="myhome-term-card__image">
                    <img
                            class="lazyload"
                        <?php if (!empty($mhImageSrcset)) : ?>
                            data-sizes="auto"
                            data-srcset="<?php echo esc_attr($mhImageSrcset); ?>"
                        <?php else : ?>
                            src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAACCAQAAAA3fa6RAAAADklEQVR42mNkAANGCAUAACMAA2w/AMgAAAAASUVORK5CYII="
                            data-src="<?php echo esc_url($mhImageUrl); ?>"
                        <?php endif; ?>
                            alt="<?php echo esc_attr($mhCardData['title']); ?>"
                    >
                </div>
            <?php endif; ?>

            <div class="myhome-term-card__content">
                <div class="myhome-term-card__name">
                    <?php echo esc_html($mhCardData['title']); ?>
                </div>

                <?php if (!empty($mhCardData['subtitle'])) : ?>
                    <div class="myhome-term-card__count">
                        <?php echo esc_html($mhCardData['subtitle']); ?>
                    </div>
                <?php endif; ?>
            </div>
        </a>
    <?php endforeach; ?>
</div>
