<?php

use Elementor\Controls_Manager;
use Elementor\Core\Schemes\Color;
use Elementor\Core\Schemes\Typography;
use Elementor\Group_Control_Typography;
use Elementor\Widget_Base;

class Zbr_Elementor_News_Widget extends Widget_Base
{

    public function get_name(): string
    {
        return "zbr_news_list";
    }

    public function get_title(): string
    {
        return "Ziber News";
    }

    public function get_icon(): string
    {
        return "fa fa-newspaper";
    }

    public function get_categories(): array
    {

        return ['general'];
    }

    public function get_script_depends(): array
    {
        return [];
    }

    public function get_style_depends(): array
    {
        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->add_control(
            'padding',
            [
                'label' => __('Padding', 'zbr-api'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em'],
                'selectors' => [
                    '{{WRAPPER}} .zbr-news-item' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );
        $this->end_controls_section();

        $this->start_controls_section('style_section', [
            'label' => __('Style', 'zbr-api'),
            'tab' => Controls_Manager::TAB_STYLE
        ]);

        $this->start_controls_tabs(
            'style_tabs'
        );

        $this->start_controls_tab(
            'style_title_tab',
            [
                'label' => __('Title', 'zbr-api'),
            ]
        );

        $this->add_group_control(Group_Control_Typography::get_type(), [

            'name' => 'style_typography',
            'label' => __('Typography', 'zbr-api'),
            'selector' => '{{WRAPPER}} h3',
            'scheme' => Typography::TYPOGRAPHY_1,
        ]);

        $this->add_control(
            'title_color',
            [
                'label' => __('Color', 'zbr-api'),
                'type' => Controls_Manager::COLOR,
                'scheme' => [
                    'type' => Color::get_type(),
                    'value' => Color::COLOR_1,
                ],
                'selectors' => [
                    '{{WRAPPER}} h3 a' => 'color: {{VALUE}}',
                ],
            ]
        );
        $this->end_controls_tab();


        $this->start_controls_tab('style_excerpt_tab', [
            'label' => __('Excerpt', 'zbr-api')
        ]);
        $this->add_group_control(Group_Control_Typography::get_type(), [

            'name' => 'excerpt_typography',
            'label' => __('Typography', 'zbr-api'),
            'scheme' => Typography::TYPOGRAPHY_1,
            'selector' => '{{WRAPPER}} p'
        ]);
        $this->add_control(
            'excerpt_color',
            [
                'label' => __('Color', 'zbr-api'),
                'type' => Controls_Manager::COLOR,
                'scheme' => [
                    'type' => Color::get_type(),
                    'value' => Color::COLOR_1,
                ],
                'selectors' => [
                    '{{WRAPPER}} p' => 'color: {{VALUE}}',
                ],
            ]
        );

        $this->end_controls_tab();
        $this->end_controls_tabs();

        $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_news',
            'orderby' => 'date',
            'order' => 'desc',
        ]);

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

                $link = get_the_permalink();
                echo "<div class='zbr-news-item'>";

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

                if ($settings['showExcerpt'] == 'yes') {
                    echo '<p>' . get_the_excerpt() . '</p>';
                }
                echo '</div>';
            }
            wp_reset_postdata();
        }
    }

    protected function _content_template()
    {
    }

}