<?php
function sifency_addons_render_section_tab_content( string $active_tab ): void {
	$args = [
		'post_type'      => 'section_templates',
		'posts_per_page' => -1,
		'orderby'        => 'date',
		'order'          => 'DESC',
	];

	// Handle Trash tab properly
	if ( 'trash' === $active_tab ) {
		$args['post_status'] = 'trash';
		$args['perm']        = 'readable';
	} else {
		$args['post_status'] = [ 'publish', 'draft', 'pending' ];
	}

	$templates_query = new WP_Query( $args );
	?>
	<div class="sifency-templates-list-container">

		<table class="wp-list-table widefat fixed striped sifency-templates-table">
			<thead>
				<tr>
					<th><?php esc_html_e( 'Title', 'sifency-addons' ); ?></th>
					<th class="sifency-post-shortcode-col"><?php esc_html_e( 'Shortcode', 'sifency-addons' ); ?></th>
					<th class="sifency-post-publish-status-col"><?php esc_html_e( 'Status', 'sifency-addons' ); ?></th>
					<th class="sifency-post-action-col"><?php esc_html_e( 'Actions', 'sifency-addons' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<?php if ( $templates_query->have_posts() ) : ?>
					<?php while ( $templates_query->have_posts() ) : $templates_query->the_post();
						$template_id = get_the_ID();
						
						// URLs
						$edit_url     = admin_url( 'post.php?post=' . $template_id . '&action=elementor' );
						$preview_url  = add_query_arg( [ 'preview' => 'true' ], get_permalink( $template_id ) );
						$restore_url  = wp_nonce_url( admin_url( 'post.php?action=untrash&post=' . $template_id ), 'untrash-post_' . $template_id );
						$delete_perm  = wp_nonce_url( admin_url( 'post.php?action=delete&post=' . $template_id ), 'delete-post_' . $template_id );

						$post_status = get_post_status( $template_id );
                        $post_status_label = ucfirst( $post_status );
						
						// Shortcode Generation
						$shortcode_string = '[section_template id="' . $template_id . '"]';
						?>
						<tr>
							<!-- TITLE COLUMN -->
							<td>
								<strong class="table-list-item-title"><?php the_title(); ?></strong><br>
								<div class="d-flex">
									<?php if ( 'trash' !== $active_tab ) : ?>
										<a href="<?php echo esc_url( $edit_url ); ?>" target="_blank" class="sifency-edit-elementor-link">
											<?php esc_html_e( 'Edit with Elementor', 'sifency-addons' ); ?>
										</a> 
										<div class="sifency-separator">|</div>
										<a href="<?php echo esc_url( $preview_url ); ?>" target="_blank" class="sifency-preview-link">
											<?php esc_html_e( 'Preview', 'sifency-addons' ); ?>
										</a>
									<?php endif; ?>
								</div>
							</td>

							<!-- SHORTCODE COPIER COLUMN -->
							<td class="sifency-post-shortcode-col">
								<div class="sifency-shortcode-wrapper">
									<input type="text" 
										   readonly 
										   value="<?php echo esc_attr( $shortcode_string ); ?>" 
										   class="sifency-shortcode-input regular-text" 
										   onclick="this.select();"
										   >
									
									<button type="button" class="button sifency-copy-btn" data-clipboard-text="<?php echo esc_attr( $shortcode_string ); ?>" title="<?php esc_attr_e('Copy Shortcode', 'sifency-addons'); ?>">
										<span class="dashicons dashicons-admin-page" style="line-height: 28px;"></span>
									</button>
								</div>
							</td>

							<!-- PUBLISH STATUS -->
							<td class="sifency-post-publish-status-col">
                                <?php 
                                $note_text = esc_html__( 'This template is not published.', 'sifency-addons' );
                                $span_attributes = '';
                                if ( in_array( $post_status, ['draft', 'pending'], true ) ) {
                                    $span_attributes = ' data-tooltip="' . esc_attr( $note_text ) . '"';
                                }
                                echo '<span class="sifency-status-label"' . wp_kses_post( $span_attributes ) . '>' . esc_html( $post_status_label ) . '</span>';
                                ?>
							</td>

							<!-- ACTIONS -->
							<td class="sifency-post-action-col">
								<?php if ( 'trash' !== $active_tab ) : ?>
									<div class="sifency-action-buttons">
										<a href="<?php echo esc_url( get_delete_post_link( $template_id ) ); ?>"
										class="sifency-btn delete">
											<span class="dashicons dashicons-trash"></span>
											<?php esc_html_e( 'Delete', 'sifency-addons' ); ?>
										</a>
									</div>
								<?php else : ?>
									<div class="sifency-action-buttons">
										<a href="<?php echo esc_url( $restore_url ); ?>" class="sifency-btn restore sifency-admin-btn">
											<span class="dashicons dashicons-undo"></span>
											<?php esc_html_e( 'Restore', 'sifency-addons' ); ?>
										</a>
										<a href="<?php echo esc_url( $delete_perm ); ?>" class="sifency-btn delete-perm sifency-admin-btn">
											<span class="dashicons dashicons-no"></span>
											<?php esc_html_e( 'Delete Permanently', 'sifency-addons' ); ?>
										</a>
									</div>
								<?php endif; ?>
							</td>

						</tr>
					<?php endwhile; wp_reset_postdata(); ?>
				<?php else : ?>
					<tr><td colspan="4"><?php esc_html_e( 'No section templates found.', 'sifency-addons' ); ?></td></tr>
				<?php endif; ?>
			</tbody>
		</table>

		<?php if ( 'trash' !== $active_tab ) : ?>
			<div class="sifency-templates-header">
				<button class="page-title-action" id="sifency-open-new-section-modal">
					<?php esc_html_e( 'Add New Section', 'sifency-addons' ); ?>
				</button>
				<button class="page-title-action sifency-floating-add-btn" id="sifency-open-new-section-modal">
					<div class="sifency-add-label"><?php esc_html_e( 'Add New Section', 'sifency-addons' ); ?></div>
					<span class="sifency-add-icon">+</span>
				</button>
			</div>
		<?php endif; ?>

	</div>
	<?php
}