<?php 
use dpcdCategoryDesign\Helper;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

    /**
     * Slider settings
     */
    $arrows = isset($settings['show_arrow']) && $settings['show_arrow'] ? true : false;
    $dot_nav = isset($settings['show_dot_navigation']) && $settings['show_dot_navigation'] ? true : false;
    $autoplay = isset($settings['enable_autoplay']) && $settings['enable_autoplay'] ? true : false;
    $arrow_position = isset($all_settings['wc_pcd_arrow_position']) ? $all_settings['wc_pcd_arrow_position'][0] : 'side';
    $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';
    $speed  = isset($settings['Autoplay_Speed']) ? $settings['Autoplay_Speed'] : '4000';
?>

<!-- Start Slider Layout -->
<div class="pcd-elementor-cat-slider pcd-cat-slider pcd-predesign-template-two cat-slider pcd-<?php echo esc_attr($settings['content_position']); ?> pcd-arrow-<?php echo esc_attr($arrow_position); ?>" 
        data-arrows="<?php echo esc_attr($arrows); ?>" 
        data-dots="<?php echo esc_attr($dot_nav); ?>"  
        data-autoplay="<?php echo esc_attr($autoplay); ?>"
        data-desktop = "<?php echo esc_attr($desktop); ?>"
        data-tablet = "<?php echo esc_attr($tablet); ?>"
        data-mobile = "<?php echo esc_attr($mobile); ?>"
        data-speed = "<?php echo esc_attr($speed); ?>"
        > 
        <?php 
            do_action( 'pcd_before_loop_start', 'slider' );
            
                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] : '';
                    $term_url = get_term_link( $category, 'product_cat' );
                    if ( is_wp_error( $term_url ) ) {
                        $term_url = ''; // Prevents invalid links from breaking the page
                    }
                    $special_offer = isset( $term_meta['wc_pcd_cat_special_offer'][0] ) ? $term_meta['wc_pcd_cat_special_offer'][0] : '';
                    $pcd_count = Helper::get_product_category_count($category->term_id); ?>

                    <div class="pcd-single-cat-slider-item pcd-single-card pcd-predesign-slider-template-<?php echo esc_attr( $layout_style ) ?> pcd-<?php echo esc_attr($settings['content_position']); ?>-design"> 
                        <div class="pcd-cat-thumbnail">
                            <a href="<?php echo esc_url( $term_url ); ?>">
                                <?php if(!empty($cat_thumb_id) && $settings['show_image']): ?>
                                    <?php echo wp_get_attachment_image( $cat_thumb_id , 'medium', false, array( 'loading' => 'lazy' ) ); ?>
                                <?php endif; ?>
                            </a>
                            <a href="<?php echo esc_url( $term_url ); ?>" class="pcd-over-lay"></a>

                            <div class="pcd-sub-category">
                                <?php Helper::get_woo_child_category_by_id($category->term_id); ?>
                            </div>
                            
                                <div class="pcd-grid-cat--details woo-cs-cat-des">
                                    <?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;?>

                                    <?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 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_lend', 'slider' );?>
    </div><!-- End Slider Layout -->