<?php

/**
 * Admin form: Podcasts 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-podcasts 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['podcasts']['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 ( 'categories' === $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_categories',
									'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>