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

namespace CptBuilder;

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

$key = (string) ( $definition['key'] ?? '' );
?>
<h2><?php esc_html_e( 'Custom Fields', 'wppowersuite-lite' ); ?></h2>

<?php if ( $key === '' ) : ?>
	<p class="description">
		<?php esc_html_e( 'Save this post type first, then return here to create field groups for it.', 'wppowersuite-lite' ); ?>
	</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 );
	$group_key  = $key . '_details';
	$field_name = 'summary';
	if ( $groups !== [] ) {
		$first_group = reset( $groups );
		$first_key   = (string) key( $groups );
		if ( $first_key !== '' ) {
			$group_key = $first_key;
		}
		if ( is_array( $first_group ) && ! empty( $first_group['fields'][0]['name'] ) ) {
			$field_name = (string) $first_group['fields'][0]['name'];
		}
	}
	?>
	<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.', 'wppowersuite-lite' ); ?>
	</p>

	<p>
		<a href="<?php echo esc_url( $create_url ); ?>" class="button button-primary">
			<?php esc_html_e( 'Create Field Group', 'wppowersuite-lite' ); ?>
		</a>
		<a href="<?php echo esc_url( $fields_url ); ?>" class="button button-secondary">
			<?php esc_html_e( 'All Field Groups', 'wppowersuite-lite' ); ?>
		</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.', 'wppowersuite-lite' ),
					esc_html( $singular )
				);
				?>
			</p>
		</div>
	<?php else : ?>
		<table class="widefat striped">
			<thead>
				<tr>
					<th scope="col"><?php esc_html_e( 'Field Group', 'wppowersuite-lite' ); ?></th>
					<th scope="col"><?php esc_html_e( 'Status', 'wppowersuite-lite' ); ?></th>
					<th scope="col"><?php esc_html_e( 'Fields', 'wppowersuite-lite' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<?php foreach ( $groups as $group_key_row => $group ) : ?>
					<?php
					$edit_url = \WP_PowerSuite\Core\NetworkScope::adminUrl(
						'admin.php',
						[
							'page'   => \CustomFields\Admin::PAGE_SLUG,
							'action' => 'edit',
							'key'    => (string) $group_key_row,
						]
					);
					$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_row ) ); ?></a></strong>
							<br><code><?php echo esc_html( (string) $group_key_row ); ?></code>
						</td>
						<td>
							<?php echo ! empty( $group['enabled'] ) ? esc_html__( 'Enabled', 'wppowersuite-lite' ) : esc_html__( 'Disabled', 'wppowersuite-lite' ); ?>
						</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', 'wppowersuite-lite' ); ?></h3>
	<p class="description"><?php esc_html_e( 'Use these patterns in templates, blocks, or page builders:', 'wppowersuite-lite' ); ?></p>
	<ul class="cptb-usage-list">
		<li><code>&lt;?php echo esc_html( wps_cf_get_field( '<?php echo esc_html( $group_key ); ?>', '<?php echo esc_html( $field_name ); ?>' ) ); ?&gt;</code></li>
		<li><code>[wpps_cf group="<?php echo esc_html( $group_key ); ?>" field="<?php echo esc_html( $field_name ); ?>"]</code></li>
		<li><code>{wp_powersuite_cf:<?php echo esc_html( $group_key ); ?>:<?php echo esc_html( $field_name ); ?>}</code></li>
	</ul>
<?php endif; ?>
