<?php
$this->load->view("top");
$QryStringArr = array();  // To store all Query Variables so to move to other view;
$QryStringArr = array_unique($QryStringArr);

if (isset($this->meta_info['entity_id']) && $this->meta_info['entity_id'] != '') {
  $QryStringArr['category_id'] = $this->meta_info['entity_id'];
}
if ($catid) {
  $QryStringArr['category_id'] = $catid;
}
if ($this->input->post('category_id')) {
  $QryStringArr['category_id'] = $this->input->post('category_id');
}

if ($this->input->get_post('sort') != '') {
  $QryStringArr['sort'] = $this->input->get_post('sort');
 }

if ($this->input->get_post('keywordSearch') != '') {
  $QryStringArr['keywordSearch'] = $this->input->get_post('keywordSearch');
}

if ($this->input->get_post('price') != '') {
  $priceArr = explode('-', $this->input->get_post('price'));
  $minPrice = $priceArr[0];
  $maxPrice = $priceArr[1];
  $QryStringArr['price'] = $this->input->get_post('price');
  $range = 'Range: ₹' . $priceArr[0] . ' - ₹' . $priceArr[1];
} else {
  $range = 'Range: ₹1 - ₹10';
  $minPrice = 10;
  $maxPrice = 100000;
}

$type = $this->uri->segment(1);
if ($type > 0) {
  if ($type == 1) {
    $QryStringArr['type'] = $type;
  }
  if ($type == 2) {
    $QryStringArr['type'] = $type;
  }
  if ($type == 3) {
    $QryStringArr['type'] = $type;
  }
  if ($type == 4) {
    $QryStringArr['type'] = $type;
  }
}
$cat_res2 = $this->db->query("SELECT * FROM wps_categories WHERE status = '1' and parent_id = '0' order by sort_order asc ")->result_array();
if (@$cat_res['bg_image'] != '') {
  $dimage = get_image('category', $cat_res['bg_image'], '', '', 'R');
} else {
  $dimage = theme_url() . 'images/listing/listing-banner.jpg';
}
?>
<link href="<?php echo theme_url(); ?>css/jquery-ui.css" rel="stylesheet" type="text/css" />
   <section class="banner_category" style="background-image: url('<?=theme_url();?>img/inner-banner.jpg');">
        <div class="container">
            <h1 class="banner_subtitle">Free Shipping Within India Only</h1>
            <h2 class="banner_title"><?=$heading_title;?></h2>
        </div>
    </section>


<div class="page_breadcrumbs">
  <div class="container">
    <ul>
      <li><a href="<?php echo site_url(); ?>">Home</a></li>
      <?php
      if ($this->input->post('keyword_search') == '') {
        if ($catid) {
          echo category_breadcrumbs($catid);
        } else {
          echo '<li>' . $heading_title . '</li>';
        }
      } else {
        echo '<li>Search Result</li>';
      }
      ?>
    </ul>
  </div>
</div>
    <section class="product_listing bg-grey">
        <div class="container">
        <form name="searchForm" id="searchForm" action="" method="post" >
            <div class="row">
                <div class="col-xxl-3 col-xl-3 col-lg-3 col-md-3 col-sm-4 col-12 leftside_bar">
                    <div class="theiaStickySidebar">
                        <div class="sidebar">
                            <div class="sidebar_title1">Filters <svg xmlns="http://www.w3.org/2000/svg" width="16"
                                    height="16" fill="currentColor" viewBox="0 0 16 16">
                                    <path
                                        d="M1.5 1.5A.5.5 0 0 1 2 1h12a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-.128.334L10 8.692V13.5a.5.5 0 0 1-.342.474l-3 1A.5.5 0 0 1 6 14.5V8.692L1.628 3.834A.5.5 0 0 1 1.5 3.5v-2zm1 .5v1.308l4.372 4.858A.5.5 0 0 1 7 8.5v5.306l2-.666V8.5a.5.5 0 0 1 .128-.334L13.5 3.308V2h-11z" />
                                </svg></div>
                            <div class="sidebar_body">
                <?php
                if (is_array($themes) && !empty($themes)) {
                  ?>
                  <div class="sidebar_group">
                 <div class="sidebar_heading"><a href="javascript:void();" class="sidebar_toggle sidebar_collapsed">Treatments & Medicines </a></div>
                   <div class="sidebar_inner">
                      <ul>
                        <?php
                        foreach ($themes as $th) {
                          ?>
                          <li><a href="<?php echo site_url($th['friendly_url']); ?>"><?php echo $th['category_name_p']; ?></a></li>
                          <?php
                        }
                        ?>
                      </ul>
                    </div>
                  </div>
                  <?php
                }
                ?>
            <div class="sidebar_group">
                  <div class="sidebar_heading">
                         <a href="javascript:void();" class="sidebar_toggle_1">Price </a>
               </div>
                       <div class="sidebar_inner">
                     <div class="price_filter_1">
                           <!--<label for="amount">Price Range:</label>-->
                        <input type="text" id="amount" readonly>
                         <input type="hidden" name="price" id="price">
                           <div id="slider-range"></div>
                         
                       </div>
                </div>
             </div>
              </div>
            </div>
          </div>
        </div>
          <div class="col-xxl-9 col-xl-9 col-lg-9 col-md-9 col-12">
                    <div class="product_right">
                        <div class="row">
              <?php
              if (is_array($products) && !empty($products)) {
                ?>
                  <div class="col-md-12 col-sm-12 col-xs-12">
                                <div class="page_show">
                    <ul>
                      <li class="hidden-xs">Showing - <?php echo $total_list_rows; ?> Result(s) found</li>

                      <li>
                        <select name="sort" onchange="$('#searchForm').submit();">
                            <option >Select</option>
                          <option value="1">Popular</option>
                          <option value="2">New</option>
                          <option value="3">Price: High to Low</option>
                          <option value="4">Price: Low to High</option>
                        </select>
                      </li>
                      <li><span>Sort:</span></li>
                    </ul>
                  </div>
                </div>
                <div class="clearfix"></div>
              </div>
                <div class="row" >
                          <?php
              foreach ($products as $val) {
                    ?>
                <div class="col-xxl-4 col-xl-4 col-lg-4 col-md-6 col-sm-6 col-6">
                      <div class="product-content-wrap product-list-box">
                              <div class="product-content-img">
                    <a href="<?= base_url($val['friendly_url']); ?>" title="<?php echo imagealtTitle('', ucwords(strtolower($val['product_name'])), ''); ?>">
                                              <img src="<?php echo get_image('products', $val['media'], 270, 370, 'R'); ?>" alt="<?php echo imagealtTitle('', ucwords(strtolower($val['product_name'])), ''); ?>" title="<?php echo imagealtTitle('', ucwords(strtolower($val['product_name'])), ''); ?>">
                                        </a>
                                    </div>
                                       <div class="product-content-details">
                                        <h3 class="product-title"><a href="<?= base_url($val['friendly_url']); ?>" title="<?php echo imagealtTitle('', ucwords(strtolower($val['product_name'])), ''); ?>">
                                            </a><?php echo substr($val['product_name'],0,50); ?>..</h3>
                                        <div class="product-content-bottom">
                                            <div class="product-price">
                                                <span> <?php if($val['category_id']==2){ echo display_price($val['product_price']);} else { echo display_price_india($val['product_price']);} ?></span>
                                            </div>
                                            <div class="add-cart">
                                                <a class="add" href="<?= base_url($val['friendly_url']); ?>" title="<?php echo imagealtTitle('', ucwords(strtolower($val['product_name'])), ''); ?>">
                                                    <img src="<?php echo theme_url(); ?>img/icons/cart-icon.svg" alt=" <?php if($val['category_id']==2){ echo "Add to cart"; } else { echo "Buy Now";} ?>">
                                                    <?php if($val['category_id']==2){ echo "Add to cart"; } else { echo "Buy Now";} ?>
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                      </div>
                    </div>
                    <?php
                  }
                  ?>
                </div>
                <div id="loadingdiv" style="display: none;">
                  <img src="<?php echo theme_url(); ?>images/loader.gif" width="110" />
                </div>
                <?php
              } else {
                ?>
             
                <div class="text-center mt10">
                  <h3 class="contact-title">Sorry, No Result Found!</h3>
                  <img src="<?php echo theme_url(); ?>images/noresult.gif" width="350" title="No Result Found" alt="No Result Found" />
                  <br/><br/>
                </div>
                    
                <?php
              }
              ?>
             
            </div>
          </div>
          <?php
                                      if (isset($cat_res['category_description'])) {
                                        echo '<div class="col-md-12"><br/>' . $cat_res['category_description'] . '</div>';
                                      }
                                      ?>
    </div>
    </form>
  </div>
</section>
<?php $this->load->view("bottom"); ?>
<script src="<?php echo theme_url(); ?>js/jquery-ui.js"></script>
<script type="text/javascript">
                  var page = 1;
                  var triggeredPaging = 0;
                  $(window).scroll(function () {
                    $('#loadingdiv').css({"display": "block", "text-align": "center"});
                    $('#loadingdiv').hide();
                    var scrollTop = $(window).scrollTop();
                    var scrollBottom = (scrollTop + $(window).height());
                    var containerTop = $('#prodListingContainer').offset().top;
                    var containerHeight = $('#prodListingContainer').height();
                    var containerBottom = Math.floor(containerTop + containerHeight);
                    var scrollBuffer = 0;
                    if ((containerBottom - scrollBuffer) <= scrollBottom) {
                      page = $('.listpager').length;
                      var queryString = '?';
<?php
if (count($QryStringArr)) {
  foreach ($QryStringArr as $qrykey => $qryval) {
    ?>
                          queryString += "<?php echo $qrykey; ?>=<?php echo $qryval; ?>";
    <?php
  }
}
?>
                      var actual_count = <?php echo $total_list_rows; ?>;
                      if (!triggeredPaging && page < actual_count) {
                        $('#loadingdiv').show();
                        triggeredPaging = 1;
                        $.ajax({
                          type: "POST",
                          url: "<?php echo base_url(); ?>products/ajax_load_product_view" + queryString,
                          error: function (res) {
                            triggeredPaging = 0;
                            $('#loadingdiv').hide();
                          },
                          beforeSend: function (jqXHR, settings) {
                            $('#loadingdiv').show();
                          },
                          success: function (res) {
                            setTimeout(function () {
                              $('#loadingdiv').hide();
                              $("#prodListingContainer").append(res);
                              triggeredPaging = 0;
                            }, 500);
                          }
                        });
                      }
                    }
                  });

</script>
<script>
  $(function () {
    $("#slider-range").slider({
      range: true,
      min: 0,
      max: 100000,
      values: [<?php echo $minPrice; ?>, <?php echo $maxPrice; ?>],
      slide: function (event, ui) {
        $("#amount").val("₹" + ui.values[ 0 ] + " - ₹" + ui.values[ 1 ]);
        $("#price").val(ui.values[ 0 ] + "-" + ui.values[ 1 ]);
      }, change: function (event, ui) {
        $("#searchForm").submit();
      }
    });
    $("#amount").val("₹" + $("#slider-range").slider("values", 0) +
            " - ₹" + $("#slider-range").slider("values", 1));
  });


  $('.page_show ul li label').click(function () {
    $('.page_show ul li label.selected').removeClass('selected');
    $(this).addClass('selected');
  });
</script>