<?php

/**
 * Accordion: Add Participants.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

// Exit if accessed directly
if ( ! defined( 'WPINC' ) ) {
	die;
}

$raw_participants = aiopod_get_participants();
$participants     = array();

if ( ! empty( $post_meta['participants'] ) && is_array( $post_meta['participants'] ) ) {
	foreach ( $post_meta['participants'] as $participant ) {
		$participants[] = maybe_unserialize( $participant );
	}

	$participants = array_filter( $participants );
}

$participant_roles = aiopod_get_participant_roles();
?>
<div class="aiopod-form-controls">
	<div class="aiopod-notice aiopod-notice-info">
		<?php
		printf(
			/* translators: %s: link to the Participants taxonomy admin page */
			esc_html__( 'Attach participants/contributors like hosts and guests to this episode. To add new participants, go to the main %s page in your left sidebar.', 'aio-podcast-studio' ),
			'<a href="' . esc_url( admin_url( 'edit-tags.php?taxonomy=aiopod_participants' ) ) . '" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Participants', 'aio-podcast-studio' ) . '</a>'
		);
		?>
	</div>

	<!-- Field: Participants -->
	<div id="aiopod-form-control-participants" class="aiopod-form-control aiopod-repeatable-ui">
		<table class="aiopod-repeatable-table form-table striped">
			<tbody>
				<?php foreach ( $participants as $key => $participant ) : ?>
					<tr>
						<td class="aiopod-sort-handle">
							<span class="dashicons dashicons-move"></span>
						</td>
						<td>
							<div class="aiopod-repeatable-fields">
								<label class="aiopod-repeatable-field">
									<select name="participant_id[]" class="widefat">
										<?php
										printf(
											'<option value="">— %s —</option>', 
											esc_html__( 'Select Participant', 'aio-podcast-studio' )
										);

										foreach ( $raw_participants as $key => $label ) {
											printf( 
												'<option value="%s"%s>%s</option>', 
												esc_attr( $key ), 
												selected( $key, $participant['id'], false ), 
												esc_html( $label )
											);
										}									
										?>
									</select>
								</label>	

								<label class="aiopod-repeatable-field">
									<select name="participant_role[]" class="widefat">
										<?php
										printf(
											'<option value="">— %s —</option>', 
											esc_html__( 'Select Role', 'aio-podcast-studio' )
										);

										foreach ( $participant_roles as $role ) {
											printf( 
												'<option value="%s"%s>%s</option>', 
												esc_attr( $role ), 
												selected( $role, $participant['role'], false ),
												esc_html( $role )
											);
										}									
										?>
									</select>
								</label>													
						
								<button type="button" class="aiopod-button aiopod-button-delete button">
									<span class="dashicons dashicons-trash"></span>
								</button>
							</div>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>

		<!-- Button: Add Participant -->
		<div class="aiopod-repeatable-button">
			<button type="button" class="aiopod-button aiopod-button-add button" data-href="#aiopod-template-participants">
				<?php esc_html_e( 'Add Participant', 'aio-podcast-studio' ); ?>
			</button>
		</div>
	</div>
</div>

<template id="aiopod-template-participants">
	<tr>
		<td class="aiopod-sort-handle">
			<span class="dashicons dashicons-move"></span>
		</td>
		<td>
			<div class="aiopod-repeatable-fields">
				<label class="aiopod-repeatable-field">
					<select name="participant_id[]" class="widefat">
						<?php
						printf(
							'<option value="">— %s —</option>', 
							esc_html__( 'Select Participant', 'aio-podcast-studio' )
						);

						foreach ( $raw_participants as $key => $label ) {
							printf( 
								'<option value="%s">%s</option>', 
								esc_attr( $key ), 
								esc_html( $label )
							);
						}									
						?>
					</select>		
				</label>	

				<label class="aiopod-repeatable-field">
					<select name="participant_role[]" class="widefat">
						<?php
						printf(
							'<option value="">— %s —</option>', 
							esc_html__( 'Select Role', 'aio-podcast-studio' )
						);

						foreach ( $participant_roles as $role ) {
							printf( 
								'<option value="%s">%s</option>', 
								esc_attr( $role ),
								esc_html( $role )
							);
						}									
						?>
					</select>
				</label>													
		
				<button type="button" class="aiopod-button aiopod-button-delete button">
					<span class="dashicons dashicons-trash"></span>
				</button>
			</div>
		</td>
	</tr>
</template>