<?php

/**
 * Admin form: Episodes Widget.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

// Exit if accessed directly
if ( ! defined( 'WPINC' ) ) {
	die;
}
?>
<div class="aiopod aiopod-widget-form aiopod-widget-form-episodes aiopod-layout-<?php echo esc_attr( $instance['layout'] ); ?><?php if ( empty( $instance['enable_filters'] ) ) : ?> aiopod-filters-disabled<?php endif; ?><?php if ( empty( $instance['enable_player'] ) ) : ?> aiopod-player-disabled<?php endif; ?> aiopod-details">
	<div class="aiopod-form-control aiopod-form-control-title">
		<label class="aiopod-form-label" for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title', 'aio-podcast-studio' ); ?></label> 
		<input type="text" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" class="aiopod-widget-field aiopod-widget-field-title widefat" value="<?php echo esc_attr( $instance['title'] ); ?>" />
	</div>

	<?php
	$i = 0;
	foreach ( $this->fields['episodes']['sections'] as $key => $section ) :	?>
		<details<?php if ( 0 === $i ) : ?> open<?php endif; ++$i; ?>>
			<summary>
				<?php echo wp_kses_post( $section['title'] ); ?>
			</summary>
			
			<div class="aiopod-form-controls aiopod-details-content">
				<?php foreach ( $section['fields'] as $field ) :
					$field_name = $field['name'];

					$classes = array();
					$classes[] = 'aiopod-form-control';
					$classes[] = 'aiopod-form-control-' . esc_attr( $field['name'] );

					if ( isset( $field['class'] ) && ! empty( $field['class'] ) ) {
						$classes[] = esc_attr( $field['class'] );
					}
					?>
					<div class="<?php echo implode( ' ', $classes ); ?>">
						<?php if ( 'header' === $field['type'] ) : ?>
							<label class="aiopod-form-label" for="<?php echo esc_attr( $this->get_field_id( $field_name ) ); ?>"><?php echo esc_html( $field['label'] ); ?></label>
						<?php elseif ( 'text' === $field['type'] || 'url' === $field['type'] || 'number' === $field['type'] ) : ?>
							<label class="aiopod-form-label" for="<?php echo esc_attr( $this->get_field_id( $field_name ) ); ?>"><?php echo esc_html( $field['label'] ); ?></label> 
							<input type="text" name="<?php echo esc_attr( $this->get_field_name( $field_name ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( $field_name ) ); ?>" class="aiopod-widget-field aiopod-widget-field-<?php echo esc_attr( $field_name ); ?> widefat" value="<?php echo esc_attr( $instance[ $field_name ] ); ?>" />
						<?php elseif ( 'select' === $field['type'] ) : ?>
							<label class="aiopod-form-label" for="<?php echo esc_attr( $this->get_field_id( $field_name ) ); ?>"><?php echo esc_html( $field['label'] ); ?></label>
							<select name="<?php echo esc_attr( $this->get_field_name( $field_name ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( $field_name ) ); ?>" class="aiopod-widget-field aiopod-widget-field-<?php echo esc_attr( $field_name ); ?> widefat"> 
								<?php				
								foreach ( $field['options'] as $key => $value ) {
									printf( 
										'<option value="%s"%s>%s</option>', 
										esc_attr( $key ), 
										selected( $key, $instance[ $field_name ], false ), 
										esc_html( $value )
									);
								}
								?>
							</select>
						<?php elseif ( 'checkbox' === $field['type'] ) : ?>						
							<label for="<?php echo esc_attr( $this->get_field_id( $field_name ) ); ?>" class="aiopod-form-label">
								<input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( $field_name ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( $field_name ) ); ?>" class="aiopod-widget-field aiopod-widget-field-<?php echo esc_attr( $field_name ); ?>" value="1" <?php checked( 1, $instance[ $field_name ] ); ?> />
								<?php echo esc_html( $field['label'] ); ?>
							</label>
						<?php elseif ( 'color' === $field['type'] ) : ?>
							<label class="aiopod-form-label" for="<?php echo esc_attr( $this->get_field_id( $field_name ) ); ?>"><?php echo esc_html( $field['label'] ); ?></label> 
							<input type="text" name="<?php echo esc_attr( $this->get_field_name( $field_name ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( $field_name ) ); ?>" class="aiopod-widget-field aiopod-widget-field-<?php echo esc_attr( $field_name ); ?> aiopod-color-picker widefat" value="<?php echo esc_attr( $instance[ $field_name ] ); ?>" />
						<?php elseif ( 'podcasts' === $field['type'] ) : ?>
							<label class="aiopod-form-label" for="<?php echo esc_attr( $this->get_field_id( $field_name ) ); ?>"><?php echo esc_html( $field['label'] ); ?></label>
							<select name="<?php echo esc_attr( $this->get_field_name( $field_name ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( $field_name ) ); ?>" class="aiopod-widget-field aiopod-widget-field-<?php echo esc_attr( $field_name ); ?> widefat"> 
								<?php
								printf(
									'<option value="">%s</option>',
									'— ' . esc_html__( 'Select Podcast', 'aio-podcast-studio' ) . ' —'
								);

								$posts = get_posts(
									array(
										'post_type'   => 'aiopod_podcasts',
										'numberposts' => -1
									)
								);

								if ( $posts ) {
									foreach ( $posts as $post ) {
										printf( 
											'<option value="%s"%s>%s</option>', 
											esc_attr( $post->ID ),
											selected( $post->ID, $instance[ $field_name ], false ), 
											esc_html( $post->post_title )
										);
									}
								}
								?>
							</select>
						<?php elseif ( 'tags' === $field['type'] ) : ?>
							<label class="aiopod-form-label" for="<?php echo esc_attr( $this->get_field_id( $field_name ) ); ?>"><?php echo esc_html( $field['label'] ); ?></label> 
							<ul id="<?php echo esc_attr( $this->get_field_id( $field_name ) ); ?>" class="aiopod-widget-field aiopod-widget-field-<?php echo esc_attr( $field_name ); ?> aiopod-checklist widefat">
								<?php
								$args = array(
									'taxonomy'      => 'aiopod_tags',
									'selected_cats' => array_map( 'intval', $instance[ $field_name ] ),
									'walker'        => new AIOPOD_Walker_Terms_Checklist( $this->get_field_name( $field_name ) ),
									'checked_ontop' => false
								); 

								wp_terms_checklist( 0, $args );
								?>
							</ul>
						<?php elseif ( 'participants' === $field['type'] ) : ?>
							<label class="aiopod-form-label" for="<?php echo esc_attr( $this->get_field_id( $field_name ) ); ?>"><?php echo esc_html( $field['label'] ); ?></label>
							<ul id="<?php echo esc_attr( $this->get_field_id( $field_name ) ); ?>" class="aiopod-widget-field aiopod-widget-field-<?php echo esc_attr( $field_name ); ?> aiopod-checklist widefat">
								<?php
								$args = array(
									'taxonomy'      => 'aiopod_participants',
									'selected_cats' => array_map( 'intval', $instance[ $field_name ] ),
									'walker'        => new AIOPOD_Walker_Terms_Checklist( $this->get_field_name( $field_name ) ),
									'checked_ontop' => false
								); 

								wp_terms_checklist( 0, $args );
								?>
							</ul>
						<?php endif; ?>

						<?php if ( ! empty( $field['description'] ) ) : // Description ?>
							<p class="description"><?php echo wp_kses_post( $field['description'] ); ?></p>
						<?php endif; ?>
					</div>
				<?php endforeach; ?>
			</div>
		</details>				
	<?php endforeach; ?>
</div>