<?php
/**
 * Admin Menu Registration and Settings Page
 *
 * @package MindWebConcierge
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

add_action( 'admin_menu', 'mindweco_register_admin_menu' );

function mindweco_register_admin_menu() {

	add_menu_page(
		esc_html__( 'Mind Web', 'mind-web-concierge' ),
		esc_html__( 'Mind Web', 'mind-web-concierge' ),
		'manage_options',
		'mind-web',
		'mindweco_settings_page',
		'dashicons-list-view',
		80
	);

	add_submenu_page(
		'mind-web',
		esc_html__( 'Mind Web Settings', 'mind-web-concierge' ),
		esc_html__( 'Mind Web Settings', 'mind-web-concierge' ),
		'manage_options',
		'mind-web',
		'mindweco_settings_page'
	);

	add_submenu_page(
		'mind-web',
		esc_html__( 'Mind Web Files', 'mind-web-concierge' ),
		esc_html__( 'Mind Web Files', 'mind-web-concierge' ),
		'manage_options',
		'mind-web-files',
		'mindweco_files_page'
	);

	add_submenu_page(
		'mind-web',
		esc_html__( 'Mind Web Model & Usage', 'mind-web-concierge' ),
		esc_html__( 'Model & Usage', 'mind-web-concierge' ),
		'manage_options',
		'mind-web-model-usage',
		'mindweco_model_usage_page'
	);

	add_submenu_page(
		'mind-web',
		esc_html__( 'Subscriptions', 'mind-web-concierge' ),
		esc_html__( 'Subscriptions', 'mind-web-concierge' ),
		'manage_options',
		'mind-web-subscriptions',
		'mindweco_render_stripe_settings'
	);

	add_submenu_page(
		'mind-web',
		esc_html__( 'Mind Spaces', 'mind-web-concierge' ),
		esc_html__( 'Mind Spaces', 'mind-web-concierge' ),
		'manage_options',
		'mind-web-spaces',
		'mindweco_knowledge_repository_page'
	);

	add_submenu_page(
		'mind-web',
		esc_html__( 'Mind Web Conversations', 'mind-web-concierge' ),
		esc_html__( 'Mind Web Conversations', 'mind-web-concierge' ),
		'manage_options',
		'mind-web-conversations',
		'mindweco_conversations_page'
	);

	if ( class_exists( 'WooCommerce' ) ) {
		add_submenu_page(
			'mind-web',
			esc_html__( 'Product Catalog', 'mind-web-concierge' ),
			esc_html__( 'Product Catalog', 'mind-web-concierge' ),
			'manage_options',
			'mind-web-product-catalog',
			'mindweco_product_catalog_page'
		);
	}
}

add_action( 'admin_enqueue_scripts', 'mindweco_admin_enqueue_scripts' );

function mindweco_admin_enqueue_scripts( $hook_suffix ) {

	if (
		'toplevel_page_mind-web' !== $hook_suffix &&
		strpos( $hook_suffix, 'mind-web_page_' ) !== 0
	) {
		return;
	}

	$plugin_url = plugin_dir_url( __DIR__ );
	$ver_css    = '3.8.4';
	$ver        = defined( 'MIND_WEB_VERSION' ) ? MIND_WEB_VERSION : '1.2.9';

	wp_enqueue_style(
		'mindweco-item-style',
		$plugin_url . 'assets/css/mindweco_item_settings.css',
		array(),
		$ver_css
	);

	wp_enqueue_style(
		'mindweco-driver-css',
		$plugin_url . 'assets/css/driver.css',
		array(),
		'1.4.0'
	);

	wp_enqueue_script(
		'mindweco-driver-js',
		$plugin_url . 'assets/js/driver.js.iife.js',
		array(),  
		'1.4.0',
		true      
	);

	wp_enqueue_script(
		'mindweco-tour-js',
		$plugin_url . 'assets/js/mindweco-tour.js',
		array( 'jquery', 'mindweco-driver-js' ), 
		$ver,                                  
		true
	);

	wp_enqueue_style(
		'mindweco-tour-style',
		$plugin_url . 'assets/css/mindweco-tour.css',
		array(),
		$ver
	);

	wp_enqueue_style(
		'mindweco-bootstrap-css',
		$plugin_url . 'assets/css/bootstrap.min.css',
		array(),
		'5.3.8'
	);

	wp_enqueue_style(
		'mindweco-font-awesome',
		$plugin_url . 'assets/css/all.min.css',
		array(),
		'6.4.2'
	);

	wp_enqueue_script(
		'mindweco-bootstrap-js',
		$plugin_url . 'assets/js/bootstrap.bundle.min.js',
		array( 'jquery' ),
		'5.3.8',
		true
	);

	wp_enqueue_script(
		'mindweco-modal',
		$plugin_url . 'assets/js/components/Modal.js',
		array( 'jquery' ),
		$ver,
		true
	);

	wp_enqueue_script(
		'mindweco-toast',
		$plugin_url . 'assets/js/components/Toast.js',
		array( 'jquery' ),
		$ver,
		true
	);

	wp_enqueue_script(
		'mindweco-index-js',
		$plugin_url . 'assets/js/mindweco_index_item.js',
		array( 'jquery', 'mindweco-modal', 'mindweco-toast' ),
		$ver,
		true
	);

	wp_enqueue_style(
		'mindweco-bootstrap-icons',
		$plugin_url . 'assets/css/bootstrap-icons.min.css',
		array(),
		'1.11.1'
	);

	wp_localize_script(
		'mindweco-index-js',
		'mindwecoPlugin',
		array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'nonce'    => wp_create_nonce( 'mindweco_login_nonce' ),
		)
	);
}


add_action( 'admin_footer', 'mindweco_admin_footer' );

function mindweco_admin_footer() {

	$screen = get_current_screen();

	if ( ! $screen || strpos( $screen->id, 'mind-web' ) === false ) {
		return;
	}

	?>
	<div class="help-container">
		<button id="start-tour-btn">
			<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"
				fill="none" stroke="currentColor" stroke-width="2"
				stroke-linecap="round" stroke-linejoin="round"
				aria-hidden="true" focusable="false">
				<circle cx="12" cy="12" r="10"></circle>
				<path d="M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3"></path>
				<path d="M12 17h.01"></path>
			</svg>
			<span class="tour-need-help">
				<?php esc_html_e( 'Need help?', 'mind-web-concierge' ); ?>
			</span>
		</button>
	</div>
	<?php
}


function mindweco_settings_page() {

	if (
		isset( $_SERVER['REQUEST_METHOD'] ) &&
		'POST' === sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) &&
		isset( $_POST['mindweco_save_settings'] )
	) {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have permission to do this.', 'mind-web-concierge' ) );
		}

		if (
			! isset( $_POST['mindweco_settings_nonce'] ) ||
			! wp_verify_nonce(
				sanitize_text_field( wp_unslash( $_POST['mindweco_settings_nonce'] ) ),
				'mindweco_save_settings_action'
			)
		) {
			wp_die( esc_html__( 'Security check failed.', 'mind-web-concierge' ) );
		}

		$services_count = isset($_POST['mindweco_services_count'])
			? max(0, absint($_POST['mindweco_services_count']))
			: 0;

		$service_titles = isset( $_POST['mindweco_service_title'] ) && is_array( $_POST['mindweco_service_title'] )
			? wp_unslash( $_POST['mindweco_service_title'] )
			: array();

		$service_descriptions = isset( $_POST['mindweco_service_description'] ) && is_array( $_POST['mindweco_service_description'] )
			? wp_unslash( $_POST['mindweco_service_description'] )
			: array();

		$service_urls = isset( $_POST['mindweco_service_url'] ) && is_array( $_POST['mindweco_service_url'] )
			? wp_unslash( $_POST['mindweco_service_url'] )
			: array();

		$service_links = array();
		$service_errors = array();

		for ( $i = 0; $i < $services_count; $i++ ) {
			$title       = isset( $service_titles[ $i ] ) ? sanitize_text_field( $service_titles[ $i ] ) : '';
			$description = isset( $service_descriptions[ $i ] ) ? sanitize_textarea_field( $service_descriptions[ $i ] ) : '';
			$url         = isset( $service_urls[ $i ] ) ? esc_url_raw( $service_urls[ $i ] ) : '';

			if ( empty( $title ) && empty( $url ) ) {
				$service_errors[] = sprintf(
					/* translators: %d: service row number */
					__( 'Service %d must include both a title and a URL.', 'mind-web-concierge' ),
					$i + 1
				);
				continue;
			}

			if ( empty( $title ) || empty( $url ) ) {
				$service_errors[] = sprintf(
					/* translators: %d: service row number */
					__( 'Service %d must include both a title and a URL.', 'mind-web-concierge' ),
					$i + 1
				);
				continue;
			}

			$service_links[] = array(
				'title'       => $title,
				'description' => $description,
				'url'         => $url,
			);
		}

		if ( empty( $service_errors ) ) {
			update_option(
				'mindweco_ask_setting',
				isset( $_POST['mindweco_ask_setting'] ) ? absint( $_POST['mindweco_ask_setting'] ) : 6
			);

			update_option(
				'mindweco_chunk_size',
				isset( $_POST['mindweco_chunk_size'] ) ? absint( $_POST['mindweco_chunk_size'] ) : 300
			);

			update_option(
				'mindweco_chunk_overlap',
				isset( $_POST['mindweco_chunk_overlap'] ) ? absint( $_POST['mindweco_chunk_overlap'] ) : 60
			);

			$headers = isset( $_POST['mindweco_header_levels'] )
				? sanitize_text_field( wp_unslash( $_POST['mindweco_header_levels'] ) )
				: 'h2,h3,h4';

			update_option( 'mindweco_header_levels', $headers );
			update_option( 'mindweco_services_count', $services_count );
			update_option( 'mindweco_service_links', $service_links );
			$iframe_css = isset( $_POST['mindweco_iframe_css'] )
				? sanitize_textarea_field( wp_unslash( $_POST['mindweco_iframe_css'] ) )
				: '';
			update_option(
				'mindweco_iframe_css',
				$iframe_css
			);
			update_option(
				'mindweco_custom_css',
				$iframe_css
			);
			update_option(
				'mindweco_calendly_url',
				! empty( $service_links[0]['url'] ) ? $service_links[0]['url'] : ''
			);

			update_option(
				'mindweco_show_powered_by',
				isset( $_POST['mindweco_show_powered_by'] ) ? 1 : 0
			);

			echo '<div class="notice notice-success is-dismissible d-none"><p>'
				. '<span class="dashicons dashicons-yes-alt" style="vertical-align:middle;margin-right:4px;"></span>'
				. esc_html__( 'Settings saved successfully!', 'mind-web-concierge' )
				. '</p></div>';

			$cat_id   = isset( $_POST['mindweco_category_id'] )   ? absint( $_POST['mindweco_category_id'] ) : 0;
			$cat_name = isset( $_POST['mindweco_category_name'] ) ? sanitize_text_field( wp_unslash( $_POST['mindweco_category_name'] ) ) : '';
			
			if ( $cat_id && $cat_name ) {
				update_option(
					'mindweco_plugin_category',
					array(
						'id'   => $cat_id,
						'name' => $cat_name,
					)
				);
			}
		} else {
			echo '<div class="notice notice-error"><p>'
				. esc_html( implode( ' ', $service_errors ) )
				. '</p></div>';
		}
	}

	$settings = array(
		'chunk_size'    => get_option( 'mindweco_chunk_size', 300 ),
		'chunk_overlap' => get_option( 'mindweco_chunk_overlap', 60 ),
		'header_levels' => get_option( 'mindweco_header_levels', 'h2,h3,h4' ),
		'topk'          => get_option( 'mindweco_ask_setting', 6 ),
		'show_powered_by' => get_option( 'mindweco_show_powered_by', 0 ),
		'calendly_url'    => get_option( 'mindweco_calendly_url', '' ),       
		'services_count'  => (int) get_option( 'mindweco_services_count', 0 ),
		'service_links'   => get_option( 'mindweco_service_links', array() ),
		'iframe_css'      => get_option( 'mindweco_iframe_css', get_option( 'mindweco_custom_css', '' ) ),
	);

	if ( ! is_array( $settings['service_links'] ) ) {
		$settings['service_links'] = array();
	}

	if ( $settings['services_count'] > 0 && empty( $settings['service_links'] ) && ! empty( $settings['calendly_url'] ) ) {
		$settings['service_links'] = array(
			array(
				'title'       => '',
				'description' => '',
				'url'         => $settings['calendly_url'],
			),
		);
	}

	if ( $settings['services_count'] <= 0 ) {
		$settings['services_count'] = count( $settings['service_links'] );
	}

	$indexing_stats = function_exists( 'mindweco_get_indexing_dashboard_stats' )
		? mindweco_get_indexing_dashboard_stats()
		: array(
			'total'    => 0,
			'indexed'  => 0,
			'to_index' => 0,
			'progress' => 0,
		);
	$total          = (int) $indexing_stats['total'];
	$indexed_count  = (int) $indexing_stats['indexed'];
	$current_indexing = (int) $indexing_stats['to_index'];

	$category        = get_option( 'mindweco_plugin_category', array( 'id' => '', 'name' => '' ) );
	$mindweco_options = get_option( 'mindweco_plugin_options', array() );
	$base_url        = isset( $mindweco_options['base_url'] ) ? $mindweco_options['base_url'] : '';
	$username        = isset( $mindweco_options['username'] ) ? $mindweco_options['username'] : '';

	$indexing_options = get_option( 'mindweco_plugin_indexing', array() );

	if ( is_string( $indexing_options ) ) {
		$indexing_options = maybe_unserialize( $indexing_options );
	}

	if ( ! is_array( $indexing_options ) ) {
		$indexing_options = array();
	}

	$indexing_id    = isset( $indexing_options['id'] )    ? $indexing_options['id']    : null;
	$indexing_title = isset( $indexing_options['title'] ) ? $indexing_options['title'] : '';

	?>
	<div id="mind-web-settings-wrapper">
		<div class="wrap">

			<div class="mindweco-card">
				<h4 class="item-settings-title">
					<i class="bi bi-sliders" aria-hidden="true"></i>
					<?php esc_html_e( 'Indexed Item Settings', 'mind-web-concierge' ); ?>
				</h4>
				<div class="toast-container position-fixed top-2 end-0 p-3">
					<div id="subscription-toast" class="toast text-bg-primary border-0" role="alert" aria-live="assertive" aria-atomic="true">
						<div class="d-flex">
							<div id="subscription-toast-body" class="toast-body"></div>
							<button type="button" class="btn-close me-2 m-auto" data-bs-dismiss="toast" aria-label="<?php esc_attr_e( 'Close', 'mind-web-concierge' ); ?>"></button>
						</div>
					</div>
				</div>
			</div>

			<h2 class="nav-tab-wrapper mindweco-nav-tabs">
				<a href="#settings" class="nav-tab nav-tab-active mindweco-nav-tab" data-tab="settings">
					<i class="bi bi-sliders" aria-hidden="true"></i>
					<?php esc_html_e( 'General', 'mind-web-concierge' ); ?>
				</a>
				<a href="#selection" class="nav-tab mindweco-nav-tab" data-tab="selection">
					<i class="bi bi-ui-checks me-1" aria-hidden="true"></i>
					<?php esc_html_e( 'Exclude Indexing', 'mind-web-concierge' ); ?>
				</a>
				<a href="#indexing" class="nav-tab mindweco-nav-tab" data-tab="indexing">
					<i class="bi bi-lightning-charge-fill" aria-hidden="true"></i>
					<?php esc_html_e( 'Indexing', 'mind-web-concierge' ); ?>
				</a>
				<a href="#manage" class="nav-tab mindweco-nav-tab" data-tab="manage">
					<i class="bi bi-list-check" aria-hidden="true"></i>
					<?php esc_html_e( 'Manage Items', 'mind-web-concierge' ); ?>
				</a>
				<a href="#technical_options" class="nav-tab mindweco-nav-tab" data-tab="technical_options">
					<i class="bi bi-gear" aria-hidden="true"></i>
					<?php esc_html_e( 'Technical Options', 'mind-web-concierge' ); ?>
				</a>
			</h2>

			<div id="tab-settings" class="tab-content" style="display:block;">
				<div class="bg-white p-4 rounded shadow-sm">
					<div class="general-info">
						<h1 class="custom-heading fw-semibold">
							<i class="bi bi-wrench" aria-hidden="mr-2" ></i>
							<?php esc_html_e( 'Indexing Configuration', 'mind-web-concierge' ); ?>
						</h1>
						<p class="text-secondary mt-2 fw-bold m-0">
							<?php esc_html_e( 'Control how your content and services are structured and used to provide better answers and interactions.', 'mind-web-concierge' ); ?>
						</p>
					</div>

					<form method="post">
						<?php wp_nonce_field( 'mindweco_save_settings_action', 'mindweco_settings_nonce' ); ?>

						<div class="mindweco-settings-card" id="mindweco-indexing-chat-settings-card">
							<h2><?php esc_html_e( 'Indexing & Chat Settings', 'mind-web-concierge' ); ?></h2>
							<p class="mindweco-settings-card-description">
								<i class="dashicons dashicons-info" aria-hidden="true"></i>
								<?php esc_html_e( 'Set how your content is organized and how the chat assistant uses it to give clear and helpful answers.', 'mind-web-concierge' ); ?>
							</p>
							<table class="form-table indexing-confing" >
								<tr>
								<th scope="row" class="row-inputs-element">
									<label for="mindweco-chunk-size">
										<i class="bi bi-arrows-expand me-2 indexing-label" aria-hidden="true"></i>
										<?php esc_html_e( 'Chunk Size', 'mind-web-concierge' ); ?>
									</label>
								</th>
								<td class="settings-field">
									<input
										type="number"
										name="mindweco_chunk_size"
										id="mindweco-chunk-size"
										value="<?php echo esc_attr( $settings['chunk_size'] ); ?>"
										class="settings-field-input"
										min="1"
									/>
									<p class="description">
										<i class="dashicons dashicons-info" aria-hidden="true"></i>
										<?php esc_html_e( 'This setting decides how much text is grouped together at one time when the system processes content.', 'mind-web-concierge' ); ?>
										<br>
										<?php esc_html_e( ' The default (300 characters) keeps things small so the assistant can understand and find information more accurately.', 'mind-web-concierge' ); ?>									
									</p>
								</td>
							</tr>
							<tr>
								<th scope="row" class="row-inputs-element">
									<label for="mindweco-chunk-overlap">
										<i class="bi bi-layers me-2 indexing-label" aria-hidden="true"></i>
										<?php esc_html_e( 'Chunk Overlap', 'mind-web-concierge' ); ?>
									</label>
								</th>
								<td class="settings-field">
									<input
										type="number"
										name="mindweco_chunk_overlap"
										id="mindweco-chunk-overlap"
										value="<?php echo esc_attr( $settings['chunk_overlap'] ); ?>"
										class="settings-field-input"
										min="0"
									/>
									<p class="description">
										<i class="dashicons dashicons-info" aria-hidden="true"></i>
										<?php esc_html_e( 'This setting controls how many characters are shared between two consecutive chunks of text.', 'mind-web-concierge' ); ?>
										<br>
										<?php esc_html_e( 'The overlap helps the system avoid missing important information that might be split between chunks. Default: 60 characters.', 'mind-web-concierge' ); ?>
									</p>
								</td>
							</tr>
							<tr>
								<th scope="row" class="row-inputs-element">
									<label for="mindweco-header-levels">
										<i class="bi bi-type me-2 indexing-label" aria-hidden="true"></i>
										<?php esc_html_e( 'Header Levels', 'mind-web-concierge' ); ?>
									</label>
								</th>
								<td class="settings-field">
									<input
										type="text"
										name="mindweco_header_levels"
										id="mindweco-header-levels"
										value="<?php echo esc_attr( $settings['header_levels'] ); ?>"
										class="settings-field-input"
									/>
									<p class="description">
										<i class="dashicons dashicons-info" aria-hidden="true"></i>
										<?php esc_html_e( 'Enter heading levels separated by commas that should be included when processing content (e.g. h2, h3, h4).', 'mind-web-concierge' ); ?>
										<br>
										<?php esc_html_e( 'This helps the system know which sections of your content to focus on.', 'mind-web-concierge' ); ?>
									</p>
								</td>
							</tr>
							<tr>
								<th scope="row" class="row-inputs-element">
									<label for="mindweco_ask_setting">
										<i class="bi bi-gear me-2 indexing-label" aria-hidden="true"></i>
										<?php esc_html_e( 'Ask Configuration', 'mind-web-concierge' ); ?>
									</label>
								</th>
								<td class="settings-field">
									<div class="mindweco-range-control">
										<input
											type="range"
											id="mindweco_ask_setting"
											name="mindweco_ask_setting"
											value="<?php echo esc_attr( $settings['topk'] ); ?>"
											min="1"
											max="20"
											step="1"
										/>
										<span class="mindweco-range-value" id="mindweco_ask_setting_value">
											<?php echo esc_html( $settings['topk'] ); ?>
										</span>
									</div>
									<p class="description">
										<i class="dashicons dashicons-info" aria-hidden="true"></i>
										<?php esc_html_e( 'This setting controls how many of the most relevant pieces of information the Mind Web assistant will look at before answering a question.', 'mind-web-concierge' ); ?>
										<br>
										<?php esc_html_e( 'It helps the assistant decide how much context to use — a smaller number keeps answers more focused and direct, while a larger number allows the assistant to consider more information for a more detailed response.', 'mind-web-concierge' ); ?>
										<br>
										<?php esc_html_e( 'Default: 6.', 'mind-web-concierge' ); ?>
									</p>
								</td>
							</tr>
							<tr>
								<th scope="row" class="row-inputs-element">
									<span>
										<i class="bi bi-link-45deg me-2 indexing-label" aria-hidden="true"></i>
										<?php esc_html_e( 'Powered By Link', 'mind-web-concierge' ); ?>
									</span>
								</th>
								<td class="settings-field">
									<label class="mindweco-powered-by-toggle">
										<input
											type="checkbox"
											name="mindweco_show_powered_by"
											id="mindweco_show_powered_by"
											value="1"
											<?php checked( 1, $settings['show_powered_by'] ); ?>
										/>
										<span><?php esc_html_e( 'Show "Powered by SmartMind.it" in chat window', 'mind-web-concierge' ); ?></span>
									</label>
									<p class="description">
										<i class="dashicons dashicons-info" aria-hidden="true"></i>
										<?php esc_html_e( 'Display attribution link at the bottom of the chat.', 'mind-web-concierge' ); ?>
									</p>
								</td>
							</tr>
							</table>
						</div>

						<div class="mindweco-settings-card" id="mindweco-services-options-card">
							<div class="mindweco-simple-section-row">
								<h2><?php esc_html_e( 'Services & Options', 'mind-web-concierge' ); ?></h2>
								<p>
									<i class="dashicons dashicons-info" aria-hidden="true"></i>
									<?php esc_html_e( 'Add services, booking pages, forms, or helpful links that visitors can access directly from the chat widget. This makes it easier for visitors to quickly find and open the resources they need.', 'mind-web-concierge' ); ?>
								</p>
							</div>
							<table class="form-table indexing-confing" >
								<tr>
									<th scope="row" class="row-inputs-element">
										<label for="mindweco_services_count">
											<i class="bi bi-grid-3x3-gap me-2 indexing-label" aria-hidden="true"></i>
											<?php esc_html_e( 'Number of Services', 'mind-web-concierge' ); ?>
										</label>
									</th>
									<td class="settings-field">
										<input
											type="number"
											name="mindweco_services_count"
											id="mindweco_services_count"
											value="<?php echo esc_attr( $settings['services_count'] ); ?>"
											class="settings-field-input"
											min="0"
										/>
										<p class="description">
											<i class="dashicons dashicons-info" aria-hidden="true"></i>
											<?php esc_html_e( 'Select how many services or options you want visitors to see in the chat widget.', 'mind-web-concierge' ); ?>
										</p>
										<div id="mindweco-services-repeater" class="mindweco-services-repeater"
											data-services="<?php echo esc_attr( wp_json_encode( $settings['service_links'] ) ); ?>">
											<?php for ( $i = 0; $i < $settings['services_count']; $i++ ) : ?>
												<?php
												$service_title = isset( $settings['service_links'][ $i ]['title'] ) ? $settings['service_links'][ $i ]['title'] : '';
												$service_description = isset( $settings['service_links'][ $i ]['description'] ) ? $settings['service_links'][ $i ]['description'] : '';
												$service_url   = isset( $settings['service_links'][ $i ]['url'] ) ? $settings['service_links'][ $i ]['url'] : '';
												?>
												<div class="mindweco-service-row">
													<div class="mindweco-service-row-head">
														<strong><?php echo esc_html( sprintf( __( 'Service %d', 'mind-web-concierge' ), $i + 1 ) ); ?></strong>
														<button
															type="button"
															class="button-link-delete mindweco-service-delete-row"
															aria-label="<?php echo esc_attr( sprintf( __( 'Delete service %d', 'mind-web-concierge' ), $i + 1 ) ); ?>"
															title="<?php echo esc_attr( sprintf( __( 'Delete service %d', 'mind-web-concierge' ), $i + 1 ) ); ?>"
														>
															<span class="dashicons dashicons-trash" aria-hidden="true"></span>
															<span><?php esc_html_e( 'Delete', 'mind-web-concierge' ); ?></span>
														</button>
													</div>
													<input
														type="text"
														name="mindweco_service_title[]"
														value="<?php echo esc_attr( $service_title ); ?>"
														class="settings-field-input mindweco-service-title-input"
														placeholder="<?php esc_attr_e( 'Service title', 'mind-web-concierge' ); ?>"
													/>
													<input
														type="url"
														name="mindweco_service_url[]"
														value="<?php echo esc_attr( $service_url ); ?>"
														class="settings-field-input mindweco-service-url-input"
														placeholder="https://example.com/service"
													/>
													<textarea
														name="mindweco_service_description[]"
														class="settings-field-input mindweco-service-description-input"
														rows="2"
														placeholder="<?php esc_attr_e( 'Service description', 'mind-web-concierge' ); ?>"
													><?php echo esc_textarea( $service_description ); ?></textarea>
												</div>
											<?php endfor; ?>
										</div>
										<p class="description">
											<i class="dashicons dashicons-info" aria-hidden="true"></i>
											<?php esc_html_e( 'Add one or more services for visitors to access through the chat widget. If only one service is added, it opens directly in the widget panel.', 'mind-web-concierge' ); ?>
											<br>
											<?php esc_html_e( 'If multiple services are added, they are shown as service cards. When a card is selected, the chosen service is displayed in the widget panel.', 'mind-web-concierge' ); ?>
											<br>
											<?php esc_html_e( 'Enter a service title, optional description, and a valid URL for each service.', 'mind-web-concierge' ); ?>
										</p>
										<div class="mindweco-custom-css-field">
											<label for="mindweco_iframe_css">
												<strong><?php esc_html_e( 'Custom CSS', 'mind-web-concierge' ); ?></strong>
											</label>
											<div class="mindweco-css-editor-shell">
												<div class="mindweco-css-editor-toolbar" aria-hidden="true">
													<span class="mindweco-css-editor-dots">
														<span class="mindweco-css-editor-dot mindweco-css-editor-dot-red"></span>
														<span class="mindweco-css-editor-dot mindweco-css-editor-dot-yellow"></span>
														<span class="mindweco-css-editor-dot mindweco-css-editor-dot-green"></span>
													</span>
													<span class="mindweco-css-editor-title">
														<?php esc_html_e( 'Iframe CSS', 'mind-web-concierge' ); ?>
													</span>
													<span class="mindweco-css-editor-hint">
														<?php esc_html_e( 'Applied automatically when the service panel opens', 'mind-web-concierge' ); ?>
													</span>
												</div>
												<textarea
													name="mindweco_iframe_css"
													id="mindweco_iframe_css"
													class="settings-field-input mindweco-custom-css-input mindweco-css-editor-input"
													rows="10"
													wrap="off"
													spellcheck="false"
													autocapitalize="off"
													autocomplete="off"
													autocorrect="off"
													placeholder="<?php esc_attr_e( 'Add custom CSS rules for the widget.', 'mind-web-concierge' ); ?>"
												><?php echo esc_textarea( $settings['iframe_css'] ); ?></textarea>
											</div>
											<p class="description">
												<i class="dashicons dashicons-info" aria-hidden="true"></i>
												<?php esc_html_e( 'Enter custom CSS to modify the service panel design, such as colors, fonts, or spacing. These styles will be applied automatically when the panel opens.', 'mind-web-concierge' ); ?>
											</p>
										</div>
									</td>
								</tr>
							</table>
						</div>

						<p class="submit btn-save-settings-mind-web">
							<button type="submit" 
									name="mindweco_save_settings" 
									class="button save-settings" 
									id="mindweco-save-settings">
								<i class="bi bi-check-circle" aria-hidden="true mr-1" ></i>
								<?php esc_html_e( 'Save Settings', 'mind-web-concierge' ); ?>
							</button>
						</p>
					</form>
				</div>
			</div>

			<div id="tab-selection" class="tab-content" style="display:none;">
				<div class="bg-white p-4 rounded shadow-sm">
					<div class="d-flex align-items-center justify-content-between border-bottom pb-3 mb-3">
						<div>
							<h1 class="custom-heading fw-semibold m-0">
								<i class="bi bi-ui-checks me-2" aria-hidden="true"></i>
								<?php esc_html_e( 'Exclude Indexing', 'mind-web-concierge' ); ?>
							</h1>
							<p class="text-secondary fw-bold m-0 mt-1">
								<?php esc_html_e( 'Pages and posts are selected by default. You can unselect anything you don’t want to be included in responses.', 'mind-web-concierge' ); ?>			
							</p>
						</div>
					
						<div class="d-flex gap-2 align-items-center">
							<div class="search-wrapper flex-shrink-0 mx-4 position-relative">
								<i class="bi bi-search position-absolute text-muted"
									aria-hidden="true"
									id="search-icon">
								</i>
								<label for="mindweco-search-input-index-selection" class="screen-reader-text">
									<?php esc_html_e( 'Search post/pages', 'mind-web-concierge' ); ?>
								</label>
								<input
									type="text"
									class="form-control ps-5"
									id="mindweco-search-input-index-selection"
									placeholder="<?php esc_attr_e( 'Search post/pages...', 'mind-web-concierge' ); ?>"
									aria-label="<?php esc_attr_e( 'Search post/pages', 'mind-web-concierge' ); ?>"
								>
							</div>
							<button id="mindweco-select-all" class="btn btn-sm ">
								<i class="bi bi-check-all me-1"></i>
								<?php esc_html_e( 'Select All', 'mind-web-concierge' ); ?>
							</button>
							<button id="mindweco-deselect-all" class="btn btn-sm ">
								<i class="bi bi-x-square me-1"></i>
								<?php esc_html_e( 'Deselect All', 'mind-web-concierge' ); ?>
							</button>
						</div>
					</div>

					<div id="mindweco-selection-list">
						<p><?php esc_html_e( 'Loading...', 'mind-web-concierge' ); ?></p>
					</div>
					<div id="mindweco-selection-pagination" class="mindweco-pagination-container"></div>
				</div>
			</div>

			<div id="tab-indexing" class="tab-content" style="display:none;">
				<div class="bg-white p-4 rounded shadow-sm">
					<div class="header-indexing">
						<h1 class="custom-heading fw-semibold">
							<i class="bi bi-lightning-charge-fill" aria-hidden="true mr-2" ></i>
							<?php esc_html_e( 'Content Indexing Status', 'mind-web-concierge' ); ?>
						</h1>
						<p class="text-secondary mt-2 fw-bold mb-0">
							<?php esc_html_e( 'View and manage the indexing progress of your posts and pages.', 'mind-web-concierge' ); ?>
						</p>
					</div>

					<div class="indexing-status">
						<div class="mindweco-indexing-">
							<div>
								<div class="indexing-elements-progress"><?php esc_html_e( 'Total Posts & Pages', 'mind-web-concierge' ); ?></div>
								<div id ="total" style="color:#1a365d;"><?php echo esc_html( $total ); ?></div>
							</div>
							<div>
								<div class="indexing-elements-progress"><?php esc_html_e( 'Not Indexed', 'mind-web-concierge' ); ?></div>
								<div id="to-index-count" style="color:#1a365d;"><?php echo esc_html( $current_indexing ); ?></div>
							</div>
							<div>
								<div class="indexing-elements-progress"><?php esc_html_e( 'Indexed', 'mind-web-concierge' ); ?></div>
								<div id="indexed-count" style="color:#10b981;"><?php echo esc_html( $indexed_count ); ?></div>
							</div>
							<div>
								<div class="indexing-elements-progress"><?php esc_html_e( 'Progress', 'mind-web-concierge' ); ?></div>
								<div id="index-progress" style="color:#1a365d;"><?php echo esc_html( $indexing_stats['progress'] ); ?>%</div>
							</div>
						</div>
					</div>

					<div class="mindweco-btn-index-refresh">
						<button id="mindweco-refresh-summary" class="mindweco-btn mindweco-btn-secondary">
							<i class="bi bi-arrow-clockwise"></i>
							<span>Regenerate Summary</span>
						</button>

						<button id="mindweco-index-all-items" class="mindweco-btn">
							<i class="bi bi-play-circle-fill"></i>
							<span>Start Indexing</span>
						</button>

						
					</div>
			
					<div id="progress-container">
						<div  class="progress-wrapper" >
							<div id="progress-bar">
								<span id="progress-text">0%</span>
							</div>
						</div>
						<div id="progress-status"></div>
					</div>

					<div id="mindweco-index-all-result" style="margin-top:20px;"></div>
				</div>
			</div>

			<div id="tab-manage" class="tab-content" style="display:none;">
				<div class="bg-white p-4 rounded shadow-sm">
					<div class="wrap">
						<div class="d-flex align-items-center border-bottom pb-3 header-search-btn">
							<div class="flex-grow-1">
								<h1 class="fw-semibold m-0">
									<i class="bi bi-list-check me-2 text-primary" aria-hidden="true"></i>
									<?php esc_html_e( 'All Indexed Items', 'mind-web-concierge' ); ?>
								</h1>
								<p class="text-secondary fw-bold m-0 mt-1">
									<?php esc_html_e( 'A complete list of all successfully indexed items.', 'mind-web-concierge' ); ?>
								</p>
							</div>

							<div class="search-wrapper flex-shrink-0 mx-4 position-relative">
								<i class="bi bi-search position-absolute text-muted"
									aria-hidden="true"
									id="search-icon">
								</i>
								<label for="search-input" class="screen-reader-text">
									<?php esc_html_e( 'Search indexed items', 'mind-web-concierge' ); ?>
								</label>
								<input
									type="text"
									class="form-control ps-5"
									id="search-input"
									placeholder="<?php esc_attr_e( 'Search indexed items...', 'mind-web-concierge' ); ?>"
									aria-label="<?php esc_attr_e( 'Search indexed items', 'mind-web-concierge' ); ?>"
								>
							</div>

							<div class="flex-grow-1 d-flex justify-content-end gap-2">
								<button
									id="mindweco-unindex-all-items"
									class="btn btn-danger d-flex align-items-center gap-2 px-3"
									aria-label="<?php esc_attr_e( 'Unindex all items', 'mind-web-concierge' ); ?>"
								>
									<i class="bi bi-trash-fill" aria-hidden="true"></i>
									<span><?php esc_html_e( 'Unindex All', 'mind-web-concierge' ); ?></span>
								</button>
								<button
									class="btn btn-success btn-index-items d-flex align-items-center gap-2 px-3"
									aria-label="<?php esc_attr_e( 'Index new items', 'mind-web-concierge' ); ?>"
								>
									<i class="bi bi-check-circle-fill" aria-hidden="true"></i>
									<span><?php esc_html_e( 'Index Items', 'mind-web-concierge' ); ?></span>
								</button>
							</div>
						</div>

						<div id="mindweco-unindex-result" class="mb-3"></div>

						<div class="table-responsive border custom-rounded" style="border-color:#e5e7eb;">
							<table id="table-indexed" class="wp-list-table widefat fixed striped m-0">
								<thead>
									<tr>
										<th class="table-indexed-title" scope="col"><?php esc_html_e( 'ID', 'mind-web-concierge' ); ?></th>
										<th class="table-indexed-title" scope="col"><?php esc_html_e( 'Item ID', 'mind-web-concierge' ); ?></th>
										<th class="table-indexed-title" scope="col"><?php esc_html_e( 'Post ID', 'mind-web-concierge' ); ?></th>			
										<th class="table-indexed-title" scope="col"><?php esc_html_e( 'Category', 'mind-web-concierge' ); ?></th>
										<th class="table-indexed-title" scope="col"><?php esc_html_e( 'Author', 'mind-web-concierge' ); ?></th>
										<th class="table-indexed-title 	table-indexed-title-style-col" scope="col"><?php esc_html_e( 'Title', 'mind-web-concierge' ); ?></th>
										<th class="table-indexed-title" scope="col"><?php esc_html_e( 'Name', 'mind-web-concierge' ); ?></th>
										<th class="table-indexed-title" scope="col"><?php esc_html_e( 'Status', 'mind-web-concierge' ); ?></th>
										<th class="table-indexed-title" scope="col"><?php esc_html_e( 'Actions', 'mind-web-concierge' ); ?></th>
									</tr>
								</thead>
								<tbody></tbody>
							</table>
						</div>

						<div id="pagination"></div>
					</div>
				</div>
			</div>

			<div id="tab-technical_options" class="tab-content" style="display:none;">
				<div class="bg-white p-4 rounded shadow-sm">
					<div class="technical-option">
						<h1 class="custom-heading fw-semibold">
							<i class="bi bi-tools" aria-hidden="true mr-1" ></i>
							<?php esc_html_e( 'Technical Options', 'mind-web-concierge' ); ?>
						</h1>
						<p class="text-secondary mt-2 fw-bold mb-0">
							<?php esc_html_e( 'Review your technical settings including URLs, credentials, indexing, and system configuration.', 'mind-web-concierge' ); ?>
						</p>
					</div>

					<table class="form-table mt-3">
						<tr>
							<th scope="row" class="row-inputs-element">
								<label for="mindweco-base-url">
									<i class="bi bi-link-45deg me-2 techinical-indexing" aria-hidden="true"></i>
									<?php esc_html_e( 'Base URL:', 'mind-web-concierge' ); ?>
								</label>
							</th>
							<td class="settings-field">
								<input
									type="text"
									name="mindweco_base_url"
									id="mindweco-base-url"
									value="<?php echo esc_attr( $base_url ); ?>"
									readonly
									class="settings-field-input"
								/>
								<p class="description">
									<i class="dashicons dashicons-info" aria-hidden="true"></i>
									<?php esc_html_e( 'The foundational URL that anchors all system connections.', 'mind-web-concierge' ); ?>
								</p>
							</td>
						</tr>
						<tr>
							<th scope="row" class="row-inputs-element">
								<label for="mindweco-username">
									<i class="bi bi-person-circle me-2 techinical-indexing" aria-hidden="true"></i>
									<?php esc_html_e( 'Username:', 'mind-web-concierge' ); ?>
								</label>
							</th>
							<td class="settings-field">
								<input
									type="text"
									name="username"
									id="mindweco-username"
									value="<?php echo esc_attr( $username ); ?>"
									readonly
									class="settings-field-input"
								/>
								<p class="description">
									<i class="dashicons dashicons-info" aria-hidden="true"></i>
									<?php esc_html_e( 'Name assigned to identify the user.', 'mind-web-concierge' ); ?>
								</p>
							</td>
						</tr>
						<tr>
							<th scope="row" class="row-inputs-element">
								<label for="mindweco-indexing-id">
									<i class="bi bi-hash me-2 techinical-indexing" aria-hidden="true"></i>
									<?php esc_html_e( 'Indexing ID:', 'mind-web-concierge' ); ?>
								</label>
							</th>
							<td class="settings-field">
								<input
									type="text"
									name="mindweco_indexing_id"
									id="mindweco-indexing-id"
									value="<?php echo esc_attr( $indexing_id ); ?>"
									readonly
									class="settings-field-input"
								/>
								<p class="description">
									<i class="dashicons dashicons-info" aria-hidden="true"></i>
									<?php esc_html_e( 'Identifier for data indexing.', 'mind-web-concierge' ); ?>
								</p>
							</td>
						</tr>
						<tr id="indexing-title-settings">
							<th scope="row" class="row-inputs-element">
								<label for="mindweco-indexing-title">
									<i class="bi bi-card-heading me-2 techinical-indexing" aria-hidden="true"></i>
									<?php esc_html_e( 'Indexing Title:', 'mind-web-concierge' ); ?>
								</label>
							</th>
							<td class="settings-field">
								<input
									type="text"
									name="mindweco_indexing_title"
									id="mindweco-indexing-title"
									value="<?php echo esc_attr( $indexing_title ); ?>"
									readonly
									class="settings-field-input"
								/>
								<p class="description">
									<i class="dashicons dashicons-info" aria-hidden="true"></i>
									<?php esc_html_e( 'Title for this indexing setup.', 'mind-web-concierge' ); ?>
								</p>
							</td>
						</tr>
						<tr id="category-settings">
							<th scope="row" class="row-inputs-element">
								<label>
									<i class="bi bi-tags me-2 techinical-indexing" aria-hidden="true"></i>
									<?php esc_html_e( 'Category:', 'mind-web-concierge' ); ?>
								</label>
							</th>
							<td class="settings-field">
								<div class="d-flex gap-2">
									<label for="mindweco-category-id" class="screen-reader-text">
										<?php esc_html_e( 'Category ID', 'mind-web-concierge' ); ?>
									</label>
									<input
										type="number"
										id="mindweco-category-id"
										name="mindweco_category_id"
										value="<?php echo esc_attr( $category['id'] ); ?>"
										placeholder="<?php esc_attr_e( 'ID', 'mind-web-concierge' ); ?>"
										readonly
										class="settings-field-input"
									/>
									<label for="mindweco-category-name" class="screen-reader-text">
										<?php esc_html_e( 'Category Name', 'mind-web-concierge' ); ?>
									</label>
									<input
										type="text"
										id="mindweco-category-name"
										name="mindweco_category_name"
										value="<?php echo esc_attr( $category['name'] ); ?>"
										placeholder="<?php esc_attr_e( 'Category Name', 'mind-web-concierge' ); ?>"
										readonly
										class="settings-field-input"
									/>
								</div>
								<p class="description">
									<i class="dashicons dashicons-info" aria-hidden="true"></i>
									<?php esc_html_e( 'Category ID and Name for indexing.', 'mind-web-concierge' ); ?>
								</p>
							</td>
						</tr>
					</table>
				</div>
			</div>
			
		</div>
	</div>
	<?php
}
