<?php
namespace dexen\Widget;

  use Elementor\Utils;

class RecentProject extends \Elementor\Widget_Base {

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

	public function get_title() {
		return esc_html__( 'Recent Project', 'dexen' );
	}

	public function get_icon() {
		return 'fa fa-object-ungroup';
	}

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

	protected function _register_controls() {

		$this->start_controls_section(
			'general',
			array(
				'label' => __( 'General', 'dexen-core' ),
			)
		);

		$this->add_control(
			'title',
			array(
				'label'   => __( 'Title', 'dexen-core' ),
				'type'    => \Elementor\Controls_Manager::TEXT,
				'default' => __( 'Team', 'dexen-core' ),

			)
		);
		$this->add_control(
			'tag_line',
			array(
				'label'   => __( 'Tag Line', 'dexen-core' ),
				'type'    => \Elementor\Controls_Manager::TEXT,
				'default' => __( 'Meet Our Team', 'dexen-core' ),

			)
		);
		$this->add_control(
			'column',
			array(
				'label'   => esc_html__( 'Column Width', 'dexen-core' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => '4',
				'options' => array(
					'3'  => esc_html__( '3', 'dexen-core' ),
					'4'  => esc_html__( '4', 'dexen-core' ),
					'5'  => esc_html__( '5', 'dexen-core' ),
					'6'  => esc_html__( '6', 'dexen-core' ),
					'12' => esc_html__( '12', 'dexen-core' ),
				),
			)
		);
		$this->end_controls_section();
		$repeater = new \Elementor\Repeater();
		$repeater->add_control(
			'project_title',
			array(
				'label' => __( 'Title', 'dexen' ),
				'type'  => \Elementor\Controls_Manager::TEXT,
			)
		);
		$repeater->add_control(
			'project_type',
			array(
				'label' => __( 'Project Type', 'dexen' ),
				'type'  => \Elementor\Controls_Manager::TEXT,

			)
		);
		$repeater->add_control(
			'project_image',
			array(
				'label'   => __( 'Project Image', 'dexen' ),
				'type'    => \Elementor\Controls_Manager::MEDIA,
				'default' => array(
					'url' => Utils::get_placeholder_image_src(),
				),

			)
		);

		$repeater->add_control(
			'button_text_link',
			array(
				'label'         => __( 'Url', 'dexen' ),
				'type'          => \Elementor\Controls_Manager::URL,
				'placeholder'   => __( 'https://your-link.com', 'plugin-domain' ),
				'show_external' => true,
				'default'       => array(
					'url'         => '',
					'is_external' => true,
					'nofollow'    => true,
				),

			)
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'project_list',
			array(
				'label' => __( 'Project List', 'dexen' ),
			)
		);

		$this->add_control(
			'items1',
			array(
				'label'       => __( 'Repeater List', 'dexen' ),
				'type'        => \Elementor\Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'list_title'   => __( 'Title #1', 'dexen' ),
						'list_content' => __( 'Item content. Click the edit button to change this text.', 'dexen' ),
					),
					array(
						'list_title'   => __( 'Title #2', 'dexen' ),
						'list_content' => __( 'Item content. Click the edit button to change this text.', 'dexen' ),
					),
				),
				'title_field' => '{{project_title}}',
			)
		);
		$this->end_controls_section();

	}
	protected function render() {

		$settings = $this->get_settings_for_display();

		$title    = $settings['title'];
		$tag_line = $settings['tag_line'];

		$columnClass = '';
		$column      = $settings['column'];

		switch ( $column ) {
			case '3':
				$columnClass = 'col-lg-3 col-md-6 col-sm-12 ';
				break;
			case '4':
				$columnClass = 'col-lg-4 col-md-12 col-sm-12 ';
				break;
			case '5':
				$columnClass = 'col-lg-5 col-md-6 col-sm-12  ';
				break;
			case '6':
				$columnClass = 'col-lg-6 col-md-12 col-sm-12 ';
				break;
			case '12':
				$columnClass = 'col-lg-12 col-md-12 col-sm-12 ';
				break;
			default:
				$columnClass = 'col-lg-3 col-md-6 col-sm-12 ';
		}

		?>    
	<section class="project-style-one sec-pad sec-pad-content-margin-80">
			<div class="container">
			  <?php if ( $tag_line || $title ) : ?>
				  <div class="block-title text-center">
					  <p class="block-title__tag-line"><?php echo esc_html( $tag_line ); ?></p>
					  <h2 class="block-title__title"><?php echo esc_html( $title ); ?></h2>
				  </div><!-- /.block-title -->
			  <?php endif; ?>
				<div class="row">
				<?php
				foreach ( $settings['items1'] as $item ) {

					$project_type  = $item['project_type'];
					$project_title = $item['project_title'];

					$project_image    = ( $item['project_image']['id'] != '' ) ? wp_get_attachment_url( $item['project_image']['id'], 'full' ) : $item['project_image']['url'];
					$project_image_id = $item['project_image']['id'];
					$alt              = get_post_meta( $project_image_id, '_wp_attachment_image_alt', true );
					?>
					<div class="<?php echo esc_attr( $columnClass ); ?>">
						<div class="project-style-one__single content-margin-80">
							<div class="project-style-one__image-block">
							<img src="<?php echo esc_url( $project_image ); ?>" alt="<?php echo esc_attr( $alt ); ?>"></a>
							</div><!-- /.project-style-one__image-block -->
							<div class="project-style-one__text-block thm-gray-bg text-center">
								<span class="project-style-one__category"><?php echo esc_html( $project_type ); ?></span>
								<h3 class="project-style-one__title">
							  <?php if ( ! empty( $item['button_text_link']['url'] ) ) : ?>
							   <a    href="<?php echo esc_url( $item['button_text_link']['url'] ); ?>"
									<?php
														$target   = $item['button_text_link']['is_external'] ? ' target=_blank' : '';
														$nofollow = $item['button_text_link']['nofollow'] ? ' rel=nofollow' : '';
														echo esc_attr( $target . $nofollow );
									?>
													 >
								  <?php endif; ?>
								  <?php echo esc_html( $project_title ); ?>
								  <?php
									if ( ! empty( $item['button_text_link']['url'] ) ) :
										?>
										</a> <?php endif; ?>
								  </h3><!-- /.project-style-one__title -->
							  <?php if ( ! empty( $item['button_text_link']['url'] ) ) : ?>
									<a 
													class="project-style-one__more-link"
													href="<?php echo esc_url( $item['button_text_link']['url'] ); ?>"
													<?php
														$target   = $item['button_text_link']['is_external'] ? ' target=_blank' : '';
														$nofollow = $item['button_text_link']['nofollow'] ? ' rel=nofollow' : '';
														echo esc_attr( $target . $nofollow );
													?>
													><i class="fa fa-plus"></i></a>
									</a>   
								<?php endif; ?>
							</div><!-- /.project-style-one__text-block -->
						</div><!-- /.project-style-one__single -->
					</div><!-- /.col-lg-4 -->
				<?php } ?>
				</div><!-- /.row -->
			</div><!-- /.container -->
		</section><!-- /.project-style-one -->
		<?php
	}

	protected function _content_template() {

	}
}

  \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new RecentProject() );
