<?php 
/**
 * Template part for displaying Featured Services Section
 *
 * @package express-movers  
 */
$express_movers_featured_services_section_maintaitle = express_movers_get_option('featured_services_section_maintaitle');
$express_movers_featured_services_section_subtitle = express_movers_get_option('featured_services_section_subtitle');
$express_movers_featured_services_section_badge = express_movers_get_option('featured_services_section_badge');
$services_excerpt_mainpagelength = absint(express_movers_get_option('services_excerpt_mainpagelength'));

$express_movers_services_badge = !empty($express_movers_featured_services_section_badge)
    ? $express_movers_featured_services_section_badge
    : __('Our Services', 'express-movers');

$express_movers_services_title = !empty($express_movers_featured_services_section_maintaitle)
    ? $express_movers_featured_services_section_maintaitle
    : __('Complete Logistics Services', 'express-movers');

$express_movers_services_subtitle = !empty($express_movers_featured_services_section_subtitle)
    ? $express_movers_featured_services_section_subtitle
    : __('Tailored solutions for every shipping need, backed by years of industry expertise.', 'express-movers');

// Support up to 6 selected pages from Customizer.
$featured_services_posts = array_filter(array(
    get_theme_mod('featured_services_page_1'),
    get_theme_mod('featured_services_page_2'),
    get_theme_mod('featured_services_page_3'),
    get_theme_mod('featured_services_page_4'),
    get_theme_mod('featured_services_page_5'),
    get_theme_mod('featured_services_page_6'),
), 'absint');

$express_movers_icon_1 = get_theme_mod('featured_services_icon_1', express_movers_get_option('featured_services_icon_1'));
$express_movers_icon_2 = get_theme_mod('featured_services_icon_2', express_movers_get_option('featured_services_icon_2'));
$express_movers_icon_1 = !empty($express_movers_icon_1) ? $express_movers_icon_1 : 'fa-ship';
$express_movers_icon_2 = !empty($express_movers_icon_2) ? $express_movers_icon_2 : 'fa-plane';

$express_movers_default_service_items = array(
    array(
        'title' => __('Ocean Freight', 'express-movers'),
        'excerpt' => __('Reliable sea cargo solutions for international shipping with competitive rates and full tracking.', 'express-movers'),
        'image' => '/assets/images/aboutusimg.png',
    ),
    array(
        'title' => __('Air Freight', 'express-movers'),
        'excerpt' => __('Fast and secure air cargo services for time-sensitive deliveries worldwide with express handling.', 'express-movers'),
        'image' => '/assets/images/aboutusimg.png',
    ),
    array(
        'title' => __('Ground Transport', 'express-movers'),
        'excerpt' => __('Efficient road freight services with real-time tracking and door-to-door delivery solutions.', 'express-movers'),
        'image' => '/assets/images/aboutusimg.png',
    ),
    array(
        'title' => __('Warehousing', 'express-movers'),
        'excerpt' => __('State-of-the-art storage facilities with inventory management and distribution support.', 'express-movers'),
        'image' => '/assets/images/aboutusimg.png',
    ),
    array(
        'title' => __('Packaging Services', 'express-movers'),
        'excerpt' => __('Professional packaging services to ensure safe transit of your goods with quality materials.', 'express-movers'),
        'image' => '/assets/images/aboutusimg.png',
    ),
    array(
        'title' => __('Global Logistics', 'express-movers'),
        'excerpt' => __('Comprehensive supply chain management across multiple countries with seamless coordination.', 'express-movers'),
        'image' => '/assets/images/aboutusimg.png',
    ),
);
?>

<section id="featured-services">
    <div class="container express-services-wrap">
        <div class="row py-3">
            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <div class="entry-content3 text-center">
                    <span class="express-services-badge"><?php echo esc_html($express_movers_services_badge); ?></span>
                    <h5><?php echo esc_html($express_movers_services_title); ?></h5>
                    <h6><?php echo esc_html($express_movers_services_subtitle); ?></h6>
                </div>
            </div>
        </div>

        <div class="row full py-3">
            <div class="grid xl:grid-cols-3 lg:grid-cols-3 md:grid-cols-2 sm:grid-cols-1 grid-cols-1 servicerightside express-services-grid">
                <?php
                $args = array(
                    'post_type'      => 'page',
                    'posts_per_page' => 6,
                );

                if (!empty($featured_services_posts)) {
                    $args['post__in'] = $featured_services_posts;
                    $args['orderby'] = 'post__in';
                    $args['posts_per_page'] = min(6, count($featured_services_posts));
                }

                $query = new WP_Query($args);
                $express_movers_icons = array($express_movers_icon_1, $express_movers_icon_2, 'fa-truck', 'fa-warehouse', 'fa-box-open', 'fa-globe');
                $express_movers_item_index = 0;
                $express_movers_card_count = 0;

                if ($query->have_posts()) :
                    while ($query->have_posts()) : $query->the_post();
                        $default_item = $express_movers_default_service_items[$express_movers_item_index % count($express_movers_default_service_items)];
                        $default_image = !empty($default_item['image'])
                            ? get_template_directory_uri() . $default_item['image']
                            : get_template_directory_uri() . '/assets/images/no-images.jpg';
                        $image = has_post_thumbnail()
                            ? wp_get_attachment_image_src(get_post_thumbnail_id(), 'large')
                            : array($default_image);
                        $icon_class = $express_movers_icons[$express_movers_item_index % count($express_movers_icons)];
                        $excerpt = express_movers_the_excerpt($services_excerpt_mainpagelength ? $services_excerpt_mainpagelength : 14);
                        $excerpt = trim(wp_strip_all_tags($excerpt));
                        if (empty($excerpt)) {
                            $excerpt = $default_item['excerpt'];
                        }

                        $card_title = trim(get_the_title());
                        if (empty($card_title)) {
                            $card_title = $default_item['title'];
                        }
                        $express_movers_item_index++;
                        ?>
                        <article class="paddingdiv">
                            <div class="featured-service-item h-full">
                                <div class="entry-container relative">
                                    <div class="express-service-card-bg" style="background-image: url(<?php echo esc_url($image[0]); ?>);">
                                        <div class="express-service-overlay"></div>
                                        <div class="express-service-content">
                                            <span class="express-service-icon"><i class="fas <?php echo esc_attr($icon_class); ?>" aria-hidden="true"></i></span>
                                            <h2><a href="<?php the_permalink(); ?>"><?php echo esc_html($card_title); ?></a></h2>
                                            <p><?php echo esc_html($excerpt); ?></p>
                                            <a href="<?php the_permalink(); ?>" class="express-service-link">
                                                <?php esc_html_e('Learn More', 'express-movers'); ?> <i class="fas fa-arrow-right" aria-hidden="true"></i>
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </article>
                        <?php $express_movers_card_count++; ?>
                    <?php endwhile;
                    wp_reset_postdata();
                endif;

                if ($express_movers_card_count < 6) :
                    for ($express_movers_i = $express_movers_card_count; $express_movers_i < 6; $express_movers_i++) :
                        $icon_class = $express_movers_icons[$express_movers_i % count($express_movers_icons)];
                        $default_item = $express_movers_default_service_items[$express_movers_i % count($express_movers_default_service_items)];
                        $default_image = !empty($default_item['image'])
                            ? get_template_directory_uri() . $default_item['image']
                            : get_template_directory_uri() . '/assets/images/no-images.jpg';
                        ?>
                        <article class="paddingdiv">
                            <div class="featured-service-item h-full">
                                <div class="entry-container relative">
                                    <div class="express-service-card-bg" style="background-image: url(<?php echo esc_url($default_image); ?>);">
                                        <div class="express-service-overlay"></div>
                                        <div class="express-service-content">
                                            <span class="express-service-icon"><i class="fas <?php echo esc_attr($icon_class); ?>" aria-hidden="true"></i></span>
                                            <h2><a href="#"><?php echo esc_html($default_item['title']); ?></a></h2>
                                            <p><?php echo esc_html($default_item['excerpt']); ?></p>
                                            <a href="#" class="express-service-link">
                                                <?php esc_html_e('Learn More', 'express-movers'); ?> <i class="fas fa-arrow-right" aria-hidden="true"></i>
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </article>
                    <?php endfor;
                endif;
                ?>
            </div>
        </div>
    </div>
</section>
