<?php
/**
 * CPT Builder - list view.
 *
 * @package CptBuilder
 *
 * @var \CptBuilder\Admin                            $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_export', 'wpps_cptb_export' );
$export_php_all = $admin->actionUrl( 'wpps_cptb_export_php', 'wpps_cptb_export_php' );
$cf_enabled     = class_exists( '\CustomFields\CptBridge' ) && \CustomFields\CptBridge::isCustomFieldsEnabled();
?>
<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-screenoptions',
			'title'     => __( 'CPT Builder', 'wp-powersuite' ),
			'tagline'   => __( 'Create and manage custom post types without writing code.', 'wp-powersuite' ),
			'actions'   => [
				[
					'label' => __( 'Add New Post Type', '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 = '';
	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_import">
			<?php wp_nonce_field( 'wpps_cptb_import' ); ?>
			<h2><?php esc_html_e( 'Import Post Types', '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,"post_types":[ ... ],"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 post types 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-screenoptions" aria-hidden="true"></span>
			<h3><?php esc_html_e( 'No custom post types yet', 'wp-powersuite' ); ?></h3>
			<p class="description"><?php esc_html_e( 'Create your first custom post type to start organizing content beyond posts and pages.', 'wp-powersuite' ); ?></p>
			<a href="<?php echo esc_url( $add_url ); ?>" class="button button-primary button-hero"><?php esc_html_e( 'Add New Post Type', 'wp-powersuite' ); ?></a>
		</div>
	<?php else : ?>
		<?php
		require_once __DIR__ . '/../includes/class-post-type-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 PostTypeListTable( $admin, $status, $search, $cf_enabled );
		$list_table->prepare_items();
		?>

		<form method="get" id="cptb-post-types-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="cptb-list-header">
				<?php $list_table->views(); ?>
				<?php $list_table->search_box( __( 'Search post types', 'wp-powersuite' ), 'post_type' ); ?>
			</div>
		</form>

		<form method="post" id="cptb-post-types-list-form">
			<?php wp_nonce_field( 'bulk-post_types' ); ?>
			<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>
