<?php
/**
 * Addons List Template
 *
 * @package Extra_Product_Options_For_WooCommerce
 * @version 1.0.0
 */

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Query addons from custom tables if enabled, otherwise fallback to wp_posts.
if ( EPOFW_Custom_Tables::is_enabled() ) {
	// NEW: Query from custom tables with JOIN to wp_posts to verify post exists.
	global $wpdb;
	// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Admin list page query, custom tables with no WordPress API equivalent.
	$epofw_addons = $wpdb->get_results(
		$wpdb->prepare(
			"SELECT
				a.id,
				a.post_id,
				a.addon_name,
				a.addon_status,
				a.addon_position,
				a.created_at,
				a.updated_at,
				(SELECT COUNT(DISTINCT f.field_type)
				 FROM {$wpdb->prefix}epofw_fields f
				 JOIN {$wpdb->prefix}epofw_rows r ON f.row_id = r.id
				 JOIN {$wpdb->prefix}epofw_row_groups rg ON r.row_group_id = rg.id
				 JOIN {$wpdb->prefix}epofw_sections s ON rg.section_id = s.id
				 WHERE s.addon_id = a.id) as field_types_count
			FROM {$wpdb->prefix}epofw_addons a
			INNER JOIN {$wpdb->posts} p ON a.post_id = p.ID AND p.post_type = %s
			ORDER BY a.id DESC",
			EPOFW_DFT_POST_TYPE
		)
	);

	$epofw_active_addons   = 0;
	$epofw_inactive_addons = 0;
	foreach ( $epofw_addons as $epofw_addon ) {
		if ( 'on' === $epofw_addon->addon_status ) {
			++$epofw_active_addons;
		} else {
			++$epofw_inactive_addons;
		}
	}
	$epofw_total_addons = count( $epofw_addons );
} else {
	// FALLBACK: Query from wp_posts (legacy).
	$epofw_args              = array(
		'post_status'    => 'any',
		'posts_per_page' => -1,
		'offset'         => 0,
		'orderby'        => 'ID',
		'order'          => 'ASC',
	);
	$epofw_args['post_type'] = EPOFW_DFT_POST_TYPE;
	$epofw_query             = new WP_Query( $epofw_args );
	$epofw_addons            = $epofw_query->query( $epofw_args );

	$epofw_active_addons   = 0;
	$epofw_inactive_addons = 0;
	$epofw_total_addons    = 0;
	foreach ( $epofw_addons as $epofw_addon ) {
		$epofw_active_addons   += 'publish' === $epofw_addon->post_status ? 1 : 0;
		$epofw_inactive_addons += 'draft' === $epofw_addon->post_status ? 1 : 0;

		++$epofw_total_addons;
	}
}

// Flag used to control the new empty‑state UI.
$epofw_has_addons = $epofw_total_addons > 0;

$epofw_view_preference = get_option( 'epofw_view_preference', 'grid' );
?>

<div class="epofw-product-addons-container">
	<!-- Quick Stats with Action Buttons -->
	<div class="epofw-stats-grid">
		<div class="epofw-stat-item">
			<div class="epofw-stat-icon epofw-primary">
				<i class="dashicons dashicons-admin-plugins"></i>
			</div>
			<div class="epofw-stat-info">
				<div class="epofw-stat-number"><?php echo intval( $epofw_total_addons ); ?></div>
				<div class="epofw-stat-label"><?php esc_html_e( 'Total Addons', 'extra-product-options-for-woocommerce' ); ?></div>
			</div>
		</div>
		<div class="epofw-stat-item">
			<div class="epofw-stat-icon epofw-stat-icon-active">
				<i class="dashicons dashicons-yes-alt"></i>
			</div>
			<div class="epofw-stat-info">
				<div class="epofw-stat-number"><?php echo intval( $epofw_active_addons ); ?></div>
				<div class="epofw-stat-label"><?php esc_html_e( 'Active', 'extra-product-options-for-woocommerce' ); ?></div>
			</div>
		</div>
		<div class="epofw-stat-item">
			<div class="epofw-stat-icon epofw-stat-icon-inactive">
				<i class="dashicons dashicons-no"></i>
			</div>
			<div class="epofw-stat-info">
				<div class="epofw-stat-number"><?php echo intval( $epofw_inactive_addons ); ?></div>
				<div class="epofw-stat-label"><?php esc_html_e( 'Inactive', 'extra-product-options-for-woocommerce' ); ?></div>
			</div>
		</div>

		<!-- Action Buttons Box -->
		<div class="epofw-action-buttons-box">
			<div class="epofw-action-buttons-title"><?php esc_html_e( 'Quick Actions', 'extra-product-options-for-woocommerce' ); ?></div>
			<div class="epofw-action-buttons">
				<?php
				// Always allow importing demos (user may have deleted them and wants to re-import).
				$epofw_import_btn_class = 'epofw-action-btn epofw-import-btn';
				$epofw_import_btn_title = esc_attr__( 'Import Demo Addons', 'extra-product-options-for-woocommerce' );
				?>
				<button type="button" class="<?php echo esc_attr( $epofw_import_btn_class ); ?>" title="<?php echo esc_attr( $epofw_import_btn_title ); ?>">
					<i class="dashicons dashicons-download"></i>
				</button>
				<?php
				// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- GET parameters used for read-only display purposes in admin page, not form processing.
				$epofw_current_page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
				// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- GET parameters used for read-only display purposes in admin page, not form processing.
				$epofw_current_tab       = isset( $_GET['tab'] ) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : '';
				$epofw_migration_done    = get_option( 'epofw_row_association_migration_done', false );
				$epofw_bg_migration_comp = get_option( 'epofw_custom_table_migration_complete', false );
				$epofw_bg_migration_run  = get_transient( 'epofw_custom_table_migration_running' );

				// Show migration progress if BG migration is running and has items to migrate.
				$epofw_total_count    = get_option( 'epofw_custom_table_migration_total', 0 );
				$epofw_migrated_count = get_option( 'epofw_custom_table_migration_count', 0 );
				if ( $epofw_bg_migration_run && ! $epofw_bg_migration_comp && $epofw_total_count > 0 && $epofw_migrated_count < $epofw_total_count ) {
					?>
					<div class="epofw-migration-progress" style="display: inline-flex; align-items: center; gap: 8px; padding: 5px 10px; background: #fff3cd; border-radius: 4px; margin-right: 10px;">
						<span class="spinner is-active" style="float: none; margin: 0;"></span>
						<span><?php esc_html_e( 'Migrating...', 'extra-product-options-for-woocommerce' ); ?>
							<?php echo esc_html( $epofw_migrated_count ); ?>/<?php echo esc_html( $epofw_total_count ); ?>
						</span>
					</div>
					<?php
				}

				// Show migrate button only if migration not done (manual OR background).
				if ( ( 'epofw-main' === $epofw_current_page || 'field-section' === $epofw_current_tab ) && ! $epofw_migration_done && ! $epofw_bg_migration_comp ) {
					?>
				<button type="button" id="epofw-migrate-to-sections-toolbar" class="epofw-action-btn epofw-migrate-btn epofw-migrate-to-sections-btn" title="<?php esc_attr_e( 'Migrate to Sections', 'extra-product-options-for-woocommerce' ); ?>">
					<i class="dashicons dashicons-migrate"></i>
				</button>
					<?php
				}
				?>
				<a href="
				<?php
				echo esc_url(
					add_query_arg(
						array(
							'page'   => 'epofw-main',
							'tab'    => 'field-section',
							'action' => 'add',
						),
						admin_url( 'admin.php' )
					)
				);
				?>
				" class="epofw-action-btn epofw-create-btn" title="<?php esc_attr_e( 'Add New Addon', 'extra-product-options-for-woocommerce' ); ?>">
					<i class="dashicons dashicons-plus"></i>
				</a>
				<a href="https://wpactpro.com" target="_blank" class="epofw-action-btn epofw-documentation-btn" title="<?php esc_attr_e( 'Documentation', 'extra-product-options-for-woocommerce' ); ?>">
					<i class="dashicons dashicons-book"></i>
				</a>
				<a href="https://wpactpro.com/contact/" target="_blank" class="epofw-action-btn epofw-support-btn" title="<?php esc_attr_e( 'Support', 'extra-product-options-for-woocommerce' ); ?>">
					<i class="dashicons dashicons-sos"></i>
				</a>
			</div>
		</div>
	</div>

	<!-- Addons Section -->
	<div class="epofw-addons-section">
		<div class="epofw-section-header">
			<h2 class="epofw-section-title"><?php esc_html_e( 'Your Addons', 'extra-product-options-for-woocommerce' ); ?></h2>
			<div class="epofw-section-actions">
				<!-- Search Box -->
				<div class="epofw-search-box">
					<input type="text" 
						   id="epofw-addon-search" 
						   class="epofw-search-input" 
						   placeholder="<?php esc_attr_e( 'Search addons...', 'extra-product-options-for-woocommerce' ); ?>"
						   autocomplete="off">
					<button type="button" class="epofw-search-clear" title="<?php esc_attr_e( 'Clear search', 'extra-product-options-for-woocommerce' ); ?>" style="display: none;">
						<i class="dashicons dashicons-no-alt"></i>
					</button>
					<i class="dashicons dashicons-search epofw-search-icon"></i>
				</div>
				<button type="button" class="epofw-view-toggle epofw-grid-view <?php echo esc_attr( 'grid' === $epofw_view_preference ? 'active' : '' ); ?>" data-view="grid" title="<?php esc_attr_e( 'Grid View', 'extra-product-options-for-woocommerce' ); ?>">
					<i class="dashicons dashicons-grid-view"></i>
				</button>
				<button type="button" class="epofw-view-toggle epofw-list-view <?php echo esc_attr( 'list' === $epofw_view_preference ? 'active' : '' ); ?>" data-view="list" title="<?php esc_attr_e( 'List View', 'extra-product-options-for-woocommerce' ); ?>">
					<i class="dashicons dashicons-list-view"></i>
				</button>
			</div>
		</div>

		<div class="epofw-addons-container epofw-addons-<?php echo esc_attr( $epofw_view_preference ); ?>" id="addons-<?php echo esc_attr( $epofw_view_preference ); ?>">
			<?php
			if ( $epofw_has_addons ) {
				foreach ( $epofw_addons as $epofw_addon ) {
					// Handle both custom tables and legacy wp_posts structures.
					if ( EPOFW_Custom_Tables::is_enabled() ) {
						// NEW: Custom tables structure.
						$epofw_addon_id      = $epofw_addon->post_id;
						$epofw_addon_name    = ! empty( $epofw_addon->addon_name ) ? $epofw_addon->addon_name : 'Addon ' . $epofw_addon->post_id;
						$epofw_addon_status  = $epofw_addon->addon_status;
						$epofw_addon_updated = $epofw_addon->updated_at;

						// Load full addon data from custom tables to get field types and rules.
						$epofw_addon_data             = EPOFW_Custom_Tables::load_addon( $epofw_addon->post_id );
						$epofw_additional_rule_detail = ! empty( $epofw_addon_data['additional_rule_data'] ) ? $epofw_addon_data['additional_rule_data'] : array();
					} else {
						// FALLBACK: Legacy wp_posts structure.
						$epofw_addon_id      = $epofw_addon->ID;
						$epofw_addon_name    = get_the_title( $epofw_addon->ID );
						$epofw_addon_status  = 'publish' === $epofw_addon->post_status ? 'on' : 'off';
						$epofw_addon_updated = $epofw_addon->post_modified;

						// Use abstraction layer instead of direct post_meta access.
						$epofw_addon_data             = epofw_get_data_from_db( $epofw_addon->ID );
						$epofw_additional_rule_detail = epofw_check_array_key_exists( 'additional_rule_data', $epofw_addon_data );
					}
					$epofw_product_used        = 0;
					$epofw_product_used_text   = '';
					$epofw_category_used       = 0;
					$epofw_category_used_text  = '';
					$epofw_tag_used            = 0;
					$epofw_tag_used_text       = '';
					$epofw_variation_used      = 0;
					$epofw_variation_used_text = '';
					$epofw_user_role_used      = 0;
					$epofw_addon_used_parts    = array();

					if ( ! empty( $epofw_additional_rule_detail ) ) {
						// Normalize rule data to handle both old flat format and new nested format.
						$epofw_normalized_rules = epofw_normalize_additional_rules_data( $epofw_additional_rule_detail );

						// Extract all conditions from all groups for display purposes.
						$epofw_all_conditions = array();
						if ( ! empty( $epofw_normalized_rules['groups'] ) && is_array( $epofw_normalized_rules['groups'] ) ) {
							// New nested format: extract conditions from all groups.
							foreach ( $epofw_normalized_rules['groups'] as $epofw_group ) {
								if ( ! empty( $epofw_group['conditions'] ) && is_array( $epofw_group['conditions'] ) ) {
									foreach ( $epofw_group['conditions'] as $epofw_condition ) {
										if ( ! empty( $epofw_condition['condition'] ) && ! empty( $epofw_condition['operator'] ) && ! empty( $epofw_condition['value'] ) ) {
											$epofw_all_conditions[] = $epofw_condition;
										}
									}
								}
							}
						} else {
							// Old flat format: use as-is.
							foreach ( $epofw_additional_rule_detail as $epofw_rule ) {
								if (
									! empty( $epofw_rule ) &&
									is_array( $epofw_rule ) &&
									! empty( $epofw_rule['condition'] ) &&
									! empty( $epofw_rule['operator'] ) &&
									! empty( $epofw_rule['value'] )
								) {
									$epofw_all_conditions[] = $epofw_rule;
								}
							}
						}

						// Process all conditions for display.
						foreach ( $epofw_all_conditions as $epofw_rule ) {
							if (
							! empty( $epofw_rule ) &&
							! empty( $epofw_rule['condition'] ) &&
							! empty( $epofw_rule['operator'] ) &&
							! empty( $epofw_rule['value'] )
							) {
								if ( 'product' === $epofw_rule['condition'] ) {
									$epofw_count         = count( $epofw_rule['value'] );
									$epofw_product_links = array();
									$epofw_max_display   = 3;
									$epofw_item_index    = 0;
									foreach ( $epofw_rule['value'] as $epofw_product_id ) {
										$epofw_product_title = get_the_title( $epofw_product_id );
										$epofw_product_url   = get_permalink( $epofw_product_id );
										if ( ! empty( $epofw_product_title ) && ! empty( $epofw_product_url ) ) {
											$epofw_link_html       = '<a href="' . esc_url( $epofw_product_url ) . '" target="_blank" class="epofw-item-link">' . esc_html( $epofw_product_title ) . '</a>';
											$epofw_product_links[] = array(
												'html'   => $epofw_link_html,
												'hidden' => $epofw_item_index >= $epofw_max_display,
											);
											++$epofw_item_index;
										}
									}
									$epofw_names_html = '';
									if ( ! empty( $epofw_product_links ) ) {
										$epofw_visible_links = array();
										$epofw_hidden_links  = array();
										foreach ( $epofw_product_links as $epofw_link_data ) {
											if ( $epofw_link_data['hidden'] ) {
												$epofw_hidden_links[] = $epofw_link_data['html'];
											} else {
												$epofw_visible_links[] = $epofw_link_data['html'];
											}
										}
										$epofw_names_html = ' (' . implode( ', ', $epofw_visible_links );
										if ( ! empty( $epofw_hidden_links ) ) {
											$epofw_names_html .= '<span class="epofw-hidden-items" style="display:none;">, ' . implode( ', ', $epofw_hidden_links ) . '</span>';
											$epofw_names_html .= ' <a href="#" class="epofw-view-more-link">' . esc_html__( 'View more', 'extra-product-options-for-woocommerce' ) . '</a>';
										}
										$epofw_names_html .= ')';
									}
									$epofw_addon_used_parts[] = $epofw_count . ' ' . esc_html( _n( 'Product', 'Products', $epofw_count, 'extra-product-options-for-woocommerce' ) ) . $epofw_names_html;
								} elseif ( 'category' === $epofw_rule['condition'] ) {
									$epofw_count          = count( $epofw_rule['value'] );
									$epofw_category_links = array();
									$epofw_max_display    = 3;
									$epofw_item_index     = 0;
									foreach ( $epofw_rule['value'] as $epofw_category_id ) {
										$epofw_term = get_term( $epofw_category_id, 'product_cat' );
										if ( ! is_wp_error( $epofw_term ) && ! empty( $epofw_term ) ) {
											$epofw_category_url     = get_term_link( $epofw_term );
											$epofw_link_html        = '<a href="' . esc_url( $epofw_category_url ) . '" target="_blank" class="epofw-item-link">' . esc_html( $epofw_term->name ) . '</a>';
											$epofw_category_links[] = array(
												'html'   => $epofw_link_html,
												'hidden' => $epofw_item_index >= $epofw_max_display,
											);
											++$epofw_item_index;
										}
									}
									$epofw_names_html = '';
									if ( ! empty( $epofw_category_links ) ) {
										$epofw_visible_links = array();
										$epofw_hidden_links  = array();
										foreach ( $epofw_category_links as $epofw_link_data ) {
											if ( $epofw_link_data['hidden'] ) {
												$epofw_hidden_links[] = $epofw_link_data['html'];
											} else {
												$epofw_visible_links[] = $epofw_link_data['html'];
											}
										}
										$epofw_names_html = ' (' . implode( ', ', $epofw_visible_links );
										if ( ! empty( $epofw_hidden_links ) ) {
											$epofw_names_html .= '<span class="epofw-hidden-items" style="display:none;">, ' . implode( ', ', $epofw_hidden_links ) . '</span>';
											$epofw_names_html .= ' <a href="#" class="epofw-view-more-link">' . esc_html__( 'View more', 'extra-product-options-for-woocommerce' ) . '</a>';
										}
										$epofw_names_html .= ')';
									}
									$epofw_addon_used_parts[] = $epofw_count . ' ' . esc_html( _n( 'Category', 'Categories', $epofw_count, 'extra-product-options-for-woocommerce' ) ) . $epofw_names_html;
								} elseif ( 'tag' === $epofw_rule['condition'] ) {
									$epofw_count       = count( $epofw_rule['value'] );
									$epofw_tag_links   = array();
									$epofw_max_display = 3;
									$epofw_item_index  = 0;
									foreach ( $epofw_rule['value'] as $epofw_tag_id ) {
										$epofw_term = get_term( $epofw_tag_id, 'product_tag' );
										if ( ! is_wp_error( $epofw_term ) && ! empty( $epofw_term ) ) {
											$epofw_tag_url     = get_term_link( $epofw_term );
											$epofw_link_html   = '<a href="' . esc_url( $epofw_tag_url ) . '" target="_blank" class="epofw-item-link">' . esc_html( $epofw_term->name ) . '</a>';
											$epofw_tag_links[] = array(
												'html'   => $epofw_link_html,
												'hidden' => $epofw_item_index >= $epofw_max_display,
											);
											++$epofw_item_index;
										}
									}
									$epofw_names_html = '';
									if ( ! empty( $epofw_tag_links ) ) {
										$epofw_visible_links = array();
										$epofw_hidden_links  = array();
										foreach ( $epofw_tag_links as $epofw_link_data ) {
											if ( $epofw_link_data['hidden'] ) {
												$epofw_hidden_links[] = $epofw_link_data['html'];
											} else {
												$epofw_visible_links[] = $epofw_link_data['html'];
											}
										}
										$epofw_names_html = ' (' . implode( ', ', $epofw_visible_links );
										if ( ! empty( $epofw_hidden_links ) ) {
											$epofw_names_html .= '<span class="epofw-hidden-items" style="display:none;">, ' . implode( ', ', $epofw_hidden_links ) . '</span>';
											$epofw_names_html .= ' <a href="#" class="epofw-view-more-link">' . esc_html__( 'View more', 'extra-product-options-for-woocommerce' ) . '</a>';
										}
										$epofw_names_html .= ')';
									}
									$epofw_addon_used_parts[] = $epofw_count . ' ' . esc_html( _n( 'Tag', 'Tags', $epofw_count, 'extra-product-options-for-woocommerce' ) ) . $epofw_names_html;
								} elseif ( 'product_variation' === $epofw_rule['condition'] ) {
									$epofw_count           = count( $epofw_rule['value'] );
									$epofw_variation_links = array();
									$epofw_max_display     = 3;
									$epofw_item_index      = 0;
									foreach ( $epofw_rule['value'] as $epofw_variation_id ) {
										$epofw_variation_title = get_the_title( $epofw_variation_id );
										$epofw_variation_url   = get_permalink( $epofw_variation_id );
										if ( ! empty( $epofw_variation_title ) && ! empty( $epofw_variation_url ) ) {
											$epofw_link_html         = '<a href="' . esc_url( $epofw_variation_url ) . '" target="_blank" class="epofw-item-link">' . esc_html( $epofw_variation_title ) . '</a>';
											$epofw_variation_links[] = array(
												'html'   => $epofw_link_html,
												'hidden' => $epofw_item_index >= $epofw_max_display,
											);
											++$epofw_item_index;
										}
									}
									$epofw_names_html = '';
									if ( ! empty( $epofw_variation_links ) ) {
										$epofw_visible_links = array();
										$epofw_hidden_links  = array();
										foreach ( $epofw_variation_links as $epofw_link_data ) {
											if ( $epofw_link_data['hidden'] ) {
												$epofw_hidden_links[] = $epofw_link_data['html'];
											} else {
												$epofw_visible_links[] = $epofw_link_data['html'];
											}
										}
										$epofw_names_html = ' (' . implode( ', ', $epofw_visible_links );
										if ( ! empty( $epofw_hidden_links ) ) {
											$epofw_names_html .= '<span class="epofw-hidden-items" style="display:none;">, ' . implode( ', ', $epofw_hidden_links ) . '</span>';
											$epofw_names_html .= ' <a href="#" class="epofw-view-more-link">' . esc_html__( 'View more', 'extra-product-options-for-woocommerce' ) . '</a>';
										}
										$epofw_names_html .= ')';
									}
									$epofw_addon_used_parts[] = $epofw_count . ' ' . esc_html( _n( 'Variation', 'Variations', $epofw_count, 'extra-product-options-for-woocommerce' ) ) . $epofw_names_html;
								} elseif ( 'user_role' === $epofw_rule['condition'] ) {
									$epofw_count       = count( $epofw_rule['value'] );
									$epofw_role_names  = array();
									$epofw_max_display = 3;
									$epofw_wp_roles    = wp_roles();
									foreach ( array_slice( $epofw_rule['value'], 0, $epofw_max_display ) as $epofw_role_slug ) {
										if ( isset( $epofw_wp_roles->role_names[ $epofw_role_slug ] ) ) {
											$epofw_role_names[] = translate_user_role( $epofw_wp_roles->role_names[ $epofw_role_slug ] );
										} else {
											$epofw_role_names[] = ucfirst( $epofw_role_slug );
										}
									}
									$epofw_hidden_roles = array();
									if ( $epofw_count > $epofw_max_display ) {
										foreach ( array_slice( $epofw_rule['value'], $epofw_max_display ) as $epofw_role_slug ) {
											if ( isset( $epofw_wp_roles->role_names[ $epofw_role_slug ] ) ) {
												$epofw_hidden_roles[] = translate_user_role( $epofw_wp_roles->role_names[ $epofw_role_slug ] );
											} else {
												$epofw_hidden_roles[] = ucfirst( $epofw_role_slug );
											}
										}
									}
									$epofw_names_html = '';
									if ( ! empty( $epofw_role_names ) ) {
										$epofw_names_html = ' (' . esc_html( implode( ', ', $epofw_role_names ) );
										if ( ! empty( $epofw_hidden_roles ) ) {
											$epofw_names_html .= '<span class="epofw-hidden-items" style="display:none;">, ' . esc_html( implode( ', ', $epofw_hidden_roles ) ) . '</span>';
											$epofw_names_html .= ' <a href="#" class="epofw-view-more-link">' . esc_html__( 'View more', 'extra-product-options-for-woocommerce' ) . '</a>';
										}
										$epofw_names_html .= ')';
									}
									$epofw_addon_used_parts[] = $epofw_count . ' ' . esc_html( _n( 'User Role', 'User Roles', $epofw_count, 'extra-product-options-for-woocommerce' ) ) . $epofw_names_html;
								}
							}
						}
					}

					$epofw_addon_used_text = implode( ', ', $epofw_addon_used_parts );

					$epofw_addon_status_class = 'on' === $epofw_addon_status ? 'active' : 'inactive';
					$epofw_addon_status_text  = 'on' === $epofw_addon_status ? esc_html__( 'Active', 'extra-product-options-for-woocommerce' ) : esc_html__( 'Inactive', 'extra-product-options-for-woocommerce' );
					?>
				<!-- Modern Addon Cards -->
				<div class="epofw-addon-card" data-status="<?php echo esc_attr( $epofw_addon_status_class ); ?>" data-id="<?php echo esc_attr( $epofw_addon_id ); ?>" data-name="<?php echo esc_attr( strtolower( $epofw_addon_name ) ); ?>">
					<div class="epofw-card-header">
						<div class="epofw-card-type">
							<span><?php echo esc_html( $epofw_addon_name ); ?></span>
						</div>
						<div class="epofw-card-status <?php echo esc_attr( $epofw_addon_status_class ); ?>">
							<span class="epofw-status-indicator"></span>
							<?php echo esc_html( $epofw_addon_status_text ); ?>
						</div>
					</div>
					<div class="epofw-card-body">
						<div class="epofw-card-description-wrapper epofw-card-description">
							<div class="epofw-card-description-item">
								<strong><?php echo esc_html__( ' Enabled fields types: ', 'extra-product-options-for-woocommerce' ); ?></strong>
							</div>
							<div class="epofw-card-description-item">
								<?php
								$epofw_field_types     = EPOFW_Admin::get_field_type_names( $epofw_addon_data );
								$epofw_ucw_field_types = array_map( 'ucfirst', $epofw_field_types );
								echo esc_html( implode( ', ', $epofw_ucw_field_types ) );
								?>
							</div>
						</div>
						<div class="epofw-card-meta">
							<div class="epofw-meta-item">
								<i class="dashicons dashicons-clock"></i>
								<span>
									<?php
									// translators: %s is the date of the last update.
									printf( esc_html__( 'Updated at %s', 'extra-product-options-for-woocommerce' ), esc_html( $epofw_addon_updated ) );
									?>
								</span>
							</div>
							<div class="epofw-meta-item epofw-used-in-meta">
								<i class="dashicons dashicons-visibility"></i>
								<span>
									<?php
									// translators: %s is the number of products used.
									$epofw_allowed_html = array(
										'a'    => array(
											'href'   => array(),
											'target' => array(),
											'class'  => array(),
										),
										'span' => array(
											'class' => array(),
											'style' => array(),
										),
									);
									printf(
										/* translators: %s: usage details with links */
										esc_html__( 'Used in %s', 'extra-product-options-for-woocommerce' ),
										wp_kses( $epofw_addon_used_text, $epofw_allowed_html )
									);
									?>
								</span>
							</div>
						</div>
					</div>
					<div class="epofw-card-footer">
						<div class="epofw-card-actions">
							<?php
							$epofw_editurl = EPOFW_Admin::dynamic_url( EPOFW_Admin::epofw_current_page(), EPOFW_Admin::epofw_current_tab(), 'edit', $epofw_addon_id );
							$epofw_editurl = wp_nonce_url( $epofw_editurl, 'edit_' . $epofw_addon_id, 'epofw_nonce' );
							?>
							<a href="<?php echo esc_url( $epofw_editurl ); ?>" class="epofw-action-btn epofw-edit-btn" title="<?php esc_attr_e( 'Edit', 'extra-product-options-for-woocommerce' ); ?>">
								<i class="dashicons dashicons-edit"></i>
							</a>
							<button type="button" class="epofw-action-btn epofw-duplicate-btn" title="<?php esc_attr_e( 'Duplicate', 'extra-product-options-for-woocommerce' ); ?>">
								<i class="dashicons dashicons-admin-page"></i>
							</button>
							<button type="button" class="epofw-action-btn epofw-export-btn" data-addon-id="<?php echo esc_attr( $epofw_addon_id ); ?>" title="<?php esc_attr_e( 'Export JSON (Dev Tool)', 'extra-product-options-for-woocommerce' ); ?>">
								<i class="dashicons dashicons-download"></i>
							</button>
							<button type="button" class="epofw-action-btn epofw-delete-btn" title="<?php esc_attr_e( 'Delete', 'extra-product-options-for-woocommerce' ); ?>">
								<i class="dashicons dashicons-trash"></i>
							</button>
						</div>
						<div class="epofw-card-toggle">
							<button type="button" class="epofw-action-btn epofw-toggle-btn" title="
							<?php
							if ( 'on' === $epofw_addon_status ) {
								esc_attr_e( 'Disable', 'extra-product-options-for-woocommerce' );
							} else {
								esc_attr_e( 'Enable', 'extra-product-options-for-woocommerce' );
							}
							?>
							">
								<i class="dashicons dashicons-admin-tools"></i>
							</button>
						</div>
					</div>
				</div>
					<?php
				}
			}
			?>
		</div>

		<!-- Empty State -->
		<div class="epofw-empty-state" id="empty-state" style="<?php echo $epofw_has_addons ? 'display: none;' : ''; ?>">
			<div class="epofw-empty-icon">
				<i class="dashicons dashicons-admin-plugins"></i>
			</div>
			<h3><?php esc_html_e( 'No addons found', 'extra-product-options-for-woocommerce' ); ?></h3>
			<p><?php esc_html_e( 'Create your first product addon to get started with the modern interface', 'extra-product-options-for-woocommerce' ); ?></p>
			<a href="
			<?php
			echo esc_url(
				add_query_arg(
					array(
						'page'   => 'epofw-main',
						'tab'    => 'field-section',
						'action' => 'add',
					),
					admin_url( 'admin.php' )
				)
			);
			?>
			" class="epofw-btn epofw-btn-primary epofw-create-first-btn">
				<i class="dashicons dashicons-plus"></i>
				<?php esc_html_e( 'Create Your First Addon', 'extra-product-options-for-woocommerce' ); ?>
			</a>
		</div>

		<!-- No Search Results -->
		<div class="epofw-no-results" id="epofw-no-results" style="display: none;">
			<div class="epofw-no-results-icon">
				<i class="dashicons dashicons-search"></i>
			</div>
			<h3><?php esc_html_e( 'No matching addons found', 'extra-product-options-for-woocommerce' ); ?></h3>
			<p><?php esc_html_e( 'Try adjusting your search term or', 'extra-product-options-for-woocommerce' ); ?> <a href="#" class="epofw-clear-search-link"><?php esc_html_e( 'clear the search', 'extra-product-options-for-woocommerce' ); ?></a></p>
		</div>
	</div>
</div>

<!-- Import Confirmation Modal Template -->
<script type="text/html" id="tmpl-epofw-import-modal">
	<div class="epofw-import-overlay">
		<div class="epofw-import-modal">
			<div class="epofw-import-icon">
				<span class="dashicons dashicons-download"></span>
			</div>
			<h2>{{ data.title }}</h2>
			<p class="epofw-import-message">{{ data.message }}</p>
			<div class="epofw-import-info">
				<p>
					<strong>{{ data.will_import }}</strong><br>
					• {{ data.demo_tshirt }}<br>
					• {{ data.demo_pizza }}<br>
					• {{ data.demo_event }}
					<# if ( data.is_premium ) { #>
					<br>• {{ data.demo_premium }}
					<# } #>
				</p>
			</div>
			<div class="epofw-import-buttons">
				<button type="button" class="button button-secondary epofw-import-cancel">{{ data.cancel }}</button>
				<button type="button" class="button button-primary epofw-import-confirm">{{ data.import_now }}</button>
			</div>
		</div>
	</div>
</script>

<!-- Card Loader Template -->
<script type="text/html" id="tmpl-epofw-card-loader">
	<div class="epofw-card-loader-overlay">
		<div class="epofw-card-loader">
			<span class="spinner is-active"></span>
			<span class="epofw-loader-text">{{ data.text }}</span>
		</div>
	</div>
</script>
