<?php
/**
 * Elementor Event Widget.
 *
 * Elementor widget that inserts an embbedable content into the page, from any given URL.
 *
 * @since 1.0.5
 */
class Elementor_Event_Widget extends \Elementor\Widget_Base {

	/**
	 * Get widget name.
	 *
	 * Retrieve Event widget name.
	 *
	 * @since 1.0.5
	 * @access public
	 *
	 * @return string Widget name.
	 */
	public function get_name() {
		return 'event';
	}

	/**
	 * Get widget title.
	 *
	 * Retrieve Event widget title.
	 *
	 * @since 1.0.5
	 * @access public
	 *
	 * @return string Widget title.
	 */
	public function get_title() {
		return __( 'Event Calendar', 'endless-addons' );
	}

	/**
	 * Get widget icon.
	 *
	 * Retrieve Event widget icon.
	 *
	 * @since 1.0.5
	 * @access public
	 *
	 * @return string Widget icon.
	 */
	public function get_icon() {
		return 'fa fa-calendar';
	}

	/**
	 * Get widget categories.
	 *
	 * Retrieve the list of categories the Event widget belongs to.
	 *
	 * @since 1.0.5
	 * @access public
	 *
	 * @return array Widget categories.
	 */
	public function get_categories() {
		return [ 'endless-elements' ];
	}

	/**
	 * Register Event widget controls.
	 *
	 * Adds different input fields to allow the user to change and customize the widget settings.
	 *
	 * @since 1.0.5
	 * @access protected
	 */
	protected function register_controls() {

		$this->start_controls_section(
			'default_section',
			[    
				'label' => __( 'Settings', 'endless-addons' ),
				'tab' => \Elementor\Controls_Manager::TAB_CONTENT
			]
		);

		$this->add_control(
			'show_event',
			[
				'label' => __( 'Show/Hide Event Section', 'endless-addons' ),
				'type' => \Elementor\Controls_Manager::CHOOSE,
				'options' => [
					'show' => [
						'title' => __( 'Show', 'endless-addons' ),
						'icon' => 'fa fa-check'
					],
					'hide' => [
						'title' => __( 'Hide', 'endless-addons' ),
						'icon' => 'fa fa-close'
					]
				],
				'default' => 'show',
				'toggle' => true
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'event_section',
			[
				'label' => __( 'Event Section', 'endless-addons' ),
				'tab' => \Elementor\Controls_Manager::TAB_CONTENT
			]
		);

		$this->add_control(
			'section_title',
			[
				'label' => __( 'Section Title', 'endless-addons' ),
				'type' => \Elementor\Controls_Manager::TEXT,
				'input_type' => 'text',
				'placeholder' => __( 'Title', 'endless-addons' ),
				'default'  => __( 'Design 01', 'endless-addons' )
			]
		);

		$this->add_control(
			'section_desc',
			[
				'label' => __( 'Section Description', 'endless-addons' ),
				'type' => \Elementor\Controls_Manager::WYSIWYG,
				'input_type' => 'WYSIWYG',
				'placeholder' => __( 'Description', 'endless-addons' ),
				'default' => __( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt dolore magna aliqua. Quis ipsum suspendisse ultrices gravida.', 'endless-addons')
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Background::get_type(),
			[
				'name' => 'section_background',
				'label' => __( 'Background', 'plugin-domain' ),
				'types' => [ 'classic', 'gradient', 'video' ],
				'selector' => '{{WRAPPER}} .ea-event-calendar-area',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'event_list',
			[
				'label' => __( 'Event List', 'endless-addons' ),
				'tab' => \Elementor\Controls_Manager::TAB_CONTENT
			]
		);

		$this->add_control(
			'event_initial_date',
			[
				'label' => __( 'Event Initial Date', 'endless-addons' ),
				'type' => \Elementor\Controls_Manager::TEXT,
				'input_type' => 'text',
				'placeholder' => __( '2021-08-01', 'endless-addons' ),
				'default'  => __( '2021-08-01', 'endless-addons' )
			]
		);

		$repeater = new \Elementor\Repeater();	

		$repeater->add_control(
			'single_event_title', [
				'label' => __( 'Event Title', 'endless-addons' ),
				'type' => \Elementor\Controls_Manager::TEXT,
				'input_type' => 'text',
				'placeholder' => __( 'Title', 'endless-addons' ),
				'default'  => __( 'Business Lunch', 'endless-addons' )
			]
		);

		$repeater->add_control(
			'single_event_start_date_time', [
				'label' => __( 'Event Start Date & Time', 'endless-addons' ),
				'type' => \Elementor\Controls_Manager::DATE_TIME,
				'input_type' => 'date_time',
				'placeholder' => __( 'Date & Time', 'endless-addons' ),
				'default' => __( '2021-08-03T13:00:00', 'endless-addons' ),
			]
		);

        $repeater->add_control(
			'single_event_end_date_time', [
				'label' => __( 'Event End Date & Time', 'endless-addons' ),
				'type' => \Elementor\Controls_Manager::DATE_TIME,
				'input_type' => 'date_time',
				'placeholder' => __( 'Date & Time', 'endless-addons' ),
				'default' => __( '2021-08-03T13:00:00', 'endless-addons' ),
			]
		);

		$this->add_control(
			'single_event',
			[
				'label' => __( 'Single Event', 'endless-addons' ),
				'type' => \Elementor\Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'default' => [
					[
						'list_title' => __( 'Single Event #1', 'endless-addons' ),
						'list_content' => __( 'Single Event Content', 'endless-addons' ),
					],
				
				]
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style',
			[
				'label' => __( 'Section Style', 'endless-addons' ),
				'tab' => \Elementor\Controls_Manager::TAB_STYLE
			]
		);

		$this->add_control(
			'section_title_color',
			[
				'label' => __( 'Section Title Color', 'endless-addons' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
                    '{{WRAPPER}} .ea-event-calendar-area .section-title h2' => 'color: {{VALUE}}'
                ]
			]
		);

		$this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'section_title_typography',
                'label' => __( 'Section Title Typography', 'endless-addons' ),
                'scheme' => \Elementor\Core\Schemes\Typography::TYPOGRAPHY_1,
                'selector' => '{{WRAPPER}} .ea-event-calendar-area .section-title h2'
            ]
        );

        $this->add_control(
			'section_desc_color',
			[
				'label' => __( 'Section Description Color', 'endless-addons' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
                    '{{WRAPPER}} .ea-event-calendar-area .section-title p' => 'color: {{VALUE}}'
                ]
			]
		);

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'section_desc_typography',
                'label' => __( 'Section Description Typography', 'endless-addons' ),
                'scheme' => \Elementor\Core\Schemes\Typography::TYPOGRAPHY_1,
                'selector' => '{{WRAPPER}} .ea-event-calendar-area .section-title p'
            ]
        );

		$this->end_controls_section();

		$this->start_controls_section(
			'event_style',
			[
				'label' => __( 'Event Style', 'endless-addons' ),
				'tab' => \Elementor\Controls_Manager::TAB_STYLE
			]
		);

		$this->add_control(
			'table_header_bg_color',
			[
				'label' => __( 'Table Header Background Color', 'endless-addons' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
                    '{{WRAPPER}} .ea-event-calendar-area div#calendar thead' => 'background-color: {{VALUE}}'
                ]
			]
		);

		$this->add_control(
			'event_item_bg_color',
			[
				'label' => __( 'Event Item Background Color', 'endless-addons' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
                    '{{WRAPPER}} .fc-direction-ltr .fc-daygrid-event.fc-event-end, .fc-direction-rtl .fc-daygrid-event.fc-event-start' => 'background-color: {{VALUE}}'
                ]
			]
		);

		$this->add_control(
			'event_item_color',
			[
				'label' => __( 'Event Item Color', 'endless-addons' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
                    '{{WRAPPER}} .fc-direction-ltr .fc-daygrid-event.fc-event-end, .fc-direction-rtl .fc-daygrid-event.fc-event-start' => 'color: {{VALUE}}'
                ]
			]
		);

		$this->add_control(
			'event_item_bg_hover_color',
			[
				'label' => __( 'Event Item Background Hover Color', 'endless-addons' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
                    '{{WRAPPER}} .fc-direction-ltr .fc-daygrid-event.fc-event-end:hover, .fc-direction-rtl .fc-daygrid-event.fc-event-start:hover' => 'background-color: {{VALUE}}'
                ]
			]
		);

		$this->add_control(
			'event_item_hover_color',
			[
				'label' => __( 'Event Item Hover Color', 'endless-addons' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
                    '{{WRAPPER}} .fc-direction-ltr .fc-daygrid-event.fc-event-end:hover, .fc-direction-rtl .fc-daygrid-event.fc-event-start:hover' => 'color: {{VALUE}}'
                ]
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'_section_overlay_style',
			[
				'label' => __('Background Overlay', 'endless-addons'),
				'tab' => \Elementor\Controls_Manager::TAB_STYLE
			]
		);

		$this->start_controls_tabs('_tabs_overlay_style');

		$this->start_controls_tab(
			'_tab_overlay_normal',
			[
				'label' => __('Normal', 'endless-addons'),
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Background::get_type(),
			[
				'name'     => 'hover_overlay_normal',
				'label' => __('Background', 'endless-addons'),
				'show_label' => true,
				'types' => ['classic', 'gradient'],
				'exclude' => [
					'classic' => 'image'
				],
				'selector' => '{{WRAPPER}} .ea-event-calendar-area',
			]
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'_tab_overlay_hover',
			[
				'label' => __('Hover', 'endless-addons'),
			]
		);


		$this->add_group_control(
			\Elementor\Group_Control_Background::get_type(),
			[
				'name'     => 'hover_overlay_hover',
				'label' => __('Background', 'endless-addons'),
				'show_label' => true,
				'types' => ['classic', 'gradient'],
				'exclude' => [
					'classic' => 'image'
				],
				'selector' => '{{WRAPPER}} .ea-event-calendar-area:hover',
			]
		);

		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->end_controls_section();

	}

	/**
	 * Render Event widget output on the frontend.
	 *
	 * Written in PHP and used to generate the final HTML.
	 *
	 * @since 1.0.5
	 * @access protected
	 */
	protected function render() {

		$settings = $this->get_settings_for_display(); 

		if( $settings['show_event'] == 'show' ) : ?>

			<!-- Start EA Event Calendar One Area -->
		        <section class="ea-event-calendar-area ea-style-one bg-shape pb-100">
		            <div class="container">
		                <div class="section-title">
		                    <h2><?php echo esc_html($settings['section_title']); ?></h2>
		                    <p><?php echo wp_kses_post($settings['section_desc']); ?></p>
		                </div>
		                <div class="row justify-content-center">
		                    <div id="calendar"></div>
		                </div>
		            </div>
		        </section>
		    <!-- End EA Event Calendar One Area -->

		    <script type="text/javascript">
			    document.addEventListener('DOMContentLoaded', function() {
			    var calendarEl = document.getElementById('calendar');

				    var calendar = new FullCalendar.Calendar(calendarEl, {
				        headerToolbar: {
				            left: 'prev,next',
				            center: 'title',
				            right: 'today,timeGridDay,timeGridWeek,dayGridMonth,listMonth'
				        },
				        initialDate: '<?php echo esc_html($settings['event_initial_date']); ?>',
				        navLinks: true, // can click day/week names to navigate views
				        businessHours: true, // display business hours
				        editable: true,
				        selectable: true,
				        events: [
				            <?php foreach ( $settings['single_event'] as $item ) : ?>
				            {
				                title: '<?php echo esc_html($item['single_event_title']); ?>',
				                start: '<?php echo esc_html($item['single_event_start_date_time']); ?>',
				                end: '<?php echo esc_html($item['single_event_end_date_time']); ?>',
				            },
				            <?php endforeach; ?> 
				        ]
				    });

			    calendar.render();

			    });
		    </script>
        
        <?php

        endif; 
    
    }

}
