<?php
	namespace Elementor;

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

	add_action( 'elementor/init','Elementor\ci_theme_elements_init' );
	function ci_theme_elements_init() {
		Plugin::instance()->elements_manager->add_category(
			'ci-theme-elements',
			[
				'title'  => __( 'Salon Elements', 'ci_theme' ),
				'icon' => 'font',
			],
			1
		);
	}

	add_action( 'elementor/widgets/widgets_registered', 'Elementor\add_ci_theme_elements' );
	function add_ci_theme_elements() {
		class Widget_Post_Type extends Widget_Base {

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

			public function get_title() {
				return __( 'Salon Post Type', 'ci_theme' );
			}

			public function get_icon() {
				return 'fa fa-window-maximize';
			}

			public function get_categories() {
				return [ 'ci-theme-elements' ];
			}

			protected function register_controls() {
				$this->start_controls_section(
					'section_title',
					[
						'label' => __( 'Salon Post Type', 'ci_theme' ),
					]
				);

				$this->add_control(
					'html_msg',
					[
						'type'    => Controls_Manager::RAW_HTML,
						'raw' => __( 'Display any post type item from Salon by first selecting the post type, e.g. "Rooms" and then the item itself.', 'ci_theme' ),
						'content_classes' => 'ci-description',
					]
				);

				$this->add_control(
					'post_types',
					[
						'label'       => __( 'Post Type', 'ci_theme' ),
						'type' => Controls_Manager::SELECT,
						'default' => '',
						'options' => ci_theme_post_types(),
					]
				);

				$this->add_control(
					'selected_post',
					[
						'label'       => __( 'Post', 'ci_theme' ),
						'type' => Controls_Manager::SELECT,
						'default' => '',
						'options' => '',
					]
				);

				$this->add_control(
					'view',
					[
						'label' => __( 'View', 'ci_theme' ),
						'type' => Controls_Manager::HIDDEN,
						'default' => 'traditional',
					]
				);

				$this->end_controls_section();

			}

			protected function render() {
				$settings = $this->get_settings();

				if ( empty( $settings['selected_post'] ) )
					return;

				$post_id = $settings['selected_post'];

				$q = new \WP_Query( array(
					'post_type' => get_post_type( $post_id ),
					'p'         => $post_id,
				) );

				while ( $q->have_posts() ) : $q->the_post(); ?>
					<div <?php post_class( 'item' ); ?>>
						<a href="<?php the_permalink(); ?>" class="item-hold">
							<figure class="item-thumb">
								<?php the_post_thumbnail( 'ci_blog_full' ); ?>
							</figure>
						</a>

						<?php get_template_part( 'listing-meta' ); ?>

					</div>
				<?php endwhile;

				wp_reset_postdata();

			}

		}
		Plugin::instance()->widgets_manager->register_widget_type( new Widget_Post_Type() );
	}

	add_action( 'elementor/editor/before_enqueue_scripts', 'Elementor\ci_theme_ajax_posts' );
	function ci_theme_ajax_posts() {

		if ( ! defined( 'ELEMENTOR_VERSION' ) ) {
			return;
		}

		wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/admin/post_ajax.js' );

		$params['no_posts_found'] = __( 'No posts found.', 'ci_theme' );
		$params['ajaxurl']        = admin_url( 'admin-ajax.php' );
		$params['ci_theme_post_nonce']  = wp_create_nonce( 'ci_theme_post_nonce' );

		wp_localize_script( 'ajax-script', 'ElementWidget', $params );
	}

