<?php

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

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

$appearance_settings = aiopod_get_option( 'aiopod_appearance_settings' );

$post_parent = (int) $post->post_parent;
if ( empty( $post_parent ) && isset( $_GET['post_parent'] ) ) {
    $post_parent = (int) wp_unslash( $_GET['post_parent'] );
}

$episode_file       = isset( $post_meta['episode_file'] ) ? $post_meta['episode_file'][0] : '';
$episode_file_id    = isset( $post_meta['episode_file_id'] ) ? $post_meta['episode_file_id'][0] : 0;
$episode_file_type  = isset( $post_meta['episode_file_type'] ) ? $post_meta['episode_file_type'][0] : '';
$episode_file_size  = isset( $post_meta['episode_file_size'] ) ? $post_meta['episode_file_size'][0] : '';
$episode_duration   = isset( $post_meta['episode_duration'] ) ? $post_meta['episode_duration'][0] : '';
$image              = isset( $post_meta['image'] ) ? $post_meta['image'][0] : '';
$image_id           = isset( $post_meta['image_id'] ) ? $post_meta['image_id'][0] : 0;
$set_featured_image = isset( $post_meta['set_featured_image'] ) ? $post_meta['set_featured_image'][0] : 1;
$episode_type       = isset( $post_meta['episode_type'] ) ? $post_meta['episode_type'][0] : 'full';
$season_number      = isset( $post_meta['season_number'] ) ? $post_meta['season_number'][0] : '';
$episode_number     = isset( $post_meta['episode_number'] ) ? $post_meta['episode_number'][0] : '';
$is_explicit        = isset( $post_meta['is_explicit'] ) ? $post_meta['is_explicit'][0] : 0;
?>
<div class="aiopod-form-controls">
    <!-- Field: Select Podcast -->
    <div id="aiopod-form-control-post_parent" class="aiopod-form-control">
        <label for="aiopod-field-post_parent" class="aiopod-form-label">
            <?php esc_html_e( 'Select Podcast', 'aio-podcast-studio' ); ?>
            <span class="aiopod-form-required">*</span>
        </label>
        
        <div class="aiopod-form-field">
            <input type="hidden" name="old_parent" value="<?php echo esc_attr( $post_parent ); ?>" />

            <select name="post_parent" id="aiopod-field-post_parent" class="widefat">
                <?php
                printf(
                    '<option value="">%s</option>',
                    '— ' . esc_html__( 'Select an option', 'aio-podcast-studio' ) . ' —'
                );

                $posts = get_posts( array(
                    'post_type'        => 'aiopod_podcasts',
                    'numberposts'      => -1,
                    'orderby'          => 'title',
                    'order'            => 'ASC',
                    'no_found_rows'    => true,
                    'suppress_filters' => true
                ) );

                if ( $posts ) {
                    foreach ( $posts as $podcast ) {
                        printf(
                            '<option value="%d"%s>%s</option>',
                            esc_attr( $podcast->ID ),
                            selected( $podcast->ID, $post_parent, false ),
                            esc_html( $podcast->post_title )
                        );
                    }
                }
                ?>
            </select>
        </div>

        <p class="description">
            <?php esc_html_e( 'Select the podcast this episode belongs to. This helps group your episodes correctly and ensures they appear under the right show.', 'aio-podcast-studio' ); ?>
        </p>
    </div>

    <!-- Field: Episode File -->
    <div id="aiopod-form-control-episode_file" class="aiopod-form-control">
        <label for="aiopod-field-episode_file" class="aiopod-form-label">
            <?php esc_html_e( 'Episode File', 'aio-podcast-studio' ); ?>
            <span class="aiopod-form-required">*</span>
        </label>
        
        <div class="aiopod-form-field aiopod-media-wrapper">
            <div class="aiopod-media-uploader">
                <div class="aiopod-media-preview"></div>

                <div class="aiopod-input-group">
                    <input type="text" name="episode_file" id="aiopod-field-episode_file" class="widefat" placeholder="<?php esc_attr_e( 'Enter your direct file URL (OR) upload your file using the button here', 'aio-podcast-studio' ); ?> &rarr;" value="<?php echo esc_url( $episode_file ); ?>" />
                    <input type="hidden" name="episode_file_id" id="aiopod-field-episode_file_id" value="<?php echo esc_attr( $episode_file_id ); ?>" />
                    <input type="hidden" name="episode_file_type" id="aiopod-field-episode_file_type" value="<?php echo esc_attr( $episode_file_type ); ?>" />
                    <button type="button" class="aiopod-upload-media button">
                        <?php esc_html_e( 'Upload File', 'aio-podcast-studio' ); ?>
                    </button>
                </div>
                
                <p class="description">
                    <?php
                    echo wp_kses_post( apply_filters(
                        'aiopod_episode_file_field_description',
                        __( 'Upload your audio episode file (mp3, m4a, ogg, wav), or paste an external audio URL.', 'aio-podcast-studio' )
                    ) );
                    ?>
                </p>
                
                <br />

                <div class="aiopod-form-grid">
                    <!-- Field: Episode File Size -->
                    <div id="aiopod-form-control-episode_file_size" class="aiopod-form-control">
                        <label for="aiopod-field-episode_file_size" class="aiopod-form-label">
                            <?php esc_html_e( 'Episode File Size', 'aio-podcast-studio' ); ?>
                            <span class="aiopod-form-required">*</span>
                        </label>
                        
                        <div class="aiopod-form-field">
                            <input type="text" name="episode_file_size" id="aiopod-field-episode_file_size" class="widefat" placeholder="1024000" value="<?php echo esc_attr( $episode_file_size ); ?>" />
                        </div>
                        
                        <p class="description">
                            <?php esc_html_e( 'The file size in bytes.', 'aio-podcast-studio' ); ?>
                        </p>
                    </div>

                    <!-- Field: Episode Duration -->
                    <div id="aiopod-form-control-episode_duration" class="aiopod-form-control">
                        <label for="aiopod-field-episode_duration" class="aiopod-form-label">
                            <?php esc_html_e( 'Episode Duration', 'aio-podcast-studio' ); ?>
                            <span class="aiopod-form-required">*</span>
                        </label>
                        
                        <div class="aiopod-form-field">
                            <input type="text" name="episode_duration" id="aiopod-field-episode_duration" class="widefat" placeholder="1683" value="<?php echo esc_attr( $episode_duration ); ?>" />
                        </div>
                        
                        <p class="description">
                            <?php esc_html_e( 'Total runtime of the episode in seconds.', 'aio-podcast-studio' ); ?>
                        </p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Field: Episode Artwork -->
    <div id="aiopod-form-control-image" class="aiopod-form-control">
        <label for="aiopod-field-image" class="aiopod-form-label">
            <?php esc_html_e( 'Episode Artwork', 'aio-podcast-studio' ); ?>
            <span class="aiopod-form-required">*</span>
        </label>

        <div class="aiopod-form-field">
            <div class="aiopod-media-wrapper">
                <div class="aiopod-media-preview">
                    <?php
                    $image_src = $image;

                    if ( ! empty( $image_id ) ) {
                        $image_data = wp_get_attachment_image_src( (int) $image_id );
                        if ( ! empty( $image_data[0] ) ) {
                            $image_src = $image_data[0];
                        }
                    }

                    if ( empty( $image_src ) ) {
                        $image_src = AIOPOD_PLUGIN_PLACEHOLDER_IMAGE_URL;
                    }
                    ?>
                    <img src="<?php echo esc_url( $image_src ); ?>" alt="" loading="lazy" decoding="async" data-default="<?php echo esc_url( AIOPOD_PLUGIN_PLACEHOLDER_IMAGE_URL ); ?>" />
                </div>

                <div class="aiopod-media-uploader">
                    <p class="description">
                        <?php esc_html_e( 'For best results, use a square JPG or PNG image between 1400x1400 and 3000x3000 pixels.', 'aio-podcast-studio' ); ?>
                    </p>

                    <div class="aiopod-input-group">
                        <input type="text" name="image" id="aiopod-field-image" class="widefat" placeholder="<?php esc_attr_e( 'Enter your direct file URL (OR) upload your file using the button here', 'aio-podcast-studio' ); ?> &rarr;" value="<?php echo esc_url( $image ); ?>" data-default="<?php echo esc_url( AIOPOD_PLUGIN_PLACEHOLDER_IMAGE_URL ); ?>" />
                        <input type="hidden" name="image_id" id="aiopod-field-image_id" value="<?php echo esc_attr( $image_id ); ?>" />
                        <button type="button" class="aiopod-upload-media button" data-format="image">
                            <?php esc_html_e( 'Upload File', 'aio-podcast-studio' ); ?>
                        </button>
                    </div>                        

                    <?php if ( ! empty( $appearance_settings['enable_featured_images'] ) ) : ?>
                        <label>
                            <input type="checkbox" name="set_featured_image" id="aiopod-field-set_featured_image" value="1" <?php checked( $set_featured_image, 1 ); ?>/>
                            <?php esc_html_e( 'Store this image as a featured image', 'aio-podcast-studio' ); ?>
                        </label>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>    

    <!-- Field: Episode Type -->
    <div id="aiopod-form-control-episode_type" class="aiopod-form-control">
        <label for="aiopod-field-episode_type" class="aiopod-form-label">
            <?php esc_html_e( 'Episode Type', 'aio-podcast-studio' ); ?>
        </label>
        
        <div class="aiopod-form-field">
            <select name="episode_type" id="aiopod-field-episode_type" class="widefat">
                <?php
                $options = array(
                    'full'    => __( 'Full', 'aio-podcast-studio' ),
                    'trailer' => __( 'Trailer', 'aio-podcast-studio' ),
                    'bonus'   => __( 'Bonus', 'aio-podcast-studio' )
                );

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

        <p class="description">
            <?php esc_html_e( 'Classify the episode as Full, Trailer, or Bonus to control how episodes are organized and displayed.', 'aio-podcast-studio' ); ?>
        </p>
    </div>

    <!-- Field: Season Number -->
    <div id="aiopod-form-control-season_number" class="aiopod-form-control">
        <label for="aiopod-field-season_number" class="aiopod-form-label">
            <?php esc_html_e( 'Season Number', 'aio-podcast-studio' ); ?>
        </label>
        
        <div class="aiopod-form-field">
            <input type="text" name="season_number" id="aiopod-field-season_number" class="widefat" placeholder="<?php esc_attr_e( 'Enter Number', 'aio-podcast-studio' ); ?>" value="<?php echo esc_attr( $season_number ); ?>" />
        </div>
        
        <p class="description">
            <?php esc_html_e( 'Enter the season number this episode belongs to.', 'aio-podcast-studio' ); ?>
        </p>
    </div>

    <!-- Field: Episode Number -->
    <div id="aiopod-form-control-episode_number" class="aiopod-form-control">
        <label for="aiopod-field-episode_number" class="aiopod-form-label">
            <?php esc_html_e( 'Episode Number', 'aio-podcast-studio' ); ?>
        </label>
        
        <div class="aiopod-form-field">
            <input type="text" name="episode_number" id="aiopod-field-episode_number" class="widefat" placeholder="<?php esc_attr_e( 'Enter Number', 'aio-podcast-studio' ); ?>" value="<?php echo esc_attr( $episode_number ); ?>" />
        </div>
        
        <p class="description">
            <?php esc_html_e( 'Enter the episode number within the season or the overall podcast for easy identification.', 'aio-podcast-studio' ); ?>
        </p>
    </div>	

    <!-- Field: Explicit Content? -->
    <div id="aiopod-form-control-is_explicit" class="aiopod-form-control">
        <label for="aiopod-field-is_explicit" class="aiopod-form-label">
            <?php esc_html_e( 'Explicit Content?', 'aio-podcast-studio' ); ?>
        </label>

        <label class="aiopod-form-field">
            <input type="checkbox" name="is_explicit" id="aiopod-field-is_explicit" value="1" <?php checked( $is_explicit, 1 ); ?> />
            <?php esc_html_e( 'Check this box if your episode contains explicit language or adult themes.', 'aio-podcast-studio' ); ?>
        </label>
    </div>
</div>