<?php

namespace AtomicSlider\Traits;

use Elementor\Controls_Manager;

defined('ABSPATH') || die();
trait Global_Terms_Query_Controls {
    protected function render_terms_query_controls($taxonomy = 'category') {

        $this->start_controls_section(
            'section_term_query',
            [
                'label' => __('Query', 'atomic-slider'),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'display_category',
            [
                'label' => __('Type', 'atomic-slider'),
                'type' => Controls_Manager::SELECT,
                'default' => 'all',
                'options' => [
                    'all' => __('All', 'atomic-slider'),
                    'parents' => __('Only Parents', 'atomic-slider'),
                    'child' => __('Only Child', 'atomic-slider')
                ],
            ]
        );

        // $this->add_control(
        // 	'item_limit',
        // 	[
        // 		'label' => esc_html__('Item Limit', 'atomic-slider'),
        // 		'type'  => Controls_Manager::SLIDER,
        // 		'range' => [
        // 			'px' => [
        // 				'min' => 1,
        // 				'max' => 20,
        // 			],
        // 		],
        // 		'default' => [
        // 			'size' => 6,
        // 		],
        // 	]
        // );

        $this->start_controls_tabs(
            'tabs_terms_include_exclude',
            [
                'condition' => ['display_category' => 'all']
            ]
        );
        $this->start_controls_tab(
            'tab_term_include',
            [
                'label' => __('Include', 'atomic-slider'),
                'condition' => ['display_category' => 'all']
            ]
        );

        $this->add_control(
            'cats_include_by_id',
            [
                'label' => __('Categories', 'atomic-slider'),
                'type' => Controls_Manager::SELECT2,
                'multiple' => true,
                'label_block' => true,
                'condition' => [
                    'display_category' => 'all'
                ],
                'options' => atomicslider_get_terms($taxonomy),
            ]
        );

        $this->end_controls_tab();

        $this->start_controls_tab(
            'tab_term_exclude',
            [
                'label' => __('Exclude', 'atomic-slider'),
                'condition' => ['display_category' => 'all']
            ]
        );

        $this->add_control(
            'cats_exclude_by_id',
            [
                'label' => __('Categories', 'atomic-slider'),
                'type' => Controls_Manager::SELECT2,
                'multiple' => true,
                'label_block' => true,
                'condition' => [
                    'display_category' => 'all'
                ],
                'options' => atomicslider_get_terms($taxonomy),
            ]
        );

        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_control(
            'child_cats_notice',
            [
                'type'              => Controls_Manager::RAW_HTML,
                'raw'               => __('WARNING!, Must Select Parent Category from Child Categories of.', 'atomic-slider'),
                'content_classes' => 'elementor-panel-alert elementor-panel-alert-warning',
                'condition' => [
                    'display_category' => 'child',
                    'parent_cats' => 'none'
                ],
            ],
        );
        $this->add_control(
            'parent_cats',
            [
                'label' => __('Child Categories of', 'atomic-slider'),
                'type' => Controls_Manager::SELECT,
                'default' => 'none',
                'options' => atomicslider_get_only_parent_cats($taxonomy),
                'condition' => [
                    'display_category' => 'child'
                ],
            ]
        );


        $this->add_control(
            'orderby',
            [
                'label' => __('Order By', 'atomic-slider'),
                'type' => Controls_Manager::SELECT,
                'default' => 'name',
                'options' => [
                    'name'       => esc_html__('Name', 'atomic-slider'),
                    'count'  => esc_html__('Count', 'atomic-slider'),
                    'slug' => esc_html__('Slug', 'atomic-slider'),
                    // 'menu_order' => esc_html__('Menu Order', 'atomic-slider'),
                    // 'rand'       => esc_html__('Random', 'atomic-slider'),
                ],
            ]
        );

        $this->add_control(
            'order',
            [
                'label' => __('Order', 'atomic-slider'),
                'type' => Controls_Manager::SELECT,
                'default' => 'desc',
                'options' => [
                    'desc' => __('Descending', 'atomic-slider'),
                    'asc' => __('Ascending', 'atomic-slider'),
                ],
            ]
        );
        $this->add_control(
            'hide_empty',
            [
                'label'         => __('Hide Empty', 'atomic-slider'),
                'type'          => Controls_Manager::SWITCHER,
            ]
        );

        $this->end_controls_section();
    }

    // Get ajax post
	protected function asa_get_posts_by_ajax($post_type, $order, $orderby, $posts_per_page, $taxonomy, $category_slug) {

		$query_args = [
			'post_status' => 'publish',
			'post_type' => $post_type,
			'order' => $order,
			'orderby' => $orderby,
			'posts_per_page' => $posts_per_page,
		];


		
		if ($category_slug !== '') {
			$query_args['tax_query'] = [
				[
					'taxonomy' => $taxonomy,
					'field' => 'slug',
					'terms' => $category_slug,
				]
			];
		}
		
		return new \WP_Query($query_args);
	}
}
