<?php
/**
 * CPT Builder - taxonomy list view.
 *
 * @package CptBuilder
 *
 * @var \CptBuilder\TaxonomyAdmin                   $admin
 * @var array<string, array<string, mixed>>        $definitions
 * @var array{type: string, text: string}|null     $notice
 */

namespace CptBuilder;

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

$definitions = isset( $definitions ) && is_array( $definitions ) ? $definitions : [];
$add_url     = $admin->url( [ 'action' => 'add' ] );
$export_all     = $admin->actionUrl( 'wpps_cptb_tax_export', 'wpps_cptb_tax_export' );
$export_php_all = $admin->actionUrl( 'wpps_cptb_tax_export_php', 'wpps_cptb_tax_export_php' );
?>
<div class="wrap wpps-cptb">
	<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'     => __( 'Taxonomies', 'wp-powersuite' ),
			'tagline'   => __( 'Create and manage custom taxonomies to organize your content.', 'wp-powersuite' ),
			'actions'   => [
				[
					'label' => __( 'Add New Taxonomy', 'wp-powersuite' ),
					'url'   => $add_url,
					'icon'  => 'dashicons-plus-alt',
				],
			],
			'meta_html' => sprintf(
				'<a href="%1$s" class="button button-secondary">%2$s</a> <a href="%3$s" class="button button-secondary">%4$s</a> <button type="button" class="button button-secondary" id="cptb-import-toggle">%5$s</button>',
				esc_url( $export_all ),
				esc_html__( 'Export JSON', 'wp-powersuite' ),
				esc_url( $export_php_all ),
				esc_html__( 'Export PHP', 'wp-powersuite' ),
				esc_html__( 'Import', 'wp-powersuite' )
			),
		]
	);
	?>

	<?php
	$current_section = 'taxonomies';
	include __DIR__ . '/section-nav.php';
	?>

	<?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; ?>

	<div class="cptb-import-panel" id="cptb-import-panel" hidden>
		<form method="post" action="<?php echo esc_url( $admin->postEndpoint() ); ?>" enctype="multipart/form-data">
			<input type="hidden" name="action" value="wpps_cptb_tax_import">
			<?php wp_nonce_field( 'wpps_cptb_tax_import' ); ?>
			<h2><?php esc_html_e( 'Import Taxonomies', 'wp-powersuite' ); ?></h2>
			<p class="description"><?php esc_html_e( 'Upload a .json export file or paste the JSON below. Combined exports import both post types and taxonomies in one go. Every field is re-validated and sanitized on import.', 'wp-powersuite' ); ?></p>
			<p>
				<input type="file" name="import_file" accept=".json,application/json">
			</p>
			<textarea name="import_json" class="large-text code" rows="8" placeholder='{"schema":1,"taxonomies":[ ... ]}'></textarea>
			<p>
				<label><input type="checkbox" name="import_disabled" value="1" checked> <?php esc_html_e( 'Import as disabled (recommended)', 'wp-powersuite' ); ?></label><br>
				<label><input type="checkbox" name="overwrite" value="1"> <?php esc_html_e( 'Overwrite taxonomies with the same key', 'wp-powersuite' ); ?></label>
			</p>
			<p>
				<button type="submit" class="button button-primary"><?php esc_html_e( 'Import', 'wp-powersuite' ); ?></button>
			</p>
		</form>
	</div>

	<?php if ( $definitions === [] ) : ?>
		<div class="cptb-empty">
			<span class="dashicons dashicons-category" aria-hidden="true"></span>
			<h3><?php esc_html_e( 'No custom taxonomies yet', 'wp-powersuite' ); ?></h3>
			<p class="description"><?php esc_html_e( 'Create your first taxonomy to group and filter your posts and custom post types.', 'wp-powersuite' ); ?></p>
			<a href="<?php echo esc_url( $add_url ); ?>" class="button button-primary button-hero"><?php esc_html_e( 'Add New Taxonomy', 'wp-powersuite' ); ?></a>
		</div>
	<?php else : ?>
		<?php
		require_once __DIR__ . '/../includes/class-taxonomy-list-table.php';

		$status     = isset( $_GET['status'] ) ? sanitize_key( wp_unslash( (string) $_GET['status'] ) ) : 'all'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		if ( ! in_array( $status, [ 'all', 'enabled', 'disabled' ], true ) ) {
			$status = 'all';
		}
		$search     = isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['s'] ) ) : '';
		$list_table = new TaxonomyListTable( $admin, $status, $search );
		$list_table->prepare_items();
		?>

		<form method="get" id="cptb-taxonomies-filter">
			<input type="hidden" name="page" value="<?php echo esc_attr( Admin::PAGE_SLUG ); ?>" />
			<input type="hidden" name="section" value="<?php echo esc_attr( TaxonomyAdmin::SECTION ); ?>" />
			<input type="hidden" name="status" value="<?php echo esc_attr( $status ); ?>" />
			<div class="cptb-list-header">
				<?php $list_table->views(); ?>
				<?php $list_table->search_box( __( 'Search taxonomies', 'wp-powersuite' ), 'taxonomy' ); ?>
			</div>
		</form>

		<form method="post" id="cptb-taxonomies-list-form">
			<?php wp_nonce_field( 'bulk-taxonomies' ); ?>
			<input type="hidden" name="page" value="<?php echo esc_attr( Admin::PAGE_SLUG ); ?>" />
			<input type="hidden" name="section" value="<?php echo esc_attr( TaxonomyAdmin::SECTION ); ?>" />
			<input type="hidden" name="status" value="<?php echo esc_attr( $status ); ?>" />
			<?php $list_table->display(); ?>
		</form>
	<?php endif; ?>
</div>
