<?php

/**
 * Provide a admin area view for the plugin
 *
 * This file is used to markup the admin-facing aspects of the plugin.
 *
 * @link       https://wp-deployer.io
 * @since      1.0.0
 *
 * @package    WP_Deployer
 * @subpackage WP_Deployer/admin/partials
 */

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

/**
 * Requires.
 */

require_once __DIR__ . '/component/confirmation-modal-list.php';

?>
<div class="wrap">
	<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
	<?php

	// check user capabilities
	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}

	do_action( 'deploy_sync_content__admin_display_pre' );

	// The Dashboard is the default tab, unless another plugin changes this.
	$default_tab = apply_filters( 'deploy_sync_content__default_route_id', 'dashboard' );

	// Get the active tab from the $_GET param.
	$current_tab_id = sanitize_text_field( wp_unslash( $_GET['tab'] ?? '' ) );
	$current_tab_id = ! empty( $current_tab_id ) ? $current_tab_id : $default_tab;

	$routes = apply_filters( 'deploy_sync_content__routes', array() );

	uasort( $routes, static fn( $a, $b ) => ( $a['weight'] ?? 0 ) <=> ( $b['weight'] ?? 0 ) );

	if ( empty( $routes[ $current_tab_id ] ) ) {
		// There is no route linked to this tab. Use the default tab.
		$current_tab_id = $default_tab;
	}

	?>
	<div class="container-fluid deploy-sync-content--container p-0">
		<nav class="nav-tab-wrapper">
			<?php
			foreach ( $routes as $route ):
				if ( empty( $route['has_tab'] ) ) {
					continue;
				}

				$nav_tab_id = 'nav-tab-' . $route['id'];
				$href       = $route['path'] ?? DEPLOY_SYNC_CONTENT__BASIC_URI . '&tab=' . $route['id'];
				$classes    = [ 'nav-tab' ];
				$current_tab_id === $route['id'] && array_push( $classes, 'nav-tab-active' );
				?>
				<a id="<?php echo esc_attr( $nav_tab_id ) ?>"
					href="<?php echo esc_attr( $href ) ?>"
					class="<?php echo esc_attr( implode( ' ', $classes ) ) ?>">
					<?php echo esc_html( $route['label'] ) ?>
				</a>
			<?php
			endforeach;
			?>
		</nav>
		<div class="tab-content">
			<?php deploy_sync_content__handle_tabs_routing( $routes[ $current_tab_id ] ?? [] ); ?>
		</div>
	</div>
</div>

<!-- Remove Modal -->
<?php deploy_sync_content__confirmation_modal( esc_html__( 'Remove Bundle', 'deploy-sync-content' ), esc_html__( 'Are you sure to remove this item?', 'deploy-sync-content' ) ) ?>
