<?php

use Tangibledesign\MyHome\Widgets\General\ServicesWidget;

/* @var ServicesWidget $mhCurrentWidget */
global $mhCurrentWidget;

$services = $mhCurrentWidget->getServices();

if ($services->isEmpty()) {
    return;
}
?>

<div class="myhome-services-widget__grid">
    <?php foreach ($services as $service) : ?>
        <div class="myhome-services-widget__card">
            <?php if (!empty($service['icon']['value'])) : ?>
                <div class="myhome-services-widget__icon">
                    <?php \Elementor\Icons_Manager::render_icon($service['icon'], ['aria-hidden' => 'true']); ?>
                </div>
            <?php endif; ?>

            <?php if (!empty($service['title'])) : ?>
                <h3 class="myhome-services-widget__title">
                    <?php echo esc_html($service['title']); ?>
                </h3>
            <?php endif; ?>

            <?php if (!empty($service['description'])) : ?>
                <div class="myhome-services-widget__description">
                    <?php echo wp_kses_post($service['description']); ?>
                </div>
            <?php endif; ?>

            <?php if (!empty($service['button_text']) && !empty($service['link_type']) && $service['link_type'] !== 'none') : ?>
                <?php
                $link_url = '';
                $openInNewWindow = false;
                $isNofollow = false;
                
                switch ($service['link_type']) {
                    case 'phone':
                        if (!empty($service['phone_number'])) {
                            $link_url = 'tel:' . str_replace([' ', '(', ')', '-'], '', $service['phone_number']);
                        }
                        break;
                        
                    case 'email':
                        if (!empty($service['email_address'])) {
                            $link_url = 'mailto:' . $service['email_address'];
                        }
                        break;
                        
                    case 'url':
                        if (!empty($service['link']['url'])) {
                            $link_url = $service['link']['url'];
                            if (!empty($service['link']['is_external'])) {
                                $openInNewWindow = true;
                            }
                            if (!empty($service['link']['nofollow'])) {
                                $isNofollow = true;
                            }
                        }
                        break;
                }
                
                // Handle open_in_new_window for phone and email links
                if (($service['link_type'] === 'phone' || $service['link_type'] === 'email') && !empty($service['open_in_new_window']) && $service['open_in_new_window'] === 'yes') {
                    $openInNewWindow = true;
                }
                ?>
                
                <?php if (!empty($link_url)) : ?>
                    <a href="<?php echo esc_url($link_url); ?>" 
                       class="myhome-services-widget__link"
                       <?php if ($openInNewWindow) : ?>target="_blank"<?php endif; ?>
                       <?php if ($isNofollow) : ?>rel="nofollow"<?php endif; ?>>
                        <?php echo esc_html($service['button_text']); ?>
                        <?php if ($service['link_type'] === 'url') : ?>
                            <i class="fas fa-arrow-right"></i>
                        <?php endif; ?>
                    </a>
                <?php endif; ?>
            <?php endif; ?>
        </div>
    <?php endforeach; ?>
</div>
