<?php
/**
 * Custom Fields - list view.
 *
 * @package CustomFields
 *
 * @var \CustomFields\Admin                     $admin
 * @var array<string, array<string, mixed>>     $definitions
 * @var array{type: string, text: string}|null  $notice
 */

namespace CustomFields;

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

$definitions = isset( $definitions ) && is_array( $definitions ) ? $definitions : [];
$add_url     = $admin->url( [ 'action' => 'add' ] );
$export_all  = $admin->actionUrl( 'wpps_cf_export', 'wpps_cf_export' );
?>
<div class="wrap wpps-cf">
	<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-list-view',
			'title'     => __( 'Custom Fields', 'wp-powersuite' ),
			'tagline'   => __( 'Add fields to posts and terms, then show them anywhere with blocks, shortcodes, or page builders.', 'wp-powersuite' ),
			'actions'   => [
				[
					'label' => __( 'Add Field Group', 'wp-powersuite' ),
					'url'   => $add_url,
					'icon'  => 'dashicons-plus-alt',
				],
			],
			'meta_html' => sprintf(
				'<a href="%1$s" class="button button-secondary">%2$s</a> <button type="button" class="button button-secondary" id="cf-import-toggle">%3$s</button>',
				esc_url( $export_all ),
				esc_html__( 'Export All', 'wp-powersuite' ),
				esc_html__( 'Import', 'wp-powersuite' )
			),
		]
	);
	?>

	<?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="cf-import-panel" id="cf-import-panel" hidden>
		<form method="post" action="<?php echo esc_url( $admin->postEndpoint() ); ?>" enctype="multipart/form-data">
			<input type="hidden" name="action" value="wpps_cf_import">
			<?php wp_nonce_field( 'wpps_cf_import' ); ?>
			<h2><?php esc_html_e( 'Import Field Groups', 'wp-powersuite' ); ?></h2>
			<p class="description"><?php esc_html_e( 'Upload a .json export file or paste the JSON below. 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":2,"field_groups":[ ... ]}'></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 field groups 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="cf-empty">
			<div class="cf-empty__icon">
				<span class="dashicons dashicons-list-view" aria-hidden="true"></span>
			</div>
			<h2 class="cf-empty__title"><?php esc_html_e( 'No field groups yet', 'wp-powersuite' ); ?></h2>
			<p class="cf-empty__desc"><?php esc_html_e( 'Create a field group to add custom fields to your posts, pages, custom post types, or taxonomy terms.', 'wp-powersuite' ); ?></p>
			<a href="<?php echo esc_url( $add_url ); ?>" class="button button-primary"><?php esc_html_e( 'Add Field Group', 'wp-powersuite' ); ?></a>
		</div>
	<?php else : ?>
		<?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 FieldGroupListTable( $admin, $status, $search );
		$list_table->prepare_items();
		?>

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

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