<?php
defined('_JEXEC') or die('Restricted access');

/**
* @package OS CCK
* @copyright 2020 OrdaSoft.
* @author Andrey Kvasnevskiy(akbet@mail.ru),Roman Akoev (akoevroman@gmail.com)
* @link http://ordasoft.com/cck-content-construction-kit-for-joomla.html
* @description OrdaSoft Content Construction Kit
*/



if(isset($layout_params['fields']['showName_'.$field->db_field_name]) &&
  $layout_params['fields']['showName_'.$field->db_field_name] == 'on'){
  $layout_html = str_replace($field->db_field_name.'-label-hidden', '', $layout_html);
}
$fName = $field->db_field_name;

$icon_alias_prefix = (isset($field_from_params[$fName.'_add_icon_alias_prefix'])) ? $field_from_params[$fName.'_add_icon_alias_prefix'] : '';
if($icon_alias_prefix != ''){
    $layout_html = str_replace($field->db_field_name.'_label_icon_prefix_hidden', 'fa '.$icon_alias_prefix, $layout_html);
}
$icon_alias_suffix = (isset($field_from_params[$fName.'_add_icon_alias_suffix'])) ? $field_from_params[$fName.'_add_icon_alias_suffix'] : '';
if($icon_alias_suffix != ''){
    $layout_html = str_replace($field->db_field_name.'_label_icon_suffix_hidden', 'fa '.$icon_alias_suffix, $layout_html);
}

$fParams['type'] = isset($layout_params['fields']['search_'.$fName])?$layout_params['fields']['search_'.$fName]:'';
$params = new JRegistry;
$params->loadString($field->params);

if($fParams['type'] == 2){?>
    <input <?php echo $field_styling?> class="<?php echo $custom_class?>" <?php echo $offset_animation; ?> type="hidden" name="os_cck_search_<?php echo $fName?>" value="on">
    <?php
    return;
}elseif($fParams['type'] == 0){ ?>
    <input type="hidden" name="os_cck_search_<?php echo $fName?>" value="<?php echo $layout_params['fields'][$fName.'_default_value']; ?>">
    <?php return;
}
$required = '';
if(isset($field_from_params[$fName.'_required']) && $field_from_params[$fName.'_required']=='on')
    $required = ' required ';
$arr = array();

$allowed_values = $params->get("allowed_value");

if (strpos($allowed_values, '\sprt') !== false){
    $allowed_values = explode('\sprt', $allowed_values);
}else{
    return "Bad set 'allow value' for this field!";
}

foreach ($allowed_values as $key => $allow_value) {
    $allowed_values[$key] = trim(urldecode($allow_value));
}


// if(!$allowed_values)
$arr[] = JHTML::_('select.option', 'all', cck_lang_text('COM_OS_CCK_PLEASE_SELECT_OPTION'), "value", "text");

//foreach ($allowed_values as $item) {
//    $key_value = explode('|', $item);
//    if(!isset($key_value[0]) && isset($key_value[1])){
//      $key_value[0] =$key_value[1];
//    }
//    if (isset($key_value[0]) && isset($key_value[1]))
//        $arr[] = JHTML::_('select.option', $key_value[0], cck_lang_text($key_value[1]), "value", "text");
//    else if (isset($key_value[0]) && !isset($key_value[1]))
//        $arr[] = JHTML::_('select.option', $key_value[0], cck_lang_text($key_value[0]), "value", "text");
//    else if (!isset($key_value[0]) && isset($key_value[1]))
//        $arr[] = JHTML::_('select.option', $key_value[1], cck_lang_text($key_value[1]), "value", "text");
//    else 
//        return "Bad set 'allow value' for this field!";
//}

foreach ($allowed_values as $key => $item) {
    $arr[] = JHTML::_('select.option', $key, cck_lang_text($item), "value", "text");
    
}


$field_json = json_encode($field);
$tag_name = "os_cck_search_".$fName;
$key = 'value';
$text = 'text';

//get params

$jinput = JFactory::getApplication()->input;
$value = $jinput->get("os_cck_search_".$field->db_field_name, '');

if($value == ''){
    
    $value = $layout_params['fields'][$fName.'_default_value'];
}
$select_type_value = isset($layout_params['fields']['select_type_'.$fName]) ? $layout_params['fields']['select_type_'.$fName] : 1;
    
if($select_type_value > 1){
  $arr[0]->disable = TRUE;
  $select_type  = 'multiple="multiple" size="'.$select_type_value.'"';
}else{
    $select_type  = '';
}

// $value = $params->get("default_value",'');

?><span <?php if(isset($layout_params['fields']['description_'.$fName]) && $layout_params['fields']['description_'.$fName]=='on' && !empty($layout_params['fields'][$fName.'_tooltip']))
        {?>
rel="tooltip" data-toggle="tooltip" data-placement="top" title="<?php echo $layout_params['fields'][$fName.'_tooltip'];?>"
    <?php } ?> ><?php


    //select type
//$select_type = '';
// $select_type_value = isset($layout_params['fields']['select_type_'.$fName]) ? $layout_params['fields']['select_type_'.$fName] : 1;

// if($select_type_value > 1){
//   $select_type  = 'multiple="multiple" size="'.$select_type_value.'"';
// }

echo JHTML::_('select.genericlist', $arr, $tag_name. '[]', $field_styling.' class="'.$custom_class.$required.'"'.$select_type . ' ' . $offset_animation, $key, $text, $value);

    if(isset($parent)){
      $unique_parent_id = $parent;
    }else{
      $unique_parent_id = '';
    }

?>
    
    <span class="select-params" data-select-params='<?php echo $field_json;?>' style="display: none" ></span>    
</span>
<!-- select-params -->

<?php if(!array($value)){ ?>
<script>
    var select_type_value = '<?php echo $select_type_value; ?>';
    if(select_type_value == 1){
        addChildSelect(<?php echo $field_json;?>, "<?php echo $layout->lid;?>", "<?php echo $value;?>", "<?php echo JURI::root()?>","<?php echo $field->fid;?>","<?php echo $unique_parent_id;?>");
    }

</script>
<?php } ?>