<?php
/**
 * MW WP Form migration wizard template.
 *
 * @package CF7MasterPro
 * @var array  $rows         MW form rows.
 * @var int    $done_id      Newly created CF7 post ID (0 = none).
 * @var string $done_title   New CF7 form title.
 * @var array  $report       Migration report.
 * @var int    $bulk_done    Bulk-converted count (-1 = none).
 * @var int    $imported     Imported data count (-1 = none).
 * @var int    $skipped      Records skipped due to corrupted saved data (0 = none).
 * @var string $error        Error message.
 * @var bool   $bulk_enabled Pro features unlocked.
 * @var string $upgrade_url  Freemius upgrade URL.
 * @var string $nonce_field  Nonce field HTML.
 */

defined( 'ABSPATH' ) || exit;
?>
<div class="wrap cf7mp-migration">
	<h1><?php esc_html_e( 'MW WP Form Migration', 'frontierline-forms' ); ?></h1>
	<p>
		<?php esc_html_e( 'Convert MW WP Form forms to Contact Form 7 with one click. Fields, validation rules, mail settings and the confirmation screen flow are migrated automatically.', 'frontierline-forms' ); ?>
	</p>

	<?php if ( $error ) : ?>
		<div class="notice notice-error"><p><?php echo esc_html( $error ); ?></p></div>
	<?php endif; ?>

	<?php if ( $bulk_done >= 0 ) : ?>
		<div class="notice notice-success"><p>
			<?php
			printf(
				/* translators: %d: converted forms count */
				esc_html__( 'Bulk conversion finished: %d forms were converted.', 'frontierline-forms' ),
				(int) $bulk_done
			);
			?>
		</p></div>
	<?php endif; ?>

	<?php if ( $imported >= 0 ) : ?>
		<div class="notice notice-success"><p>
			<?php
			printf(
				/* translators: %d: imported inquiries count */
				esc_html__( 'Data import finished: %d inquiries were imported into Submissions.', 'frontierline-forms' ),
				(int) $imported
			);
			?>
		</p></div>
		<?php if ( ! empty( $skipped ) ) : ?>
			<div class="notice notice-warning"><p>
				<?php
				printf(
					/* translators: %d: skipped inquiries count */
					esc_html__( '%d records were skipped because their saved data was corrupted. They were not marked as imported, so you can fix the data in MW WP Form and run the import again.', 'frontierline-forms' ),
					(int) $skipped
				);
				?>
			</p></div>
		<?php endif; ?>
	<?php endif; ?>

	<?php if ( $done_id ) : ?>
		<div class="cf7mp-migration__done" style="background:#fff;border:1px solid #c3e6cb;border-left:4px solid #46b450;border-radius:4px;padding:16px 20px;margin:16px 0;max-width:852px;">
			<h2 style="margin-top:0;">
				✅ <?php esc_html_e( 'Migration complete!', 'frontierline-forms' ); ?>
				<?php echo esc_html( $done_title ); ?>
			</h2>
			<?php if ( ! empty( $report['total'] ) ) : ?>
				<p>
					<?php
					printf(
						/* translators: 1: converted count 2: total count */
						esc_html__( '%1$d of %2$d fields were converted automatically.', 'frontierline-forms' ),
						(int) $report['converted'],
						(int) $report['total']
					);
					?>
					<?php esc_html_e( 'The confirmation screen has been enabled.', 'frontierline-forms' ); ?>
				</p>
			<?php endif; ?>

			<p style="background:#f6f7f7;padding:8px 12px;border-radius:4px;">
				<strong><?php esc_html_e( 'Next step:', 'frontierline-forms' ); ?></strong>
				<?php esc_html_e( 'Replace the [mwform_formkey] shortcode on your page with:', 'frontierline-forms' ); ?>
				<code>[contact-form-7 id="<?php echo (int) $done_id; ?>"]</code>
			</p>

			<?php if ( ! empty( $report['renames'] ) ) : ?>
				<h3><?php esc_html_e( 'Renamed fields', 'frontierline-forms' ); ?></h3>
				<table class="widefat striped" style="max-width:520px;">
					<thead><tr><th>MW WP Form</th><th>Contact Form 7</th></tr></thead>
					<tbody>
					<?php foreach ( $report['renames'] as $from => $to ) : ?>
						<tr><td><?php echo esc_html( $from ); ?></td><td><code><?php echo esc_html( $to ); ?></code></td></tr>
					<?php endforeach; ?>
					</tbody>
				</table>
			<?php endif; ?>

			<?php if ( ! empty( $report['notes'] ) ) : ?>
				<h3><?php esc_html_e( 'Please review manually', 'frontierline-forms' ); ?></h3>
				<ul style="list-style:disc;padding-left:20px;">
					<?php foreach ( $report['notes'] as $note ) : ?>
						<li><?php echo esc_html( $note ); ?></li>
					<?php endforeach; ?>
				</ul>
			<?php endif; ?>

			<p>
				<a href="<?php echo esc_url( admin_url( 'admin.php?page=wpcf7&post=' . $done_id . '&action=edit' ) ); ?>" class="button button-primary button-hero">
					<?php esc_html_e( 'Open the new form', 'frontierline-forms' ); ?>
				</a>
			</p>
		</div>
	<?php endif; ?>

	<?php if ( empty( $rows ) ) : ?>
		<div class="notice notice-info inline"><p>
			<?php esc_html_e( 'No MW WP Form forms were found on this site.', 'frontierline-forms' ); ?>
		</p></div>
	<?php else : ?>
		<table class="widefat striped" style="max-width:900px;margin-top:8px;">
			<thead>
				<tr>
					<th><?php esc_html_e( 'MW WP Form', 'frontierline-forms' ); ?></th>
					<th style="width:80px;">ID</th>
					<th style="width:140px;"><?php esc_html_e( 'Saved inquiries', 'frontierline-forms' ); ?></th>
					<th style="width:150px;"><?php esc_html_e( 'Status', 'frontierline-forms' ); ?></th>
					<th style="width:180px;"></th>
				</tr>
			</thead>
			<tbody>
			<?php foreach ( $rows as $row ) : ?>
				<tr>
					<td><strong><?php echo esc_html( $row['title'] ); ?></strong></td>
					<td>#<?php echo (int) $row['id']; ?></td>
					<td><?php echo (int) $row['data_count']; ?></td>
					<td>
						<?php if ( $row['cf7_id'] ) : ?>
							<a href="<?php echo esc_url( admin_url( 'admin.php?page=wpcf7&post=' . $row['cf7_id'] . '&action=edit' ) ); ?>">
								<?php esc_html_e( 'Converted', 'frontierline-forms' ); ?> ✓
							</a>
						<?php else : ?>
							<span style="color:#996800;"><?php esc_html_e( 'Not converted', 'frontierline-forms' ); ?></span>
						<?php endif; ?>
					</td>
					<td>
						<form method="post">
							<?php echo $nonce_field; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_nonce_field HTML. ?>
							<button type="submit" name="cf7mp_mw_convert" value="<?php echo (int) $row['id']; ?>" class="button<?php echo $row['cf7_id'] ? '' : ' button-primary'; ?>">
								<?php $row['cf7_id'] ? esc_html_e( 'Convert again', 'frontierline-forms' ) : esc_html_e( 'Convert now', 'frontierline-forms' ); ?>
							</button>
						</form>
					</td>
				</tr>
			<?php endforeach; ?>
			</tbody>
		</table>

		<?php if ( $bulk_enabled ) : ?>
			<form method="post" style="margin-top:12px;">
				<?php echo $nonce_field; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_nonce_field HTML. ?>
				<button type="submit" name="cf7mp_mw_convert_all" value="1" class="button button-primary button-hero">
					<?php esc_html_e( 'Convert all remaining forms', 'frontierline-forms' ); ?>
				</button>
			</form>
			<?php do_action( 'cf7mp_mw_migration_pro_panel', $rows ); ?>
		<?php else : ?>
			<div class="cf7mp-migration__pro" style="background:linear-gradient(135deg,#2c3e50,#3d566e);color:#fff;border-radius:8px;padding:20px 24px;margin:24px 0;max-width:852px;">
				<h2 style="color:#fff;margin-top:0;">🚀 <?php esc_html_e( 'Migrate everything at once with Pro', 'frontierline-forms' ); ?></h2>
				<ul style="list-style:disc;padding-left:20px;">
					<li><?php esc_html_e( 'Bulk convert all MW WP Form forms in one click', 'frontierline-forms' ); ?></li>
					<li><?php esc_html_e( 'Import past inquiry data (saved submissions) into the Frontierline Forms database', 'frontierline-forms' ); ?></li>
					<li><?php esc_html_e( 'Advanced spam protection, Slack / Google Sheets integrations and more', 'frontierline-forms' ); ?></li>
				</ul>
				<?php if ( $upgrade_url ) : ?>
					<a href="<?php echo esc_url( $upgrade_url ); ?>" class="button button-hero" style="background:#f0b849;border-color:#f0b849;color:#1d2327;font-weight:600;">
						<?php esc_html_e( 'Upgrade to Pro', 'frontierline-forms' ); ?> →
					</a>
				<?php endif; ?>
			</div>
		<?php endif; ?>
	<?php endif; ?>
</div>
