<?php
/**
 * CPT Builder - taxonomy add/edit view (tabbed).
 *
 * @package CptBuilder
 *
 * @var \CptBuilder\TaxonomyAdmin              $admin
 * @var array<string, mixed>                   $definition
 * @var bool                                   $is_edit
 * @var array{type: string, text: string}|null $notice
 * @var array<int, string>                     $errors
 * @var array<string, string>                  $post_types
 */

namespace CptBuilder;

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

require_once __DIR__ . '/render-helpers.php';

$definition = isset( $definition ) && is_array( $definition ) ? $definition : Config::defaultTaxonomyDefinition();
$is_edit    = ! empty( $is_edit );
$errors     = isset( $errors ) && is_array( $errors ) ? $errors : [];
$post_types = isset( $post_types ) && is_array( $post_types ) ? $post_types : [];
$key        = (string) ( $definition['key'] ?? '' );

$tabs = [
	'basic'      => __( 'Basic', 'wppowersuite-lite' ),
	'post-types' => __( 'Post Types', 'wppowersuite-lite' ),
	'labels'     => __( 'Labels', 'wppowersuite-lite' ),
	'visibility' => __( 'Visibility', 'wppowersuite-lite' ),
	'permalinks' => __( 'Permalinks', 'wppowersuite-lite' ),
	'rest'       => __( 'REST API', 'wppowersuite-lite' ),
];

if ( class_exists( '\CustomFields\CptBridge' ) && \CustomFields\CptBridge::isCustomFieldsEnabled() ) {
	$tabs['fields'] = __( 'Custom Fields', 'wppowersuite-lite' );
}
?>
<div class="wrap wpps-cptb wpps-cptb-edit">
	<hr class="wp-header-end" style="margin:0;padding:0;border:none;height:0;overflow:hidden;">
	<?php
	\WP_PowerSuite\Admin\PageHero::render_for_module(
		[
			'icon'    => 'dashicons-category',
			'title'   => $is_edit ? __( 'Edit Taxonomy', 'wppowersuite-lite' ) : __( 'Add New Taxonomy', 'wppowersuite-lite' ),
			'tagline' => $is_edit
				? __( 'Update this taxonomy and its settings.', 'wppowersuite-lite' )
				: __( 'Define a new taxonomy and its behavior.', 'wppowersuite-lite' ),
			'actions' => [
				[
					'label' => __( 'All Taxonomies', 'wppowersuite-lite' ),
					'url'   => $admin->url(),
					'icon'  => 'dashicons-arrow-left-alt',
				],
			],
		]
	);
	?>

	<?php if ( $notice !== null ) : ?>
		<div class="notice notice-<?php echo esc_attr( $notice['type'] ); ?> is-dismissible">
			<p><?php echo esc_html( $notice['text'] ); ?></p>
		</div>
	<?php endif; ?>

	<?php if ( $errors !== [] ) : ?>
		<div class="cptb-error-box" role="alert">
			<p><strong><?php esc_html_e( 'Your changes were not saved. Please fix the following:', 'wppowersuite-lite' ); ?></strong></p>
			<ul class="cptb-error-list">
				<?php foreach ( $errors as $error ) : ?>
					<li><?php echo esc_html( $error ); ?></li>
				<?php endforeach; ?>
			</ul>
		</div>
	<?php endif; ?>

	<form method="post" action="<?php echo esc_url( $admin->postEndpoint() ); ?>" class="cptb-form" id="cptb-form" novalidate>
		<input type="hidden" name="action" value="wpps_cptb_tax_save">
		<?php wp_nonce_field( 'wpps_cptb_tax_save' ); ?>
		<input type="hidden" name="original_key" value="<?php echo esc_attr( $is_edit ? $key : '' ); ?>">

		<div class="cptb-actions-bar">
			<label class="cptb-enabled-toggle">
				<input type="checkbox" name="enabled" value="1" <?php checked( ! empty( $definition['enabled'] ) ); ?>>
				<?php esc_html_e( 'Enabled', 'wppowersuite-lite' ); ?>
			</label>
			<button type="submit" class="button button-primary">
				<?php echo $is_edit ? esc_html__( 'Save Changes', 'wppowersuite-lite' ) : esc_html__( 'Create Taxonomy', 'wppowersuite-lite' ); ?>
			</button>
		</div>

		<div class="cptb-layout">
			<nav class="cptb-tabs" role="tablist">
				<?php $first = true; ?>
				<?php foreach ( $tabs as $tab_id => $tab_label ) : ?>
					<button type="button" class="cptb-tab<?php echo $first ? ' is-active' : ''; ?>" data-tab="<?php echo esc_attr( $tab_id ); ?>" role="tab" aria-selected="<?php echo $first ? 'true' : 'false'; ?>">
						<?php echo esc_html( $tab_label ); ?>
					</button>
					<?php $first = false; ?>
				<?php endforeach; ?>
			</nav>

			<div class="cptb-panels">
				<?php
				$first = true;
				foreach ( array_keys( $tabs ) as $tab_id ) {
					$panel_file = __DIR__ . '/taxonomy-tabs/' . $tab_id . '.php';
					echo '<section class="cptb-panel' . ( $first ? ' is-active' : '' ) . '" id="cptb-tab-' . esc_attr( $tab_id ) . '" role="tabpanel"' . ( $first ? '' : ' hidden' ) . '>';
					if ( is_readable( $panel_file ) ) {
						include $panel_file;
					}
					echo '</section>';
					$first = false;
				}
				?>
			</div>
		</div>

		<div class="cptb-actions-bar cptb-actions-bar--bottom">
			<?php if ( $is_edit ) : ?>
				<a href="<?php echo esc_url( $admin->actionUrl( 'wpps_cptb_tax_delete', 'wpps_cptb_tax_delete_' . $key, [ 'key' => $key ] ) ); ?>" class="button button-link-delete cptb-delete">
					<?php esc_html_e( 'Delete Taxonomy', 'wppowersuite-lite' ); ?>
				</a>
			<?php endif; ?>
			<button type="submit" class="button button-primary">
				<?php echo $is_edit ? esc_html__( 'Save Changes', 'wppowersuite-lite' ) : esc_html__( 'Create Taxonomy', 'wppowersuite-lite' ); ?>
			</button>
		</div>
	</form>
</div>
