<?php
use dpcdCategoryDesign\Helper;
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
    $desktop = isset($settings['columns']) ? $settings['columns'] : '4';
    $tablet = isset($settings['columns_tablet']) ? $settings['columns_tablet'] : '3';
    $mobile = isset($settings['columns_mobile']) ? $settings['columns_mobile'] : '1';
    $layout_style = isset($settings['grid_layout_style']) ? $settings['grid_layout_style'] : '';
    
?>
<!-- Start Grid Layout -->

<div class="pcd-elementor-widget pcd-predesign-template-<?php echo esc_attr( $layout_style ) ?> pcd-row pcd-row-cols-<?php echo esc_attr( isset( $settings['columns'] ) ? $settings['columns'] : '2' ); ?>"> 
        <?php 
                
            do_action( 'pcd_before_loop_start', 'grid' );

                foreach ( $pcd_cats as $category ):
                    $term_meta = get_term_meta( $category->term_id );
                    $cat_thumb_id = isset( $term_meta['thumbnail_id'][0] ) ? $term_meta['thumbnail_id'][0] : '';
                    $special_offer = isset( $term_meta['wc_pcd_cat_special_offer'][0] ) ? $term_meta['wc_pcd_cat_special_offer'][0] : '';
                    $term_url = get_term_link( $category, 'product_cat' );
                    if ( is_wp_error( $term_url ) ) {
                        $term_url = ''; // Prevents invalid links from breaking the page
                    }
                    $pcd_count = Helper::get_product_category_count($category->term_id);
                    ?>
                    
                    <div class="pcd-single-cat-grid-item pcd-single-card pcd-single-card__overlay-design pcd-col-lg-6 pcd-col-md-12 pcd-col-12"> 
                        <div class="pdc-reap">
                            <?php if(!empty( $cat_thumb_id ) AND $settings['show_image']): ?>
                                <a href="<?php echo esc_url( $term_url ); ?>">
                                    <?php echo wp_get_attachment_image( $cat_thumb_id , 'medium', false, array( 'loading' => 'lazy' ) ); ?>
                                </a>
                            <?php endif; ?>
                
                            <div class="pcd-grid-cat--details">
                                <div class="pcd-grid-layout-<?php echo esc_attr( $layout_style ) ?>">
                                    <?php if($settings['show_category_name']):?>
                                        <a  href="<?php echo esc_url( $term_url ); ?>" class="pcd-category-name">
                                            <?php echo esc_html( $category->name ); ?>
                                        </a>
                                    <?php endif;?>
                                    
                                    <?php if(!empty( $pcd_count ) && $settings['show_count'] ):?>
                                        <div class="pcd-slide-product-count">
                                            <span class="pcd-count-text"><?php echo esc_html(  $pcd_count );?></span><span class="pcd-count-text" ><?php esc_html_e( 'products', 'dynamic-product-categories-design' ); ?></span>
                                        </div>
                                    <?php endif;?>
                                </div>
                                
                                <?php if(!empty($special_offer) && $settings['show_offer'] ): ?>
                                    <div class="pcd-grid-special-offer">
                                        <?php echo wp_kses_post( $special_offer ); ?>
                                    </div>
                                <?php endif; ?>

                                <?php if( $settings['show_category_description'] ):?>
                                    <div class="pcd-grid-description">
                                        <?php echo wp_kses_post( category_description( $category->term_id ) ); ?>
                                    </div>
                                <?php endif;?>
                            
                                <?php if('yes' === $settings['show_button']):?>
                                    <a class="pcd-btn pcd-shop-now" href="<?php echo esc_url( $term_url ); ?>">
                                        <?php echo esc_html( isset( $settings['shop_now_text'] ) ? $settings['shop_now_text'] : __( 'Shop Now', 'dynamic-product-categories-design' ) ); ?>
                                    </a>
                                <?php endif;?>
                            </div><!--.pcd-grid-cat--details-->
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php do_action( 'pcd_after_loop_start', 'grid' );?>
    </div>
<!-- End Grid Layout -->