<?php get_header(); ?>
<?php
// Function to convert numbers to Roman numerals
function numberToRoman($num) {
    $n = intval($num);
    $result = '';
    
    $romanNumerals = array(
        'M'  => 1000,
        'CM' => 900,
        'D'  => 500,
        'CD' => 400,
        'C'  => 100,
        'XC' => 90,
        'L'  => 50,
        'XL' => 40,
        'X'  => 10,
        'IX' => 9,
        'V'  => 5,
        'IV' => 4,
        'I'  => 1
    );
    
    foreach ($romanNumerals as $roman => $number) {
        $matches = intval($n / $number);
        $result .= str_repeat($roman, $matches);
        $n = $n % $number;
    }
    
    return $result; // Convert the Roman numerals to lowercase
}
?>

<?php
// Get the current term object
$current_term = get_queried_object();
$term_id = $current_term->term_id; // Get the current term ID

// Now you can use get_field with the term ID
$banner_image = get_field('banner_image', 'category_' . $term_id);
$category_content = get_field('category_content', 'category_' . $term_id);
$category_image = get_field('category_image', 'category_' . $term_id);
?>
<!-- Start Breadcrumb 
    ============================================= -->
<div class="breadcrumb-area text-center shadow dark bg-fixed text-light"
    style="background-image: url(<?php echo esc_url($banner_image); ?>);">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 offset-lg-2">
                <h1><?php single_term_title();?></h1>
                <ul class="breadcrumb">
                    <li><a href="<?php echo site_url() ?>"><i class="fas fa-home"></i> Home</a></li>
                    <li class="active"><?php single_term_title();?></li>
                </ul>
            </div>
        </div>
    </div>
</div>
<!-- End Breadcrumb -->
<div class="about-content-area overflow-hidden inc-shape default-padding">
    <!-- Illustration -->
    <div class="illustration">
        <img src="<?php echo get_template_directory_uri() ?>/assets/img/shape/4.png" alt="Illustration">
    </div>
    <!-- Illustration -->
    <div class="container">
        <div class="row">

            <div class="col-lg-6 info">
                <div class="content wow fadeInLeft" data-wow-delay="300ms">
                    <h5>Our Product</h5>
                    <h2 class="area-title"><?php single_term_title();?></h2>
                    <?php echo wp_kses_post($category_content); ?>
                </div>
            </div>
            <div class="col-lg-6">
                <div class="thumb">
                    <img class="wow fadeInRight" data-wow-delay="700ms" src=" <?php echo esc_url($category_image); ?>" alt="Thumb">
                </div>
            </div>

        </div>
    </div>
</div>

<!-- Star Services Area
    ============================================= -->
    <div class="services-area about-area shape-less overflow-hidden default-padding">
        <div class="container">
            

                <?php
                // Query to get posts
                $child_terms = get_terms( array(
                    'taxonomy' => 'project-category', // Taxonomy name
                    'child_of' => $current_term->term_id, // Get child terms of the current category
                    'hide_empty' => false, // Show even if there are no posts
                ) );

                
                if ( !empty( $child_terms ) && !is_wp_error( $child_terms ) ) : 
                    $i='a';
                    foreach ( $child_terms as $term ) : 
                        // Get the term link and term meta (if available)
                        $term_link = get_term_link( $term );
                        // Optional: Add ACF fields for images or content if applicable
                        $term_image = get_field('category_image', $term); // If using ACF for image field
                ?>
                <div class="services-contents features-area text-center mt-3">

                    <div class="row">
                        
                        <div class="col-lg-12">
                        <h5 class="product-section-title"><?php echo '('.$i.')' ?> <?php echo esc_html( $term->name ); ?></h5>
                        </div>
                        
                        <?php
                         // Query to get posts
                        $args = array(
                            'post_type' => 'product', // Change this to your custom post type if needed
                            'posts_per_page' => -1, // Number of posts to display
                            'tax_query' => array(
                                array(
                                    'taxonomy' => 'project-category', // Taxonomy name
                                    'field' => 'slug', // Field to match the term ('slug' or 'term_id')
                                    'terms' => $term->slug, // Term slug to filter by
                                ),
                            ),
                        );

                        $query = new WP_Query($args); 
                        ?>
                        <?php if ( $query->have_posts() ) : ?>
                            
                        <?php 
                        $x = 0;
                            while ( $query->have_posts() ) : $query->the_post(); ?>
                        <?php $x++; ?>
                        
                        <!-- Single Item -->
                        <div class="col-lg-3 col-md-6 single-item">
                            <?php if( $current_term->slug == 'seeds'): ?>
                            <a>
                                <?php else: ?>
                                    <a href="<?php the_permalink(); ?>">
                                    <?php endif; ?>
                            <div class="item">
                                <div class="thumb">
                                <?php 
                                // Get the featured image URL
                                $thumbnail_url = '';
                                if ( has_post_thumbnail() ) {
                                    $thumbnail_url = wp_get_attachment_image_url(get_post_thumbnail_id(), 'medium'); // 'medium' size can be replaced with any desired size
                                } else {
                                    // Set a default image URL if no thumbnail is available
                                    $thumbnail_url = home_url() . '/wp-content/uploads/2024/09/New-Project-21.png';
                                }
                                ?>
                                <img src="<?php echo esc_url($thumbnail_url); ?>" alt="">
                
                                </div>
                                <h5><span style="text-transform: lowercase "><?php echo (numberToRoman($x)) . '. ' ?></span> <?php the_title() ?></h5>
                            </div>
                            </a>
                        </div>
                        <!-- End Single Item -->


                        <?php endwhile; ?>
                        <?php wp_reset_postdata(); ?>
                        <?php else : ?>
                            
                        <?php endif; ?>
                    

                    </div>
                </div>
                    <?php 
                    $i++;
                    endforeach;
                else:
                ?>
                <div class="services-contents features-area text-center">
                <div class="row">
                    <div class="col-lg-12 col-md-12">
                        <h5 class="product-section-title"><?php echo esc_html( $current_term->name ); ?></h5>
                    </div>
                    
                        <?php
                         // Query to get posts
                        $args = array(
                            'post_type' => 'product', // Change this to your custom post type if needed
                            'posts_per_page' => -1, // Number of posts to display
                            'tax_query' => array(
                                array(
                                    'taxonomy' => 'project-category', // Taxonomy name
                                    'field' => 'slug', // Field to match the term ('slug' or 'term_id')
                                    'terms' => $current_term->slug, // Term slug to filter by
                                ),
                            ),
                        );

                        $query = new WP_Query($args); 
                        ?>
                        <?php if ( $query->have_posts() ) : ?>
                        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                        
                        <!-- Single Item -->
                        <div class="col-lg-3 col-md-6 single-item">
                            <a>
                            <div class="item">
                                <div class="thumb">
                                <?php 
                                // Get the featured image URL
                                $thumbnail_url = '';
                                if ( has_post_thumbnail() ) {
                                    $thumbnail_url = wp_get_attachment_image_url(get_post_thumbnail_id(), 'medium'); // 'medium' size can be replaced with any desired size
                                } else {
                                    // Set a default image URL if no thumbnail is available
                                    $thumbnail_url = home_url() . '/wp-content/uploads/2024/09/New-Project-21.png';
                                }
                                ?>
                                <img src="<?php echo esc_url($thumbnail_url); ?>" alt="">
                
                                </div>
                                <h5><?php the_title() ?></h5>
                            </div>
                            </a>
                        </div>
                        <!-- End Single Item -->


                        <?php endwhile; ?>
                        <?php wp_reset_postdata(); ?>
                        <?php else : ?>
                            
                        <?php endif; ?>
                    

                    </div>

                    </div>
               
               
               <?php     
                endif;
                ?>
                   
            
        </div>
    </div>
    <!-- End Services Area -->



<!-- Star Services Area
    ============================================= -->
    <div class="services-area about-area shape-less overflow-hidden default-padding d-none">
        <div class="container">
            <div class="services-contents features-area text-center">
            <?php
                // Query to get posts
                $args = array(
                    'post_type' => 'product', // Change this to your custom post type if needed
                    'posts_per_page' => -1, // Number of posts to display
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'project-category', // Taxonomy name
                            'field' => 'slug', // Field to match the term ('slug' or 'term_id')
                            'terms' => $current_term->slug, // Term slug to filter by
                        ),
                    ),
                );

                $query = new WP_Query($args); 
                ?>
                <?php if ( $query->have_posts() ) : ?>
                <?php while ( $query->have_posts() ) : $query->the_post(); ?>

                
                    <div class="row">
                                    
                    
                    
                    <!-- Single Item -->
                    <div class="col-lg-3 col-md-6 single-item">
                        <a href="">
                        <div class="item">
                            <div class="thumb">
                            <?php 
                            // Get the featured image URL
                            $thumbnail_url = '';
                            if ( has_post_thumbnail() ) {
                                $thumbnail_url = wp_get_attachment_image_url(get_post_thumbnail_id(), 'medium'); // 'medium' size can be replaced with any desired size
                            } else {
                                // Set a default image URL if no thumbnail is available
                                $thumbnail_url = get_template_directory_uri() . '/assets/img/thumb/2.png';
                            }
                            ?>
                            <img src="<?php echo esc_url($thumbnail_url); ?>" alt="">
            
                            </div>
                            <h5><?php the_title() ?></h5>
                        </div>
                        </a>
                    </div>
                    <!-- End Single Item -->
                     <!-- End Single Item -->
                    

                    </div>
                    <?php endwhile; ?>
                    <?php wp_reset_postdata(); ?>
                    <?php else : ?>
                        
                    <?php endif; ?>
            </div>
        </div>
    </div>
    <!-- End Services Area -->


<?php get_footer(); ?>