<?php
/**
 * Template part for displaying About section
 *
 * @package Home Interior Decor
 * @subpackage home_interior_decor
 */
?>

<?php if (get_theme_mod('home_interior_decor_show_hide_product_section', true)) : ?>
    <section id="product_cat_slider" class="my-5 mx-md-0 mx-3">
        <div class="container">
            <?php 
            if ($home_interior_decor_product_heading = get_theme_mod('home_interior_decor_product_heading')) : 
                $home_interior_decor_heading_words = explode(' ', $home_interior_decor_product_heading);

                $home_interior_decor_last_word = array_pop($home_interior_decor_heading_words);

                $home_interior_decor_remaining_heading = implode(' ', $home_interior_decor_heading_words);
                ?>
                <h3 class="text-start mb-2 text-capitalize">
                  <?php echo esc_html($home_interior_decor_remaining_heading); ?> 
                  <span class="last-word"><?php echo esc_html($home_interior_decor_last_word); ?></span>
                </h3>
              <hr>
            <?php endif; ?>

            <?php
            // Get the number of categories to display from customizer
            $home_interior_decor_num_of_categories = get_theme_mod('home_interior_decor_num_of_categories', '');
            $home_interior_decor_args = array(
                'taxonomy'   => 'product_cat',
                'hide_empty' => false,
                'number'     => $home_interior_decor_num_of_categories,
                'orderby'    => 'name',
                'order'      => 'ASC',
            );

            // Fetch product categories
            $home_interior_decor_terms = get_terms($home_interior_decor_args);

            // Check if categories exist and are not empty
            if (!empty($home_interior_decor_terms) && !is_wp_error($home_interior_decor_terms)) : ?>
                <div class="owl-carousel">
                    <?php 
                    $home_interior_decor_i = 1; // Initialize counter for product icons
                    foreach ($home_interior_decor_terms as $home_interior_decor_term) : ?>
                        <div class="product_cat_box text-center py-4 px-2">
                            <div class="product-images">
                                <?php 
                                // Get category thumbnail ID
                                $home_interior_decor_thumbnail_id = get_term_meta($home_interior_decor_term->term_id, 'thumbnail_id', true);

                                // Check if category image exists
                                if ($home_interior_decor_thumbnail_id) {
                                    // Get the category image URL
                                    $image_url = wp_get_attachment_url($home_interior_decor_thumbnail_id);
                                } else {
                                    // Set a default image URL
                                    $image_url = get_template_directory_uri() . '/assets/images/sliderimage.png';
                                }
                                ?>

                                <!-- Display the image -->
                                <img src="<?php echo esc_url($image_url); ?>" alt="<?php echo esc_attr($home_interior_decor_term->name); ?>" class="img-fluid">
                            </div>

                            <h4 class="my-2 text-capitalize"><a href="<?php echo esc_url(get_term_link($home_interior_decor_term)); ?>"><?php echo esc_html($home_interior_decor_term->name); ?></a></h4>

                            <?php 
                            // Display category description if available
                            if (!empty($home_interior_decor_term->description)) : ?>
                                <p class="category-description">
                                    <?php echo wp_kses_post($home_interior_decor_term->description); ?>
                                </p>
                            <?php endif; ?>

                            <div class="category-icon mt-3">
                              <a href="<?php echo esc_url(get_term_link($home_interior_decor_term)); ?>" class="icon-link">
                                  <i class="fas fa-chevron-right"></i>
                              </a>
                            </div>
                        </div>
                    <?php 
                    $home_interior_decor_i++; // Increment counter for next product icon
                    endforeach; ?>
                </div>
            <?php else : ?>
                <p class="text-center">No product categories found.</p>
            <?php endif; ?>
        </div>
    </section>
<?php endif; ?>