<?php

/**
 * Accordion: Subscribe Buttons.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

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

$subscribe_buttons = array();

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

	$subscribe_buttons = array_filter( $subscribe_buttons );
}
?>
<div class="aiopod-form-controls">
	<div class="aiopod-notice aiopod-notice-info">
		<?php esc_html_e( 'Add subscribe buttons for popular podcast platforms so listeners can easily follow your show on their preferred app.', 'aio-podcast-studio' ); ?>
	</div>

	<!-- Field: Subscribe Buttons -->
	<div id="aiopod-form-control-subscribe_buttons" class="aiopod-form-control aiopod-repeatable-ui">
		<table class="aiopod-repeatable-table form-table striped">
			<tbody>
				<?php foreach ( $subscribe_buttons as $subscribe_button ) : ?>
					<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="subscribe_platform[]" class="widefat">
										<?php
										require_once AIOPOD_PLUGIN_DIR . 'includes/podcast-directories.php';

										printf(
											'<option value="">— %s —</option>', 
											esc_html__( 'Select Platform', 'aio-podcast-studio' )
										);

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

								<label class="aiopod-repeatable-field">
									<input type="text" name="subscribe_url[]" class="widefat" placeholder="<?php esc_attr_e( 'Enter the URL here', 'aio-podcast-studio' ); ?>" value="<?php echo esc_url( $subscribe_button['url'] ); ?>" />
								</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 Subscribe Button -->
		<div class="aiopod-repeatable-button">
			<button type="button" class="aiopod-button aiopod-button-add button" data-href="#aiopod-template-subscribe_buttons">
				<?php esc_html_e( 'Add Subscribe Button', 'aio-podcast-studio' ); ?>
			</button>
		</div>
	</div>
</div>

<template id="aiopod-template-subscribe_buttons">
	<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="subscribe_platform[]" class="widefat">
						<?php
						require_once AIOPOD_PLUGIN_DIR . 'includes/podcast-directories.php';

						printf(
							'<option value="">— %s —</option>', 
							esc_html__( 'Select Platform', 'aio-podcast-studio' )
						);

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

				<label class="aiopod-repeatable-field">
					<input type="text" name="subscribe_url[]" class="widefat" placeholder="<?php esc_attr_e( 'Enter the URL here', 'aio-podcast-studio' ); ?>" />
				</label>													
		
				<button type="button" class="aiopod-button aiopod-button-delete button">
					<span class="dashicons dashicons-trash"></span>
				</button>
			</div>
		</td>
	</tr>		
</template>