<?php

/**
 * CSV Import Wizard for Reviews
 *
 * @link       http://ljapps.com
 * @since      1.0.0
 *
 * @package    WP_Review_Pro
 * @subpackage WP_Review_Pro/admin/partials
 */

    // check user capabilities
	if (!current_user_can('edit_pages') && $this->wprev_canuserseepage('reviews')==false) {
        return;
    }

global $wpdb;
$table_name = $wpdb->prefix . 'wpfb_reviews';
$table_forms = $wpdb->prefix . 'wpfb_getapps_forms';
$table_custom_sources = $wpdb->prefix . 'wpfb_custom_sources';

// Fetch available source pages for dropdown
$source_pages = $wpdb->get_results("SELECT id, title, site_type, reviewlistpageid, url, iscustom FROM $table_forms ORDER BY site_type, title");

// Fetch custom source icon URLs for custom pages
$custom_sources = array();
if (!empty($source_pages)) {
	$custom_types = array();
	foreach ($source_pages as $page) {
		if ($page->iscustom === 'yes' || $page->iscustom == 1 || $page->iscustom === '1') {
			if (!in_array($page->site_type, $custom_types)) {
				$custom_types[] = $page->site_type;
			}
		}
	}
	
	if (!empty($custom_types)) {
		// Build query with proper escaping
		$where_clause = array();
		foreach ($custom_types as $type) {
			$where_clause[] = $wpdb->prepare('site_name = %s', $type);
		}
		
		if (!empty($where_clause)) {
			$where_sql = implode(' OR ', $where_clause);
			$custom_sources_query = $wpdb->get_results(
				"SELECT site_name, icon_url FROM $table_custom_sources WHERE $where_sql"
			);
			
			if ($custom_sources_query) {
				foreach ($custom_sources_query as $cs) {
					$custom_sources[$cs->site_name] = $cs->icon_url;
				}
			}
		}
	}
	
	// Add icon_url to each source page
	foreach ($source_pages as $page) {
		if (isset($custom_sources[$page->site_type])) {
			$page->icon_url = $custom_sources[$page->site_type];
		} else {
			$page->icon_url = '';
		}
	}
}

?>

<div class="wrap wp_pro-settings">
<h1 class="wprev_headertextdiv"><img class="wprev_headerimg" src="<?php echo plugin_dir_url( __FILE__ ) . 'logoicon.png'; ?>"><span class="wprev_headertext">WP Review Slider Pro</span></h1>

<?php include("tabmenu.php"); ?>

<div class="w3-row">

<!-- Wizard Container -->
<div class="wprevpro_margin10 wprevpro_import_wizard_container">
	
	<!-- Progress Indicator -->
	<div class="wizard_progress">
		<div class="wizard_step active" data-step="1">
			<span class="step_number">1</span>
			<span class="step_label">Select Source</span>
		</div>
		<div class="wizard_step" data-step="2">
			<span class="step_number">2</span>
			<span class="step_label">Upload CSV</span>
		</div>
		<div class="wizard_step" data-step="3">
			<span class="step_number">3</span>
			<span class="step_label">Map Columns</span>
		</div>
		<div class="wizard_step" data-step="4">
			<span class="step_number">4</span>
			<span class="step_label">Review JSON</span>
		</div>
		<div class="wizard_step" data-step="5">
			<span class="step_number">5</span>
			<span class="step_label">Preview & Import</span>
		</div>
	</div>

	<!-- Step Content Areas -->
	<div class="wizard_content">
		
		<!-- Step 1: Select Review Source/Type -->
		<div class="wizard_step_content active" id="step_1">
			<h2><?php _e('Step 1: Select Review Source', 'wp-review-slider-pro'); ?></h2>
			<p class="description"><?php _e('Choose where these reviews are coming from. This determines how they are categorized and displayed.', 'wp-review-slider-pro'); ?></p>
			
			<table class="form-table">
				<tr>
					<th scope="row">
						<label><?php _e('Review Source Type:', 'wp-review-slider-pro'); ?></label>
					</th>
					<td>
						<label style="display:block; margin-bottom:10px;">
							<input type="radio" name="source_type" value="existing" checked> 
							<?php _e('Existing Source Page', 'wp-review-slider-pro'); ?>
						</label>
						<label style="display:block; margin-bottom:10px;">
							<input type="radio" name="source_type" value="submitted"> 
							<?php _e('Submitted Reviews', 'wp-review-slider-pro'); ?>
						</label>
					</td>
				</tr>
				<tr id="existing_source_row">
					<th scope="row">
						<label for="source_page_select"><?php _e('Select Source Page:', 'wp-review-slider-pro'); ?></label>
					</th>
					<td>
						<select id="source_page_select" name="source_page" class="regular-text">
							<option value=""><?php _e('-- Select a Source Page --', 'wp-review-slider-pro'); ?></option>
							<?php foreach($source_pages as $page): ?>
								<option value="<?php echo esc_attr($page->id); ?>" 
									data-sitetype="<?php echo esc_attr($page->site_type); ?>"
									data-reviewlistpageid="<?php echo esc_attr($page->reviewlistpageid); ?>"
									data-url="<?php echo esc_attr($page->url); ?>"
									data-title="<?php echo esc_attr($page->title); ?>"
									data-iscustom="<?php echo esc_attr($page->iscustom); ?>"
									data-icon-url="<?php echo esc_attr($page->icon_url ? $page->icon_url : ''); ?>">
									<?php echo esc_html($page->site_type . ' - ' . $page->title); ?>
								</option>
							<?php endforeach; ?>
						</select>
						<p class="description">
							<?php _e('Select an existing review source page. Reviews will be associated with this location.', 'wp-review-slider-pro'); ?>
						</p>
					</td>
				</tr>
			</table>
			
			<p class="description" style="margin-top:20px; padding:12px; background:#f0f0f0; border-left:4px solid #2271b1;">
				<strong><?php _e('Note:', 'wp-review-slider-pro'); ?></strong> 
				<?php _e('Need to add a custom review source? Visit the Get Reviews tab to add an unsupported review source site first.', 'wp-review-slider-pro'); ?>
			</p>
		</div>

		<!-- Step 2: Upload CSV File -->
		<div class="wizard_step_content" id="step_2" style="display:none;">
			<h2><?php _e('Step 2: Upload CSV File', 'wp-review-slider-pro'); ?></h2>
			<p class="description"><?php _e('Select a CSV file containing your reviews. The file will be parsed in your browser.', 'wp-review-slider-pro'); ?></p>
			
			<div style="margin:20px 0;">
				<input type="file" id="csv_file_input" accept=".csv" />
				<p class="description"><?php _e('Select a CSV file (comma-separated values)', 'wp-review-slider-pro'); ?></p>
			</div>
			
			<div style="margin:20px 0;">
				<label>
					<input type="checkbox" id="csv_has_headers" checked> 
					<?php _e('First row contains column headers', 'wp-review-slider-pro'); ?>
				</label>
			</div>
			
			<div id="csv_preview_section" style="display:none; margin-top:20px;">
				<h3><?php _e('CSV Preview (First 3 Rows)', 'wp-review-slider-pro'); ?></h3>
				<div id="csv_preview_table" style="overflow-x:auto; border:1px solid #ddd; padding:10px; background:#fff;"></div>
				<p class="description" style="margin-top:10px;">
					<?php _e('Preview shows the first few rows. Click Next to continue with column mapping.', 'wp-review-slider-pro'); ?>
				</p>
			</div>
		</div>

		<!-- Step 3: Map CSV Columns -->
		<div class="wizard_step_content" id="step_3" style="display:none;">
			<h2><?php _e('Step 3: Map CSV Columns to Review Fields', 'wp-review-slider-pro'); ?></h2>
			<p class="description"><?php _e('Match each CSV column to the appropriate review field. Required fields are marked with *.', 'wp-review-slider-pro'); ?></p>
			
			<div id="column_mapping_container" style="margin-top:20px;">
				<!-- This will be populated dynamically -->
			</div>
		</div>

		<!-- Step 4: Special Fields -->
		<div class="wizard_step_content" id="step_4" style="display:none;">
			<h2><?php _e('Step 4: Review JSON Fields', 'wp-review-slider-pro'); ?></h2>
			<p class="description"><?php _e('Review your JSON field mappings. The wizard will automatically convert comma-separated values to the proper JSON format.', 'wp-review-slider-pro'); ?></p>
			
			<div id="special_fields_container" style="margin-top:20px;">
				<!-- This will be populated dynamically -->
			</div>
		</div>

		<!-- Step 5: Preview & Validate -->
		<div class="wizard_step_content" id="step_5" style="display:none;">
			<h2><?php _e('Step 5: Preview & Import', 'wp-review-slider-pro'); ?></h2>
			<p class="description"><?php _e('Review the data below and fix any errors before importing.', 'wp-review-slider-pro'); ?></p>
			
			<div id="validation_errors" style="margin:20px 0;"></div>
			<div id="import_summary" style="margin:20px 0; padding:15px; background:#f0f0f0; border-left:4px solid #2271b1;">
				<!-- Summary stats will be populated here -->
			</div>
			<div id="preview_table_container" style="margin-top:20px; overflow-x:auto;">
				<!-- Preview table will be populated here -->
			</div>
		</div>

	</div>

	<!-- Navigation Buttons -->
	<div class="wizard_navigation">
		<button type="button" id="wizard_btn_back" class="button" style="display:none;">
			<span class="dashicons dashicons-arrow-left-alt2" style="margin-top:4px;"></span>
			<?php _e('Back', 'wp-review-slider-pro'); ?>
		</button>
		<button type="button" id="wizard_btn_next" class="button button-primary">
			<?php _e('Next', 'wp-review-slider-pro'); ?>
			<span class="dashicons dashicons-arrow-right-alt2" style="margin-top:4px;"></span>
		</button>
		<button type="button" id="wizard_btn_import" class="button button-primary" style="display:none;">
			<span class="dashicons dashicons-upload" style="margin-top:4px;"></span>
			<?php _e('Import Reviews', 'wp-review-slider-pro'); ?>
		</button>
		<a href="<?php echo admin_url('admin.php?page=wp_pro-reviews'); ?>" class="button">
			<?php _e('Cancel', 'wp-review-slider-pro'); ?>
		</a>
		<span id="wizard_loading" style="display:none; margin-left:15px;">
			<img src="<?php echo WPREV_PLUGIN_URL; ?>/public/partials/imgs/loading_ripple.gif" style="width:24px; height:24px; vertical-align:middle;">
			<span id="wizard_loading_text"><?php _e('Processing...', 'wp-review-slider-pro'); ?></span>
		</span>
	</div>

</div>

</div>
</div>

<style>
.wprevpro_import_wizard_container {
	background: #fff;
	padding: 20px;
	border: 1px solid #ccc;
	box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.wizard_progress {
	display: flex;
	justify-content: space-between;
	margin-bottom: 30px;
	position: relative;
}

.wizard_progress::before {
	content: '';
	position: absolute;
	top: 20px;
	left: 0;
	right: 0;
	height: 2px;
	background: #ddd;
	z-index: 0;
}

.wizard_step {
	display: flex;
	flex-direction: column;
	align-items: center;
	position: relative;
	z-index: 1;
	flex: 1;
}

.step_number {
	width: 40px;
	height: 40px;
	border-radius: 50%;
	background: #ddd;
	color: #666;
	display: flex;
	align-items: center;
	justify-content: center;
	font-weight: bold;
	margin-bottom: 8px;
	transition: all 0.3s;
}

.wizard_step.active .step_number {
	background: #2271b1;
	color: #fff;
}

.wizard_step.completed .step_number {
	background: #46b450;
	color: #fff;
}

.step_label {
	font-size: 12px;
	text-align: center;
	color: #666;
}

.wizard_step.active .step_label {
	color: #2271b1;
	font-weight: bold;
}

.wizard_content {
	min-height: 400px;
	margin-bottom: 20px;
}

.wizard_navigation {
	padding-top: 20px;
	border-top: 1px solid #ddd;
	display: flex;
	gap: 10px;
	align-items: center;
}

.wizard_step_content h2 {
	margin-top: 0;
	padding-bottom: 10px;
	border-bottom: 2px solid #2271b1;
}

#column_mapping_container table {
	width: 100%;
	border-collapse: collapse;
}

#column_mapping_container th,
#column_mapping_container td {
	padding: 12px;
	text-align: left;
	border-bottom: 1px solid #ddd;
}

#column_mapping_container th {
	background: #f0f0f0;
	font-weight: bold;
}

#column_mapping_container select {
	width: 100%;
	/*max-width: 300px;*/
}

.field_required {
	color: #d63638;
	font-weight: bold;
}

.field_preview {
	font-family: monospace;
	font-size: 12px;
	color: #666;
	background: #f9f9f9;
	padding: 4px 8px;
	border-radius: 3px;
	max-width: 200px;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.error_message {
	background: #fef5f5;
	border-left: 4px solid #d63638;
	padding: 12px;
	margin-bottom: 15px;
}

.warning_message {
	background: #fffbf0;
	border-left: 4px solid #dba617;
	padding: 12px;
	margin-bottom: 15px;
}

.success_message {
	background: #f0f6f0;
	border-left: 4px solid #46b450;
	padding: 12px;
	margin-bottom: 15px;
}
</style>

<?php
// Enqueue the import wizard JavaScript
wp_enqueue_script('wprevpro_review_list_import-js', plugin_dir_url(dirname(__FILE__)) . 'js/wprevpro_review_list_import.js', array('jquery'), $this->version, false);
wp_localize_script('wprevpro_review_list_import-js', 'wprevpro_import_vars', array(
	'ajax_url' => admin_url('admin-ajax.php'),
	'nonce' => wp_create_nonce('wprevpro_import_wizard'),
	'strings' => array(
		'select_source' => __('Please select a source page', 'wp-review-slider-pro'),
		'select_file' => __('Please select a CSV file', 'wp-review-slider-pro'),
		'parsing_file' => __('Parsing CSV file...', 'wp-review-slider-pro'),
		'mapping_columns' => __('Setting up column mapping...', 'wp-review-slider-pro'),
		'validating' => __('Validating data...', 'wp-review-slider-pro'),
		'importing' => __('Importing reviews...', 'wp-review-slider-pro'),
		'import_complete' => __('Import complete!', 'wp-review-slider-pro'),
		'import_error' => __('Import failed. Please try again.', 'wp-review-slider-pro'),
		'skip_field' => __('-- Skip this field --', 'wp-review-slider-pro'),
	)
));
?>
