<?php
/**
 * Render a forum item with its sub-forums recursively.
 *
 * @param int   $forum_id     The forum ID.
 * @param int   $depth        The nesting depth level.
 * @param array $parent_forums Reference to the parent forums array.
 * @param bool  $is_active    Whether this forum should be active.
 *
 * @return void
 */
function forumax_render_forum_item( $forum_id, $depth = 0, &$parent_forums = [], $is_active = false ) {
	$current_post   = $forum_id;
	$active_class   = $is_active ? 'is-active' : '';
	$depth_class    = $depth > 0 ? 'is-subforum depth-' . $depth : '';
	$count_children = get_children(
		[
			'post_parent' => $current_post,
			'post_type'   => bbp_get_topic_post_type(),
			'orderby'     => 'menu_order',
			'order'       => 'asc',
			'post_status' => [ 'any', 'spam' ],
		]
	);

	// Get sub-forums for this forum.
	$sub_forums = new WP_Query(
		[
			'post_type'      => bbp_get_forum_post_type(),
			'posts_per_page' => -1,
			'post_parent'    => $forum_id,
			'orderby'        => 'menu_order',
			'order'          => 'ASC',
			'post_status'    => 'publish',
		]
	);

	$has_subforums = $sub_forums->have_posts();
	?>
	<li class="easydocs-navitem <?php echo esc_attr( $active_class . ' ' . $depth_class ); ?>" data-rel="tab-<?php echo esc_attr( $current_post ); ?>" data-id="<?php echo esc_attr( $current_post ); ?>" data-depth="<?php echo esc_attr( $depth ); ?>">
		<?php if ( $has_subforums ) : ?>
			<button type="button" class="subforum-toggle" aria-expanded="true" title="<?php esc_attr_e( 'Toggle sub-forums', 'forumax' ); ?>">
				<span class="dashicons dashicons-arrow-down-alt2"></span>
			</button>
		<?php endif; ?>
		<div class="title">
			<div class="featured-image">
				<?php
				if ( get_the_post_thumbnail( $current_post ) ) :
					echo get_the_post_thumbnail( $current_post, 'forumax_32x32', array( 'alt' => get_the_title( $current_post ) ) );
				else :
					?>
					<span class="dashicons dashicons-buddicons-forums"></span>
				<?php endif; ?>
			</div>

			<span class="easydocs-forums-title"><?php echo esc_html( get_the_title( $current_post ) ); ?></span>
		</div>
		<div class="total-page">
			<span>
				<?php echo count( $count_children ) > 0 ? count( $count_children ) : ''; ?>
			</span>
		</div>
		<div class="link-wrapper link">
			<?php
			if ( current_user_can( 'editor' ) || current_user_can( 'administrator' ) ) :
				?>
				<a href="<?php echo esc_url( get_edit_post_link( $current_post ) ); ?>" class="link edit" target="_blank" title="<?php esc_attr_e( 'Edit this forum.', 'forumax' ); ?>">
					<span class="dashicons dashicons-edit"></span>
				</a>
				<?php
			endif;
			?>

			<a href="<?php echo esc_url( get_permalink( $current_post ) ); ?>" class="link external-link" target="_blank" data-id="tab-<?php echo esc_attr( $current_post ); ?>" title="<?php esc_attr_e( 'View this forum in new tab', 'forumax' ); ?>">
				<span class="dashicons dashicons-external"></span>
			</a>
			<?php
			if ( current_user_can( 'editor' ) || current_user_can( 'administrator' ) ) :
				?>
				<a href="javascript:void(0);" bbp_forum_id="<?php echo esc_attr( $current_post ); ?>" class="link forum-delete" title="<?php esc_attr_e( 'Move this forum to the Trash', 'forumax' ); ?>">
					<span class="dashicons dashicons-trash"></span>
				</a>
				<a href="javascript:void(0);" bbp_parent_forum_id="<?php echo esc_attr( $current_post ); ?>" class="link add-subforum" title="<?php esc_attr_e( 'Add Sub-Forum', 'forumax' ); ?>">
					<span class="dashicons dashicons-plus-alt2"></span>
				</a>
			<?php endif; ?>
		</div>
	</li>
	<?php
	// Render sub-forums if any.
	if ( $has_subforums ) :
		?>
		<li class="easydocs-subforum-container" data-parent="<?php echo esc_attr( $forum_id ); ?>">
			<ul class="easydocs-subforums">
				<?php
				while ( $sub_forums->have_posts() ) :
					$sub_forums->the_post();
					$sub_forum_id    = get_the_ID();
					$parent_forums[] = $sub_forum_id;
					forumax_render_forum_item( $sub_forum_id, $depth + 1, $parent_forums, false );
				endwhile;
				wp_reset_postdata();
				?>
			</ul>
		</li>
		<?php
	endif;
}
?>
<div class="tab-menu <?php echo $count > 12 ? '' : 'short'; ?>">
	<div class="tab-menu-left-layer"></div>
	<ul class="easydocs-navbar">
		<?php
		$i = 0;
		while ( $forum_query->have_posts() ) :
			$forum_query->the_post();
			$i++;
			$current_post    = get_the_ID();
			$parent_forums[] = $current_post;
			$is_active       = ( 1 === $i );
			forumax_render_forum_item( $current_post, 0, $parent_forums, $is_active );
		endwhile;
		wp_reset_postdata();
		?>
	</ul>
</div>