<?php
/**
 * Ayuda del plugin JB Planeación.
 *
 * @package Logistica
 */

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

require_once LOGISTICA_PLUGIN_DIR . 'admin/views/partials.php';

$context      = logistica_is_tabbed_shell() ? 'frontend' : 'admin';
$hero         = Logistica_Help::hero();
$sections     = Logistica_Help::uses_custom_html() ? array() : Logistica_Help::sections();
$nav_sections = Logistica_Help::uses_custom_html() ? array() : Logistica_Help::nav_sections();
$custom       = Logistica_Help::custom_html();
$version      = Logistica_Help::plugin_version_label();

if ( ! logistica_is_tabbed_shell() ) {
	logistica_admin_header( __( 'Ayuda', 'logistica' ) );
}
?>
<section class="logistica-help" data-logistica-help>
	<header class="logistica-help__hero">
		<div class="logistica-help__hero-icon" aria-hidden="true">
			<span class="dashicons dashicons-editor-help"></span>
		</div>
		<div>
			<h2><?php echo esc_html( $hero['title'] ); ?></h2>
			<p><?php echo esc_html( $hero['intro'] ); ?></p>
			<?php if ( '' !== $version ) : ?>
				<p class="logistica-help__version">
					<?php
					printf(
						/* translators: %s: plugin version */
						esc_html__( 'Versión del plugin: %s', 'logistica' ),
						esc_html( $version )
					);
					?>
				</p>
			<?php endif; ?>
		</div>
	</header>

	<?php if ( '' !== $custom ) : ?>
		<div class="logistica-help__custom logistica-help__sections">
			<?php echo wp_kses_post( $custom ); ?>
		</div>
	<?php elseif ( ! empty( $sections ) ) : ?>
		<div class="logistica-config-section-board logistica-conversion-board logistica-help-section-board is-nav-collapsed" data-conversion-board data-config-section-board>
			<aside class="logistica-conversion-nav" data-conversion-nav aria-label="<?php esc_attr_e( 'Secciones de ayuda', 'logistica' ); ?>">
				<div class="logistica-conversion-nav__head">
					<span class="logistica-conversion-nav__title"><?php esc_html_e( 'Navegación', 'logistica' ); ?></span>
					<button
						type="button"
						class="logistica-conversion-nav__toggle"
						data-action="toggle-conversion-nav"
						aria-expanded="false"
						aria-controls="logistica-help-section-nav-list"
						title="<?php esc_attr_e( 'Ocultar menú', 'logistica' ); ?>"
					>
						<span aria-hidden="true">&lsaquo;</span>
						<span class="screen-reader-text"><?php esc_html_e( 'Ocultar navegación de ayuda', 'logistica' ); ?></span>
					</button>
				</div>
				<nav id="logistica-help-section-nav-list" data-logistica-config-nav>
					<ul class="logistica-conversion-nav__list">
						<?php foreach ( $nav_sections as $help_nav_section ) : ?>
							<li>
								<a
									class="logistica-conversion-nav__link"
									href="#<?php echo esc_attr( $help_nav_section['id'] ); ?>"
									data-config-section-nav="<?php echo esc_attr( $help_nav_section['id'] ); ?>"
								><?php echo esc_html( $help_nav_section['label'] ); ?></a>
							</li>
						<?php endforeach; ?>
					</ul>
				</nav>
			</aside>

			<div class="logistica-config-section-main">
				<button
					type="button"
					class="logistica-conversion-nav-reopen"
					data-action="toggle-conversion-nav"
					aria-expanded="false"
					hidden
				>
					<span aria-hidden="true">&#9776;</span>
					<?php esc_html_e( 'Mostrar menú', 'logistica' ); ?>
				</button>

				<div class="logistica-help__sections">
					<?php foreach ( $sections as $section ) : ?>
						<?php
						$section_id    = (string) ( $section['id'] ?? '' );
						$tab_slug      = (string) ( $section['tab_slug'] ?? '' );
						$section_intro = (string) ( $section['intro'] ?? '' );
						$table         = is_array( $section['table'] ?? null ) ? $section['table'] : array();
						$list          = is_array( $section['list'] ?? null ) ? $section['list'] : array();
						$notes         = is_array( $section['notes'] ?? null ) ? $section['notes'] : array();
						$examples      = is_array( $section['examples'] ?? null ) ? $section['examples'] : array();
						?>
						<article class="logistica-help__section" id="<?php echo esc_attr( $section_id ); ?>">
							<header class="logistica-help__section-head">
								<h3><?php echo esc_html( (string) ( $section['title'] ?? '' ) ); ?></h3>
								<?php if ( '' !== $tab_slug ) : ?>
									<a class="logistica-help__section-link" href="<?php echo esc_url( Logistica_Help::tab_url( $tab_slug, $context ) ); ?>">
										<?php esc_html_e( 'Ir a la sección →', 'logistica' ); ?>
									</a>
								<?php endif; ?>
							</header>

							<?php if ( '' !== $section_intro ) : ?>
								<p class="logistica-help__intro"><?php echo esc_html( $section_intro ); ?></p>
							<?php endif; ?>

							<?php if ( ! empty( $table['rows'] ) && is_array( $table['rows'] ) ) : ?>
								<div class="logistica-help__table-wrap">
									<table class="logistica-help__table">
										<?php if ( ! empty( $table['headers'] ) && is_array( $table['headers'] ) ) : ?>
											<thead>
												<tr>
													<?php foreach ( $table['headers'] as $header ) : ?>
														<th scope="col"><?php echo esc_html( (string) $header ); ?></th>
													<?php endforeach; ?>
												</tr>
											</thead>
										<?php endif; ?>
										<tbody>
											<?php foreach ( $table['rows'] as $row ) : ?>
												<?php if ( ! is_array( $row ) ) { continue; } ?>
												<tr>
													<?php foreach ( $row as $cell ) : ?>
														<td><?php echo esc_html( (string) $cell ); ?></td>
													<?php endforeach; ?>
												</tr>
											<?php endforeach; ?>
										</tbody>
									</table>
								</div>
							<?php endif; ?>

							<?php if ( ! empty( $list ) ) : ?>
								<ul class="logistica-help__list">
									<?php foreach ( $list as $item ) : ?>
										<li><?php echo esc_html( (string) $item ); ?></li>
									<?php endforeach; ?>
								</ul>
							<?php endif; ?>

							<?php if ( ! empty( $notes ) ) : ?>
								<div class="logistica-help__notes">
									<strong><?php esc_html_e( 'Notas', 'logistica' ); ?></strong>
									<ul>
										<?php foreach ( $notes as $note ) : ?>
											<li><?php echo esc_html( (string) $note ); ?></li>
										<?php endforeach; ?>
									</ul>
								</div>
							<?php endif; ?>

							<?php if ( ! empty( $examples ) ) : ?>
								<div class="logistica-help__examples">
									<?php foreach ( $examples as $example ) : ?>
										<?php
										if ( ! is_array( $example ) ) {
											continue;
										}
										$example_title = (string) ( $example['title'] ?? '' );
										$example_body  = (string) ( $example['body'] ?? '' );
										$example_list  = is_array( $example['list'] ?? null ) ? $example['list'] : array();
										$example_table = is_array( $example['table'] ?? null ) ? $example['table'] : array();
										?>
										<div class="logistica-help__example">
											<?php if ( '' !== $example_title ) : ?>
												<h4 class="logistica-help__example-title"><?php echo esc_html( $example_title ); ?></h4>
											<?php endif; ?>
											<?php if ( '' !== $example_body ) : ?>
												<p class="logistica-help__example-body"><?php echo esc_html( $example_body ); ?></p>
											<?php endif; ?>
											<?php if ( ! empty( $example_table['rows'] ) && is_array( $example_table['rows'] ) ) : ?>
												<div class="logistica-help__table-wrap">
													<table class="logistica-help__table logistica-help__table--example">
														<?php if ( ! empty( $example_table['headers'] ) && is_array( $example_table['headers'] ) ) : ?>
															<thead>
																<tr>
																	<?php foreach ( $example_table['headers'] as $header ) : ?>
																		<th scope="col"><?php echo esc_html( (string) $header ); ?></th>
																	<?php endforeach; ?>
																</tr>
															</thead>
														<?php endif; ?>
														<tbody>
															<?php foreach ( $example_table['rows'] as $row ) : ?>
																<?php if ( ! is_array( $row ) ) { continue; } ?>
																<tr>
																	<?php foreach ( $row as $cell ) : ?>
																		<td><?php echo esc_html( (string) $cell ); ?></td>
																	<?php endforeach; ?>
																</tr>
															<?php endforeach; ?>
														</tbody>
													</table>
												</div>
											<?php endif; ?>
											<?php if ( ! empty( $example_list ) ) : ?>
												<ul class="logistica-help__list logistica-help__list--example">
													<?php foreach ( $example_list as $item ) : ?>
														<li><?php echo esc_html( (string) $item ); ?></li>
													<?php endforeach; ?>
												</ul>
											<?php endif; ?>
										</div>
									<?php endforeach; ?>
								</div>
							<?php endif; ?>
						</article>
					<?php endforeach; ?>
				</div>
			</div>
		</div>
	<?php endif; ?>
</section>
<?php
if ( ! logistica_is_tabbed_shell() ) {
	logistica_admin_footer();
}
