<?php

/**
 * Layout: Dropdown.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

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

$pages_settings = aiopod_get_option( 'aiopod_pages_settings' );

$category_url = '/';
if ( ! empty( $pages_settings['category'] ) ) {
	$category_url = get_permalink( (int) $pages_settings['category'] );
}

$attributes['id'] = 0;
?>
<div class="aiopod aiopod-categories aiopod-categories--layout-dropdown" data-uri="<?php echo esc_url( $category_url ); ?>">
	<?php
	// Title
	if ( ! empty( $attributes['title'] ) ) : ?>
		<div class="aiopod-categories__title aiopod-text-heading">
			<?php echo esc_html( $attributes['title'] ); ?>
		</div>
	<?php endif;

	// Categories
	$args = array(
		'taxonomy'          => 'aiopod_categories',
		'name'              => 'aiopod_categories',
		'show_option_none'  => '— ' . esc_html__( 'Select a Category', 'aio-podcast-studio' ) . ' —',
		'option_none_value' => (int) $attributes['id'],
		'orderby'           => sanitize_text_field( $attributes['orderby'] ),
		'order'             => sanitize_text_field( $attributes['order'] ),
		'hide_empty'        => (int) $attributes['hide_empty'], 
		'hierarchical'      => (int) $attributes['hierarchical'],                
		'show_count'        => (int) $attributes['show_count'], 		
		'walker'            => new AIOPOD_Walker_Terms_Dropdown(),   
		'echo'              => 0
	);

	if ( $args['hierarchical'] ) {
		$args['child_of'] = (int) $attributes['id'];
	} else {
		$args['parent'] = (int) $attributes['id'];
	}

	if ( ! empty( $attributes['include'] ) ) { // Include category IDs
		$args['include'] = array_map( 'intval', explode( ',', $attributes['include'] ) );
	}

	if ( ! empty( $attributes['exclude'] ) ) { // Exclude category IDs
		$args['exclude'] = array_map( 'intval', explode( ',', $attributes['exclude'] ) );
	}

	if ( $term_slug = get_query_var( 'aiopod_category' ) ) {
		$term = get_term_by( 'slug', sanitize_title( $term_slug ), 'aiopod_categories' );
		$args['selected'] = $term->term_id;
	}

	if ( isset( $_GET['ca'] ) ) {
		$args['selected'] = (int) wp_unslash( $_GET['ca'] );
	}
	
	$args = apply_filters( 'aiopod_categories_args', $args, $attributes );
	$html = wp_dropdown_categories( $args );

	echo $html;
	?>
</div>