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

namespace CptBuilder;

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

$key   = (string) ( $definition['key'] ?? '' );
$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 taxonomy.', 'wp-powersuite' ); ?>
	</p>
<?php elseif ( $key === '' ) : ?>
	<p class="description">
		<?php esc_html_e( 'Save this taxonomy first, then return here to create field groups for its terms.', 'wp-powersuite' ); ?>
	</p>
<?php else : ?>
	<?php
	$groups     = \CustomFields\CptBridge::groupsForTaxonomy( $key );
	$create_url = \CustomFields\CptBridge::createTaxonomyGroupUrl( $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 taxonomy appear on add/edit term screens. Values are stored as native term 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: taxonomy singular label. */
					esc_html__( 'No field groups are assigned to “%s” terms 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( 'Read term meta in templates with get_term_meta() or wps_cf_get_term():', 'wp-powersuite' ); ?></p>
	<ul class="cptb-usage-list">
		<li><code>&lt;?php echo esc_html( wps_cf_get_term( '<?php echo esc_html( $key ); ?>_term_fields_description', $term_id ) ); ?&gt;</code></li>
	</ul>
<?php endif; ?>
