<?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
*/

$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);
}

$tag = isset($layout_params['fields']['field_tag_' . $fName]) ? $layout_params['fields']['field_tag_' . $fName] : 'span';
$show_title = 0;
$value = (isset($value[0]->data))?$value[0]->data : '';
$value = json_decode($value);
$params = new JRegistry;
$params->loadString($field->params);
$allowed_values = $params->get("allowed_value");
$allowed_values = explode('\sprt', $allowed_values);
$tmp_val = array();

if(is_array($value)){
    
    foreach($value as $val){
        
        if(isset($allowed_values[$val])){
            $tmp_val[] = cck_lang_text($allowed_values[$val]);
        }
    }
}

$value = implode(', ', $tmp_val);


?>

<<?php echo $tag; ?> <?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

     $show_count = (isset($layout_params['fields'][$fName.'_show_count'])) ? $layout_params['fields'][$fName.'_show_count'] : '0';
     $max_lenght = (isset($layout_params['fields'][$fName.'_max_lenght'])) ? $layout_params['fields'][$fName.'_max_lenght'] : '250';
     
     
//    if($value != '0'){ 
//     $value = json_decode($value);
//    }
     
    if ($value){
        if(isset($field_from_params[$fName.'_add_icon_prefix_prefix']) && !empty($field_from_params[$fName.'_add_icon_prefix_prefix'])){
            echo '<span class="fa '.$field_from_params[$fName.'_add_icon_prefix_prefix'].'"></span>';
        }
        if(!empty($layout_params['fields'][$fName.'_prefix'])){
            echo '<span class="cck-prefix">'.$layout_params['fields'][$fName.'_prefix'].'</span>';
        }
        if(isset($field_from_params[$fName.'_add_icon_prefix_suffix']) && !empty($field_from_params[$fName.'_add_icon_prefix_suffix'])){
            echo '<span class="fa '.$field_from_params[$fName.'_add_icon_prefix_suffix'].'"></span>';
        }
    
        if(isset($layout_params['fields']['showName_'.$field->db_field_name]) &&
            $layout_params['fields']['showName_'.$field->db_field_name] == 'on'){
            $show_title = 1;
            $layout_html = str_replace($field->db_field_name.'-label-hidden', '', $layout_html);
        }


        // print_r($value);
        // exit;

         //category output
//         if($show_count == 0 || $show_count > count($value)) $show_count = count($value);
//
//         $select_output = '';
//         for ($icount = 0; $icount < $show_count; $icount++) { 
//             $select_output .= $value[$icount];
//             
//             if($icount < ($show_count-1)){
//               $select_output .= ", ";
//             }
//
//             if(strlen($select_output) > $max_lenght){
//               $select_output = substr($select_output, 0, $max_lenght)."...";
//             } 
//         }
         
        echo $value;



        if(isset($field_from_params[$fName.'_add_icon_suffix_prefix']) && !empty($field_from_params[$fName.'_add_icon_suffix_prefix'])){
            echo '<span class="fa '.$field_from_params[$fName.'_add_icon_suffix_prefix'].'"></span>';
        }
        if(!empty($layout_params['fields'][$fName.'_suffix'])){
            echo '<span class="cck-suffix">'.$layout_params['fields'][$fName.'_suffix'].'</span>';
        }
        if(isset($field_from_params[$fName.'_add_icon_suffix_suffix']) && !empty($field_from_params[$fName.'_add_icon_suffix_suffix'])){
            echo '<span class="fa '.$field_from_params[$fName.'_add_icon_suffix_suffix'].'"></span>';
        }
    }?>
</<?php echo $tag; ?>>
<script>
    if('<?php echo $show_title; ?>' == '1'){
        var elem = jQuerOs('[data-field-name=<?php echo $field->db_field_name;?>]').attr('class')
        var elem_classes = elem.split(' ')
        if(elem_classes.length > 0){
            jQuerOs.each(elem_classes, function(){
                if(this.indexOf('-label-hidden') > -1){
                    
                    jQuerOs('[data-field-name=<?php echo $field->db_field_name;?>]').removeClass(String(this))
                }
                
            })
        }
        
    }
</script>