<?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_'.$child_entity->data_field_name]) &&
  $layout_params['fields']['showName_'.$child_entity->data_field_name] == 'on' && isset($layout_html)){
  $layout_html = str_replace($child_entity->data_field_name.'-label-hidden', '', $layout_html);
}
$fName = $child_entity->data_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);
}
$sql_where = isset($layout_params['fields'][$fName.'_sql_show']) ? $layout_params['fields'][$fName.'_sql_show'] : '';
$autocomplite = isset($layout_params['fields']['autocomplite_'.$fName]) ? $layout_params['fields']['autocomplite_'.$fName] : '';     


$offset_animation = get_field_offset_animation($child_entity->data_field_name, $layout);
$order_by = isset($layout_params['fields']['order_by_'.$fName]) ? $layout_params['fields']['order_by_'.$fName] : '';     
$arr = array();

$arr = getValuesForChildEnteties($child_entity->childEntityFields, $child_entity->childEntityId, $order_by, '', $sql_where);

$value = getChildEntityFieldValue($entityInstance, $child_entity->data_field_name);
// var_dump($value);

$tag_name = "child_field_".$fName;
$key = 'value';
$text = 'text';
$value_arr = array();


if($value != '' && is_array($value)) {
    foreach ($value as $val){
        $value_arr[] = $val->fid_child;
    }
}elseif($value != '' && !is_array(json_decode($value))){
    $value_arr[] = $value;
}
if($autocomplite){
    $text_value = '';
    $placeholder = isset($layout_params['fields'][$fName.'_placeholder']) ? $layout_params['fields'][$fName.'_placeholder'] : '';     
    //var_dump($arr);
    if(!empty($value_arr) && $value_arr[0]){
        foreach($arr as $item){
            if($value_arr[0] == $item->value){
                $text_value = $item->text;
            }
        }
    }
}

?>
<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){
   $arr[0]->disable = TRUE;
   $select_type  = 'multiple="multiple" size="'.$select_type_value.'"';
 }
 //var_dump($text_value);
if($autocomplite){ ?>
    <input type="text" id="autocomplete_<?php echo $fName; ?>" placeholder="<?php $placeholder; ?>" value='<?php echo $text_value; ?>'/>
    <input type="hidden" id="autocomplete_<?php echo $fName; ?>_hidden" name="<?php echo $tag_name; ?>[]" value="<?php echo (!empty($value_arr)) ? $value_arr[0] : ''; ?>"/>
    <?php
} else{
    echo JHTML::_('select.genericlist', $arr, $tag_name . '[]', $layout_params['field_styling'].' class="'.$layout_params['custom_class']. ' ' . $field->db_field_name .'" ' . $select_type . ' ' . $offset_animation, $key, $text, $value_arr);
}

?>
</span>



<script>
    jQuerOs( "#autocomplete_<?php echo $fName; ?>" ).autocomplete({
      source: function(request, response){

        jQuerOs.ajax({
          dataType: "json",
          type: 'POST',
          url: 'index.php?option=com_os_cck&format=raw',

          data:{
            task: 'getChildEntityFieldsAutocomplite',
            childEntityFields: "<?php echo $child_entity->childEntityFields; ?>",
            childEntityId: "<?php echo $child_entity->childEntityId; ?>",
            order_by: "<?php echo $order_by; ?>",
            name_startsWith: request.term
          },

          success: function(data){
            response(data);
          },
          
        });
      },
      select: function( event, ui ) {
          //var terms = split( this.value );
          
          jQuerOs(this).val(ui.item.label);
          jQuerOs("#autocomplete_<?php echo $fName; ?>_hidden").val(''+ui.item.value+'');
          
          return false;
      }
      
    });
    
    
</script>
