<?php
	// If this file is called directly, abort.
	if (!defined('WPINC')) {
		die;
	}

	$current_page = $_GET['page'] ?? '';

	$options = get_option('athena_options', []);
	$has_valid_config = (
		!empty($options['athena_dashboard_url']) &&
		!empty($options['dashboard_website_token']) &&
		!empty($options['client_id']) &&
		!empty($options['client_secret']) &&
		!empty($options['athena_landing_page'])
	);

	$reindex_in_progress = athena_is_reindex_in_progress();
	$should_disable_reindex = !$has_valid_config || $reindex_in_progress;
?>

<div class="athena-admin-header">
	<div class="left-side">
		<img src="<?php echo ASWP_PLUGIN_URL . 'public/img/athena-logo.svg'; ?>" alt="Athena Logo">
	</div>

	<div class="right-side">
		<h1>Athena Omnispect Search</h1>
		<p class="header-subtitle">
			Search plugin for boosting eCommerce sales & conversions
		</p>
		<p>by <a href="https://a-x.ai" target="_blank">Athena XAI</a></p>
	</div>
</div>

<div id="athena-reindex-in-progress-msg" class="reindex-is-running-message-wrp" style="display:<?php echo athena_is_reindex_in_progress() ? 'block' : 'none'; ?>">
	<div class="reindex-status-header">
		<div class="warning-sign-wrp">
			<img src="<?php echo ASWP_PLUGIN_URL . 'public/img/warning-sign.svg'; ?>" alt="Warning Sign">
		</div>
		<p>Reindex is running. Please wait.</p>
	</div>
</div>

<div class="athena-reindex-progress-bar" style="display:<?php echo $reindex_in_progress ? 'block' : 'none'; ?>">
	<?php
	$reindex_status = get_option('athena_reindex_status', []);
	$current_percentage = isset($reindex_status['progress_percentage']) ? (int) $reindex_status['progress_percentage'] : 0;
	?>
	<div class="athena-progress-bar-container">
		<div id="athena-progress-bar-fill" class="athena-progress-bar-fill" style="width: <?php echo $current_percentage; ?>%;"></div>
	</div>
	<div class="progress-bar-bottom">
		<a href="#" id="athena-stop-reindex-btn" class="athena-stop-reindex-btn">Stop Reindex</a>
		<p id="athena-progress-text" class="athena-progress-text"><?php echo $current_percentage; ?>% Complete</p>
	</div>
</div>

<div id="athena-log-modal" class="athena-modal hidden">
	<div class="athena-modal-content">
		<div class="athena-modal-header">
			<p class="modal-title">Reindex Logs</p>
			<span class="athena-close-modal">&times;</span>
		</div>
		<div class="athena-modal-body">
			<pre id="athena-log-viewer-content"></pre>
		</div>
	</div>
</div>

<div class="athena-reindex-response hidden"><p></p></div>

<div class="reindex-and-show-logs-wrp">
	<!-- Full Reindex Button -->
	<form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" id="athena-full-reindex-form">
		<input type="hidden" name="action" value="athena_full_reindex_action">
		<?php wp_nonce_field('athena_full_reindex', 'athena_full_reindex_nonce'); ?>
		<input type="submit" id="athena-full-reindex-btn" class="full-reindex-btn" value="Full Reindex" <?php echo $should_disable_reindex ? 'disabled' : ''; ?>>
	</form>
	<!-- END Full Reindex Button -->

	<!-- Show Reindex Logs Button -->
	<button type="button" id="athena-show-logs-btn" class="purple-btn">Show Reindex Logs</button>
	<!-- END Show Reindex Logs Button -->
</div>

<div class="athena-admin-menu-wrp">
	<div class="athena-admin-menu">
		<a href="<?php echo esc_url(admin_url('admin.php?page=athena_about_us_slug')); ?>" class="<?php echo $current_page === 'athena_about_us_slug' ? 'active-page' : ''; ?>">About</a>
		<a href="<?php echo esc_url(admin_url('admin.php?page=athena_options_slug')); ?>" class="<?php echo $current_page === 'athena_options_slug' ? 'active-page' : ''; ?>">Configuration</a>
		<a href="<?php echo esc_url(admin_url('admin.php?page=athena_help_and_support_slug')); ?>" class="<?php echo $current_page === 'athena_help_and_support_slug' ? 'active-page' : ''; ?>">Help & Support</a>
	</div>
</div>