<?php

use Jet\MVC_View;
use Jet\Form_Renderer_Field_Input_Select;
use Jet\Form_Field_Select;
use Jet\Data_Text;
use Jet\Data_Tree_Node;

/**
 * @var MVC_View $this
 * @var Form_Renderer_Field_Input_Select $r
 * @var Form_Field_Select $field
 */
$r = $this->getRaw( 'renderer' );
$field = $r->getField();


if( !$r->getBaseCssClasses() ) {
	$r->setBaseCssClass( 'form-control' );
}

$value = $field->getValue();
if( $field->getLastErrorCode() ) {
	$r->addCustomCssClass( 'is-invalid' );
}
?>
<select <?=$r->renderTagAttributes()?>>
	<?php foreach( $field->getSelectOptions() as $val => $label ):
		
		$css = $label->getSelectOptionCss();
		
		$text = Data_Text::htmlSpecialChars( $label );
		if($label instanceof Data_Tree_Node) {
			$text = str_pad('', ($label->getDepth()*36), '&nbsp;', STR_PAD_LEFT).$text;
		}
		
		$selected = $field->optionIsSelected($val) ? ' selected="selected"' : '';
		
		?><option value="<?=Data_Text::htmlSpecialChars( $val )?>" <?=$css?><?=$selected?>><?=Data_Text::htmlSpecialChars( $label )?></option><?php
	endforeach; ?>
</select>