<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
function sifency_addons_render_section_tab_content( string $active_tab ): void {
	$args = array(
		'post_type'      => 'section_templates',
		'posts_per_page' => -1,
		'orderby'        => 'date',
		'order'          => 'DESC',
	);

	if ( 'trash' === $active_tab ) {
		$args['post_status'] = 'trash';
		$args['perm']        = 'readable';
	} else {
		$args['post_status'] = array( '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();
						$dup_nonce   = wp_create_nonce( 'sifency_duplicate_post_' . $template_id );

						// URLs
						$edit_url    = admin_url( 'post.php?post=' . $template_id . '&action=elementor' );
						$preview_url = add_query_arg( array( '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 );

						// Shortcode Generation
						$shortcode_string = '[section_template id="' . $template_id . '"]';
						?>
						<tr class="sifency-post-lists-wrap" data-template-id="<?php echo esc_attr( $template_id ); ?>">
							<!-- TITLE COLUMN -->
							<td>
								<?php
								sifency_addons_render_template_inline_edit(
									$template_id,
									array( 'editable' => ( 'trash' !== $active_tab ) )
								);
								?>
								<div class="d-flex">
									<?php if ( 'trash' !== $active_tab ) : ?>
										<a href="<?php echo esc_url( $edit_url ); ?>" target="_blank" class="sifency-edit-elementor-link sifency-admin-btn text-underline-animation">
											<span><?php esc_html_e( 'Edit with Elementor', 'sifency-addons' ); ?></span>
										</a> 
										<div class="sifency-separator">|</div>
										<?php $duplicate_link = admin_url( 'admin.php?action=sifency_duplicate_post&post=' . $template_id . '&sifency_duplicate_nonce=' . $dup_nonce ); ?>
										<a href="<?php echo esc_url( $preview_url ); ?>" target="_blank" class="sifency-preview-link sifency-admin-btn text-underline-animation">
											<span><?php esc_html_e( 'Preview', 'sifency-addons' ); ?></span>
										</a>
										<div class="sifency-separator">|</div>
										<a href="<?php echo esc_url( $duplicate_link ); ?>" class="sifency-admin-btn text-underline-animation"><span>Duplicate</span></a>
										<?php
										$post_id    = get_the_ID();
										$exporter   = new \Sifency\Admin\Elementor_Native_Exporter();
										$export_url = $exporter->get_export_url( $post_id );

										if ( $export_url ) :
											?>
											<div class="sifency-separator">|</div>

											<a href="<?php echo esc_url( $export_url ); ?>"
											class="sifency-admin-btn sifency-export-link text-underline-animation">
												<span><?php esc_html_e( 'Export JSON', 'sifency-addons' ); ?></span>
											</a>
										<?php endif; ?>
									<?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"></span>
									</button>
								</div>
							</td>

							<!-- PUBLISH STATUS -->
							<td class="sifency-post-publish-status-col">
								<?php sifency_addons_render_template_status_badge( $template_id ); ?>
							</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 reusable templates found.', 'sifency-addons' ); ?></td></tr>
				<?php endif; ?>
			</tbody>
		</table>

		<?php if ( 'trash' !== $active_tab ) : ?>
			<div class="sifency-templates-header">
				<button type="button" class="page-title-action sifency-open-new-section-modal">
					<?php esc_html_e( 'Add New Section', 'sifency-addons' ); ?>
				</button>
				<button type="button" class="page-title-action sifency-floating-add-btn 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
}
