<?php
/**
 * Mind Spaces / Knowledge Repository Admin Page
 *
 * @package MindWebConcierge
 */

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

require_once plugin_dir_path( __DIR__ ) . 'includes/mindweco_config.php';

add_action( 'admin_enqueue_scripts', 'mindweco_knowledge_repository_enqueue_scripts' );

function mindweco_knowledge_repository_enqueue_scripts( $hook_suffix ) {
	if ( 'mind-web_page_mind-web-spaces' !== $hook_suffix ) {
		return;
	}

	$plugin_url = plugin_dir_url( __DIR__ );
	$ver        = defined( 'MIND_WEB_VERSION' ) ? MIND_WEB_VERSION : '1.3.0';

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

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


	wp_enqueue_script(
		'mindweco-pagination',
		$plugin_url . 'assets/js/components/Pagination.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-modal',
		$plugin_url . 'assets/js/components/Modal.js',
		array( 'jquery' ),
		$ver,
		true
	);

	wp_enqueue_script(
		'mindweco-knowledge-repository-js',
		$plugin_url . 'assets/js/mindweco_knowledge_repository.js',
		array( 'jquery', 'mindweco-pagination', 'mindweco-toast', 'mindweco-modal' ),
		$ver,
		true
	);

	wp_localize_script(
		'mindweco-knowledge-repository-js',
		'mindwecoDataKnowledgeRepository',
		array(
			'ajax_url'      => admin_url( 'admin-ajax.php' ),
			'nonce'         => wp_create_nonce( 'mindweco_login_nonce' ),
			'notebooks_url' => MINDWECO_NOTEBOOKS_URL,
		)
	);
}

function mindweco_knowledge_repository_page() {

	if ( ! current_user_can( 'manage_options' ) ) {
		wp_die( esc_html__( 'You do not have permission to access this page.', 'mind-web-concierge' ) );
	}

	$knowledge_repository_options = get_option( 'mindweco_knowledge_repository_options', array() );
	if ( ! is_array( $knowledge_repository_options ) ) {
		$knowledge_repository_options = array();
	}

	$base_url = ( isset( $knowledge_repository_options['base_url'] ) && ! empty( $knowledge_repository_options['base_url'] ) )
    ? $knowledge_repository_options['base_url']
    : MINDWECO_NOTEBOOKS_URL;

	$username         = isset( $knowledge_repository_options['username'] ) ? $knowledge_repository_options['username'] : '';
	$password_display = ! empty( $knowledge_repository_options['password'] ) ? mindweco_decrypt_api_key( $knowledge_repository_options['password'] ) : '';
	$register_url = trailingslashit( MINDWECO_NOTEBOOKS_URL ) . 'Identity/Account/Register';

	?>
	<div id="mind-spaces-wrapper">
		<div class="wrap mindweco-knowledge-wrapper" >

			<div class="mindweco-card-knowledge">
				<h4 class="mind-spaces-title">
					<i class="bi bi-sliders" aria-hidden="true"></i>
					<?php esc_html_e( 'Mind Spaces Settings', 'mind-web-concierge' ); ?>
				</h4>
			</div>

			<h2 class="nav-tab-wrapper mindweco-nav-tabs">
				<a href="#general" class="nav-tab nav-tab-active mindweco-nav-tab" data-tab="general">
					<i class="bi bi-sliders" aria-hidden="true"></i>
					<?php esc_html_e( 'General', 'mind-web-concierge' ); ?>
				</a>
				<a href="#page_based_notebooks" class="nav-tab mindweco-nav-tab" data-tab="page_based_notebooks" id="page-based-notebooks-tab">
					<i class="bi bi-journal-text" aria-hidden="true"></i>
					<?php esc_html_e( 'Mind Spaces Page Based Notebooks', 'mind-web-concierge' ); ?>
				</a>
			</h2>

			<div id="tab-general" class="tab-content" style="display:block;">
				<div class="mindweco-card p-5 mx-auto mt-5" style="max-width:700px;background:#fff;border-radius:12px;box-shadow:0 8px 20px rgba(0,0,0,.08);">

					<div style="text-align:center;">
						<h2 style="margin:0;font-weight:700;color:#1e293b;">
							<i class="bi bi-lightbulb me-2" aria-hidden="true" style="color:#1a365d;"></i>
							<?php esc_html_e( 'Mind Spaces Login', 'mind-web-concierge' ); ?>
						</h2>
						<p style="margin-top:10px;color:#6b7280;font-size:16px;">
							<?php esc_html_e( 'Enter your credentials to access your account.', 'mind-web-concierge' ); ?>
						</p>
					</div>
					<form id="mindweco-login-form" autocomplete="off">
						<table class="form-table mindweco-knowledge-repository w-100 mb-3">
							<tr>
								<th scope="row" style="width:30%;">
									<label for="mindweco-base-url">
										<i class="bi bi-link-45deg" aria-hidden="true"></i>
										<?php esc_html_e( 'Base URL', 'mind-web-concierge' ); ?>
									</label>
								</th>
								<td>
									<input
										type="url"
										id="mindweco-base-url"
										class="form-control"
										value="<?php echo esc_attr( $base_url ); ?>"
										placeholder="<?php echo esc_attr( MINDWECO_NOTEBOOKS_URL ); ?>"
									/>
								</td>
							</tr>
							<tr>
								<th scope="row">
									<label for="mindweco-username">
										<i class="bi bi-person" aria-hidden="true"></i>
										<?php esc_html_e( 'Username', 'mind-web-concierge' ); ?>
									</label>
								</th>
								<td class="d-flex flex-column">
									<input
										type="text"
										id="mindweco-username"
										class="form-control"
										autocomplete="off"
										placeholder="<?php esc_attr_e( 'name@example.com', 'mind-web-concierge' ); ?>"
										value="<?php echo esc_attr( $username ); ?>"
									/>
									<span class="text-danger small d-none username-error"></span>
								</td>
							</tr>
							<tr>
								<th scope="row">
									<label for="mindweco-password">
										<i class="bi bi-lock" aria-hidden="true"></i>
										<?php esc_html_e( 'Password', 'mind-web-concierge' ); ?>
									</label>
								</th>
								<td class="d-flex flex-column">
									<div class="password-wrapper position-relative">
										<input
											type="password"
											id="mindweco-password"
											class="form-control"
											autocomplete="off"
											placeholder="******"
											value="<?php echo esc_attr( $password_display ); ?>"
										/>
										<i class="bi bi-eye-fill eye-icon position-absolute"
											id="toggle-password-visibility"
											role="button"
											tabindex="0"
											aria-label="<?php esc_attr_e( 'Toggle password visibility', 'mind-web-concierge' ); ?>"
											aria-hidden="false">
										</i>
									</div>
									<span class="text-danger small d-none password-error"></span>
								</td>
							</tr>
						</table>
					</form>
					<div class="text-center mt-4">
						<div class="login-btn-index_link" id="login-btn-index_link">
							<button id="mindweco-knowledge-repository-login-btn" class="btn btn-primary w-50 py-2">
								<?php esc_html_e( 'Log in', 'mind-web-concierge' ); ?>
							</button>
							<p class="mt-3 mb-0 small text-muted">
								<?php esc_html_e( "Don't have an account?", 'mind-web-concierge' ); ?>
								<a href="<?php echo esc_url( $register_url ); ?>" target="_blank" rel="noopener noreferrer">
									<?php esc_html_e( 'Sign up here', 'mind-web-concierge' ); ?>
								</a>
							</p>
						</div>

						<div class="log-out d-flex justify-content-center" id="log-out">
							<button class="mindweco-knowledge-repository-sign-out btn btn-danger w-50 py-2 d-none" id="mindweco-knowledge-repository-sign-out">
								<span>
									<?php esc_html_e( 'Log out', 'mind-web-concierge' ); ?>
									<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#ffffff" aria-hidden="true" focusable="false">
										<path d="M21 12L13 12" stroke="#ffffff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
										<path d="M18 15L20.913 12.087V12.087C20.961 12.039 20.961 11.961 20.913 11.913V11.913L18 9" stroke="#ffffff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
										<path d="M16 5V4.5V4.5C16 3.67157 15.3284 3 14.5 3H5C3.89543 3 3 3.89543 3 5V19C3 20.1046 3.89543 21 5 21H14.5C15.3284 21 16 20.3284 16 19.5V19.5V19" stroke="#ffffff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
									</svg>
								</span>
							</button>
						</div>
					</div>
				</div>
			</div>

			<div id="tab-page_based_notebooks" class="tab-content" style="display:none;">
				<div class="mindweco-card">
					<div class="notebooks-container">
						<div class="notebooks-header">
							<div class="notebooks-title-wrapper">
								<div class="notebook-header-title">
									<h1 class="notebooks-title">
										<?php esc_html_e( '📚 Mind Spaces Page Knowledge Base', 'mind-web-concierge' ); ?>
									</h1>
									<p class="notebooks-subtitle">
										<?php esc_html_e( 'All spaces linked for page.', 'mind-web-concierge' ); ?>
									</p>
								</div>

								<div class="flex-shrink-0 mx-4 position-relative" style="width:450px;">
									<i class="bi bi-search position-absolute text-muted"
										aria-hidden="true"
										style="left:12px;top:50%;transform:translateY(-50%);pointer-events:none;">
									</i>
									<label for="search-input-page-notebooks" class="screen-reader-text">
										<?php esc_html_e( 'Search Page Knowledge Base', 'mind-web-concierge' ); ?>
									</label>
									<input
										type="text"
										class="form-control ps-5"
										id="search-input-page-notebooks"
										placeholder="<?php esc_attr_e( 'Search Page Knowledge Base...', 'mind-web-concierge' ); ?>"
										aria-label="<?php esc_attr_e( 'Search Page Knowledge Base', 'mind-web-concierge' ); ?>"
									/>
								</div>
							</div>
						</div>

						<div class="d-flex justify-content-center my-4" style="gap:20px;">
							<button id="add-page-based-notebook" class="btn btn-primary">
								<i class="bi bi-plus" aria-hidden="true" style="margin-right:5px;"></i>
								<?php esc_html_e( 'Knowledge base / reference', 'mind-web-concierge' ); ?>
							</button>
							<button id="remove-all-page-notebooks" class="btn btn-danger btn-sm">
								<i class="bi bi-trash3" aria-hidden="true" style="margin-right:5px;"></i>
								<?php esc_html_e( 'Remove All Page Spaces', 'mind-web-concierge' ); ?>
							</button>
						</div>
					</div>
				</div>
			</div>

		</div>

		<div class="modal fade" id="pageBasedNotebookModal" tabindex="-1" aria-labelledby="pageBasedNotebookModalTitle" aria-hidden="true">
			<div class="modal-dialog modal-dialog-centered modal-xl modal-dialog-scrollable">
				<div class="modal-content">
					<div class="modal-header text-white">
						<div>
							<h5 class="modal-title mb-1" id="pageBasedNotebookModalTitle">
								<i class="bi bi-journal-text me-2" aria-hidden="true"></i>
								<?php esc_html_e( 'Mind Spaces Knowledge Base / Reference', 'mind-web-concierge' ); ?>
							</h5>
							<small class="opacity-75">
								<?php
								printf(
									esc_html__( 'Select posts and pages to add to your %s base', 'mind-web-concierge' ),
									'<strong>' . esc_html__( 'Mind Spaces', 'mind-web-concierge' ) . '</strong>'
								);
								?>
							</small>
						</div>
						<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="<?php esc_attr_e( 'Close', 'mind-web-concierge' ); ?>"></button>
					</div>
					<div class="modal-header bg-light border-bottom">
						<div class="d-flex align-items-center gap-3 w-100 header-select-search">
							<div class="form-check" style="display:flex;align-items:baseline;gap:5px;">
								<input class="form-check-input" type="checkbox" id="select-all-pages" />
								<label class="form-check-label fw-semibold select-all text-info" for="select-all-pages">
									<?php esc_html_e( 'Select All', 'mind-web-concierge' ); ?>
								</label>
							</div>
							<div class="search-container">
								<label for="search-input" class="screen-reader-text">
									<?php esc_html_e( 'Search', 'mind-web-concierge' ); ?>
								</label>
								<input
									type="text"
									class="search-input-glass"
									id="search-input"
									placeholder="<?php esc_attr_e( 'Search pages…', 'mind-web-concierge' ); ?>"
								/>
							</div>
						</div>
					</div>
					<div class="modal-body" style="max-height:60vh;overflow-y:auto;">
						<div class="text-center py-5">
							<div class="spinner-border text-primary" role="status">
								<span class="visually-hidden"><?php esc_html_e( 'Loading...', 'mind-web-concierge' ); ?></span>
							</div>
						</div>
					</div>
					<div class="modal-footer bg-light">
						<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
							<i class="bi bi-x-circle me-1" aria-hidden="true"></i>
							<?php esc_html_e( 'Cancel', 'mind-web-concierge' ); ?>
						</button>
						<button type="button" class="btn btn-primary" id="save-selected-pages" disabled>
							<i class="bi bi-check-circle me-1" aria-hidden="true"></i>
							<?php esc_html_e( 'Add Selected', 'mind-web-concierge' ); ?>
						</button>
					</div>
				</div>
			</div>
		</div>
	</div>
	<?php
}