<?php

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

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

$enable_chapters = isset( $post_meta['enable_chapters'] ) ? $post_meta['enable_chapters'][0] : 0;
$chapters = array();

if ( ! empty( $post_meta['chapters'] ) ) {
	foreach ( $post_meta['chapters'] as $chapter ) {
		$chapters[] = maybe_unserialize( $chapter );
	}

	$chapters = array_filter( $chapters );
}
?>
<div class="aiopod-form-controls">
	<div class="aiopod-notice aiopod-notice-info">
		<?php esc_html_e( 'Chapters help listeners jump to key moments in your episode for a more interactive experience.', 'aio-podcast-studio' ); ?>
	</div>

	<!-- Field: Enable Chapters? -->
	<div id="aiopod-form-control-enable_chapters" class="aiopod-form-control">					
		<label class="aiopod-form-field">
			<input type="checkbox" name="enable_chapters" id="aiopod-field-enable_chapters" value="1" <?php checked( $enable_chapters, 1 ); ?> />
			<?php esc_html_e( 'Enable Chapters?', 'aio-podcast-studio' ); ?>
		</label>						
	</div>			

	<!-- Field: Chapters -->
	<div id="aiopod-form-control-chapters" class="aiopod-form-control aiopod-repeatable-ui"<?php if ( empty( $enable_chapters ) ) { echo ' style="display:none;"'; } ?>>
		<table class="aiopod-repeatable-table form-table striped">
			<tbody>
				<?php foreach ( $chapters as $key => $chapter ) : ?>
					<tr>
						<td class="aiopod-sort-handle">
							<span class="dashicons dashicons-move"></span>
						</td>
						<td>
							<div class="aiopod-repeatable-fields">
								<label class="aiopod-repeatable-field">
									<?php esc_html_e( 'Time', 'aio-podcast-studio' ); ?>			
									<input type="text" name="chapter_time[]" class="widefat" placeholder="<?php esc_attr_e( 'HH:MM:SS', 'aio-podcast-studio' ); ?>" value="<?php echo esc_attr( $chapter['time'] ); ?>" />
								</label>	

								<label class="aiopod-repeatable-field">
									<?php esc_html_e( 'Label', 'aio-podcast-studio' ); ?>			
									<input type="text" name="chapter_label[]" class="widefat" placeholder="<?php esc_attr_e( 'Chapter Title', 'aio-podcast-studio' ); ?>" value="<?php echo esc_attr( $chapter['label'] ); ?>" />
								</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 New Chapter -->
		<div class="aiopod-repeatable-button">
			<button type="button" class="aiopod-button aiopod-button-add button" data-href="#aiopod-template-chapter">
				<?php esc_html_e( 'Add New Chapter', 'aio-podcast-studio' ); ?>
			</button>
		</div>
	</div>				
</div>

<template id="aiopod-template-chapter">
	<tr>
		<td class="aiopod-sort-handle">
			<span class="dashicons dashicons-move"></span>
		</td>
		<td>
			<div class="aiopod-repeatable-fields">
				<label class="aiopod-repeatable-field">
					<?php esc_html_e( 'Time', 'aio-podcast-studio' ); ?>		
					<input type="text" name="chapter_time[]" class="widefat" placeholder="<?php esc_attr_e( 'HH:MM:SS', 'aio-podcast-studio' ); ?>" />
				</label>

				<label class="aiopod-repeatable-field">
					<?php esc_html_e( 'Label', 'aio-podcast-studio' ); ?>			
					<input type="text" name="chapter_label[]" class="widefat" placeholder="<?php esc_attr_e( 'Chapter Title', '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>