<?php

/**
 * Export & Import view
 *
 * Rendered by `SlideMind\Admin\ExportToolPage::render()` and mirrored into
 * the Settings → Export/Import Data tab via the shared `get_view_data()`.
 *
 * @package SlideMind
 * @since   0.6.0
 *
 * @var array<string, string>                                               $sections
 * @var array<string, int>                                                  $counts
 * @var array<string, array<int, array{id:int,title:string}>>               $items
 * @var string                                                              $action_url
 * @var string                                                              $action_slug
 * @var string                                                              $nonce_name
 * @var string                                                              $nonce_action
 * @var string                                                              $upgrade_url
 * @var string                                                              $status
 * @var string                                                              $admin_ajax_url
 * @var string                                                              $admin_nonce
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
?>
<div class="sm-wrap" id="sm-export-import-root" data-ajax-url="<?php 
echo esc_url( $admin_ajax_url );
?>" data-nonce="<?php 
echo esc_attr( $admin_nonce );
?>">
	<div class="sm-header">
		<h1><?php 
esc_html_e( 'Export & Import', 'slidemind' );
?></h1>
		<p class="sm-muted">
			<?php 
esc_html_e( 'Download a JSON backup of the selected sections and items, or restore from a previously-exported file. API keys are never included.', 'slidemind' );
?>
		</p>
	</div>

	<?php 
if ( $status === 'empty' ) {
    ?>
		<div class="notice notice-warning inline">
			<p><?php 
    esc_html_e( 'Select at least one section before exporting.', 'slidemind' );
    ?></p>
		</div>
	<?php 
}
?>

	<!-- ───────────── Export card ───────────── -->
	<div class="sm-card sm-export-card">
		<h2 class="sm-card-title"><?php 
esc_html_e( 'Export', 'slidemind' );
?></h2>

		<form method="post" action="<?php 
echo esc_url( $action_url );
?>">
			<input type="hidden" name="action" value="<?php 
echo esc_attr( $action_slug );
?>" />
			<?php 
wp_nonce_field( $nonce_action, $nonce_name );
?>

			<table class="sm-table" style="margin-bottom: 16px;">
				<thead>
					<tr>
						<th style="width: 40px;"></th>
						<th><?php 
esc_html_e( 'Section', 'slidemind' );
?></th>
						<th style="width: 160px; text-align: right;"><?php 
esc_html_e( 'Items', 'slidemind' );
?></th>
					</tr>
				</thead>
				<tbody>
					<?php 
foreach ( $sections as $key => $label ) {
    ?>
						<?php 
    $count = ( isset( $counts[$key] ) ? (int) $counts[$key] : 0 );
    $checkbox = 'sm-export-section-' . $key;
    $has_item_picker = in_array( $key, [
        'courses',
        'modules',
        'slides',
        'templates'
    ], true );
    $section_items = $items[$key] ?? [];
    ?>
						<tr class="sm-export-row" data-section="<?php 
    echo esc_attr( $key );
    ?>">
							<td>
								<input
									type="checkbox"
									name="sections[]"
									id="<?php 
    echo esc_attr( $checkbox );
    ?>"
									value="<?php 
    echo esc_attr( $key );
    ?>"
									class="sm-export-section-toggle"
									<?php 
    checked( true );
    ?>
								/>
							</td>
							<td>
								<label for="<?php 
    echo esc_attr( $checkbox );
    ?>"><?php 
    echo esc_html( $label );
    ?></label>
								<?php 
    if ( $has_item_picker && !empty( $section_items ) ) {
        ?>
									<button type="button" class="sm-link sm-toggle-items" data-target="sm-export-items-<?php 
        echo esc_attr( $key );
        ?>">
										<?php 
        esc_html_e( 'Choose specific items…', 'slidemind' );
        ?>
									</button>
								<?php 
    }
    ?>
							</td>
							<td style="text-align: right;">
								<?php 
    if ( $key === 'settings' ) {
        ?>
									<span class="sm-muted">&mdash;</span>
								<?php 
    } else {
        ?>
									<strong class="sm-item-count" data-total="<?php 
        echo esc_attr( $count );
        ?>"><?php 
        echo esc_html( number_format_i18n( $count ) );
        ?></strong>
								<?php 
    }
    ?>
							</td>
						</tr>
						<?php 
    if ( $has_item_picker && !empty( $section_items ) ) {
        ?>
							<tr id="sm-export-items-<?php 
        echo esc_attr( $key );
        ?>" class="sm-export-items-row" hidden>
								<td colspan="3">
									<?php 
        if ( count( $section_items ) > 20 ) {
            ?>
										<input type="text" class="sm-form-input sm-item-filter" placeholder="<?php 
            esc_attr_e( 'Filter by title…', 'slidemind' );
            ?>" data-section="<?php 
            echo esc_attr( $key );
            ?>" style="margin-bottom: 8px; max-width: 360px;">
									<?php 
        }
        ?>
									<ul class="sm-item-list" data-section="<?php 
        echo esc_attr( $key );
        ?>">
										<?php 
        foreach ( $section_items as $item ) {
            ?>
											<li>
												<label>
													<input
														type="checkbox"
														name="item_ids[<?php 
            echo esc_attr( $key );
            ?>][]"
														value="<?php 
            echo esc_attr( (int) $item['id'] );
            ?>"
														class="sm-item-check"
														<?php 
            checked( true );
            ?>
													/>
													<?php 
            echo esc_html( ( $item['title'] !== '' ? $item['title'] : '—' ) );
            ?>
												</label>
											</li>
										<?php 
        }
        ?>
									</ul>
								</td>
							</tr>
						<?php 
    }
    ?>
					<?php 
}
?>
				</tbody>
			</table>

			<p class="sm-muted" style="margin-top: 0;">
				<?php 
esc_html_e( 'Exports use the slidemind-export-v1 JSON format and download directly in your browser.', 'slidemind' );
?>
			</p>

			<p>
				<button type="submit" class="sm-btn sm-btn-primary">
					<span class="dashicons dashicons-download" style="font-size:18px;width:18px;height:18px;vertical-align:text-bottom;"></span>
					<?php 
esc_html_e( 'Download export file', 'slidemind' );
?>
				</button>
			</p>
		</form>
	</div>

	<!-- ───────────── Import card ───────────── -->
	<div class="sm-card sm-import-card" style="margin-top: 20px;">
		<h2 class="sm-card-title"><?php 
esc_html_e( 'Import', 'slidemind' );
?></h2>

		<div class="sm-notice sm-notice-warning">
			<span class="dashicons dashicons-warning"></span>
			<div>
				<?php 
esc_html_e( 'Import accepts any SlideMind export (full, partial, or single-item). Preview what the file contains before confirming. Memory variables are never imported.', 'slidemind' );
?>
			</div>
		</div>

		<div class="sm-form-group">
			<label class="sm-form-label" for="sm-import-root-file">
				<?php 
esc_html_e( 'JSON file', 'slidemind' );
?>
			</label>
			<input type="file" id="sm-import-root-file" accept=".json" class="sm-form-input">
		</div>

		<div class="sm-form-group">
			<label class="sm-form-label" for="sm-import-root-json">
				<?php 
esc_html_e( 'or paste the JSON content', 'slidemind' );
?>
			</label>
			<textarea id="sm-import-root-json" class="sm-form-input" rows="6" placeholder='{ "format": "slidemind-export-v1", ... }'></textarea>
		</div>

		<p>
			<button type="button" id="sm-import-root-analyze" class="sm-btn sm-btn-secondary">
				<?php 
esc_html_e( 'Analyze file', 'slidemind' );
?>
			</button>
		</p>

		<div id="sm-import-root-preview" hidden></div>
		<div id="sm-import-root-result" hidden style="margin-top: 16px;"></div>
	</div>

	<?php 
// Migration upsell — premium build only, absent from the free zip.
?>
	<?php 
?>
</div>
