<?php
/**
 * CPT Builder - Basic tab.
 *
 * @package CptBuilder
 *
 * @var array<string, mixed> $definition
 * @var bool                 $is_edit
 * @var string               $key
 */

namespace CptBuilder;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>
<h2><?php esc_html_e( 'Basic settings', 'wp-powersuite' ); ?></h2>
<p class="description"><?php esc_html_e( 'The key and labels are the foundation of your post type. The key cannot be changed after creation.', 'wp-powersuite' ); ?></p>

<table class="form-table" role="presentation">
	<tbody>
		<tr>
			<th scope="row"><label for="cptb-key"><?php esc_html_e( 'Post type key', 'wp-powersuite' ); ?> <span class="cptb-req">*</span></label></th>
			<td>
				<?php if ( $is_edit ) : ?>
					<code class="cptb-key-readonly"><?php echo esc_html( $key ); ?></code>
					<p class="description"><?php esc_html_e( 'The key is locked after creation to keep stored content reachable.', 'wp-powersuite' ); ?></p>
				<?php else : ?>
					<input type="text" id="cptb-key" name="key" value="<?php echo esc_attr( $key ); ?>" class="regular-text" maxlength="<?php echo esc_attr( (string) Config::MAX_KEY_LENGTH ); ?>" pattern="[a-z][a-z0-9_]*" required>
					<p class="description">
						<?php
						printf(
							/* translators: %d: maximum characters. */
							esc_html__( 'Lowercase letters, numbers, and underscores only. Must start with a letter. Maximum %d characters.', 'wp-powersuite' ),
							(int) Config::MAX_KEY_LENGTH
						);
						?>
					</p>
				<?php endif; ?>
			</td>
		</tr>
		<tr>
			<th scope="row"><label for="cptb-singular"><?php esc_html_e( 'Singular label', 'wp-powersuite' ); ?> <span class="cptb-req">*</span></label></th>
			<td>
				<input type="text" id="cptb-singular" name="labels[singular_name]" value="<?php echo esc_attr( cptb_label( $definition, 'singular_name' ) ); ?>" class="regular-text" placeholder="<?php esc_attr_e( 'e.g. Book', 'wp-powersuite' ); ?>" required>
				<p class="description"><?php esc_html_e( 'Name for one item.', 'wp-powersuite' ); ?></p>
			</td>
		</tr>
		<tr>
			<th scope="row"><label for="cptb-plural"><?php esc_html_e( 'Plural label', 'wp-powersuite' ); ?> <span class="cptb-req">*</span></label></th>
			<td>
				<input type="text" id="cptb-plural" name="labels[name]" value="<?php echo esc_attr( cptb_label( $definition, 'name' ) ); ?>" class="regular-text" placeholder="<?php esc_attr_e( 'e.g. Books', 'wp-powersuite' ); ?>" required<?php echo $is_edit ? '' : ' data-cptb-autoslug="cptb-key"'; ?>>
				<p class="description"><?php esc_html_e( 'Name for multiple items. Used to suggest the key and other labels.', 'wp-powersuite' ); ?></p>
			</td>
		</tr>
		<tr>
			<th scope="row"><label for="cptb-menu-icon"><?php esc_html_e( 'Menu icon', 'wp-powersuite' ); ?></label></th>
			<td>
				<?php
				$cptb_menu_icon  = (string) cptb_arg( $definition, 'menu_icon' );
				$cptb_icon_class = str_starts_with( $cptb_menu_icon, 'dashicons-' ) ? $cptb_menu_icon : 'dashicons-admin-post';
				?>
				<div class="cptb-icon-picker">
					<button type="button" class="cptb-icon-current" id="cptb-icon-current" aria-label="<?php esc_attr_e( 'Choose an icon', 'wp-powersuite' ); ?>">
						<span class="dashicons <?php echo esc_attr( $cptb_icon_class ); ?>"></span>
					</button>
					<input type="text" id="cptb-menu-icon" name="args[menu_icon]" value="<?php echo esc_attr( $cptb_menu_icon ); ?>" class="regular-text" placeholder="dashicons-admin-post">
					<div class="cptb-icon-grid" id="cptb-icon-grid" hidden>
						<input type="search" class="cptb-icon-search" id="cptb-icon-search" placeholder="<?php esc_attr_e( 'Search icons…', 'wp-powersuite' ); ?>">
						<div class="cptb-icon-list" id="cptb-icon-list"></div>
					</div>
				</div>
				<p class="description">
					<?php esc_html_e( 'Icon shown in the admin menu. Click the icon to browse, or enter a Dashicons class, image URL, base64 SVG, or "none".', 'wp-powersuite' ); ?>
					<a href="https://developer.wordpress.org/resource/dashicons/" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Dashicons reference', 'wp-powersuite' ); ?></a>
				</p>
			</td>
		</tr>
		<tr>
			<th scope="row"><label for="cptb-description"><?php esc_html_e( 'Description', 'wp-powersuite' ); ?></label></th>
			<td>
				<textarea id="cptb-description" name="args[description]" class="large-text" rows="2"><?php echo esc_textarea( (string) cptb_arg( $definition, 'description' ) ); ?></textarea>
				<p class="description"><?php esc_html_e( 'A short description of the post type. Shown in some themes and the REST API.', 'wp-powersuite' ); ?></p>
			</td>
		</tr>
		<?php if ( ! $is_edit && class_exists( '\CustomFields\CptBridge' ) && \CustomFields\CptBridge::isCustomFieldsEnabled() ) : ?>
		<tr>
			<th scope="row"><?php esc_html_e( 'Field group', 'wp-powersuite' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="create_default_field_group" value="1" checked>
					<?php esc_html_e( 'Create a default Custom Fields group for this post type', 'wp-powersuite' ); ?>
				</label>
				<p class="description"><?php esc_html_e( 'Adds a starter “Details” field group with a Summary field. You can customize it anytime under WP PowerSuite → Custom Fields.', 'wp-powersuite' ); ?></p>
			</td>
		</tr>
		<?php endif; ?>
	</tbody>
</table>
