<?php
namespace dexen\Widget;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Elementor\Controls_Manager;
use Elementor\Plugin;
use Elementor\Utils;
use Elementor\Group_Control_Image_Size;
use Elementor\Widget_Base;

class CTA_Three extends Widget_Base {

	public function get_name() {
		return 'dexen_cta_three';
	}

	public function get_title() {
		return esc_html__( 'Call To Action Two', 'dexen-core' );
	}

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

	public function get_categories() {
		return array( 'dexencore' );
	}

	protected function _register_controls() {

		$this->start_controls_section(
			'introduction_section',
			array(
				'label' => esc_html__( 'Content', 'dexen-core' ),
			)
		);

		$this->add_control(
			'style',
			array(
				'label'   => esc_html__( 'Style', 'dexen-core' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => 'one',
				'options' => array(
					'one'   => esc_html__( 'One', 'dexen-core' ),
					'two'   => esc_html__( 'Two', 'dexen-core' ),
					'three' => esc_html__( 'Three', 'dexen-core' ),
				),
			)
		);

		$this->add_control(
			'title_1',
			array(
				'label'   => esc_html__( 'Title 1', 'dexen-core' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'Get your Stationery', 'dexen-core' ),
			)
		);

		$this->add_control(
			'title_2',
			array(
				'label'   => esc_html__( 'Title 2', 'dexen-core' ),
				'type'    => Controls_Manager::TEXTAREA,
				'default' => 'Try Our Premium <br> Dexen Stationery <br> Designs',
			)
		);

		$this->add_control(
			'image',
			array(
				'label'   => __( 'Image', 'dexen-core' ),
				'type'    => Controls_Manager::MEDIA,
				'dynamic' => array(
					'active' => true,
				),
				'default' => array(
					'url' => Utils::get_placeholder_image_src(),
				),

			)
		);
		$this->add_control(
			'image_content',
			array(
				'label'     => esc_html__( 'Image Content', 'dexen-core' ),
				'type'      => Controls_Manager::TEXTAREA,
				'default'   => '<h3>Printfinity Makes Every Business Card <br> Unique &amp; Professional</h3>',
				'condition' => array(
					'style' => 'one',
				),
			)
		);

		$this->add_control(
			'content',
			array(
				'label'   => esc_html__( 'Content', 'dexen-core' ),
				'type'    => Controls_Manager::WYSIWYG,
				'default' => '',
			)
		);

		$this->add_control(
			'button_text',
			array(
				'label'   => esc_html__( 'Button Text', 'dexen-core' ),
				'type'    => Controls_Manager::TEXT,
				'default' => 'Contact Us',
			)
		);

		$this->add_control(
			'action_link',
			array(
				'label'         => __( 'Action Button', 'dexen-core' ),
				'type'          => Controls_Manager::URL,
				'default'       => array(
					'url'         => '#',
					'is_external' => '',
				),
				'show_external' => true,
			)
		);

		$this->end_controls_section();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$style    = $settings['style'];
		$url      = '#';
		$target   = '';
		if ( ! empty( $settings['action_link'] ) ) {
			$link   = $settings['action_link'];
			$url    = $link['url'];
			$target = $link['is_external'] ? 'target="_blank"' : '';
		}

		$image_url = ( $settings['image']['id'] != '' ) ? wp_get_attachment_url( $settings['image']['id'], 'full' ) : $settings['image']['url'];
		$image_id  = $settings['image']['id'];
		$alt       = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
		if ( $style == 'one' ) :
			?>
		<section class="cta-one thm-black-bg">
					<div class="container-fluid p-0">
						<div class="row flex-row-reverse">
							<div class="col-lg-6 d-flex">
								<div class="cta-one__content-block my-auto">
									<div class="block-title">
										<p class="block-title__tag-line block-title__primary-color"><?php echo wp_kses_post( $settings['title_1'] ); ?></p>
										<h2 class="block-title__title block-title__light-color"><?php echo wp_kses_post( $settings['title_2'] ); ?></h2>
									</div><!-- /.block-title -->
									<p class="cta-one__text">  <?php echo wp_kses_post( $settings['content'] ); ?></p>
									<a 
													class="thm-btn thm-btn__base-bg cta-one__btn"
													href="<?php echo esc_url( $settings['action_link']['url'] ); ?>"
													<?php
														$target   = $settings['action_link']['is_external'] ? ' target=_blank' : '';
														$nofollow = $settings['action_link']['nofollow'] ? ' rel=nofollow' : '';
														echo esc_attr( $target . $nofollow );
													?>
													><?php echo wp_kses_post( $settings['button_text'] ); ?></a>
									</a>
								</div><!-- /.cta-one__content-block -->
							</div><!-- /.col-lg-6 -->
							<div class="col-lg-6">
								<div class="cta-one__image-block">
								<img src="<?php echo esc_url( $image_url ); ?>" alt="<?php echo esc_attr( $alt ); ?>" />
									<div class="cta-one__image-block__content text-center">
									<?php echo wp_kses_post( $settings['image_content'] ); ?>
									</div><!-- /.cta-one__image-block__content -->
								</div><!-- /.cta-one__image-block -->
							</div><!-- /.col-lg-6 -->
						</div><!-- /.row -->
					</div><!-- /.container -->
				</section>
	  
		<?php elseif ( $style == 'two' ) : ?>
		<section class="cta-two">            
			<div class="container">
				<div class="row">
					<div class="col-lg-6 d-flex">
						<div class="cta-two__content my-auto">
							<div class="block-title">
								<p class="block-title__tag-line block-title__light-color"><?php echo wp_kses_post( $settings['title_1'] ); ?></p>
								<h2 class="block-title__title block-title__light-color"><?php echo wp_kses_post( $settings['title_2'] ); ?></h2>
							</div><!-- /.block-title -->
							<?php echo wp_kses_post( $settings['content'] ); ?>
								<!-- /.cta-two__features -->
							<a href="#" class="thm-btn cta-two__btn">Learn More</a>
						</div><!-- /.cta-two__content -->
					</div><!-- /.col-lg-6 -->
					<div class="col-lg-6">
						<img src="<?php echo esc_url( $image_url ); ?>" class="float-left" alt="<?php echo esc_attr( $alt ); ?>" />
					</div><!-- /.col-lg-6 -->
				</div><!-- /.row -->
			</div><!-- /.container -->
		</section>

			<?php
	   else :
			?>
		  <div class="cta-three sec-pad">
			 <img src="<?php echo esc_url( $image_url ); ?>" class="cta-three__image" alt="<?php echo esc_attr( $alt ); ?>" />
			<div class="container">
				<div class="row">
					<div class="col-xl-5 col-lg-7">
						<div class="block-title">
							<p class="block-title__tag-line block-title__light-color">
							<?php
								echo wp_kses_post( $settings['title_1'] );
							?>
								</p>
							<h2 class="block-title__title block-title__light-color">
							<?php
								echo wp_kses_post( $settings['title_2'] );
							?>
								</h2>
						</div><!-- /.block-title -->
						<div class="row">
							<div class="col-lg-12">
								<ul class="cta-three__features">
							 <?php
								echo wp_kses_post( $settings['content'] );
								?>
								</ul><!-- /.cta-three__features -->
							</div><!-- /.col-lg-12 -->
						</div><!-- /.row -->
						<?php if ( ! empty( $settings['action_link']['url'] ) ) : ?>
									<a 
													class="cta-three__btn thm-btn"
													href="<?php echo esc_url( $settings['action_link']['url'] ); ?>"
													<?php
														$target   = $settings['action_link']['is_external'] ? ' target=_blank' : '';
														$nofollow = $settings['action_link']['nofollow'] ? ' rel=nofollow' : '';
														echo esc_attr( $target . $nofollow );
													?>
													><?php echo wp_kses_post( $settings['button_text'] ); ?></a>
									</a>   
								<?php endif; ?>
					</div><!-- /.col-lg-5 -->
				</div><!-- /.row -->
			</div><!-- /.container -->
		</div>
		   <?php

		endif;
	}

	protected function content_template() {

	}

}

Plugin::instance()->widgets_manager->register_widget_type( new CTA_Three() );
