<?php
/**
 * Template 3 -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-t4 <?php echo esc_attr($args['faqtheme']); ?> <?php echo esc_attr($args['class']); ?>">

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

		<?php if ($args['search']) { ?>
			<form action="?" method="get" class="wpfaqblock-t4-search">
			<?php wp_nonce_field( 'wpfaqblock_search' ); ?>
				<div class="wpfaqblock-t4-search-inner">
					<input type="text" class="wpfaqblock-t4-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-t4-search-btn"><?php echo esc_html__('Search', 'wpfaqblock'); ?></button>
				</div>
			</form>
		<?php } ?>

		<?php if ($query->have_posts()) { ?>
			<div class="wpfaqblock-t4-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-t4-item <?php echo $is_open ? 'wpfaqblock-t4-item-open' : ''; ?>">
						<div class="wpfaqblock-t4-question <?php echo $is_open ? 'wpfaqblock-t4-question-open' : ''; ?>">
							<span><?php echo esc_html(get_the_title()); ?></span>
							<div class="wpfaqblock-t4-icon">
								<?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'] : 'arrow';
								switch ($icon_style) {
									case 'arrow':
										// Minus state (open: down chevron)
										echo '<svg class="wpfaqblock-t4-icon-minus" xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"/></svg>';
										// Plus state (closed: right chevron)
										echo '<svg class="wpfaqblock-t4-icon-plus" xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 18 15 12 9 6"/></svg>';
										break;
									case 'modern':
										// Minus state (open: down arrow)
										echo '<svg class="wpfaqblock-t4-icon-minus" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><line x1="12" y1="5" x2="12" y2="19"/><polyline points="19 12 12 19 5 12"/></svg>';
										// Plus state (closed: right arrow)
										echo '<svg class="wpfaqblock-t4-icon-plus" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" 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 'plus':
										// Minus state (open: minus)
										echo '<svg class="wpfaqblock-t4-icon-minus" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round"><line x1="5" y1="12" x2="19" y2="12"/></svg>';
										// Plus state (closed: plus)
										echo '<svg class="wpfaqblock-t4-icon-plus" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>';
										break;
									case 'circle':
										// Circle Minus (shown when open)
										echo '<svg class="wpfaqblock-t4-icon-minus" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="8" y1="12" x2="16" y2="12"/></svg>';
										// Circle Plus (shown when closed)
										echo '<svg class="wpfaqblock-t4-icon-plus" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" 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>';
										break;
									case 'chevron':
									default:
										// Minus state (open: up chevron)
										echo '<svg class="wpfaqblock-t4-icon-minus" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="18 15 12 9 6 15"/></svg>';
										// Plus state (closed: down chevron)
										echo '<svg class="wpfaqblock-t4-icon-plus" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"/></svg>';
										break;
								}
								}
								?>
							</div>
						</div>
						<div class="wpfaqblock-t4-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-t4-noresults"><?php echo esc_html__('No FAQs found.', 'wpfaqblock'); ?></p>
		<?php } ?>
	</div>
</div>



