<?php

use Elementor\Controls_Manager;

class Zbr_Elementor_Agenda_Widget extends \Elementor\Widget_Base
{

    public function get_name() {
        return "zbr_agenda_list";
    }

    public function get_title() {
        return "Ziber Agenda";
    }

    public function get_icon() {
        return "fa fa-calendar";
    }

    public function get_categories() {

        return [ 'general' ];
    }

    public function get_script_depends() {
        return [];
    }

    public function get_style_depends() {
        return [];
    }

    protected function _register_controls() {

        $this->start_controls_section(
            'content_section',
            [
                'label' => __( 'Content', 'zbr-api' ),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'numberOfPosts',
            [
                'label' => __( 'Number of posts', 'zbr-api' ),
                'type' => Controls_Manager::NUMBER,
                'min' => 1,
                'max' => 10,
                'step' => 1,
                'default' => 3,
            ]
        );

        $this->add_control('showExcerpt', [
            'label' => __('Show excerpt', 'zbr-api'),
            'type'=> Controls_Manager::SWITCHER,
            'return_value' =>'yes',
            'default'=> 'yes'
        ]);

        $this->end_controls_section();
    }

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

        $postQuery = new WP_Query([
            'posts_per_page' => $settings['numberOfPosts'],
            'post_type' => 'zbr_agenda',
            'orderby' => 'date',
            'order' => 'desc',
        ]);

        if ($postQuery->have_posts()) {
            while ($postQuery->have_posts()) {
                $postQuery->the_post();

                $link = get_the_permalink();

                echo "<h3><a href=$link>" . get_the_title() . "</a></h3>";

                if($settings['showExcerpt'] == 'yes'){
                    echo get_the_excerpt();
                }

            }
            wp_reset_postdata();
        }
    }

    protected function _content_template() {
    }

}