<?php
/**
 * Abstract base class for Smart Post Show Blocks.
 *
 * @package Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/blocks/includes
 */

namespace SmartPostShow\Blocks;

use SmartPostShow\Blocks\Block_Base;

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

/**
 * Smart Search Block Class
 */
class Smart_Search extends Block_Base {


	/**
	 * Set block properties.
	 *
	 * @return void
	 */
	protected function set_block_properties() {
		$this->block_name     = 'smart-search';
		$this->editor_scripts = array( 'sp_smart_post_blocks_index_js' );
		$this->styles         = array( 'sp_smart_post_blocks_css', 'sp_smart_post_blocks_style' );
		$this->scripts        = array( 'sp_smart_post_smart_search_script' );
	}

	/**
	 * Block render function.
	 *
	 * @param  array  $attributes attributes.
	 * @param  string $content  Content to be rendered.
	 * @param  array  $blocks  blocks.
	 * @return string Rendered HTML content.
	 */
	public function render_block( $attributes, $content = '', $blocks = array() ) {
		if ( \SmartPostShow\Blocks\Helper::is_editor_page() ) {
			return $content;
		}

		$is_license_activated = $this->is_license_activated();
		if ( 'valid' !== $is_license_activated ) {
			return $is_license_activated;
		}

		$unique_id                       = $attributes['uniqueId'] ?? '';
		$additional_css_class            = $attributes['additionalCssClass'] ?? '';
		$display_type                    = $attributes['displayType'] ?? '';
		$enable_popup_heading            = $attributes['enablePopupHeading'] ?? '';
		$enable_close_btn                = $attributes['enablePopupCloseButton'] ?? false;
		$popup_heading_text              = $attributes['popupHeadingText'] ?? '';
		$popup_heading_global_typo_class = isset( $attributes['popupHeadingGlobalTypography']['class'] ) ? $attributes['popupHeadingGlobalTypography']['class'] : '';
		$search_form_preset              = $attributes['searchFormPreset'] ?? '';
		$input_global_typo_class         = isset( $attributes['inputPlaceHolderGlobalTypography']['class'] ) ? $attributes['inputPlaceHolderGlobalTypography']['class'] : '';
		$is_placeholder_enabled          = $attributes['placeholderEnable'] ?? false;
		$placeholder_text                = $is_placeholder_enabled ? $attributes['placeholder'] ?? '' : '';
		$post_type                       = $attributes['postType'] ?? '';
		$is_taxonomy_enabled             = $attributes['taxonomyFilterEnable'] ?? false;
		$filter_label_text               = $attributes['filterLabelText'] ?? '';
		$enable_search_icon              = $attributes['searchIcon'] ?? false;
		$enable_search_btn_label         = $attributes['searchBtnLabel'] ?? false;
		$search_btn_label_text           = $attributes['searchBtnLabelText'] ?? '';
		$search_result_display_type      = $attributes['searchResultDisplayType'] ?? '';
		$is_ajax_search                  = $attributes['ajaxSearch'] ?? false;
		$show_image                      = $attributes['searchResultShowImage'] ?? false;
		$show_date                       = $attributes['searchResultShowDate'] ?? false;
		$show_taxonomy                   = $attributes['searchResultShowTaxonomy'] ?? false;
		$show_author                     = $attributes['searchResultShowAuthor'] ?? false;
		$show_excerpt                    = $attributes['searchResultShowExcerpt'] ?? false;
		$excerpt_limit                   = $attributes['searchResultExcerptLimit'] ?? 20;
		$title_global_typo               = isset( $attributes['searchResultTitleGlobalTypography'] ) ? $attributes['searchResultTitleGlobalTypography'] : '';
		$meta_global_typo                = isset( $attributes['searchResultMetaTypography'] ) ? $attributes['searchResultMetaTypography'] : '';
		$post_limit                      = isset( $attributes['initialPostDisplay']['value'] ) ? $attributes['initialPostDisplay']['value'] : 10;
		$view_more_text                  = $attributes['moreResultLabelText'] ?? '';
		$more_result_click_action        = $attributes['moreResultClickAction'] ?? 'expanded';

		$taxonomy_type  = $attributes['selectedTaxonomy'] ?? 'category';
		$selected_terms = $attributes['selectedTerms'] ?? array();

		$search_params = array(
			'post_tags' => 'tag',
			'category'  => 'cat',
		);

		$search_settings = array(
			'postType'                => $post_type,
			'isTaxonomyEnabled'       => $is_taxonomy_enabled,
			'taxonomyType'            => $taxonomy_type,
			'isAjaxSearch'            => $is_ajax_search,
			'showDate'                => $show_date,
			'showTaxonomy'            => $show_taxonomy,
			'showExcerpt'             => $show_excerpt,
			'showAuthor'              => $show_author,
			'excerptLimit'            => $excerpt_limit,
			'titleGlobalTypography'   => $title_global_typo,
			'metaGlobalTypography'    => $meta_global_typo,
			'searchResultDisplayType' => $search_result_display_type,
			'showImage'               => $show_image,
			'displayType'             => $display_type,
			'postLimit'               => $post_limit,
			'moreResultClickAction'   => $more_result_click_action,
			'hightLightSearchTerm'    => $attributes['hightLightSearchTerm'] ?? true,
			'showResultsCount'        => $attributes['showResultsCount'] ?? true,
		);

		ob_start();

		?>
		<div id="<?php echo esc_attr( $unique_id ); ?>" class="sp-smart-post-wrapper <?php echo esc_attr( $additional_css_class ); ?>">
			<?php if ( 'popup' === $display_type ) { ?>
				<div class="sp-smart-post-search-trigger">
					<button class="sp-smart-post-search-trigger-button">
						<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
							<path d="M17.9424 17.0578L14.0306 13.1469C15.1644 11.7857 15.7298 10.0398 15.6091 8.27244C15.4884 6.50506 14.691 4.85223 13.3828 3.65779C12.0745 2.46334 10.3561 1.81926 8.5851 1.85951C6.81406 1.89976 5.12671 2.62125 3.87407 3.87389C2.62143 5.12653 1.89994 6.81388 1.85969 8.58492C1.81944 10.356 2.46353 12.0744 3.65797 13.3826C4.85241 14.6909 6.50524 15.4883 8.27263 15.6089C10.04 15.7296 11.7859 15.1642 13.1471 14.0305L17.058 17.9422C17.1161 18.0003 17.185 18.0463 17.2609 18.0777C17.3367 18.1092 17.4181 18.1253 17.5002 18.1253C17.5823 18.1253 17.6636 18.1092 17.7395 18.0777C17.8154 18.0463 17.8843 18.0003 17.9424 17.9422C18.0004 17.8841 18.0465 17.8152 18.0779 17.7393C18.1094 17.6634 18.1255 17.5821 18.1255 17.5C18.1255 17.4179 18.1094 17.3366 18.0779 17.2607C18.0465 17.1848 18.0004 17.1159 17.9424 17.0578ZM3.12518 8.75C3.12518 7.63748 3.45508 6.54994 4.07316 5.62491C4.69124 4.69989 5.56975 3.97892 6.59758 3.55317C7.62542 3.12743 8.75642 3.01604 9.84756 3.23308C10.9387 3.45012 11.941 3.98585 12.7277 4.77252C13.5143 5.55919 14.0501 6.56147 14.2671 7.65261C14.4841 8.74376 14.3727 9.87475 13.947 10.9026C13.5213 11.9304 12.8003 12.8089 11.8753 13.427C10.9502 14.0451 9.8627 14.375 8.75018 14.375C7.25884 14.3733 5.82906 13.7802 4.77453 12.7256C3.72 11.6711 3.12683 10.2413 3.12518 8.75Z" fill="currentColor" />
						</svg>
					</button>
				</div>
			<?php } ?>

			<?php if ( 'popup' === $display_type ) { ?>
				<div class="sp-smart-post-search-overlay <?php echo esc_attr( $search_result_display_type ); ?>"></div>
			<?php } ?>
			<div class="sp-smart-post-search-wrapper <?php echo esc_attr( 'popup' === $display_type ? 'sp-smart-post-search-popup' : '' ); ?>" tabindex="-1">
				<?php if ( 'popup' === $display_type && $enable_popup_heading ) { ?>
					<h4 class='sp-smart-post-popup-heading <?php echo esc_attr( $popup_heading_global_typo_class ); ?>'><?php echo esc_html( $popup_heading_text ); ?></h4>
				<?php } ?>

				<?php if ( 'popup' === $display_type && $enable_close_btn ) { ?>
					<span class='sp-smart-post-popup-close-icon'>
						<svg width="10" height="10" viewBox="0 0 10 10" fill="none" xmlns="http://www.w3.org/2000/svg">
							<path
								d="M5.00003 6.06069L8.71233 9.77299L9.77299 8.71233L6.06069 5.00003L9.773 1.28772L8.71234 0.227059L5.00003 3.93937L1.28771 0.227051L0.227051 1.28771L3.93937 5.00003L0.22706 8.71234L1.28772 9.773L5.00003 6.06069Z"
								fill="#1E1E1E" />
						</svg>
					</span>
				<?php } ?>

				<form role="search" method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>" class="sp-smart-post-search-form <?php echo esc_attr( $search_form_preset ); ?>" data-search_settings='<?php echo json_encode( $search_settings ); ?>'>
					<div class="sp-smart-post-search-input-wrapper">
						<input
							type="search"
							name="s"
							required
							action="<?php echo esc_url( home_url( '/' ) ); ?>"
							class="sp-smart-post-search-input <?php echo esc_attr( $input_global_typo_class ); ?>"
							placeholder="<?php echo esc_attr( $placeholder_text ); ?>"
						/>
						<?php if ( '' !== $post_type && $is_taxonomy_enabled ) { ?>
							<div class="sp-smart-post-search-select">
								<span class="sp-smart-post-search-dropdown-icon">
									<svg
										width="21"
										height="20"
										viewBox="0 0 21 20"
										fill="none"
										xmlns="http://www.w3.org/2000/svg">
										<path
											d="M5.7373 6L10.7373 11L15.7373 6L17.7373 7L10.7373 14L3.7373 7L5.7373 6Z"
											fill="#2F2F2F" />
									</svg>
								</span>
								<select name="<?php echo esc_attr( $search_params[ $taxonomy_type ] ?? $taxonomy_type ); ?>" class="sp-smart-post-search-cate-list">
									<option value=""><?php echo esc_html( $filter_label_text ); ?></option>
									<?php
									if ( ! empty( $selected_terms ) ) {
										foreach ( $selected_terms as $term ) {
											$term       = get_term( $term['value'], $taxonomy_type );
											$id         = $term->term_id;
											$slug       = $term->slug;
											$term_label = $term->name;
											?>
											<option value="<?php echo esc_attr( $slug ); ?>" data-label="<?php echo esc_html( $term_label ); ?>">
												<?php echo esc_html( $term_label ); ?>
											</option>
											<?php
										}
									} else {
										$taxonomy_terms = get_terms(
											array(
												'taxonomy' => $taxonomy_type,
												'hide_empty' => false,
											)
										);

										foreach ( $taxonomy_terms as $term ) {
											$id         = $term->term_id;
											$slug       = $term->slug;
											$term_label = $term->name;
											?>
											<option value="<?php echo esc_attr( $slug ); ?>" data-label="<?php echo esc_html( $term_label ); ?>">
												<?php echo esc_html( $term_label ); ?>
											</option>
											<?php
										}
									}
									?>
								</select>
							</div>
						<?php } ?>
					</div>
					<button type="submit" class='sp-smart-post-search-button'>
						<?php if ( $enable_search_icon ) { ?>
							<svg class="sp-smart-post-search-icon" width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
								<path d="M17.9424 17.0578L14.0306 13.1469C15.1644 11.7857 15.7298 10.0398 15.6091 8.27244C15.4884 6.50506 14.691 4.85223 13.3828 3.65779C12.0745 2.46334 10.3561 1.81926 8.5851 1.85951C6.81406 1.89976 5.12671 2.62125 3.87407 3.87389C2.62143 5.12653 1.89994 6.81388 1.85969 8.58492C1.81944 10.356 2.46353 12.0744 3.65797 13.3826C4.85241 14.6909 6.50524 15.4883 8.27263 15.6089C10.04 15.7296 11.7859 15.1642 13.1471 14.0305L17.058 17.9422C17.1161 18.0003 17.185 18.0463 17.2609 18.0777C17.3367 18.1092 17.4181 18.1253 17.5002 18.1253C17.5823 18.1253 17.6636 18.1092 17.7395 18.0777C17.8154 18.0463 17.8843 18.0003 17.9424 17.9422C18.0004 17.8841 18.0465 17.8152 18.0779 17.7393C18.1094 17.6634 18.1255 17.5821 18.1255 17.5C18.1255 17.4179 18.1094 17.3366 18.0779 17.2607C18.0465 17.1848 18.0004 17.1159 17.9424 17.0578ZM3.12518 8.75C3.12518 7.63748 3.45508 6.54994 4.07316 5.62491C4.69124 4.69989 5.56975 3.97892 6.59758 3.55317C7.62542 3.12743 8.75642 3.01604 9.84756 3.23308C10.9387 3.45012 11.941 3.98585 12.7277 4.77252C13.5143 5.55919 14.0501 6.56147 14.2671 7.65261C14.4841 8.74376 14.3727 9.87475 13.947 10.9026C13.5213 11.9304 12.8003 12.8089 11.8753 13.427C10.9502 14.0451 9.8627 14.375 8.75018 14.375C7.25884 14.3733 5.82906 13.7802 4.77453 12.7256C3.72 11.6711 3.12683 10.2413 3.12518 8.75Z" fill="currentColor" />
							</svg>

						<?php } ?>

						<?php
						if ( $enable_search_btn_label && ! in_array( $search_form_preset, array( 'smart-search-form-preset-one', 'smart-search-form-preset-two' ) ) ) {
							echo esc_html( $search_btn_label_text );
						}
						?>

					</button>
				</form>
				<div class="sp-smart-post-search-results">
					<div class="sp-smart-post-search-list <?php echo esc_attr( $search_result_display_type ); ?>"></div>
					<div class="sp-smart-post-search-load-more" data-view_more_text='<?php echo esc_attr( $view_more_text ); ?>'>

					</div>
				</div>
			</div>
		</div>
		<?php
		return ob_get_clean();
	}
}
