<?php
/**
 * Template 2 - Simple Card FAQ.
 *
 * @package     wp-faq-block
 * @since 1.0.0
 */

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

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
?>

<div class="faq-plugin-wrapper">
<div class="wpfaqblock-t2 <?php echo esc_attr($args['faqtheme']); ?> <?php echo esc_attr($args['class']); ?>">

	<?php if ($args['title']) { ?>
		<h2 class="wpfaqblock-t2-title"><?php echo esc_html($args['title']); ?></h2>
	<?php } ?>

	<?php if ($args['search']) { ?>
		<form action="?" method="get" class="wpfaqblock-t2-search">
			<?php wp_nonce_field( 'wpfaqblock_search' ); ?>
			<input type="text" class="wpfaqblock-t2-search-input"
				placeholder="<?php echo esc_attr($args['placeholder']); ?>" name="wpfaqblocksearch" value="<?php
				   if (isset($_GET['wpfaqblocksearch']) && isset($_GET['_wpnonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'])), 'wpfaqblock_search')) {
					   echo esc_attr(trim(sanitize_text_field(wp_unslash($_GET['wpfaqblocksearch']))));
				   }
				   ?>">
			<button type="submit" class="wpfaqblock-t2-search-btn">Search</button>
		</form>
	<?php } ?>

	<?php if ($query->have_posts()) { ?>
		<div class="wpfaqblock-t2-list">
			<?php while ($query->have_posts()) {
				$query->the_post();
				$is_open = $this->is_item_open($args, $query);
				?>
				<div id="<?php echo esc_attr( apply_filters( 'wpfaqblock_faq_item_id', '', get_the_ID() ) ); ?>" class="wpfaqblock-t2-item <?php echo $is_open ? 'wpfaqblock-t2-item-open' : ''; ?>">
					<div class="wpfaqblock-t2-question <?php echo $is_open ? 'wpfaqblock-t2-question-open' : ''; ?>">
					<span><?php echo esc_html(get_the_title()); ?></span>
					<?php
					$custom_icon_html = $this->render_custom_icons($args);
					if ($custom_icon_html) {
						echo wp_kses_post($custom_icon_html);
					} else {
					$icon_style = isset($args['faqicon']) ? $args['faqicon'] : 'plus';
					switch ($icon_style) {
						case 'arrow':
							echo '<svg class="wpfaqblock-svg-icon" width="18" height="18" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 18l6-6-6-6"/></svg>';
							break;
						case 'chevron':
							echo '<svg class="wpfaqblock-svg-icon" width="18" height="18" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"/></svg>';
							break;
						case 'modern':
							echo '<svg class="wpfaqblock-svg-icon" width="18" height="18" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></svg>';
							break;
						case 'circle':
							echo '<div class="wpfaqblock-circle-icon-wrapper">';
							echo '<svg class="wpfaqblock-circle-minus" xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="8" y1="12" x2="16" y2="12"/></svg>';
							echo '<svg class="wpfaqblock-circle-plus" xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="16"/><line x1="8" y1="12" x2="16" y2="12"/></svg>';
							echo '</div>';
							break;
						case 'plus':
						default:
							echo '<svg class="wpfaqblock-svg-icon wpfaqblock-plus-icon" width="18" height="18" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line class="wpfaqblock-horiz" x1="5" y1="12" x2="19" y2="12"/><line class="wpfaqblock-vert" x1="12" y1="5" x2="12" y2="19"/></svg>';
							break;
					}
					}
					?>
				</div>
					<div class="wpfaqblock-t2-answer" <?php echo !$is_open ? 'style="display:none;"' : ''; ?>>
						<?php 
						$content = get_the_content();
						echo wp_kses_post( apply_filters( 'the_content', $content ) );
						echo apply_filters( 'wpfaqblock_item_content_end', '', get_post(), $args );
						?>
					</div>
				</div>
			<?php } ?>
			<?php wp_reset_postdata(); ?>
		</div>
	<?php } else { ?>
		<p class="wpfaqblock-t2-noresults"><?php echo esc_html__('No FAQs found.', 'wpfaqblock'); ?></p>
	<?php } ?>
</div>
</div>

