<?php
/**
 * CPT Builder - Custom Fields tab.
 *
 * @package CptBuilder
 *
 * @var array<string, mixed> $definition
 */

namespace CptBuilder;

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

$key     = (string) ( $definition['key'] ?? '' );
$enabled = ! empty( $definition['enabled'] );
$cf_on   = class_exists( '\CustomFields\CptBridge' ) && \CustomFields\CptBridge::isCustomFieldsEnabled();
?>
<h2><?php esc_html_e( 'Custom Fields', 'wp-powersuite' ); ?></h2>

<?php if ( ! $cf_on ) : ?>
	<p class="description">
		<?php esc_html_e( 'Enable the Custom Fields module in WP PowerSuite to attach field groups to this post type.', 'wp-powersuite' ); ?>
	</p>
<?php elseif ( $key === '' ) : ?>
	<p class="description">
		<?php esc_html_e( 'Save this post type first, then return here to create field groups for it.', 'wp-powersuite' ); ?>
	</p>
<?php else : ?>
	<?php
	$groups      = \CustomFields\CptBridge::groupsForPostType( $key );
	$create_url  = \CustomFields\CptBridge::createGroupUrl( $key );
	$fields_url  = \CustomFields\CptBridge::fieldsAdminUrl();
	$labels      = isset( $definition['labels'] ) && is_array( $definition['labels'] ) ? $definition['labels'] : [];
	$singular    = (string) ( $labels['singular_name'] ?? $key );
	?>
	<p class="description">
		<?php esc_html_e( 'Field groups with a location rule matching this post type appear in the block editor sidebar and on the edit screen. Values are stored as native post meta.', 'wp-powersuite' ); ?>
	</p>

	<p>
		<a href="<?php echo esc_url( $create_url ); ?>" class="button button-primary">
			<?php esc_html_e( 'Create Field Group', 'wp-powersuite' ); ?>
		</a>
		<a href="<?php echo esc_url( $fields_url ); ?>" class="button button-secondary">
			<?php esc_html_e( 'All Field Groups', 'wp-powersuite' ); ?>
		</a>
	</p>

	<?php if ( $groups === [] ) : ?>
		<div class="cptb-empty cptb-empty--inline">
			<p>
				<?php
				printf(
					/* translators: %s: post type singular label. */
					esc_html__( 'No field groups are assigned to “%s” yet.', 'wp-powersuite' ),
					esc_html( $singular )
				);
				?>
			</p>
		</div>
	<?php else : ?>
		<table class="widefat striped">
			<thead>
				<tr>
					<th scope="col"><?php esc_html_e( 'Field Group', 'wp-powersuite' ); ?></th>
					<th scope="col"><?php esc_html_e( 'Status', 'wp-powersuite' ); ?></th>
					<th scope="col"><?php esc_html_e( 'Fields', 'wp-powersuite' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<?php foreach ( $groups as $group_key => $group ) : ?>
					<?php
					$edit_url = \WP_PowerSuite\Core\NetworkScope::adminUrl(
						'admin.php',
						[
							'page'   => \CustomFields\Admin::PAGE_SLUG,
							'action' => 'edit',
							'key'    => (string) $group_key,
						]
					);
					$field_count = isset( $group['fields'] ) && is_array( $group['fields'] ) ? count( $group['fields'] ) : 0;
					?>
					<tr>
						<td>
							<strong><a href="<?php echo esc_url( $edit_url ); ?>"><?php echo esc_html( (string) ( $group['title'] ?? $group_key ) ); ?></a></strong>
							<br><code><?php echo esc_html( (string) $group_key ); ?></code>
						</td>
						<td>
							<?php echo ! empty( $group['enabled'] ) ? esc_html__( 'Enabled', 'wp-powersuite' ) : esc_html__( 'Disabled', 'wp-powersuite' ); ?>
						</td>
						<td><?php echo esc_html( (string) $field_count ); ?></td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
	<?php endif; ?>

	<h3><?php esc_html_e( 'Front-end usage', 'wp-powersuite' ); ?></h3>
	<p class="description"><?php esc_html_e( 'Use these patterns in templates, blocks, or page builders:', 'wp-powersuite' ); ?></p>
	<ul class="cptb-usage-list">
		<li><code>&lt;?php echo esc_html( wps_cf_get_field( '<?php echo esc_html( $key ); ?>_details', 'field_name' ) ); ?&gt;</code></li>
		<li><code>[wpps_cf group="<?php echo esc_html( $key ); ?>_details" field="field_name"]</code></li>
		<li><code>{wp_powersuite_cf:<?php echo esc_html( $key ); ?>_details:field_name}</code></li>
	</ul>
<?php endif; ?>
