<?php

namespace Layerdrops\Ostech\Widgets;


class CustomBlog extends \Elementor\Widget_Base
{
    public function get_name()
    {
        return 'ostech-custom-blog';
    }

    public function get_title()
    {
        return __('Custom Blog', 'ostech-addon');
    }

    public function get_icon()
    {
        return 'eicon-cogs';
    }

    public function get_categories()
    {
        return ['ostech-category'];
    }

    protected function register_controls()
    {
        $this->start_controls_section(
            'layout_section',
            [
                'label' => __('Layout', 'ostech-addon'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'layout_type',
            [
                'label' => __('Select Layout', 'ostech-addon'),
                'type' => \Elementor\Controls_Manager::SELECT2,
                'default' => 'layout_one',
                'options' => [
                    'layout_one' => __('Layout One', 'ostech-addon'),
                    'layout_two' => __('Layout Two', 'ostech-addon'),
                ]
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'layout_one_header_title',
            [
                'label' => __('Blog Header', 'ostech-addon'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
                'condition' => [
                    'layout_type' => ['layout_one', 'layout_two']
                ]
            ]
        );

        $this->add_control(
            'sec_icon',
            [
                'label' => __('Section Icon', 'ostech-addon'),
                'type' => \Elementor\Controls_Manager::ICONS,
                'default' => [
                    'value' => 'ostech-icons-two-logo',
                    'library' => 'custom-icon',
                ],
            ]
        );

        $this->add_control(
            'sec_title',
            [
                'label' => __('Section Title', 'ostech-addon'),
                'type' => \Elementor\Controls_Manager::TEXTAREA,
                'rows' => '2',
                'placeholder' => __('Add title', 'ostech-addon'),
                'default' => __('Default Title', 'ostech-addon'),
            ]
        );

        ostech_elementor_heading_option($this, 'Section Title', 'h3', 'layout_one');


        $this->add_control(
            'sec_sub_title',
            [
                'label' => __('Section Sub Title', 'ostech-addon'),
                'type' => \Elementor\Controls_Manager::TEXTAREA,
                'rows' => '2',
                'placeholder' => __('Add sub title', 'ostech-addon'),
                'default' => __('Default Sub Title', 'ostech-addon'),
            ]
        );

        ostech_elementor_heading_option($this, 'Section Sub Title', 'h6', 'layout_one');

        $this->add_control(
            'sec_summary',
            [
                'label' => __('Section Summary', 'ostech-addon'),
                'type' => \Elementor\Controls_Manager::TEXTAREA,
                'rows' => '2',
                'placeholder' => __('Add Summary', 'ostech-addon'),
                'default' => __('Default Summary', 'ostech-addon'),
            ]
        );

        $this->add_control(
            'button_label',
            [
                'label' => __('Button Label', 'ostech-addon'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => __('Discover More', 'ostech-addon'),
                'label_block' => true,
            ]
        );

        $this->add_control(
            'button_url',
            [
                'label' => __('Button Url', 'ostech-addon'),
                'type' => \Elementor\Controls_Manager::URL,
                'placeholder' => __('#', 'ostech-addon'),
                'show_external' => false,
                'default' => [
                    'url' => '#',
                    'is_external' => false,
                    'nofollow' => false,
                ],
                'show_label' => true,
            ]
        );

        $this->end_controls_section();


        $this->start_controls_section(
            'layout_one_post_content',
            [
                'label' => __('Blog Post', 'ostech-addon'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
                'condition' => [
                    'layout_type' => 'layout_one'
                ]
            ]
        );

        $layout_one_blog_post = new \Elementor\Repeater();

        $layout_one_blog_post->add_control(
            'select_post',
            [
                'label'       => esc_html__('Select Post', 'ostech-addon'),
                'label_block' => true,
                'type'        => \Elementor\Controls_Manager::SELECT,
                'options'     => ostech_post_query('post'),
            ]
        );

        $layout_one_blog_post->add_control(
            'custom_title',
            [
                'label' => __('Custom Title', 'ostech-addon'),
                'type' => \Elementor\Controls_Manager::TEXTAREA,
                'placeholder' => __('Add Custom Title', 'ostech-addon'),
                'default' => '',
            ]
        );

        $layout_one_blog_post->add_control(
            'read_more_txt',
            [
                'label' => __('Read More Text', 'ostech-addon'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'placeholder' => __('Add Read More Text', 'ostech-addon'),
                'default' => __('Read More', 'ostech-addon'),
                'label_block' => true
            ]
        );

        $layout_one_blog_post->add_control(
            'image',
            [
                'label' => __('Image', 'ostech-addon'),
                'type' => \Elementor\Controls_Manager::MEDIA,
                'default' => [],
            ]
        );

        $layout_one_blog_post->add_control(
            'title_length',
            [
                'label'         => esc_html__('Title Length', 'ostech-addon'),
                'type'             => \Elementor\Controls_Manager::TEXT,
                'default'         => '12',
                'placeholder'     => esc_html__('Type Title Length', 'ostech-addon'),
            ]
        );

        $this->add_control(
            'layout_one_blog_post',
            [
                'label' => __('Blog Post', 'ostech-addon'),
                'prevent_empty' => false,
                'type' => \Elementor\Controls_Manager::REPEATER,
                'fields' => $layout_one_blog_post->get_controls(),
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'layout_two_post_content',
            [
                'label' => __('Blog Post', 'ostech-addon'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
                'condition' => [
                    'layout_type' => 'layout_two'
                ]
            ]
        );

        $layout_two_blog_post = new \Elementor\Repeater();

        $layout_two_blog_post->add_control(
            'select_post',
            [
                'label'       => esc_html__('Select Post', 'ostech-addon'),
                'label_block' => true,
                'type'        => \Elementor\Controls_Manager::SELECT,
                'options'     => ostech_post_query('post'),
            ]
        );

        $layout_two_blog_post->add_control(
            'custom_title',
            [
                'label' => __('Custom Title', 'ostech-addon'),
                'type' => \Elementor\Controls_Manager::TEXTAREA,
                'placeholder' => __('Add Custom Title', 'ostech-addon'),
                'default' => '',
            ]
        );

        $layout_two_blog_post->add_control(
            'summary_text',
            [
                'label' => __('Summary Text', 'ostech-addon'),
                'type' => \Elementor\Controls_Manager::TEXTAREA,
                'placeholder' => __('Add Summary Text', 'ostech-addon'),
                'default' => 'Default Text',
            ]
        );

        $layout_two_blog_post->add_control(
            'title_length',
            [
                'label'         => esc_html__('Title Length', 'ostech-addon'),
                'type'             => \Elementor\Controls_Manager::TEXT,
                'default'         => '12',
                'placeholder'     => esc_html__('Type Title Length', 'ostech-addon'),
            ]
        );


        $layout_two_blog_post->add_control(
            'read_more_txt',
            [
                'label' => __('Read More Text', 'ostech-addon'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'placeholder' => __('Add Read More Text', 'ostech-addon'),
                'default' => __('Read More', 'ostech-addon'),
                'label_block' => true
            ]
        );

        $layout_two_blog_post->add_control(
            'image',
            [
                'label' => __('Image', 'ostech-addon'),
                'type' => \Elementor\Controls_Manager::MEDIA,
                'default' => [],
            ]
        );

        $this->add_control(
            'layout_two_blog_post',
            [
                'label' => __('Blog Post', 'ostech-addon'),
                'prevent_empty' => false,
                'type' => \Elementor\Controls_Manager::REPEATER,
                'fields' => $layout_two_blog_post->get_controls(),
            ]
        );


        $this->end_controls_section();

        //General style
        $this->start_controls_section(
            'general_style',
            [
                'label' => esc_html__('Content Style', 'ostech-addon'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        ostech_elementor_general_style_options($this, 'Section Title', '{{WRAPPER}} .sec-title__title,{{WRAPPER}} .sec-title-two__title', ['layout_one', 'layout_two']);
        ostech_elementor_general_style_options($this, 'Section Sub Title', '{{WRAPPER}} .sec-title--two .sec-title__tagline,{{WRAPPER}} .sec-title-two__tagline', ['layout_one', 'layout_two']);

        ostech_elementor_general_style_options($this, 'Post Title', '{{WRAPPER}} .blog-card-five__title a,{{WRAPPER}} .blog-card-three__title a,{{WRAPPER}} .blog-card-four__title a', ['layout_one', 'layout_two']);
        ostech_elementor_general_style_options($this, 'Comment', '{{WRAPPER}} .blog-card-five__meta li a,{{WRAPPER}} .blog-four__single__comment a', ['layout_one', 'layout_two']);

        $this->end_controls_section();

        ostech_get_elementor_carousel_options($this, ['layout_one', 'layout_two']);
    }

    protected function render()
    {
        $settings = $this->get_settings_for_display();
        include ostech_get_template('custom-blog-one.php');
        include ostech_get_template('custom-blog-two.php');
    }
}
