<?php
require_once(get_template_directory() . '/includes/models/charsoogh_ads_posts.php');
$search_class_fields = new charsoogh_posts();
$category = get_queried_object();
$exist_cat = false;
if(!empty($category->term_id)){
$fields = $search_class_fields -> getCatFields($category->term_id);
//price_divShow
$none_price = get_option('none_price_cat');
if(is_array($none_price)){
if(in_array($category->term_id , $none_price)){$exist_cat = true;}
}
}else{
$fields = array();
}
$cookie_city = @$_COOKIE['city_user'];
$wcatTerms = get_terms('city_categories', array('hide_empty' => 0, 'parent' =>0));
foreach($wcatTerms as $wcatTerm) :
if($cookie_city == $wcatTerm->term_id){
$city_selected = $wcatTerm->name;
$city_selected_slug = $wcatTerm->slug;
}
endforeach;
?>
<div class="collapse" id="collapseAlternativeSearchForm" style="">
  <form role="form" action="" method="post" id="search_submit">
    <div class="wrapper">
        <div class="row">
          <div class="<?php if(!is_search()){ ?>col-md-3<?php }else{ ?>col-md-6<?php } ?>">
      <label class="ads_featured">
          <div class="icheckbox"><input type="checkbox" id="ads_featured" name="ads_featured"></div>
          <?php _e('Featured' , 'charsoogh'); ?>
      </label>
      </div>
      <?php if(!is_search()){ ?>
      <div class="col-md-3">
        <div class="form-group">
        <label for="district_select" class="col-form-label"><?php _e( 'Location' , 'charsoogh'); ?></label>
        <?php
        if(empty($cookie_city) || $cookie_city == 'all'){
          if(empty($city)){$city = $cookie_city;}
      wp_dropdown_categories( 'class=filter_city_categories&taxonomy=city_categories&name=filter_city_categories&hierarchical=1&depth=1&show_option_none='. __("All cities" , "charsoogh") .'&value_field=slug&option_none_value=0&selected='. $city .'' );
      }else{
        if(empty($city)){$city = $cookie_city;}
        wp_dropdown_categories( "class=filter_city_categories&taxonomy=city_categories&name=filter_city_categories&child_of=$cookie_city&hierarchical=1&show_option_none=$city_selected&value_field=slug&option_none_value=$city_selected_slug&selected=$city");
      }
        ?>
        </div>
      </div>
    <?php } ?>
      <?php if(!$exist_cat): ?>
        <div class="col-md-3">
          <div class="row">
          <div class="col-md-12 p-0">
            <div class="form-group price_divShow">
                <label for="min_price" class="col-form-label"><?php _e( 'Min Price' , 'charsoogh'); ?></label>
                <input name="min_price_show" type="text" class="form-control" id="min_price_show">
               <input name="min_price" type="hidden" class="form-control" id="min_price">
                <span class="input-group-addon"><?php echo get_woocommerce_currency_symbol(); ?></span>
                <p class="min_price_show" id="min_price_show"></p>
            </div>
        </div>
        </div>
      </div>
      <div class="col-md-3">
        <div class="row">
        <div class="col-md-12 p-0">
          <div class="form-group price_divShow">
              <label for="max_price" class="col-form-label"><?php _e( 'Max Price' , 'charsoogh'); ?></label>
              <input name="max_price_show" type="text" class="form-control" id="max_price_show">
             <input name="max_price" type="hidden" class="form-control" id="max_price">
              <span class="input-group-addon"><?php echo get_woocommerce_currency_symbol(); ?></span>
              <p class="max_price_show" id="max_price_show"></p>
          </div>
      </div>
      </div>
    </div>
    <script>
    jQuery(document).ready(function() {
      var faToEnDigits = function (input) {
         if (input == undefined)
             return;
         var returnModel = "", symbolMap = {
             '۱': '1',
             '۲': '2',
             '۳': '3',
             '۴': '4',
             '۵': '5',
             '۶': '6',
             '۷': '7',
             '۸': '8',
             '۹': '9',
             '۰': '0'
         };
         input = input.toString();
         for (var i = 0; i < input.length; i++)
             if (symbolMap[input[i]])
                 returnModel += symbolMap[input[i]];
             else
                 returnModel += input[i];
         returnModel = returnModel.replace(/\D/g,'');
         return returnModel;
        }

     var min_textbox = '#min_price_show';
     var hidden = '#min_price';
     setInterval(function() {
     var num = jQuery(min_textbox).val();
       var comma = /,/g;
       num = num.replace(comma,'');
       jQuery(hidden).val(num);
       jQuery('#min_price').val(num);
       num = faToEnDigits(num);
       var numCommas = addCommas(num);
       jQuery(min_textbox).val(numCommas);
     });

     var max_textbox = '#max_price_show';
     var hidden_max = '#max_price';
     setInterval(function() {
     var num_max = jQuery(max_textbox).val();
     var comma = /,/g;
       num_max = num_max.replace(comma,'');
       jQuery(hidden_max).val(num_max);
       jQuery('#max_price').val(num_max);
       num_max = faToEnDigits(num_max);
       var numCommass = addCommas(num_max);
       jQuery(max_textbox).val(numCommass);
     });

   });

     function addCommas(nStr) {
       nStr += '';
       var comma = /,/g;
       nStr = nStr.replace(comma,'');
       x = nStr.split('.');
       x1 = x[0];
       x2 = x.length > 1 ? '.' + x[1] : '';
       var rgx = /(\d+)(\d{3})/;
       while (rgx.test(x1)) {
         x1 = x1.replace(rgx, '$1' + ',' + '$2');
       }
       return x1 + x2;
     }
    </script>
  <?php endif; ?>
      </div>
      <?php
      echo '<div class="form-group"><div class="row">';
      foreach ($fields as $field) :
        if(!isset($field['only_admin']) || $field['only_admin'] != 'yes'){
        if($field['in_search'] == true):
          switch ($field['row_type']) {
        case 'textbox':
        ?>
            <div class="col-md-4 m-b-10">
              <div class="row">
              <div class="col-md-12 p-0">
                <?php
                if(isset($field['is_numeric']) && $field['is_numeric'] == 'yes'):
                  $keyup = 'onkeyup="separateNum(this.value,this);"';
                else:
                  $keyup = '';
                endif;
                ?>
                <label for="<?php echo $field['meta_key']; ?>" class="col-form-label"><?php echo $field['row_name']; ?></label>
                <input name="<?php echo $field['meta_key']; ?>" type="text" class="form-control" id="<?php echo $field['meta_key']; ?>" <?php echo $keyup; ?> <?php if($field['is_force'] == 'yes'){echo 'required';} ?>>
                <span id="errmsg"></span>
            </div>
            </div>
          </div>
        <?php
        break;
        case 'dropdown':
        ?>
          <div class="col-md-4 m-b-10">
              <label for="<?php echo $field['meta_key']; ?>" class="col-form-label"><?php echo $field['row_name']; ?></label>
              <select name="<?php echo $field['meta_key']; ?>" id="<?php echo $field['meta_key']; ?>" data-placeholder="<?php _e('Select' , 'charsoogh'); ?>">
                  <?php echo '<option value="">'. __('-- Select --' , 'charsoogh') .'</option>'; ?>
                  <?php foreach ($field['row_values'] as $options) {
                    if(strlen(trim($options)) > 0){
                       echo '<option value="'. $options .'">'. $options .'</option>';
                  }
                }
                ?>
              </select>
          </div>
        <?php
            break;
        case 'radio':
        ?>
        <div class="col-md-4" id="type">
            <label for="<?php echo $field['meta_key']; ?>" class="col-form-label"><?php echo $field['row_name']; ?></label>
            <figure>
              <?php
              foreach ($field['row_values'] as $options) {
                if(strlen(trim($options)) > 0){
              ?>
                <label class="framed">
                    <input type="radio" name="<?php echo $field['meta_key']; ?>" value="<?php echo $options; ?>">
                    <?php echo $options; ?>
                </label>
              <?php } } ?>
            </figure>
        </div>
    <?php
            break;
            case 'textarea':
            ?>
            <div class="col-md-4 m-b-10">
              <div class="row">
              <div class="col-md-12 p-0">
                <label for="<?php echo $field['meta_key']; ?>" class="col-form-label"><?php echo $field['row_name']; ?></label>
                <input name="<?php echo $field['meta_key']; ?>" type="text" class="form-control" id="<?php echo $field['meta_key']; ?>">
            </div>
            </div>
          </div>

          <?php
          break;
          case 'checkbox':
          ?>
          <div class="col-md-4 m-b-10">
            <label for="<?php echo $field['meta_key']; ?>" class="col-form-label"><?php echo $field['row_name']; ?></label>
              <div class="col-md-12">
          <?php
          foreach ($field['row_values'] as $options) {
            if(strlen(trim($options)) > 0){
          ?>
          <label>
              <input type="checkbox" <?php if($field['is_force'] == 'yes'){echo 'class="required_field"';} ?> name="*checkbox*<?php echo $field['meta_key']; ?>[]" value="<?php echo $options; ?>">
              <?php echo $options; ?>
          </label>
        <?php
            }
          }
          ?>
        </div>
        </div>
    <script>
      jQuery("input[type=checkbox], input[type=radio]").iCheck();
    </script>
          <?php
          } // end swich
        endif;
      }
      endforeach;
      echo '</div></div>';
       ?>
       <button type="submit" class="btn btn-primary submit-advance"><?php _e('Search' , 'charsoogh'); ?></button>
    </div>
    <!--end wrapper-->
  </form>
</div>
<!--end collapse-->
