<?php
/**
 * Zeen control multi select class
 *
 * @package Zeen
 * @since 1.0.0
 */
class Zeen_Control_Multi_Select extends WP_Customize_Control {

	/**
	 * Control type
	 *
	 * @since  1.0.0
	 */
	public $type = 'zeen-multi-select';
	public $multi = 'on';

	/**
	 * Enqueue control related scripts
	 *
	 * @since 1.0.0
	 */
	public function enqueue() {

		wp_enqueue_script( 'searchable-option-list', get_parent_theme_file_uri( 'assets/admin/js/searchable-option-list.js' ), array( 'jquery' ), ZEEN_VERSION, true );
		wp_enqueue_script( 'zeen-control-multi-select', get_parent_theme_file_uri( 'assets/admin/js/zeen-control-multi-select.js' ), array( 'jquery', 'searchable-option-list' ), false, true );
	}

	/**
	 * Render content of the control
	 *
	 * @since 1.0.0
	 */
	public function render_content() {
		$multi = 'off' == $this->multi ? false : true;
		$wrap_class = 'zeen-select-wrap';
		if ( ! empty( $multi ) ) {
			$wrap_class .= ' zeen-select-wrap-multi';
		}
	?>
	<label class="control-vertical-c">
		<?php
		if ( ! empty( $this->label )  ) {
			echo '<span class="customize-control-title">' . sanitize_text_field( $this->label );
			if ( ! empty( $this->description ) ) {
				echo '<span class="tipi-tip description customize-control-description dashicons dashicons-editor-help" data-title="' . esc_attr( $this->description ) . '"></span>';
			}
			echo '</span>';
		}
		?>
		<div class="<?php echo esc_attr( $wrap_class ); ?>">
			<select<?php if ( ! empty( $multi ) ) { ?> multiple="multiple" <?php } ?> <?php $this->link(); ?> name="_customize-zeen-multi-select-<?php echo esc_attr( $this->id ); ?>">
				<?php
				foreach ( $this->choices as $key => $value ) {
					echo '<option value="' . esc_attr( $key ) . '"';
					if ( ! empty( $multi ) ) {
						if ( is_array( $this->value() ) && in_array( $key, $this->value() ) ) {
							echo 'selected="selected"';
						}
					} else {
						if ( $this->value() == $key ) {
							echo 'selected="selected"';
						}
					}
					echo '>' . sanitize_text_field( $value ) . '</option>';
				}
				?>
			</select>
		</div>
	</label>
	<?php
	}
}
