<?php

	// Exit if accessed directly
	if ( ! defined( 'ABSPATH' ) ) {
		exit;
	}

	// Loader icon
	WS_Form_Common::loader();

	// Read settings
	$ws_form_settings = array('id', 'form_id_source', 'method', 'form_id_destination', 'duplicate');
	$ws_form_wsf_form_migrate = WS_Form_Common::get_query_var('wsf_form_migrate', array());

	$ws_form_changed = WS_Form_Common::get_query_var_nonce('changed', false, false, false, true, 'POST');
	$ws_form_change_do = true;
	foreach($ws_form_settings as $ws_form_setting) {

		if(isset($ws_form_wsf_form_migrate[$ws_form_setting]) && $ws_form_change_do) {

			WS_Form_Common::option_set('migrate_' . $ws_form_setting, $ws_form_wsf_form_migrate[$ws_form_setting]);
		}

		if(!$ws_form_change_do) { WS_Form_Common::option_set('migrate_' . $ws_form_setting, ''); }

		$ws_form_wsf_form_migrate[$ws_form_setting] = WS_Form_Common::option_get('migrate_' . $ws_form_setting, false);

		if($ws_form_changed === $ws_form_setting) { $ws_form_change_do = false; }
	}

	// Field mapping enabled?
	$ws_form_show_field_mapping = (

		($ws_form_wsf_form_migrate['id'] != '') &&
		($ws_form_wsf_form_migrate['form_id_source'] != '') &&
		(
			($ws_form_wsf_form_migrate['method'] == 'submissions') &&
			($ws_form_wsf_form_migrate['form_id_destination'] != '') && 
			($ws_form_wsf_form_migrate['duplicate'] != '')
		)
	);

	// Show field mappings
	if($ws_form_show_field_mapping) {

		// Get destination fields
		$ws_form_form = new WS_Form_Form();
		$ws_form_form->id = $ws_form_wsf_form_migrate['form_id_destination'];
	
		try {

			// Get form object
			$ws_form_form_object = $ws_form_form->db_read(true, true);

 			// Get form fields
			$ws_form_fields_destination = WS_Form_Common::get_fields_from_form($ws_form_form_object);

		} catch (Exception $e) {

			$ws_form_show_field_mapping = false;
		}
	}

	// Check for field mapping
	if($ws_form_show_field_mapping) {

		// Set field mapping option key
		$ws_form_field_mapping_option_key = 'migrate_field_mapping_' . $ws_form_wsf_form_migrate['id'] . '_' . $ws_form_wsf_form_migrate['form_id_source'] . '_' . $ws_form_wsf_form_migrate['form_id_destination'];
	}

	// Loader icon
	WS_Form_Common::loader();
?>
<div id="wsf-wrapper" class="<?php WS_Form_Common::wrapper_classes(); ?>">

<?php

	ob_start();
?>
<button type="button" class="button button-primary" id="wsf-migrate-button" disabled data-action="wsf_migrate" title="<?php esc_attr_e('Start Import', 'ws-form'); ?>"><?php WS_Form_Common::render_icon_16_svg('upload'); ?> <span class="wsf-admin-header-action-label"><?php esc_html_e('Start Import', 'ws-form'); ?></span></button>
<?php

	WS_Form_Common::admin_header(__('Migrate', 'ws-form'), ob_get_clean());

	// Review nag
	WS_Form_Common::review();
?>
<!-- Wrapper -->
<div id="wsf_migrate">
<?php

	require_once WS_FORM_PLUGIN_DIR_PATH . 'includes/core/class-ws-form-migrate.php';

	$ws_form_migrate = new WS_Form_Migrate();

	// Get migrations
	$ws_form_migrations = $ws_form_migrate->get_migrations();

	// Check migrate ID exists
	if(!isset($ws_form_migrations[$ws_form_wsf_form_migrate['id']])) {

		$ws_form_wsf_form_migrate['id'] = '';
	}
?>
<form action="admin.php?page=ws-form-migrate" id="wsf-migrate-form" method="post">

<table class="form-table"><tbody>

<tr>
<th scope="row"><label for="wsf_migrate_id"><?php esc_html_e('Import From:', 'ws-form'); ?></label></th>
<td><select id="wsf_migrate_id" name="wsf_form_migrate[id]" data-action="wsf-submit" data-id="id">
<option value=""><?php esc_html_e('Select plugin...', 'ws-form'); ?></option>
<?php

	foreach($ws_form_migrations as $ws_form_migration) {

?><option value="<?php WS_Form_Common::echo_esc_attr($ws_form_migration['id']); ?>"<?php

		if(($ws_form_wsf_form_migrate['id'] === false) && $ws_form_migration['select']) { $ws_form_wsf_form_migrate['id'] = $ws_form_migration['id']; }
		if($ws_form_wsf_form_migrate['id'] == $ws_form_migration['id']) { ?> selected<?php }
		if(!$ws_form_migration['data']) { ?> disabled="disabled"<?php }

?>><?php WS_Form_Common::echo_esc_html($ws_form_migration['label']); ?> (<?php WS_Form_Common::echo_esc_html($ws_form_migration['version']); ?>)<?php

		if($ws_form_migration['active']) { ?> (Active)<?php }

?></option>
<?php

	}
?>
</select></td>
</tr>
<?php

	$ws_form_record_count = 0;

	// Select migrate form
	if($ws_form_wsf_form_migrate['id'] != '') {

		// Migrate name
		$ws_form_migrate_name = $ws_form_migrations[$ws_form_wsf_form_migrate['id']]['label'];

		// Get forms
		$ws_form_migrate->id = $ws_form_wsf_form_migrate['id'];
		$ws_form_get_data_return = $ws_form_migrate->get_data('form');
		$ws_form_forms = $ws_form_get_data_return['data'];
?>
<tr>
<th scope="row"><label for="wsf-migrate-form-id-source"><?php

	WS_Form_Common::echo_esc_html(sprintf(

		/* translators: %s: Form label */
		__('%s Form:', 'ws-form'),
		$ws_form_migrate_name
	));

?></label></th>
<td><select id="wsf-migrate-form-id-source" name="wsf_form_migrate[form_id_source]" data-action="wsf-submit" data-id="form_id_source">
<option value=""><?php esc_html_e('Select form...', 'ws-form'); ?></option>
<?php

		foreach($ws_form_forms as $ws_form_form) {

?><option value="<?php WS_Form_Common::echo_esc_attr($ws_form_form['id']); ?>"<?php if($ws_form_wsf_form_migrate['form_id_source'] == $ws_form_form['id']) { ?> selected<?php } ?>><?php WS_Form_Common::echo_esc_html($ws_form_form['label']); ?> (<?php esc_html_e('ID', 'ws-form'); ?>: <?php WS_Form_Common::echo_esc_html($ws_form_form['id']); ?>)</option>
<?php

		}
?>
</select></td>
</tr>
<?php
	}

	// Select method
	if(
		($ws_form_wsf_form_migrate['id'] != '') &&
		($ws_form_wsf_form_migrate['form_id_source'] != '')
	) {

		// Get submission count
		$ws_form_record_count = $ws_form_migrate->get_data('submission', $ws_form_wsf_form_migrate['form_id_source'], false, false, true);
?>
<tr>
<th scope="row"><label for="wsf-migrate-method"><?php esc_html_e('Data To Import:', 'ws-form'); ?></label></th>
<td><select id="wsf-migrate-method" name="wsf_form_migrate[method]" data-action="wsf-submit" data-id="method">
<option value=""><?php esc_html_e('Select...', 'ws-form'); ?></option>
<option value="form"<?php if($ws_form_wsf_form_migrate['method'] == 'form') { ?> selected<?php } ?>>Form</option>
<option value="submissions"<?php if($ws_form_wsf_form_migrate['method'] == 'submissions') { ?> selected<?php } ?><?php if($ws_form_record_count == 0) { ?> disabled<?php } ?>><?php esc_html_e('Submissions', 'ws-form'); ?> (<?php

	WS_Form_Common::echo_esc_html(sprintf(

		/* translators: %u: Number of records */
		_n('%u record', '%u records', $ws_form_record_count, 'ws-form'),
		$ws_form_record_count
	));

?>)</option>
</select></td>
</tr>
<?php
	}

	// Select WS Form
	if(
		($ws_form_wsf_form_migrate['id'] != '') &&
		($ws_form_wsf_form_migrate['form_id_source'] != '') &&
		($ws_form_wsf_form_migrate['method'] == 'submissions')
	) {

		// Select form
		$ws_form_form = new WS_Form_Form();
		$ws_form_forms = $ws_form_form->db_read_all('', "NOT (status = 'trash')", 'label, id', '', '', false);

		if($ws_form_forms) {
?>
<tr>
<th scope="row"><label for="wsf-migrate-form-id-destination"><?php

	WS_Form_Common::echo_esc_html(sprintf(

		/* translators: %s: WS Form */
		__('Import to %s:', 'ws-form'),

		WS_FORM_NAME_GENERIC
	));

?></label></th>
<td><select id="wsf-migrate-form-id-destination" name="wsf_form_migrate[form_id_destination]" data-action="wsf-submit" data-id="form_id_destination">
<option value=""><?php esc_html_e('Select form...', 'ws-form'); ?></option>
<?php
			foreach($ws_form_forms as $ws_form_form) {

?><option value="<?php WS_Form_Common::echo_esc_attr($ws_form_form['id']); ?>"<?php if($ws_form_form['id'] == $ws_form_wsf_form_migrate['form_id_destination']) { ?> selected<?php } ?>><?php WS_Form_Common::echo_esc_html($ws_form_form['label']); ?> (<?php esc_html_e('ID', 'ws-form'); ?>: <?php WS_Form_Common::echo_esc_html($ws_form_form['id']); ?>)</option>
<?php
			}
?>
</select></td>
</tr>
<?php
		} else {

?><p><?php
			WS_Form_Common::echo_esc_html(

				sprintf(

					/* translators: %s: WS Form */
					__('You first need to migrate or create a form in %s to migrate submission data into.', 'ws-form'),
					WS_FORM_NAME_GENERIC
				)
			);
?></p><?php
		}
	}

	// Select duplicate handling
	if(
		($ws_form_wsf_form_migrate['id'] != '') &&
		($ws_form_wsf_form_migrate['form_id_source'] != '') &&
		($ws_form_wsf_form_migrate['method'] == 'submissions') &&
		($ws_form_wsf_form_migrate['form_id_destination'] != '')
	) {
?>
<tr>
<th scope="row"><label for="wsf-migrate-duplicate"><?php esc_html_e('Duplicates:', 'ws-form'); ?></label></th>
<td><select id="wsf-migrate-duplicate" name="wsf_form_migrate[duplicate]" data-action="wsf-submit" data-id="duplicate">
<option value=""><?php esc_html_e('Select...', 'ws-form'); ?></option>
<option value="update"<?php if($ws_form_wsf_form_migrate['duplicate'] == 'update') { ?> selected<?php } ?>>Update duplicate</option>
<option value="create"<?php if($ws_form_wsf_form_migrate['duplicate'] == 'create') { ?> selected<?php } ?>>Add as new duplicate</option>
<option value="ignore"<?php if($ws_form_wsf_form_migrate['duplicate'] == 'ignore') { ?> selected<?php } ?>>Ignore and do not import</option>
</select></td>
</tr>
<?php
	}

	// Show field mappings
	if($ws_form_show_field_mapping) {
?>
<tr>
<th scope="row"><label><?php esc_html_e('Field Mapping', 'ws-form'); ?></label></th>
<td><?php

		// Get saved field mapping
		$ws_form_field_mapping = WS_Form_Common::option_get($ws_form_field_mapping_option_key, array());

		// Get source fields
		$ws_form_get_data_return = $ws_form_migrate->get_data('field', $ws_form_wsf_form_migrate['form_id_source']);
		$ws_form_fields_source = $ws_form_get_data_return['data'];
?>
<div class="wsf-table-migrate-outer">
<table id="wsf_field_mapping" class="wsf-table-migrate"><thead>

<tr>
<th><?php WS_Form_Common::echo_esc_html(sprintf(

	'%s %s',
	$ws_form_migrate_name,
	__('Field', 'ws-form')

)); ?></th>
<th><?php WS_Form_Common::echo_esc_html(sprintf(

	'%s %s',
	WS_FORM_NAME_GENERIC,
	__('Field', 'ws-form')

)); ?></th>
<th data-icon></th>
</tr>

</thead>

<tbody>
<?php
		// Output existing field mappings
		foreach($ws_form_field_mapping as $ws_form_field_map) {

			$ws_form_field_id_source = $ws_form_field_map['s'];
			$ws_form_field_id_destination = $ws_form_field_map['d'];

			ws_form_field_mapping_row($ws_form_fields_source, $ws_form_fields_destination, $ws_form_field_id_source, $ws_form_field_id_destination);
		}

		ws_form_field_mapping_row($ws_form_fields_source, $ws_form_fields_destination, false, false, true);
?>
</tbody></table>

<div data-action="wsf-field-mapping-add"><div <?php WS_Form_Common::echo_esc_attr_tooltip(__('Add Row', 'ws-form'), 'left'); ?>><?php WS_Form_Common::render_icon_16_svg('plus-circle'); ?></div></div>

</td>
</tr>
<?php
	}
?>
</tbody></table>

<input type="hidden" name="_wpnonce" value="<?php WS_Form_Common::echo_esc_attr(wp_create_nonce('wp_rest')); ?>">
<?php wp_nonce_field(WS_FORM_POST_NONCE_ACTION_NAME, WS_FORM_POST_NONCE_FIELD_NAME); ?>
<input type="hidden" id="wsf_changed" name="changed" value="" />
<input type="hidden" id="wsf_action" name="action" value="" />
<input type="hidden" name="page" value="ws-form-migrate" />

</form>

<div id="wsf-migrate-status">

<div id="wsf-migrate-status-form">
<label class="wsf-label"><?php esc_html_e('Importing form...', 'ws-form'); ?></label>
<p class="wsf-helper"><?php esc_html_e('Please wait...', 'ws-form'); ?></p>
</div>
</div>

<div id="wsf-migrate-status-submissions">
<label class="wsf-label"><?php esc_html_e('Importing submissions...', 'ws-form'); ?></label>
<progress id="wsf-migrate-status-progress" class="wsf-progress wsf-progress-large" value="0" max="100"></progress>
<p class="wsf-helper"><?php esc_html_e('Total records processed', 'ws-form'); ?>: <span id="wsf-migrate-status-description"></span></p>
</div>

</div>
<!-- /Wrapper -->

<script>

	'use strict';

	(function($) {

		// On load
		$(function() {

			var wsf_obj = new $.WS_Form();

			wsf_obj.init_partial();
			wsf_obj.tooltips();

			// Manually inject language strings (Avoids having to call the full config)
			$.WS_Form.settings_form = [];
			$.WS_Form.settings_form.language = [];
			$.WS_Form.settings_form.language['migrate_status_description_default'] = '<?php esc_html_e('Please wait...', 'ws-form'); ?>';
			$.WS_Form.settings_form.language['migrate_success_submissions'] = '<?php esc_html_e('Successfully imported submissions:', 'ws-form'); ?>';
			$.WS_Form.settings_form.language['migrate_success_form'] = '<?php esc_html_e('Successfully imported form:', 'ws-form'); ?>';
			$.WS_Form.settings_form.language['migrate_created'] = '<?php 

						/* translators: %s: Submission records */
						esc_html_e('%s created.', 'ws-form');
			?>';
			$.WS_Form.settings_form.language['migrate_updated'] = '<?php

						/* translators: %s: Submission records */
						esc_html_e('%s updated.', 'ws-form');
			?>';
			$.WS_Form.settings_form.language['migrate_ignored'] = '<?php

						/* translators: %s: Submission records */
						esc_html_e('%s ignored.', 'ws-form');
			?>';
			$.WS_Form.settings_form.language['migrate_view_submissions'] = '<?php esc_html_e('View Submissions', 'ws-form'); ?>';
			$.WS_Form.settings_form.language['migrate_edit_form'] = '<?php esc_html_e('Edit Form', 'ws-form'); ?>';
			$.WS_Form.settings_form.language['migrate_import_submissions'] = '<?php esc_html_e('Import Submissions', 'ws-form'); ?>';
			$.WS_Form.settings_form.language['migrate_error'] = '<?php esc_html_e('An error occurred during the import.', 'ws-form'); ?>';
			$.WS_Form.settings_form.language['dismiss'] = '<?php esc_html_e('Dismiss', 'ws-form'); ?>';
			$.WS_Form.settings_form.language['close'] = '<?php esc_html_e('Close', 'ws-form'); ?>';
			$.WS_Form.settings_form.language['cancel'] = '<?php esc_html_e('Cancel', 'ws-form'); ?>';
			$.WS_Form.settings_form.language['confirm'] = '<?php esc_html_e('Confirm', 'ws-form'); ?>';
			$.WS_Form.settings_form.language['import'] = '<?php esc_html_e('Import', 'ws-form'); ?>';

			wsf_import_button();

			// Select changes
			$('[data-action="wsf-submit"]').on('change', function(e) {

				$('#wsf_changed').val($(this).attr('data-id'));

				$('#wsf-migrate-button').prop('disabled', true);

				wsf_obj.loader_on();

				$('#wsf-migrate-form').trigger('submit');
			});

			// Delete field mapping row
			$('[data-action="wsf-field-mapping-delete"] > svg').on('click', function(e) {

				e.preventDefault();

				wsf_field_mapping_remove($(this));
			});

			// Add field mapping row
			$('[data-action="wsf-field-mapping-add"] > div').on('click', function(e) {

				e.preventDefault();

				var blank_row = $('#wsf-field-mapping-blank').clone().removeAttr('id');
				$('#wsf_field_mapping tbody').append(blank_row).each(function() {

					$('[data-action="wsf-field-mapping-delete"]', $(this)).on('click', function(e) {

						e.preventDefault();

						wsf_field_mapping_remove($(this));
					});

					$('[name^="wsf_field_mapping"]', $(this)).on('change', function() {

						wsf_field_mapping_save();
					});

					wsf_import_button();
				});
			});

			// Start migrate
			$('[data-action="wsf_migrate"]').on('click', function(e) {

				e.preventDefault();

				wsf_obj.confirm_modal(

					'<?php echo esc_js(__('Are you sure you want to start the import?', 'ws-form')); ?>',
					function() {

						wsf_obj.loader_on();

						switch($('[name="wsf_form_migrate[method]"]').val()) {

							case 'form' :

								wsf_migrate_form_import();
								break;

							case 'submissions' :

								wsf_migrate_submission_import();
								break;
						}
					},
					{
						title: wsf_obj.language('confirm'),
						confirm_label: wsf_obj.language('import')
					}
				);
			});

			// Migrate field mapping update
			$('[name^="wsf_field_mapping"]').on('change', function() {

				wsf_field_mapping_save();
			});

			// Delete row
			function wsf_field_mapping_remove(obj) {

				obj.closest('tr').remove();

				wsf_field_mapping_save();
			}

			// Determine whether the import button can be clicked
			function wsf_import_button() {

				// Check mappings
				var field_mapping_valid = false;
				var field_mapping_source_ids = $('[name="wsf_field_mapping[source][]"]').map(function() {return $(this).val();}).get();
				var field_mapping_destination_ids = $('[name="wsf_field_mapping[destination][]"]').map(function() {return $(this).val();}).get();

				for(var field_mapping_index = 0; field_mapping_index < field_mapping_source_ids.length; field_mapping_index++) {

					var field_mapping_source_id = field_mapping_source_ids[field_mapping_index];
					var field_mapping_destination_id = field_mapping_destination_ids[field_mapping_index]
					if((field_mapping_source_id != 0) && (field_mapping_destination_id != 0)) { field_mapping_valid = true; break; }
				}

				var button_enable = (<?php WS_Form_Common::echo_esc_html($ws_form_show_field_mapping ? 'true' : 'false'); ?> && field_mapping_valid) || <?php WS_Form_Common::echo_esc_html(($ws_form_wsf_form_migrate['method'] == 'form') ? 'true' : 'false'); ?>;

				var button_obj = $('#wsf-migrate-button');

				if(button_enable) {

					button_obj.prop('disabled', false);

				} else {

					button_obj.prop('disabled', true);
				}
			}

			function wsf_field_mapping_save() {

				wsf_import_button();

				wsf_obj.loader_on();

				// Build form data
				var form_data = {

					'wsf_form_migrate[id]' : $('[name="wsf_form_migrate[id]"]').val(),
					'wsf_form_migrate[form_id_source]' : $('[name="wsf_form_migrate[form_id_source]"]').val(),
					'wsf_form_migrate[form_id_destination]' : $('[name="wsf_form_migrate[form_id_destination]"]').val(),
					'wsf_field_mapping[source][]' : $('[name="wsf_field_mapping[source][]"]').map(function() {return $(this).val();}).get(),
					'wsf_field_mapping[destination][]' : $('[name="wsf_field_mapping[destination][]"]').map(function() {return $(this).val();}).get()
				};

				// Call submission import API endpoint
				wsf_obj.api_call('migrate/field-mapping', 'POST', form_data, function(response) {

					wsf_obj.loader_off();

				}, function() {

					wsf_obj.loader_off();
				});
			}

			function wsf_migrate_form_import() {

				$('#wsf-migrate-button').prop('disabled', true);
				$('#wsf-migrate-form').hide();
				$('#wsf-migrate-status-form').show();
				$('#wsf-migrate-status').show();

				// Build form data
				var form_data = {
<?php
	foreach($ws_form_settings as $ws_form_setting) {
?>
					'wsf_form_migrate[<?php WS_Form_Common::echo_esc_html($ws_form_setting); ?>]' : $('[name="wsf_form_migrate[<?php WS_Form_Common::echo_esc_html($ws_form_setting); ?>]"]').val(),
<?php
	}
?>
					'wsf_field_mapping[source][]' : $('[name="wsf_field_mapping[source][]"]').map(function() {return $(this).val();}).get()
				};

				// Call form import API endpoint
				wsf_obj.api_call('migrate/form/import', 'POST', form_data, function(response) {

					// Redirect to new form
					if(typeof(response.form_id) !== 'undefined') {

						// Build success message
						var success_message_array = [];

						success_message_array.push($.WS_Form.this.language('migrate_success_form'));
						success_message_array.push($('#wsf-migrate-form-id-source option:selected').text()  + '.');
						success_message_array.push('<a href="admin.php?page=ws-form-edit&id=' + response.form_id + '" target="_blank">' + $.WS_Form.this.language('migrate_edit_form') + '</a>');
<?php

	if($ws_form_record_count > 0) {
?>						
						success_message_array.push('<a href="admin.php?page=ws-form-migrate&wsf_form_migrate[method]=submissions&wsf_form_migrate[duplicate]=update">' + $.WS_Form.this.language('migrate_import_submissions') + '</a>');
<?php
	}
?>
						var success_message = success_message_array.join(' ');

						// Process complete
						$.WS_Form.this.message(success_message, true, 'notice-success'); 
						$('#wsf-migrate-button').prop('disabled', false);
						$('#wsf-migrate-form').show();
						$('#wsf-migrate-status').hide();
						$('#wsf-migrate-status-form').hide();
						wsf_obj.loader_off();

					} else {

						// Error
						$.WS_Form.this.message($.WS_Form.this.language('migrate_error'), true, 'notice-error'); 
						wsf_obj.loader_off();
					}

				}, function() {

					// Error
					$.WS_Form.this.message($.WS_Form.this.language('migrate_error'), true, 'notice-error'); 
					wsf_obj.loader_off();
				});
			}

			function wsf_migrate_submission_import() {

				$('#wsf-migrate-status-progress').val(0);
				$('#wsf-migrate-status-description').html($.WS_Form.this.language('migrate_status_description_default'));
				$('#wsf-migrate-button').prop('disabled', true);
				$('#wsf-migrate-form').hide();
				$('#wsf-migrate-status-submissions').show();
				$('#wsf-migrate-status').show();

				wsf_migrate_submission_import_process(0,0,0,0);
			}

			function wsf_migrate_submission_import_process(offset, created, updated, ignored) {

				// Build form data
				var form_data = {
<?php
	foreach($ws_form_settings as $ws_form_setting) {
?>
					'wsf_form_migrate[<?php WS_Form_Common::echo_esc_html($ws_form_setting); ?>]' : $('[name="wsf_form_migrate[<?php WS_Form_Common::echo_esc_html($ws_form_setting); ?>]"]').val(),
<?php
	}
?>
					'wsf_field_mapping[source][]' : $('[name="wsf_field_mapping[source][]"]').map(function() {return $(this).val();}).get(),
					'wsf_field_mapping[destination][]' : $('[name="wsf_field_mapping[destination][]"]').map(function() {return $(this).val();}).get(),
					'offset' : offset
				};

				// Call submission import API endpoint
				wsf_obj.api_call('migrate/submission/import', 'POST', form_data, function(response) {

					// Read response data
					var remaining = (typeof(response.remaining) !== 'undefined') ? response.remaining : false;
					var progress = (typeof(response.progress) !== 'undefined') ? response.progress : false;
					var offset = (typeof(response.offset) !== 'undefined') ? response.offset : 0;
					var total = (typeof(response.total) !== 'undefined') ? response.total : 0;

					created += (typeof(response.created) !== 'undefined') ? response.created : 0;
					updated += (typeof(response.updated) !== 'undefined') ? response.updated : 0;
					ignored += (typeof(response.ignored) !== 'undefined') ? response.ignored : 0;

					// Show status
					$('#wsf-migrate-status-progress').val(progress);
					$('#wsf-migrate-status-description').html(offset + ' of ' + total);

					if(remaining) {

						// Make next AJAX call
						wsf_migrate_submission_import_process(offset, created, updated, ignored);

					} else {

						// Build success message
						var success_message_array = [];
						success_message_array.push($.WS_Form.this.language('migrate_success_submissions'));
						success_message_array.push($('#wsf-migrate-form-id-source option:selected').text()  + '.');
						if(created > 0) { success_message_array.push($.WS_Form.this.language('migrate_created', created)); }
						if(updated > 0) { success_message_array.push($.WS_Form.this.language('migrate_updated', updated)); }
						if(ignored > 0) { success_message_array.push($.WS_Form.this.language('migrate_ignored', ignored)); }
						success_message_array.push('<a href="admin.php?page=ws-form-submit&id=<?php WS_Form_Common::echo_esc_attr($ws_form_wsf_form_migrate['form_id_destination']); ?>" target="_blank">' + $.WS_Form.this.language('migrate_view_submissions') + '</a>');
						var success_message = success_message_array.join(' ');

						// Process complete
						$.WS_Form.this.message(success_message, true, 'notice-success'); 
						$('#wsf-migrate-button').prop('disabled', false);
						$('#wsf-migrate-form').show();
						$('#wsf-migrate-status').hide();
						$('#wsf-migrate-status-submissions').hide();
						wsf_obj.loader_off();
					}

				}, function() {

					// Error
					$.WS_Form.this.message($.WS_Form.this.language('migrate_error'), true, 'notice-error'); 
					wsf_obj.loader_off();
				});
			}
		});

	})(jQuery);

</script>
<?php

	function ws_form_field_mapping_row($ws_form_fields_source, $ws_form_fields_destination, $ws_form_field_id_source, $ws_form_field_id_destination, $blank = false) {
?>
<tr<?php if($blank) { ?> id="wsf-field-mapping-blank"<?php } ?>>
<td>

<select name="wsf_field_mapping[source][]" class="wsf-field wsf-field-small">
<option value="0"><?php esc_html_e('Select...', 'ws-form'); ?></option>
<?php
	
		foreach($ws_form_fields_source as $ws_form_field_id => $ws_form_field_data) {

?><option value="<?php WS_Form_Common::echo_esc_attr($ws_form_field_id); ?>"<?php if(($ws_form_field_id_source !== false) && ($ws_form_field_id_source == $ws_form_field_id)) { ?> selected<?php } ?>><?php WS_Form_Common::echo_esc_html($ws_form_field_data['label']); ?> (ID: <?php WS_Form_Common::echo_esc_html($ws_form_field_id); ?>)</option>
<?php
		}
?>
</select>

</td>
<td>

<select name="wsf_field_mapping[destination][]" class="wsf-field wsf-field-small">
<option value="0"><?php esc_html_e('Select...', 'ws-form'); ?></option>
<?php
	
		foreach($ws_form_fields_destination as $ws_form_field) {

?><option value="<?php WS_Form_Common::echo_esc_attr($ws_form_field->id); ?>"<?php if(($ws_form_field_id_destination !== false) && ($ws_form_field_id_destination == $ws_form_field->id)) { ?> selected<?php } ?>><?php WS_Form_Common::echo_esc_html($ws_form_field->label); ?> (<?php WS_Form_Common::echo_esc_html($ws_form_field->id); ?>)</option>
<?php
		}
?>
</select>

</td>
<td data-icon><div data-action="wsf-field-mapping-delete" <?php WS_Form_Common::echo_esc_attr_tooltip(__('Delete', 'ws-form'), 'top-right'); ?>><?php WS_Form_Common::render_icon_16_svg('delete'); ?></div></td>
</tr>
<?php
	}
