<?php

/**
 * Dropdown field
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $wpcb_field;

$wpcb_field_options = $wpcb_field['options'];

if ( count( $wpcb_field_options ) > 0 ) {
	
	$attrs = array();
	$class = '';
	if ( isset( $wpcb_field['field']['dropdown_display'] ) && is_array( $wpcb_field['field']['dropdown_display'] ) && count( $wpcb_field['field']['dropdown_display'] ) > 0 ) {
		$class .= ' wpcb-image-dropdown';
		$attrs = $wpcb_field['field']['dropdown_display'];
	}

	echo '<div class="wpcb-dropdown' . esc_attr( $class ) . '">';

	$required = ( isset( $wpcb_field['field']['required'] ) && 'yes' === $wpcb_field['field']['required'] ) ? ' required' : '';

	echo '<select class="wpcb-select" name="' . esc_attr( wpcb_sanitize_title( $wpcb_field['field']['label'] ) ) . '"' . esc_attr( $required ) . '>';

	if ( isset( $wpcb_field['field']['placeholder'] ) && '' !== $wpcb_field['field']['placeholder'] ) {
		echo '<option value="">' . esc_attr( $wpcb_field['field']['placeholder'] ) . '</option>';
	}

	$summary_html = '';

	foreach ( $wpcb_field_options as $wpcb_option_index => $wpcb_option ) {
		
		$visual_preview = ' data-id="' . wpcb_sanitize_title( $wpcb_option_index ) . '"';
		if ( isset( $wpcb_option['sale_price'] ) ) {
			$visual_preview .= ' data-sale="' . $wpcb_option['sale_price'] . '"';
		}

		$visual_keys = array(
			'price',
			'sale',
			'key',
			'preview_value',
			'note',
			'url',
			'sku',
			'stock',
			'product_id'
		);
		foreach ( $visual_keys as $visual_key ) {
			if ( isset( $wpcb_option[ $visual_key ] ) && '' !== $wpcb_option[ $visual_key ] ) {
				$visual_preview .= sprintf( 
					' data-%s="%s"',
					str_replace( '_', '-', $visual_key ),
					$wpcb_option[ $visual_key ]
				);
			}
		}

		$price_hidden = 'no';
		if ( isset( $wpcb_field['field']['price_hidden'] ) && '' !== $wpcb_field['field']['price_hidden'] ) {
			$price_hidden = $wpcb_field['field']['price_hidden'];
		}

		if ( $wpcb_option['thumbnail'] && in_array( 'thumb', $attrs ) ) {
			$visual_preview .= ' data-thumb="' . wp_get_attachment_image_url( $wpcb_option['thumbnail'], 'thumbnail' ) . '"';
		}
		if ( $wpcb_option['price'] && in_array( 'price', $attrs ) && 'no' === $price_hidden ) {
			$visual_preview .= ' data-show-price="1"';
		}
		if ( $wpcb_option['note'] && in_array( 'note', $attrs ) ) {
			$visual_preview .= ' data-show-note="1"';
		}

		$option_class = '';
		if ( isset( $wpcb_option['stock'] ) && '' !== $wpcb_option['stock'] ) {
			if ( 0 >= (int) $wpcb_option['stock'] ) {
				if ( $wpcb_stock_settings['wpcb_hide_out_of_stock_options'] ) {
					continue; // Don't include out of stock option.
				}

				if ( ! $wpcb_stock_settings['wpcb_allow_backorders'] ) {
					$option_class .= ' wpcb-option-out-of-stock';
					$visual_preview .= ' disabled="disabled"';
				}
			}
		}

		$wpcb_option_id = wpcb_sanitize_title( $wpcb_field['field']['label'] . '-' . $wpcb_option['label'] );

		echo '<option id="' . esc_attr( $wpcb_option_id ) . '" value="' . esc_attr( $wpcb_option['label'] ) . '"';
		echo wp_kses_post( $visual_preview );
		echo ' data-label="' . esc_attr( $wpcb_field['field']['label'] ) . '"';
		if ( isset( $wpcb_option['rules'] ) ) {
			echo ' data-display-rules="' . wp_kses_post( urldecode( str_replace( '"', "'", wp_json_encode( $wpcb_option['rules'] ) ) ) ) . '"';
		}
		if ( isset( $wpcb_field['field']['default'] ) && is_array( $wpcb_field['field']['default'] ) && in_array( $wpcb_option_index, $wpcb_field['field']['default'] ) ) {
			echo ' data-default="true"';
		}
		echo '>' . esc_attr( $wpcb_option['label'] ) . '</option>';

		$summary_html .= wpcb_get_option_summary_html( $wpcb_option_id, $wpcb_option, $price_hidden );
	}

	echo '</select>';

	echo wp_kses( wpcb_get_icon('chevron-down'), wpcb_get_icon_kses_args() );

	echo '</div>';

	echo wp_kses_post( $summary_html );
	
}
