<?php
/**
 * Shell compartido del panel (operaciones en sitio).
 *
 * Variables esperadas:
 * - string $logistica_panel_context  operations
 * - string $logistica_panel_heading  operations|config|default
 * - bool   $logistica_panel_show_user_header
 *
 * @package Logistica
 */

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

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

$panel_context            = isset( $logistica_panel_context ) ? Logistica_Tabs::normalize_context( $logistica_panel_context ) : Logistica_Tabs::CONTEXT_OPERATIONS;
$panel_heading            = isset( $logistica_panel_heading ) ? sanitize_key( (string) $logistica_panel_heading ) : 'default';
$show_user_header         = ! empty( $logistica_panel_show_user_header );
$tab_views                = logistica_get_tab_views();
$visible_slugs            = array_keys( logistica_get_nav_tabs_for_context( $panel_context ) );
$initial_tab              = logistica_get_active_tab_slug( $panel_context );

if ( ! in_array( $initial_tab, $visible_slugs, true ) ) {
	$initial_tab = logistica_get_panel_initial_tab( $panel_context );
}

if ( empty( $visible_slugs ) ) {
	echo '<div class="wrap logistica-admin"><p>' . esc_html__( 'No tienes pestañas asignadas. Contacta al administrador.', 'logistica' ) . '</p></div>';
	return;
}

$shell_classes = array(
	'wrap',
	'logistica-admin',
	'logistica-frontend',
	'logistica-operations',
	'is-nav-collapsed',
);
if ( $show_user_header ) {
	$shell_classes[] = 'logistica-backend';
}
?>
<div
	class="<?php echo esc_attr( implode( ' ', $shell_classes ) ); ?>"
	data-logistica-app
	data-logistica-inventory-shell
	data-logistica-panel-context="<?php echo esc_attr( $panel_context ); ?>"
	data-logistica-initial-tab="<?php echo esc_attr( $initial_tab ); ?>"
	data-logistica-lazy-tabs="1"
>
	<?php logistica_nav_tabs( $panel_context ); ?>

	<div class="logistica-inventory-body">
		<?php
		$user_bar_hidden_tabs = array( 'logistica', 'logistica-perfil' );
		$hide_user_bar_init  = $show_user_header && in_array( $initial_tab, $user_bar_hidden_tabs, true );
		?>
		<header class="logistica-header logistica-header--operations<?php echo $show_user_header ? ' logistica-header--backend' : ''; ?>" data-logistica-panel-header<?php echo $show_user_header ? '' : ' hidden'; ?>>
			<?php logistica_render_panel_heading( $panel_heading ); ?>
			<?php if ( $show_user_header ) : ?>
				<?php logistica_render_panel_user_bar( array( 'hidden' => $hide_user_bar_init ) ); ?>
			<?php endif; ?>
		</header>
		<?php logistica_render_status_notices(); ?>

		<div class="logistica-tab-panels">
			<?php
			foreach ( $tab_views as $slug => $view_file ) :
				if ( ! in_array( $slug, $visible_slugs, true ) ) {
					continue;
				}
				$view_path = logistica_view_path( $view_file );
				if ( ! is_readable( $view_path ) ) {
					continue;
				}
				$is_initial = ( $slug === $initial_tab );
				?>
				<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' !== $slug ) : ?>
							<?php logistica_tab_loading_overlay( logistica_tab_loading_message( $slug ), 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;
			?>
		</div>
	</div>
</div>
