<?php
namespace CSSIgniter\Ignition\Elementor;

use \Elementor\Group_Control_Base;

trait Common_Functions {

	/**
	 * Exclude posts control.
	 */
	public function add_exclude_controls() {
		foreach ( \CSSIgniter\Ignition\Elementor\Widgets_Helper::element_post_types() as $name => $label ) {
			$this->add_control( 'post__not_in_' . $name, array(
				'label'       => __( 'Exclude', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::SELECT2,
				'options'     => \CSSIgniter\Ignition\Elementor\Widgets_Helper::get_all_post_type_items( $name ),
				'label_block' => true,
				'multiple'    => true,
				'condition'   => array(
					'posts_group_post_type' => $name,
				),
			) );
		}
	}

	/**
	 * Include posts control.
	 */
	public function add_include_controls() {
		foreach ( \CSSIgniter\Ignition\Elementor\Widgets_Helper::element_post_types() as $name => $label ) {
			$this->add_control( 'post__in_' . $name, array(
				'label'       => __( 'Include', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::SELECT2,
				'options'     => \CSSIgniter\Ignition\Elementor\Widgets_Helper::get_all_post_type_items( $name ),
				'label_block' => true,
				'multiple'    => true,
				'condition'   => array(
					'posts_group_post_type' => $name,
				),
			) );
		}
	}

	/**
	 * Constructs the query control.
	 *
	 * @since 1.0.0
	 */
	protected function query_controls() {

		$this->add_group_control( \CSSIgniter\Ignition\Elementor\Posts_Group_Control::get_type(), array(
			'name' => 'posts_group',
		) );

		$this->add_exclude_controls();

		$this->add_include_controls();

		$this->add_control( 'posts_per_page', array(
			'label'   => __( 'Posts Per Page', 'ignition-elementor-widgets' ),
			'type'    => \Elementor\Controls_Manager::NUMBER,
			'min'     => 1,
			'default' => 4,
		) );

		$this->add_control( 'pagination', array(
			'label'        => __( 'Pagination', 'ignition-elementor-widgets' ),
			'type'         => \Elementor\Controls_Manager::SWITCHER,
			'label_on'     => __( 'On', 'your-plugin' ),
			'label_off'    => __( 'Off', 'your-plugin' ),
			'return_value' => 'yes',
			'default'      => '',
		) );

		$this->add_control( 'offset', array(
			'label'     => __( 'Offset', 'ignition-elementor-widgets' ),
			'type'      => \Elementor\Controls_Manager::NUMBER,
			'min'       => 0,
			'default'   => 0,
			'condition' => array(
				'post_type!' => 'by_id',
			),
		) );

		$this->add_control( 'orderby', array(
			'label'   => __( 'Order By', 'ignition-elementor-widgets' ),
			'type'    => \Elementor\Controls_Manager::SELECT,
			'options' => \CSSIgniter\Ignition\Elementor\Widgets_Helper::get_post_orderby_options(),
			'default' => 'date',
		) );

		$this->add_control( 'order', array(
			'label'   => __( 'Order', 'ignition-elementor-widgets' ),
			'type'    => \Elementor\Controls_Manager::SELECT,
			'options' => array(
				'asc'  => __( 'Ascending', 'ignition-elementor-widgets' ),
				'desc' => __( 'Descending', 'ignition-elementor-widgets' ),
			),
			'default' => 'desc',
		) );

	}

}
