<?php
/**
 * Shell del panel de configuración (jb-planeacion-backend?config=1).
 *
 * @package Logistica
 */

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

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

if ( ! logistica_is_config_panel() ) {
	echo '<div class="wrap logistica-admin"><p>' . esc_html__( 'No tienes permisos para configurar el plugin.', 'logistica' ) . '</p></div>';
	return;
}

$panel_context   = Logistica_Tabs::CONTEXT_CONFIG;
$panel_heading   = isset( $logistica_panel_heading ) ? sanitize_key( (string) $logistica_panel_heading ) : 'config';
$tabs            = logistica_get_nav_tabs_for_context( 'config' );
$visible_slugs   = array_keys( $tabs );
$initial_tab     = logistica_get_panel_initial_tab( 'config' );
$all_tab_labels  = logistica_get_nav_tabs();

if ( empty( $visible_slugs ) ) {
	echo '<div class="wrap logistica-admin"><p>' . esc_html__( 'No hay pestañas de configuración disponibles.', 'logistica' ) . '</p></div>';
	return;
}
?>
<div
	class="wrap logistica-admin logistica-frontend logistica-config is-nav-collapsed logistica-backend"
	data-logistica-app
	data-logistica-inventory-shell
	data-logistica-panel-context="config"
	data-logistica-initial-tab="<?php echo esc_attr( $initial_tab ); ?>"
	data-logistica-lazy-tabs="1"
>
	<?php logistica_nav_tabs( 'config' ); ?>

	<div class="logistica-inventory-body">
		<header class="logistica-header logistica-header--config logistica-header--backend logistica-header--slim" data-logistica-panel-header>
			<?php logistica_render_panel_heading( $panel_heading ); ?>
			<?php logistica_render_panel_user_bar(); ?>
		</header>
		<?php logistica_render_status_notices(); ?>

		<div class="logistica-tab-panels">
			<?php
			foreach ( $visible_slugs as $slug ) :
				$view_path = Logistica_Tabs::config_tab_path( $slug );
				if ( ! is_readable( $view_path ) ) {
					continue;
				}
				$is_initial                 = ( $slug === $initial_tab );
				$logistica_config_tab_label = $all_tab_labels[ $slug ] ?? $slug;
				?>
				<section
					class="logistica-tab-panel<?php echo $is_initial ? ' is-active' : ''; ?>"
					id="<?php echo esc_attr( 'logistica-tab-' . $slug ); ?>"
					data-tab-panel="<?php echo esc_attr( $slug ); ?>"
					data-tab-lazy="<?php echo $is_initial ? '0' : '1'; ?>"
					<?php echo $is_initial ? ' data-tab-content-loaded="1"' : ''; ?>
					role="tabpanel"
					aria-labelledby="<?php echo esc_attr( 'logistica-tab-link-' . $slug ); ?>"
					<?php echo $is_initial ? '' : 'hidden'; ?>
				>
					<?php if ( $is_initial ) : ?>
						<?php if ( ! Logistica_Tabs::config_tab_has_settings( $slug ) ) : ?>
							<?php logistica_tab_loading_overlay( '', false ); ?>
						<?php endif; ?>
						<?php
						$GLOBALS['logistica_tabbed_shell'] = true;
						include $view_path;
						unset( $GLOBALS['logistica_tabbed_shell'] );
						?>
					<?php else : ?>
						<?php logistica_tab_loading_overlay( logistica_tab_loading_message( $slug ), true ); ?>
					<?php endif; ?>
				</section>
				<?php
			endforeach;
			unset( $logistica_config_tab_label );
			?>
		</div>
	</div>
</div>
