<?php

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;


if (! defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

class WAFDAE_Dokan_Stores extends Widget_Base
{
    public function __construct($data = [], $args = null)
    {
        parent::__construct($data, $args);

        if (defined('DOKAN_PLUGIN_VERSION')) {
            // wp_register_script('dokan-script-js', plugins_url('dokan-lite/assets/js/dokan-pro.js'), ['elementor-frontend'], '1.0.0', true);
            // wp_register_script('dokan-select2-js', plugins_url('dokan-lite/assets/vendors/select2/select2.full.min.js'), ['elementor-frontend'], '1.0.0', true);
            // wp_register_script( 'serializejson', WC()->plugin_url() . '/assets/js/jquery-serializejson/jquery.serializejson.js', ['elementor-frontend'], '1.0.0', true);
            // wp_register_script('dokan-vendors-js', plugins_url('assets/js/dokan-vendors.js', __DIR__), ['elementor-frontend'], '1.0.0', true);
        }
        if (defined('DOKAN_PRO_PLUGIN_VERSION')) {
            // wp_register_style('dokan-pro-css', plugins_url('dokan-pro/assets/css/dokan-pro.css'));
            // wp_register_style('dokan-pro-reviews', plugins_url('dokan-pro/modules/store-reviews/assets/css/style.css'));  
            // wp_register_script('dokan-pro-js', plugins_url('dokan-pro/assets/js/dokan-pro.js'), ['elementor-frontend'], '1.0.0', true);

        }
        // wp_deregister_script('dokan-script');

        // $assets= new Assets();

        // $scripts = $assets->get_scripts();
        // $assets->enqueue_front_scripts();
        // $assets->register_all_scripts();

        // foreach ( $scripts as $handle => $script ) {
        //             wp_deregister_script($handle);

        //     $deps      = isset( $script['deps'] ) ? $script['deps'] : false;
        //     $in_footer = isset( $script['in_footer'] ) ? $script['in_footer'] : true;
        //     $version   = isset( $script['version'] ) ? $script['version'] : DOKAN_PLUGIN_VERSION;

        //     wp_register_script( $handle, $script['src'], ['elementor-frontend'], $version, $in_footer );
        // }

        wp_register_style('dokan-widgets', plugin_dir_url(__DIR__) . 'assets/css/dokan-slider.css', '1.0.0');

    }

    public function get_script_depends()
    {
        // return ['dokan-script-js','dokan-select2-js','dokan-vendors-js','dokan-pro-js'];
        return [];
    }
    public function get_style_depends()
    {
        // return ['dokan-pro-css','dokan-pro-reviews'];
        return ['dokan-widgets'];
    }

    public function get_name()
    {
        return 'dokan_stores';
    }

    public function get_title()
    {
        return __('Dokan Stores', 'wpsaad-addons-for-dokan-and-elementor');
    }

    public function get_icon()
    {
        return 'eicon-post-list';
    }

    public function get_categories()
    {
        return ['wpsaad'];
    }
    public function get_keywords()
    {
        return ['vendor', 'stores', 'dokan'];
    }

    protected function _register_controls()
    {

        $this->start_controls_section(
            'content_section',
            [
                'label' => __('Settings', 'wpsaad-addons-for-dokan-and-elementor'),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

        // Existing controls
        $this->add_control(
            'per_page',
            [
                'label'   => __('Stores Per Page', 'wpsaad-addons-for-dokan-and-elementor'),
                'type'    => Controls_Manager::NUMBER,
                'default' => 10,
            ]
        );

        $this->add_control(
            'per_row',
            [
                'label'   => __('Stores Per Row', 'wpsaad-addons-for-dokan-and-elementor'),
                'type'    => Controls_Manager::NUMBER,
                'default' => 3,
                'max' => 5,
            ]
        );

        $this->add_control(
            'store_id',
            [
                'label' => __('Store ID\'s', 'wpsaad-addons-for-dokan-and-elementor'),
                'type' => Controls_Manager::TEXT,
                'input_type' => 'text',
                'placeholder' => __('Store ID\'s, Sperate with comma', 'wpsaad-addons-for-dokan-and-elementor'),
            ]
        );

        $this->add_control(
            'store_category',
            [
                'label' => __('Store Categories', 'wpsaad-addons-for-dokan-and-elementor'),
                'type' => Controls_Manager::TEXT,
                'input_type' => 'text',
                'placeholder' => __('Store Categories ID\'s, Sperate with comma', 'wpsaad-addons-for-dokan-and-elementor'),
            ]
        );

        $this->add_control(
            'show_search',
            [
                'label'   => __('Enable Search', 'wpsaad-addons-for-dokan-and-elementor'),
                'type'    => Controls_Manager::SWITCHER,
                'label_on' => __('Yes', 'wpsaad-addons-for-dokan-and-elementor'),
                'label_off' => __('No', 'wpsaad-addons-for-dokan-and-elementor'),
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'show_filter',
            [
                'label'   => __('Enable Filter', 'wpsaad-addons-for-dokan-and-elementor'),
                'type'    => Controls_Manager::SWITCHER,
                'label_on' => __('Yes', 'wpsaad-addons-for-dokan-and-elementor'),
                'label_off' => __('No', 'wpsaad-addons-for-dokan-and-elementor'),
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'with_product',
            [
                'label'   => __('With Products Only', 'wpsaad-addons-for-dokan-and-elementor'),
                'type'    => Controls_Manager::SWITCHER,
                'label_on' => __('Yes', 'wpsaad-addons-for-dokan-and-elementor'),
                'label_off' => __('No', 'wpsaad-addons-for-dokan-and-elementor'),
                'default' => 'No',
            ]
        );


        $this->add_control(
            'featured',
            [
                'label'   => __('Show Featured Only', 'wpsaad-addons-for-dokan-and-elementor'),
                'type'    => Controls_Manager::SWITCHER,
                'label_on' => __('Yes', 'wpsaad-addons-for-dokan-and-elementor'),
                'label_off' => __('No', 'wpsaad-addons-for-dokan-and-elementor'),
                'default' => 'no',
            ]
        );

        // New controls for showing/hiding elements
        $this->add_control(
            'show_phone',
            [
                'label'   => __('Show Phone Number', 'wpsaad-addons-for-dokan-and-elementor'),
                'type'    => Controls_Manager::SWITCHER,
                'label_on' => __('Yes', 'wpsaad-addons-for-dokan-and-elementor'),
                'label_off' => __('No', 'wpsaad-addons-for-dokan-and-elementor'),
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'show_follow_button',
            [
                'label'   => __('Show Follow Button', 'wpsaad-addons-for-dokan-and-elementor'),
                'type'    => Controls_Manager::SWITCHER,
                'label_on' => __('Yes', 'wpsaad-addons-for-dokan-and-elementor'),
                'label_off' => __('No', 'wpsaad-addons-for-dokan-and-elementor'),
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'show_image',
            [
                'label'   => __('Show Store Image', 'wpsaad-addons-for-dokan-and-elementor'),
                'type'    => Controls_Manager::SWITCHER,
                'label_on' => __('Yes', 'wpsaad-addons-for-dokan-and-elementor'),
                'label_off' => __('No', 'wpsaad-addons-for-dokan-and-elementor'),
                'default' => 'yes',
            ]
        );

        $this->end_controls_section();

        // Container Styling Section
        $this->start_controls_section('container_style', [
            'label' => __('Container', 'wpsaad-addons-for-dokan-and-elementor'),
            'tab' => \Elementor\Controls_Manager::TAB_STYLE,
        ]);

        $this->add_control('container_background', [
            'label' => __('Background Color', 'wpsaad-addons-for-dokan-and-elementor'),
            'type' => \Elementor\Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} #dokan-seller-listing-wrap' => 'background-color: {{VALUE}};',
            ],
        ]);

        $this->add_control('container_padding', [
            'label' => __('Padding', 'wpsaad-addons-for-dokan-and-elementor'),
            'type' => \Elementor\Controls_Manager::DIMENSIONS,
            'size_units' => ['px', '%', 'em'],
            'selectors' => [
                '{{WRAPPER}} #dokan-seller-listing-wrap' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
            ],
        ]);


        $this->end_controls_section();

        // Filter Styling Section
        $this->start_controls_section('filter_style', [
            'label' => __('Filter', 'wpsaad-addons-for-dokan-and-elementor'),
            'tab' => \Elementor\Controls_Manager::TAB_STYLE,
        ]);

        $this->add_control('filter_background', [
            'label' => __('Filter Background', 'wpsaad-addons-for-dokan-and-elementor'),
            'type' => \Elementor\Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} #dokan-store-listing-filter-wrap' => 'background-color: {{VALUE}};',
            ],
        ]);

        $this->add_control('filter_button_text_color', [
            'label' => __('Filter Button Text Color', 'wpsaad-addons-for-dokan-and-elementor'),
            'type' => \Elementor\Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .dokan-store-list-filter-button' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_control('filter_button_background', [
            'label' => __('Filter Button Background', 'wpsaad-addons-for-dokan-and-elementor'),
            'type' => \Elementor\Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .dokan-store-list-filter-button' => 'background-color: {{VALUE}}!important;',
            ],
        ]);

        $this->add_group_control(\Elementor\Group_Control_Typography::get_type(), [
            'name' => 'filter_button_typography',
            'label' => __('Filter Button Typography', 'wpsaad-addons-for-dokan-and-elementor'),
            'selector' => '{{WRAPPER}} .dokan-store-list-filter-button',
        ]);

        $this->end_controls_section();


        // Store Image Styling Section
        $this->start_controls_section(
            'section_style_image',
            [
                'label' => esc_html__('Vendor Avatar', 'wpsaad-addons-for-dokan-and-elementor'),
                'tab'   => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_responsive_control(
            'width',
            [
                'label' => esc_html__('Width', 'wpsaad-addons-for-dokan-and-elementor'),
                'type' => Controls_Manager::SLIDER,
                'default' => [
                    'unit' => '%',
                ],
                'tablet_default' => [
                    'unit' => '%',
                ],
                'mobile_default' => [
                    'unit' => '%',
                ],
                'size_units' => ['px', '%', 'em', 'rem', 'vw', 'custom'],
                'range' => [
                    '%' => [
                        'min' => 1,
                        'max' => 100,
                    ],
                    'px' => [
                        'min' => 1,
                        'max' => 1000,
                    ],
                    'vw' => [
                        'min' => 1,
                        'max' => 100,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} #dokan-seller-listing-wrap ul.dokan-seller-wrap li .store-footer .seller-avatar' => 'width: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'space',
            [
                'label' => esc_html__('Max Width', 'wpsaad-addons-for-dokan-and-elementor'),
                'type' => Controls_Manager::SLIDER,
                'default' => [
                    'unit' => 'px',
                ],
                'tablet_default' => [
                    'unit' => 'px',
                ],
                'mobile_default' => [
                    'unit' => 'px',
                ],
                'size_units' => ['px', '%', 'em', 'rem', 'vw', 'custom'],
                'range' => [
                    '%' => [
                        'min' => 1,
                        'max' => 100,
                    ],
                    'px' => [
                        'min' => 1,
                        'max' => 1000,
                    ],
                    'vw' => [
                        'min' => 1,
                        'max' => 100,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} #dokan-seller-listing-wrap ul.dokan-seller-wrap li .store-footer .seller-avatar' => 'max-width: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'height',
            [
                'label' => esc_html__('Height', 'wpsaad-addons-for-dokan-and-elementor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', '%', 'em', 'rem', 'vh', 'custom'],
                'range' => [
                    'px' => [
                        'min' => 1,
                        'max' => 500,
                    ],
                    'vh' => [
                        'min' => 1,
                        'max' => 100,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} #dokan-seller-listing-wrap ul.dokan-seller-wrap li .store-footer .seller-avatar' => 'height: {{SIZE}}{{UNIT}};',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'image_border',
                'selector' => '{{WRAPPER}} #dokan-seller-listing-wrap ul.dokan-seller-wrap li .store-footer .seller-avatar',
                'separator' => 'before',
            ]
        );

        $this->add_responsive_control(
            'image_border_radius',
            [
                'label' => esc_html__('Border Radius', 'wpsaad-addons-for-dokan-and-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'selectors' => [
                    '{{WRAPPER}} #dokan-seller-listing-wrap ul.dokan-seller-wrap li .store-footer .seller-avatar' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'image_box_shadow',
                'exclude' => [
                    'box_shadow_position',
                ],
                'selector' => '{{WRAPPER}} #dokan-seller-listing-wrap ul.dokan-seller-wrap li .store-footer .seller-avatar',
            ]
        );

        $this->end_controls_section();

        // Store Item Styling Section
        $this->start_controls_section('store_item_style', [
            'label' => __('Vendor Item', 'wpsaad-addons-for-dokan-and-elementor'),
            'tab' => \Elementor\Controls_Manager::TAB_STYLE,
        ]);

        $this->add_control('item_background', [
            'label' => __('Item Background', 'wpsaad-addons-for-dokan-and-elementor'),
            'type' => \Elementor\Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} ul.dokan-seller-wrap li .store-wrapper' => 'background-color: {{VALUE}};',
            ],
        ]);

        $this->add_control('item_padding', [
            'label' => __('Item Padding', 'wpsaad-addons-for-dokan-and-elementor'),
            'type' => \Elementor\Controls_Manager::DIMENSIONS,
            'size_units' => ['px', '%', 'em'],
            'selectors' => [
                '{{WRAPPER}} ul.dokan-seller-wrap li .store-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
            ],
        ]);

        $this->add_responsive_control(
            'item_radius',
            [
                'label' => esc_html__('Item Border Radius', 'wpsaad-addons-for-dokan-and-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'selectors' => [
                    '{{WRAPPER}} ul.dokan-seller-wrap li .store-wrapper' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                    '{{WRAPPER}} #dokan-seller-listing-wrap.grid-view .store-content' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                    '{{WRAPPER}} .store-header .store-banner img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} 0 0 ;',
                    '{{WRAPPER}} ul.dokan-seller-wrap li .store-footer' => 'border-radius: 0 0 {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}} ;',

                ],
            ]
        );

        // Store Label Controls
        $this->add_control('label_color', [
            'label' => __('Label Color', 'wpsaad-addons-for-dokan-and-elementor'),
            'type' => \Elementor\Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} #dokan-seller-listing-wrap .featured-favourite .featured-label' => 'color: {{VALUE}};',
            ],
        ]);
        $this->add_group_control(\Elementor\Group_Control_Typography::get_type(), [
            'name' => 'label_typography',
            'label' => __('Label Typography', 'wpsaad-addons-for-dokan-and-elementor'),
            'selector' => '{{WRAPPER}} #dokan-seller-listing-wrap .featured-favourite .featured-label',
        ]);

        $this->add_control('label_background', [
            'label' => __('Label Background', 'wpsaad-addons-for-dokan-and-elementor'),
            'type' => \Elementor\Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} #dokan-seller-listing-wrap .featured-favourite .featured-label' => 'background-color: {{VALUE}}!important;',
            ],
        ]);

        // Store Title Controls
        $this->add_control('title_color', [
            'label' => __('Title Color', 'wpsaad-addons-for-dokan-and-elementor'),
            'type' => \Elementor\Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .store-data h2 a' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_group_control(\Elementor\Group_Control_Typography::get_type(), [
            'name' => 'title_typography',
            'label' => __('Title Typography', 'wpsaad-addons-for-dokan-and-elementor'),
            'selector' => '{{WRAPPER}} #dokan-seller-listing-wrap.grid-view .store-content .store-data-container .store-data h2 a',
        ]);


        // Store Data Controls
        $this->add_control('store_data_text_color', [
            'label' => __('Store Data Text Color', 'wpsaad-addons-for-dokan-and-elementor'),
            'type' => \Elementor\Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .store-data-container' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_control('store_data_background', [
            'label' => __('Store Data Background', 'wpsaad-addons-for-dokan-and-elementor'),
            'type' => \Elementor\Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .store-data-container' => 'background-color: {{VALUE}};',
            ],
        ]);

        $this->add_control('store_data_padding', [
            'label' => __('Store Data Padding', 'wpsaad-addons-for-dokan-and-elementor'),
            'type' => \Elementor\Controls_Manager::DIMENSIONS,
            'size_units' => ['px', '%', 'em'],
            'selectors' => [
                '{{WRAPPER}} .store-data-container' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
            ],
        ]);

        $this->add_group_control(\Elementor\Group_Control_Typography::get_type(), [
            'name' => 'store_data_typography',
            'label' => __('Store Data Typography', 'wpsaad-addons-for-dokan-and-elementor'),
            'selector' => '{{WRAPPER}} .store-data-container',
        ]);


        $this->add_control('button_text_color', [
            'label' => __('Button Text Color', 'wpsaad-addons-for-dokan-and-elementor'),
            'type' => \Elementor\Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .dokan-btn, {{WRAPPER}} #dokan-seller-listing-wrap.grid-view .store-footer .dokan-btn-theme' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_control('button_background', [
            'label' => __('Button Background', 'wpsaad-addons-for-dokan-and-elementor'),
            'type' => \Elementor\Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .store-footer .dokan-btn-theme' => 'background-color: {{VALUE}}!important;',
            ],
        ]);

        $this->add_group_control(\Elementor\Group_Control_Typography::get_type(), [
            'name' => 'button_typography',
            'label' => __('Button Typography', 'wpsaad-addons-for-dokan-and-elementor'),
            'selector' => '{{WRAPPER}} .dokan-btn, {{WRAPPER}} #dokan-seller-listing-wrap.grid-view .store-footer .dokan-btn-theme',
        ]);

        $this->add_responsive_control(
            'button_radius',
            [
                'label' => esc_html__('Button Border Radius', 'wpsaad-addons-for-dokan-and-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'selectors' => [
                    '{{WRAPPER}} .dokan-btn, {{WRAPPER}} .dokan-follow-store-button, {{WRAPPER}} #dokan-seller-listing-wrap .store-footer .dokan-btn-round' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'item_box_shadow',
                'exclude' => [
                    'box_shadow_position',
                ],
                'selector' => '{{WRAPPER}} .dokan-btn, {{WRAPPER}} .dokan-follow-store-button, {{WRAPPER}} #dokan-seller-listing-wrap .store-footer .dokan-btn-round',
            ]
        );

        $this->add_control('rating_star_color', [
            'label' => __('Star Color', 'wpsaad-addons-for-dokan-and-elementor'),
            'type' => \Elementor\Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} #dokan-seller-listing-wrap .seller-listing-content .dokan-seller-wrap .dokan-single-seller .store-wrapper .store-data .dokan-seller-rating i.dashicons.dashicons-star-filled' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_group_control(\Elementor\Group_Control_Typography::get_type(), [
            'name' => 'rating_size',
            'label' => __('Rating Size', 'wpsaad-addons-for-dokan-and-elementor'),
            'selector' => '{{WRAPPER}} #dokan-seller-listing-wrap .seller-listing-content .dokan-seller-wrap .dokan-single-seller .store-wrapper .store-data .dokan-seller-rating i.dashicons',
        ]);

        $this->end_controls_section();
    }

    protected function render()
    {
        global $post;

        $settings = $this->get_settings_for_display();
        $atts = [];
        $limit = $settings['per_page'];
        $per_row = $settings['per_row'];
        $store_id = $settings['store_id'];
        $store_category = $settings['store_category'];
        $offset = '';
        $show_filter_query = '';
        $show_filter = $settings['show_filter'] ? 'yes' : 'no';
        $show_search = $settings['show_search'] ? 'yes' : 'no';
        $with_product = $settings['with_product'] ? 'yes' : 'no';

        $featured = $settings['featured'] == 'yes' ? 'yes' : 'no';

        $paged  = (int) is_front_page() ? max(1, get_query_var('page')) : max(1, get_query_var('paged'));
        $pagination_base = empty($post) ? '' : str_replace($post->ID, '%#%', esc_url(get_pagenum_link($post->ID)));

        $image_size = $settings['show_image'] ? 'yes' : 'no';

        $defaults = array(
            'per_page'           => $limit,
            'search'             => $show_filter,
            'per_row'            => $per_row,
            'featured'           => 'no',
            'category'           => '',
            'order'              => '',
            'orderby'            => '',
            'with_products_only' => '',
        );

        /**
         * Filter return the number of store listing number per page.
         *
         * @since 2.2
         *
         * @param array
         */
        // $attr   = shortcode_atts( apply_filters( 'dokan_store_listing_per_page', $defaults ), $atts );
        $paged  = (int) is_front_page() ? max(1, get_query_var('page')) : max(1, get_query_var('paged'));
        // $limit  = $attr['per_page'];
        $offset = ($paged - 1) * $limit;

        // prepare filter data
        $dokan_seller_search = '';
        $requested_data      = [];

        // check if nonce verified
        if (isset($_GET['_store_filter_nonce']) && wp_verify_nonce(sanitize_key(wp_unslash($_GET['_store_filter_nonce'])), 'dokan_store_lists_filter_nonce')) {
            $dokan_seller_search = isset($_GET['dokan_seller_search']) ? sanitize_text_field(wp_unslash($_GET['dokan_seller_search'])) : $dokan_seller_search;
            $requested_data = wc_clean(wp_unslash($_GET));
        }

        $seller_args = array(
            'number' => $limit,
            'offset' => $offset,
            'order'  => 'DESC',
        );

        if (! empty($store_id)) {
            $seller_args['include'] = explode(',', $store_id);
        }


        if(taxonomy_exists( 'store_category' )){
            
                    // Check if store categories exists in the GET request.
                    if (isset($_GET['store_categories'])) {
                        $filter_data=[];
                        $storecat= sanitize_text_field(wp_unslash($_GET['store_categories']));
                        foreach($storecat as $slug){
                            $filter_data[] = wc_clean(wp_unslash(esc_attr($slug)));
                        }
                        $seller_args['store_category_query'][] = array(
                            'taxonomy' => 'store_category',
                            'field'    => 'slug',
                            'terms'    => $filter_data,
                        );

                    }
            
            if ( ! empty( $store_category) ) {
                $seller_args['store_category_query'][] = array(
                    'taxonomy' => 'store_category',
                    'field'    => 'id',
                    'terms'    => explode( ',', $store_category ),
                );
            }



        }
        if ($with_product == 'yes') {
            $seller_args['has_published_posts'] = ['product'];
        }
        // if search is enabled, perform a search
        if ($show_search == 'yes') {
            if (! empty($dokan_seller_search)) {
                $seller_args['meta_query'] = [ // phpcs:ignore
                    [
                        'key'     => 'dokan_store_name',
                        'value'   => $dokan_seller_search,
                        'compare' => 'LIKE',
                    ],
                ];
            }
        }

        if ('yes' === $featured) {
            $seller_args['featured'] = 'yes';
        }

        $sellers = dokan_get_sellers($seller_args, $requested_data);
// var_dump($featured);
        /**
         * Filter for store listing args
         *
         * @since 2.4.9
         */
        $template_args = [
            'sellers'             => $sellers,
            'limit'               => $limit,
            'offset'              => $offset,
            'paged'               => $paged,
            'image_size'          => 'full',
            'search'              => $show_filter,
            'per_row'             => $per_row,
            'dokan_seller_search' => $dokan_seller_search,
            'pagination_base' => $pagination_base,
        ];


        if ($show_search == 'yes') {
            $args = [
                'search_query'    => $show_filter_query,
                'pagination_base' => $pagination_base,
                'per_row'         => $per_row,
            ];
            dokan_get_template_part('seller-search-form', false, $args);
        }

        if ($show_filter == 'yes') {
            do_action('dokan_store_lists_filter_form', $sellers);
        }

        // var_dump($pagination_base);
        // var_dump(DOKAN_LOAD_SCRIPTS);       
        // var_dump(dokan_is_store_listing());
        // dokan_get_template_part( 'store-lists', false, $template_args );
        dokan_get_template_part('store-lists-loop', false, $template_args);


        // echo $stores->render_shortcode($template_args);
        // // dokan_is_store_listing();
        // add_filter('dokan_is_store_listing', function($found, $page){return true;});
        // echo do_shortcode('[dokan-stores]');
        // Include the store loop template and pass the settings
        // include plugin_dir_path( __FILE__ ) .'templates/store-loop-template.php';
    }
}
