<?php

namespace Elementor;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
/**
 * Define our Pt Elementor Call Out settings.
 */
class multiblog_elementor_custom_heading extends Widget_Base {
	/**
	 * Define our get_name settings.
	 */
	public function get_name() {
		return 'multiblog-heading';
	}
	/**
	 * Define our get_title settings.
	 */
	public function get_title() {
		return __( 'Custom Heading', 'multiblog' );
	}
	/**
	 * Define our get_icon settings.
	 */
	public function get_icon() {
		return 'eicon-call-to-action';
	}
	/**
	 * Define our get_categories settings.
	 */
	public function get_categories() {
		return [ 'multiblog-elementor-widget' ];
	}
	/**
	 * Define our _register_controls settings.
	 */
    protected function register_controls() {
        // Heading content
        $this->start_controls_section(
            'multiblog_heading_section',
            [
                'label' => esc_html__('Heading Options', 'multiblog'),
            ]
        );


        $this->add_control(
            'multiblog_banner_title',
            [
                'label' => __('Enter Heading', 'multiblog'),
                'type' => Controls_Manager::TEXT,
                'placeholder' => __('Heading', 'multiblog'),
            ]
        );

        $this->add_control(
            'heading_url',
            [
                'label' => __('Heading URL', 'multiblog'),
                'type' => Controls_Manager::URL,
                'placeholder' => __('https://your-link.com', 'multiblog'),
                'show_external' => true,
                'default' => [
                    'url' => '',
                    'is_external' => false,
                    'nofollow' => false,
                ],
            ]
        );

        $this->end_controls_section();

        // Heading style
        $this->start_controls_section(
            'multiblog_heading_style',
            [
                'label' => esc_html__('Heading Style', 'multiblog'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'heading_typography',
                'label' => __('Typography', 'multiblog'),
                'selector' => '{{WRAPPER}} .newsx-widget-title-text',
            ]
        );

        $this->add_control(
            'heading_color',
            [
                'label' => __('Heading Color', 'multiblog'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .newsx-widget-title-text' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_section();

        // Layout 2 specific styles
        $this->start_controls_section(
            'layout_2_style_section',
            [
                'label' => esc_html__('Heading Style', 'multiblog'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'heading_color_palette',
            [
                'label' => __('Heading Color Palette', 'multiblog'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'default' => '#FF184E',
                'selectors' => [
                    '{{WRAPPER}} .newsx-widget-title-wrap h3' => 'background: {{VALUE}};',
                    '{{WRAPPER}} .newsx-widget-title-wrap' => 'border-bottom-color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_section();
    }

	/**
	 * Define our Content Display inline settings.
	 */
	protected function add_inline_editing_attributes( $key, $toolbar = 'basic' ) {
		if ( ! Plugin::$instance->editor->is_edit_mode() ) {
			return;
		}
		$this->add_render_attribute( $key, [
			'class' => 'elementor-inline-editing',
			'data-elementor-setting-key' => $key,
		] );
		if ( 'basic' !== $toolbar ) {
			$this->add_render_attribute( $key, [
				'data-elementor-inline-editing-toolbar' => $toolbar,
			] );
		}
	}

	/**
	 * Define our Content Display settings.
	 */
	protected function render() {
        $settings = $this->get_settings();
        $multiblog_banner_title = $settings['multiblog_banner_title'];
        $url = $settings['heading_url']['url'];
        $external = $settings['heading_url']['is_external'] ? 'target="_blank"' : '';
        $nofollow = $settings['heading_url']['nofollow'] ? 'rel="nofollow"' : '';
        
        if (!empty($url)) {
            $link_attrs = 'href="' . esc_url($url) . '" ' . $external . ' ' . $nofollow;
        } else {
            $link_attrs = '';
        }
        ?>
        <div class="newsx-widget-title-wrap">
            <?php if ($multiblog_banner_title) {
                if (!empty($url)) {
                    echo '<h3 class="newsx-widget-title"><a ' . $link_attrs . ' class="newsx-widget-title-text">' . esc_html($multiblog_banner_title) . '</a></h3>';
                } else {
                    echo '<h3 class="newsx-widget-title"><span class="newsx-widget-title-text">' . esc_html($multiblog_banner_title) . '</span></h3>';
                }
            } ?>
            <span></span>
        </div>
        <?php
	}
	
}
/*=============Call this every widget ====================*/
Plugin::instance()->widgets_manager->register_widget_type( new multiblog_elementor_custom_heading() );