<?php

/**
 * Shortcode Builder.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

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

$fields = aiopod_get_shortcode_fields();
?>
<div id="aiopod-shortcode-builder" class="aiopod aiopod-shortcode-builder"> 
    <div class="aiopod-container">
        <div class="aiopod-header">
			<h1><?php esc_html_e( 'Build your shortcode...', 'aio-podcast-studio' ); ?></h1>
	        <p><?php esc_html_e( 'Quickly generate a shortcode to display your podcasts and episodes anywhere on your site — no coding required.', 'aio-podcast-studio' ); ?></p>
		</div>

        <!-- Shortcode Forms -->
        <div id="aiopod-shortcode-forms" class="aiopod-body"> 
            <div class="aiopod-card">
                <h3>
					<span class="dashicons dashicons-admin-appearance"></span>
					<?php esc_html_e( 'Select the type of content you want to display', 'aio-podcast-studio' ); ?>
				</h3>

                <!-- Shortcode Selector -->  
                <select name="shortcode" id="aiopod-shortcode-selector" class="widefat" autofocus="autofocus">
                    <?php
                    foreach ( $fields as $shortcode => $params ) {
                        printf( 
                            '<option value="%s"%s>%s</option>', 
                            esc_attr( $shortcode ),
                            selected( $shortcode, 'episodes', false ), 
                            esc_html( $params['title'] )
                        );
                    }
                    ?>
                </select>
            </div>

            <!-- Shortcode Fields -->
            <?php foreach ( $fields as $shortcode => $params ) : ?>
                <div id="aiopod-shortcode-form-<?php echo esc_attr( $shortcode ); ?>" class="aiopod-shortcode-form aiopod-details"<?php if ( 'episodes' !== $shortcode ) { echo ' style="display: none;"'; } ?>>
                    <?php
                    $i = 0;
                    foreach ( $params['sections'] as $name => $section ) : ?>
                        <details<?php if ( 0 === $i ) { echo ' open'; ++$i; } ?>>
                            <summary>            
                                <?php echo esc_html( $section['title'] ); ?>
                            </summary>  
                                                    
                            <div class="aiopod-form-controls aiopod-details-content">
                                <?php foreach ( $section['fields'] as $field ) : 
                                    $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"><?php echo esc_html( $field['label'] ); ?></label>                                                 
                                        <?php elseif ( 'text' === $field['type'] || 'url' === $field['type'] || 'number' === $field['type'] ) : ?>                                        
                                            <label class="aiopod-form-label"><?php echo esc_html( $field['label'] ); ?></label>
                                            <input type="text" name="<?php echo esc_attr( $field['name'] ); ?>" class="aiopod-shortcode-field widefat" value="<?php echo esc_attr( $field['value'] ); ?>" data-default="<?php echo esc_attr( $field['value'] ); ?>" />
                                        <?php elseif ( 'textarea' === $field['type'] ) : ?>
                                            <label class="aiopod-form-label"><?php echo esc_html( $field['label'] ); ?></label>
                                            <textarea name="<?php echo esc_attr( $field['name'] ); ?>" class="aiopod-shortcode-field widefat" rows="8" data-default="<?php echo esc_attr( $field['value'] ); ?>"><?php echo esc_textarea( $field['value'] ); ?></textarea>
                                        <?php elseif ( 'select' === $field['type'] || 'radio' === $field['type'] ) : ?>
                                            <label class="aiopod-form-label"><?php echo esc_html( $field['label'] ); ?></label> 
                                            <select name="<?php echo esc_attr( $field['name'] ); ?>" class="aiopod-shortcode-field widefat" data-default="<?php echo esc_attr( $field['value'] ); ?>">
                                                <?php
                                                foreach ( $field['options'] as $value => $label ) {
                                                    printf( 
                                                        '<option value="%s"%s>%s</option>', 
                                                        esc_attr( $value ), 
                                                        selected( $value, $field['value'], false ), 
                                                        esc_html( $label ) 
                                                    );
                                                }
                                                ?>
                                            </select>                                                                               
                                        <?php elseif ( 'checkbox' === $field['type'] ) : ?>                                        
                                            <label class="aiopod-form-label">				
                                                <input type="checkbox" name="<?php echo esc_attr( $field['name'] ); ?>" class="aiopod-shortcode-field" value="1" data-default="<?php echo esc_attr( $field['value'] ); ?>" <?php checked( $field['value'] ); ?> />
                                                <?php echo esc_html( $field['label'] ); ?>
                                            </label>                                            
                                        <?php elseif ( 'color' === $field['type'] ) : ?>                                        
                                            <label class="aiopod-form-label"><?php echo esc_html( $field['label'] ); ?></label>
                                            <input type="text" name="<?php echo esc_attr( $field['name'] ); ?>" class="aiopod-shortcode-field aiopod-color-picker widefat" value="<?php echo esc_attr( $field['value'] ); ?>" data-default="<?php echo esc_attr( $field['value'] ); ?>" />
                                        <?php elseif ( 'podcasts' === $field['type'] ) : ?>
                                            <label class="aiopod-form-label"><?php echo esc_html( $field['label'] ); ?></label> 
                                            <select name="<?php echo esc_attr( $field['name'] ); ?>" class="aiopod-shortcode-field widefat" data-default="<?php echo esc_attr( $field['value'] ); ?>">
                                                <?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</option>', 
                                                            esc_attr( $post->ID ),
                                                            esc_html( $post->post_title )
                                                        );
                                                    }
                                                }
                                                ?>
                                            </select>  
                                        <?php elseif ( 'categories' === $field['type'] ) : ?>
                                            <label class="aiopod-form-label"><?php echo esc_html( $field['label'] ); ?></label> 
                                            <ul name="<?php echo esc_attr( $field['name'] ); ?>" class="aiopod-shortcode-field aiopod-checklist widefat" data-default="">
                                                <?php
                                                $args = array(
                                                    'taxonomy'      => 'aiopod_categories',
                                                    'walker'        => null,
                                                    'checked_ontop' => false
                                                ); 
                                            
                                                wp_terms_checklist( 0, $args );
                                                ?>
                                            </ul>
                                        <?php elseif ( 'tags' === $field['type'] ) : ?>
                                            <label class="aiopod-form-label"><?php echo esc_html( $field['label'] ); ?></label> 
                                            <ul name="<?php echo esc_attr( $field['name'] ); ?>" class="aiopod-shortcode-field aiopod-checklist widefat" data-default="">
                                                <?php
                                                $args = array(
                                                    'taxonomy'      => 'aiopod_tags',
                                                    'walker'        => null,
                                                    'checked_ontop' => false
                                                ); 
                                            
                                                wp_terms_checklist( 0, $args );
                                                ?>
                                            </ul>
                                        <?php elseif ( 'participants' === $field['type'] ) : ?>
                                            <label class="aiopod-form-label"><?php echo esc_html( $field['label'] ); ?></label> 
                                            <ul name="<?php echo esc_attr( $field['name'] ); ?>" class="aiopod-shortcode-field aiopod-checklist widefat" data-default="">
                                                <?php
                                                $args = array(
                                                    'taxonomy'      => 'aiopod_participants',
                                                    'walker'        => null,
                                                    'checked_ontop' => false
                                                ); 
                                            
                                                wp_terms_checklist( 0, $args );
                                                ?>
                                            </ul>
                                        <?php endif; ?>
                                        
                                        <?php if ( ! empty( $field['description'] ) ) : // Description ?>    
                                            <span class="description aiopod-text-muted"><?php echo wp_kses_post( $field['description'] ); ?></span>                        
                                        <?php endif; ?>
                                    </div>    
                                <?php endforeach; ?>
                            </div>
                        </details>
                    <?php endforeach; ?>
                </div>
            <?php endforeach; ?>

            <!-- Generate Shortcode -->
            <div>
                <button type="button" id="aiopod-generate-shortcode" class="aiopod-modal-button button button-primary button-hero" data-target="aiopod-shortcode-modal">
                    <?php esc_html_e( 'Generate Shortcode', 'aio-podcast-studio' ); ?>
                </button>
            </div>
        </div>
    </div>

    <!-- Shortcode Modal -->
    <dialog id="aiopod-shortcode-modal" class="aiopod aiopod-modal">
        <div class="aiopod-modal__header">
            <div class="aiopod-modal__title"><?php esc_html_e( 'Generate Shortcode', 'aio-podcast-studio' ); ?></div>
            <button type="button" class="aiopod-modal__close" aria-label="<?php esc_attr_e( 'Close', 'aio-podcast-studio' ); ?>">&times;</button>
        </div>
        <div class="aiopod-modal__body">
            <div><?php esc_html_e( 'Congrats! copy the shortcode below and paste it in your POST or PAGE where you need the gallery.', 'aio-podcast-studio' ); ?></div>
            <textarea id="aiopod-shortcode" class="aiopod-display-block widefat code" rows="3" autofocus="autofocus" onfocus="this.select()"></textarea>
            <div class="aiopod-text-right">
                <button type="button" class="aiopod-button-copy button button-primary">
                    <?php esc_html_e( 'Copy Shortcode', 'aio-podcast-studio' ); ?>
                </button>
            </div>
        </div>
    </dialog>
</div>
