<?php
/**
 * Import and export settings tab template.
 *
 * @package KooKoa_Folders
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; }
?>
	<!-- ============================================
		TAB 4: Import & Export
		============================================ -->
	<div class="kookoawpf-tab-panel" data-panel="import-export" style="display:none;">

		<!-- Export / Import Section -->
		<div class="kookoawpf-settings-section">
			<h2><?php esc_html_e( 'Export / Import Folders', 'kookoa-folders' ); ?></h2>
			<p class="description" style="margin-bottom:16px;">
				<?php esc_html_e( 'Export your complete folder structure as a JSON file, or import folders from a previously exported file. Item assignments are NOT included — only the folder tree, colors, pins, and locks.', 'kookoa-folders' ); ?>
			</p>

			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Export', 'kookoa-folders' ); ?></th>
					<td>
						<button type="button" id="kookoawpf-settings-export" class="button kookoawpf-btn-save" style="margin-right:8px;" <?php disabled( $kookoawpf_total_folder_count, 0 ); ?>>
							<span class="dashicons dashicons-upload" style="vertical-align:middle;margin-right:4px;margin-top:-2px;"></span>
							<?php esc_html_e( 'Export All Folders (JSON)', 'kookoa-folders' ); ?>
						</button>
						<p class="description" style="margin-top:8px;">
							<?php if ( 0 === $kookoawpf_total_folder_count ) : ?>
								<?php esc_html_e( 'No folders to export. Create some folders first.', 'kookoa-folders' ); ?>
							<?php else : ?>
								<?php esc_html_e( 'Downloads a .json file with the complete folder structure for all post types.', 'kookoa-folders' ); ?>
							<?php endif; ?>
						</p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Import', 'kookoa-folders' ); ?></th>
					<td>
						<div style="display:flex;align-items:center;gap:12px;margin-bottom:8px;">
							<input type="file" id="kookoawpf-settings-import-file" accept=".json" style="font-family:'Share Tech Mono',monospace;" />
						</div>
						<div style="margin-bottom:12px;">
							<label style="display:block;margin-bottom:4px;">
								<input type="radio" name="kookoawpf_settings_import_mode" value="merge" checked>
								<?php esc_html_e( 'Merge with existing folders', 'kookoa-folders' ); ?>
							</label>
							<label style="display:block;">
								<input type="radio" name="kookoawpf_settings_import_mode" value="replace">
								<span style="color:#FF3B30;"><?php esc_html_e( 'Replace all (deletes existing folders first)', 'kookoa-folders' ); ?></span>
							</label>
						</div>
						<button type="button" id="kookoawpf-settings-import" class="button kookoawpf-btn-save" disabled>
							<span class="dashicons dashicons-download" style="vertical-align:middle;margin-right:4px;margin-top:-2px;"></span>
							<?php esc_html_e( 'Import Folders', 'kookoa-folders' ); ?>
						</button>
						<div id="kookoawpf-settings-import-preview" style="margin-top:12px;background:#161b22;border:1px solid #30363d;border-radius:6px;padding:12px;max-height:200px;overflow:auto;font-family:'Share Tech Mono',monospace;font-size:12px;color:#ffffff;display:none;"></div>
					</td>
				</tr>
			</table>
		</div>

		<!-- Upload Folder Structure Section -->
		<div id="kookoawpf-settings-upload-folder-section" class="kookoawpf-settings-section" style="margin-top:30px;">
			<h2><?php esc_html_e( 'Upload Folder Structure', 'kookoa-folders' ); ?></h2>
			<p class="description" style="margin-bottom:16px;">
				<?php esc_html_e( 'Select a folder from your computer to upload. The folder structure will be recreated and all files placed in the correct folders.', 'kookoa-folders' ); ?>
			</p>

			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Upload', 'kookoa-folders' ); ?></th>
					<td>
						<input type="file" id="kookoawpf-folder-input" webkitdirectory directory multiple style="display:none;">
						<button type="button" id="kookoawpf-upload-folder-btn" class="button kookoawpf-btn-save" style="margin-right:8px;">
							<span class="dashicons dashicons-open-folder" style="vertical-align:middle;margin-right:4px;margin-top:-2px;"></span>
							<?php esc_html_e( 'Select Folder', 'kookoa-folders' ); ?>
						</button>
						<p class="description" style="margin-top:8px;">
							<?php esc_html_e( 'Uploads all files and recreates the folder hierarchy. Empty folders are skipped (browser limitation). Supported in Chrome, Edge, Firefox, and Safari.', 'kookoa-folders' ); ?>
						</p>
						<div id="kookoawpf-upload-folder-progress" class="kookoawpf-upload-folder-progress" style="display:none;">
							<div class="kookoawpf-upload-folder-bar-wrap">
								<div class="kookoawpf-upload-folder-bar"></div>
							</div>
							<div class="kookoawpf-upload-folder-status"></div>
						</div>
					</td>
				</tr>
			</table>
		</div>

		<?php
		$kookoawpf_registry         = KookoaWPF_Importer_Registry::instance();
		$kookoawpf_visible_imports  = $kookoawpf_registry->get_visible();
		$kookoawpf_has_hidden_cards = $kookoawpf_registry->has_hidden_cards();
		$kookoawpf_show_section     = $kookoawpf_registry->should_show_section();
		$kookoawpf_is_pro_active    = function_exists( 'kookoawpf_pro_bootstrap' );
		// "All done" state: nothing left to import (every source has been
		// imported or dismissed), but the section is still rendered for
		// the Reset / Hide-forever controls.
		$kookoawpf_all_done = $kookoawpf_show_section && empty( $kookoawpf_visible_imports );
		?>
		<?php if ( $kookoawpf_show_section ) : ?>
		<!-- Migrate from other plugins Section -->
		<div id="kookoawpf-migrate-section" class="kookoawpf-settings-section" style="margin-top:30px;">
			<h2><?php esc_html_e( 'Migrate from Other Plugins', 'kookoa-folders' ); ?></h2>
			<p class="description" style="margin-bottom:16px;">
				<?php esc_html_e( 'Detected folder structures from competing plugins on this site. Bring them into KooKoa Folders in one click — the full tree and file assignments come over intact.', 'kookoa-folders' ); ?>
			</p>

			<div class="kookoawpf-migrate-list">
				<?php foreach ( $kookoawpf_visible_imports as $kookoawpf_importer ) : ?>
					<?php
					$kookoawpf_id           = $kookoawpf_importer->get_id();
					$kookoawpf_folder_count = $kookoawpf_importer->count_folders();
					$kookoawpf_item_count   = $kookoawpf_importer->count_items();
					$kookoawpf_requires_pro = $kookoawpf_importer->requires_pro();
					$kookoawpf_pro_locked   = $kookoawpf_requires_pro && ! $kookoawpf_is_pro_active;
					$kookoawpf_radio_name   = 'kookoawpf_migrate_mode_' . esc_attr( $kookoawpf_id );

					// Pluralize folder/item counts independently — a single
					// _n() driven by one of the two would mismatch (e.g.
					// "1 folder with 2 item" or "2 folders with 1 items").
					$kookoawpf_folder_label = sprintf(
						/* translators: %d: number of folders detected by the importer */
						_n( '%d folder', '%d folders', $kookoawpf_folder_count, 'kookoa-folders' ),
						$kookoawpf_folder_count
					);
					$kookoawpf_item_label = sprintf(
						/* translators: %d: number of items (attachments / posts / pages) detected by the importer */
						_n( '%d item', '%d items', $kookoawpf_item_count, 'kookoa-folders' ),
						$kookoawpf_item_count
					);
					$kookoawpf_summary = sprintf(
						/* translators: 1: localized folder count (e.g. "13 folders"), 2: localized item count (e.g. "26 items") */
						__( 'Found <strong>%1$s</strong> with <strong>%2$s</strong>.', 'kookoa-folders' ),
						esc_html( $kookoawpf_folder_label ),
						esc_html( $kookoawpf_item_label )
					);
					?>
					<?php
					$kookoawpf_card_class = 'kookoawpf-migrate-card' . ( $kookoawpf_pro_locked ? ' kookoawpf-migrate-card-locked' : '' );
					?>
					<div class="<?php echo esc_attr( $kookoawpf_card_class ); ?>" data-importer-id="<?php echo esc_attr( $kookoawpf_id ); ?>">
						<div class="kookoawpf-migrate-card-head">
							<div class="kookoawpf-migrate-card-info">
								<div class="kookoawpf-migrate-card-title">
									<span class="dashicons dashicons-portfolio"></span>
									<?php echo esc_html( $kookoawpf_importer->get_name() ); ?>
									<?php if ( $kookoawpf_pro_locked ) : ?>
										<span class="kookoawpf-migrate-card-pro-badge">PRO</span>
									<?php endif; ?>
								</div>
								<div class="kookoawpf-migrate-card-author"><?php echo esc_html( sprintf( /* translators: %s: plugin author */ __( 'by %s', 'kookoa-folders' ), $kookoawpf_importer->get_author() ) ); ?></div>
								<div class="kookoawpf-migrate-card-summary">
									<?php echo wp_kses( $kookoawpf_summary, array( 'strong' => array() ) ); ?>
								</div>
							</div>
							<div class="kookoawpf-migrate-card-actions">
								<?php if ( $kookoawpf_pro_locked ) : ?>
									<a class="button kookoawpf-btn-upgrade" href="<?php echo esc_url( admin_url( 'admin.php?page=kookoa-folders-upgrade' ) ); ?>">
										<span class="dashicons dashicons-lock" style="vertical-align:middle;margin-right:4px;margin-top:-2px;"></span>
										<?php esc_html_e( 'Upgrade to PRO', 'kookoa-folders' ); ?>
									</a>
								<?php else : ?>
									<button type="button" class="button kookoawpf-btn-save kookoawpf-migrate-import-btn" data-importer-id="<?php echo esc_attr( $kookoawpf_id ); ?>">
										<span class="dashicons dashicons-download" style="vertical-align:middle;margin-right:4px;margin-top:-2px;"></span>
										<?php esc_html_e( 'Import Now', 'kookoa-folders' ); ?>
									</button>
								<?php endif; ?>
								<button type="button" class="kookoawpf-migrate-dismiss" data-importer-id="<?php echo esc_attr( $kookoawpf_id ); ?>" aria-label="<?php esc_attr_e( "Don't show again", 'kookoa-folders' ); ?>">
									&times;
								</button>
							</div>
						</div>

						<?php if ( ! $kookoawpf_pro_locked ) : ?>
						<div class="kookoawpf-migrate-card-mode" style="display:none;">
							<div style="margin-bottom:8px;">
								<label style="display:block;margin-bottom:4px;">
									<input type="radio" name="<?php echo esc_attr( $kookoawpf_radio_name ); ?>" value="merge" checked>
									<?php esc_html_e( 'Merge with existing folders (recommended)', 'kookoa-folders' ); ?>
								</label>
								<label style="display:block;">
									<input type="radio" name="<?php echo esc_attr( $kookoawpf_radio_name ); ?>" value="replace">
									<span style="color:#FF3B30;"><?php esc_html_e( 'Replace (deletes existing folders first)', 'kookoa-folders' ); ?></span>
								</label>
							</div>
							<button type="button" class="button kookoawpf-btn-save kookoawpf-migrate-confirm-btn" data-importer-id="<?php echo esc_attr( $kookoawpf_id ); ?>">
								<?php esc_html_e( 'Start Import', 'kookoa-folders' ); ?>
							</button>
							<button type="button" class="button kookoawpf-migrate-cancel-btn" style="margin-left:8px;">
								<?php esc_html_e( 'Cancel', 'kookoa-folders' ); ?>
							</button>
						</div>
						<?php endif; ?>

						<div class="kookoawpf-migrate-card-result" style="display:none;"></div>
					</div>
				<?php endforeach; ?>
			</div>

			<?php
			// Route the conditional visibility through dedicated style
			// values so esc_attr() handles the fragment instead of an
			// echoed raw attribute string (PHPCS strict-friendly).
			$kookoawpf_reset_link_style   = $kookoawpf_has_hidden_cards ? '' : 'display:none;';
			$kookoawpf_hide_forever_style = $kookoawpf_all_done ? '' : 'display:none;';
			?>
			<p class="kookoawpf-migrate-footer-links" style="<?php echo esc_attr( $kookoawpf_reset_link_style ); ?>">
				<button type="button" id="kookoawpf-migrate-reset" class="kookoawpf-migrate-footer-link">
					<?php esc_html_e( 'Reset import detection (show cards again)', 'kookoa-folders' ); ?>
				</button>
			</p>

			<p class="kookoawpf-migrate-footer-links" style="<?php echo esc_attr( $kookoawpf_hide_forever_style ); ?>">
				<button type="button" id="kookoawpf-migrate-hide-forever" class="kookoawpf-migrate-footer-link kookoawpf-migrate-footer-link-danger">
					<?php esc_html_e( 'Remove this section permanently — not reversible', 'kookoa-folders' ); ?>
				</button>
			</p>
		</div>
		<?php endif; ?>

	</div>
