<?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;
$rand = rand();

$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]:'';

if($fParams['type'] == '2'){?>
    <tr>
        <td class="search_description">
            <input type="checkbox" value="on" name='os_cck_search_<?php echo $fName?>' checked="checked" >
        </td>
    <tr>
    <?php
    return;
}
if($fParams['type'] == '3'){ ?>
    <tr>
        <td class="search_description">
            <input type="hidden" name='os_cck_search_<?php echo $fName?>' value="on" >
        </td>
    <tr>
    <?php
    return;
}

if(stripos($fName, 'e-p-ch') !== false){
    $child_field = new os_cckEntityField($db->_db);
    $child_field->load($field->fid);
    $db_field_name = $child_field->db_field_name;
}else{
    $db_field_name = $fName;
}

$prices = getFieldsResultsForRangeSearchNumberfield($db_field_name, $field->fk_eid);
//var_dump($field); exit;
if($fParams['type'] == '4' || $fParams['type'] == '5'){
    $plus = '';
    if($fParams['type'] == '5'){
        $plus = '+';
    }
    sort($prices,SORT_NUMERIC);
    $prices = array_unique($prices,SORT_NUMERIC);
    
    $arr = array();
    $arr[] = JHTML::_('select.option', '', cck_lang_text('COM_OS_CCK_PLEASE_SELECT_OPTION'), "value", "text");
    foreach($prices as $val){
        $arr[] = JHTML::_('select.option', $val, floor($val).$plus, "value", "text");
    }
    
    $jinput = JFactory::getApplication()->input;
    $value = $jinput->get("os_cck_search_".$field->db_field_name, '');
    ?>
    <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 = '';
        $tag_name = "os_cck_search_".$fName."_select"."$plus";
        echo JHTML::_('select.genericlist', $arr, $tag_name, $field_styling.' class="'.$custom_class.'"'.$select_type . ' ' . $offset_animation, 'value', 'text', $value);

        ?>

        
    </span>
    <?php
    return;
    //var_dump($arr);
} 

rsort($prices,SORT_NUMERIC);

$max_number = (!empty($prices)) ? ceil($prices[0]) : 0;
if(protectInjectionWithoutQuote('os_cck_search_'.$field->db_field_name . '_to', '') != ''){
    $max_value = protectInjectionWithoutQuote('os_cck_search_'.$field->db_field_name . '_to');
}else{
    $max_value = $max_number;
}



if(!$max_number){?>
    <tr>
        <td class="search_description">
            <input type="hidden" name='os_cck_search_<?php echo $fName?>' value="on" >
        </td>
    <tr>
    <?php
    return;
}

$min_number = (!empty($prices)) ? $prices[count($prices)-1] : 0;
if(!$min_number)$min_number = '0';
$min_number = floor($min_number);
if(protectInjectionWithoutQuote('os_cck_search_'.$field->db_field_name . '_from', '') != ''){
    $min_value = protectInjectionWithoutQuote('os_cck_search_'.$field->db_field_name . '_from');
}else{
    $min_value = $min_number;
    
}

$i = $layout->lid;

?>
<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 } ?> >
<tr>
    <td>
        <div <?php echo $field_styling?> class="<?php echo $custom_class.$i?> col_box_1" <?php echo $offset_animation; ?>>
            <div class="pricefrom_2">
                <span>
                    <?php echo cck_lang_text("COM_OS_CCK_SEARCH_NAMBER_FROM")?>
                </span>
                <input type="text" id="os_cck_search_<?php echo $fName.$i?>_from" name="os_cck_search_<?php echo $fName?>_from" value="<?php echo $min_value?>" />
                <br>
                <span>
                    <?php echo cck_lang_text("COM_OS_CCK_SEARCH_NAMBER_To")?>
                </span>
                <input type="text" id="os_cck_search_<?php echo $fName.$i?>_to" name="os_cck_search_<?php echo $fName?>_to" style="margin-left: 16px;" value="<?php echo $max_value?>"/>
            </div>
            <div id="slider_os_cck_search_<?php echo $fName.$i?>_<?php echo $rand;?>" class="ui-slider ui-slider-horizontal ui-widget ui-widget-content ui-corner-all"></div>
        </div>
    </td>
</tr>
</span>
<script type="text/javascript">


    jQuerOs(function() {

        jQuerOs("#slider_os_cck_search_<?php echo $fName.$i?>_<?php echo $rand;?>").slider({
            min: <?php echo (int) $min_number;?>,
            max: <?php echo (int) $max_number;?>,
            values: [<?php echo (int) $min_value;?>,<?php echo (int) $max_value;?>],
            range: true,
            stop: function(event, ui) {
                jQuerOs("input#os_cck_search_<?php echo $fName.$i?>_from").val(jQuerOs("#slider_os_cck_search_<?php echo $fName.$i?>_<?php echo $rand;?>").slider("values",0));
                jQuerOs("input#os_cck_search_<?php echo $fName.$i?>_to").val(jQuerOs("#slider_os_cck_search_<?php echo $fName.$i?>_<?php echo $rand;?>").slider("values",1));
            },
            slide: function(event, ui){
                jQuerOs("input#os_cck_search_<?php echo $fName.$i?>_from").val(jQuerOs("#slider_os_cck_search_<?php echo $fName.$i?>_<?php echo $rand;?>").slider("values",0));
                jQuerOs("input#os_cck_search_<?php echo $fName.$i?>_to").val(jQuerOs("#slider_os_cck_search_<?php echo $fName.$i?>_<?php echo $rand;?>").slider("values",1));
            }
        });

        jQuerOs("input#os_cck_search_<?php echo $fName.$i?>_from").change(function(){

            var value1=jQuerOs("input#os_cck_search_<?php echo $fName.$i?>_from").val();
            var value2=jQuerOs("input#os_cck_search_<?php echo $fName.$i?>_to").val();

            if(parseInt(value1) > parseInt(value2))
                {
                    value1 = value2;
                    jQuerOs("input#os_cck_search_<?php echo $fName.$i?>_from").val(value1);
                }

            if(parseInt(value1)<(<?php echo (int) $min_number;?>))
                {
                    jQuerOs("input#os_cck_search_<?php echo $fName.$i?>_from").val(<?php echo (int) $min_number;?>);
                }

            jQuerOs("#slider_os_cck_search_<?php echo $fName.$i?>_<?php echo $rand;?>").slider("values",0,value1);
        });

        jQuerOs("input#os_cck_search_<?php echo $fName.$i?>_to").change(function(){
            var value1=jQuerOs("input#os_cck_search_<?php echo $fName.$i?>_from").val();
            var value2=jQuerOs("input#os_cck_search_<?php echo $fName.$i?>_to").val();

            if (value2 > parseInt('<?php echo $max_number?>') ) {
                value2 = '<?php echo $max_number?>';
                jQuerOs("input#os_cck_search_<?php echo $fName.$i?>_to").val('<?php echo $max_number?>')
            }

            if(parseInt(value1) > parseInt(value2)){
                value2 = value1;
                jQuerOs("input#os_cck_search_<?php echo $fName.$i?>_to").val(value2);
            }

          jQuerOs("#slider_os_cck_search_<?php echo $fName.$i?>_<?php echo $rand;?>").slider("values",1,value2);
      });
    });
</script>
