<?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('keywordSearch') != '') {
  $QryStringArr['keywordSearch'] = $this->input->get_post('keywordSearch');
}

if ($this->input->get_post('sort') != '') {
  $QryStringArr['sort'] = $this->input->get_post('sort');
}
if ($this->input->get_post('color') != '') {
  $QryStringArr['color'] = implode(',', $this->input->get_post('color'));
}
if ($this->input->get_post('size') != '') {
  $QryStringArr['size'] = implode(',', $this->input->get_post('size'));
}
// if ($this->input->get_post('brand') != '') {
//   $QryStringArr['brand'] = implode(',', $this->input->get_post('brand'));
// }
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 = 100;
  $maxPrice = 10000;
}

$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 ($heading_title == "Wall Stickers") {
  $dimage = theme_url() . 'images/listing/listing-banner.jpg';
} else if ($heading_title == "Wallpapers") {
  $dimage = theme_url() . 'images/listing/listing-banner.jpg';
} else if ($heading_title == "Wall Panels") {
  $dimage = theme_url() . 'images/listing/listing-banner.jpg';
} else if ($heading_title == "Grass mat") {
  $dimage = theme_url() . 'images/listing/listing-banner.jpg';
} else if ($heading_title == "Doormats") {
  $dimage = theme_url() . 'images/listing/listing-banner.jpg';
} else {
  $dimage = theme_url() . 'images/listing/listing-banner.jpg';
}
?>
<link href="<?php echo theme_url(); ?>css/jquery-ui.css" rel="stylesheet" type="text/css">
<link href="<?php echo theme_url(); ?>css/jquery.mCustomScrollbar.css" rel="stylesheet" type="text/css">
<section class="banner_category" style="background-image: url('<?php echo $dimage; ?>');">
  <div class="container">
          <!-- <h2 class="banner_subtitle">check out over <span>200+</span></h2> -->
    <h1 class="banner_title"><?php echo $heading_title; ?></h1>
  </div>
</section>
<div class="page_breadcrumbs">
  <div class="container">
    <ul>
      <li><a href="<?php echo site_url(); ?>" title="Home">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="service_page">
  <div class="container">
    <div class="row">
      <div class="col-lg-9 col-md-9">
        <?php if (is_array($cat_res) && !empty($cat_res)) { ?>
          <h2 class="title_2"><?php echo imagealtTitle('', $cat_res['category_name'], ''); ?></h2>
          <div class="service_details">
            <div class="service_details_image ">
              <a href="javascript:void();" data-toggle="modal" data-target="#getquote" title="" class="getQuote" data-friendly_url="<?php echo site_url($cat_res['friendly_url']); ?>" data-ProductID="<?php echo $cat_res['category_id']; ?>" data-ProductName="<?php echo $cat_res['category_name']; ?>" data-ProductImage="<?php echo get_image('category', $cat_res['category_image'], '', '', 'AR'); ?>" data-id="<?php echo $cat_res['category_id']; ?>">
                <div class="service_details_image_thumb">
                  <img src="<?php echo get_image('category', @$cat_res['category_image'], '289', '220', 'R'); ?>" alt="<?php echo imagealtTitle2('', $cat_res['category_name'], ''); ?>" title="<?php echo imagealtTitle2('', $cat_res['category_name'], ''); ?>">
                </div>
                <span class="details_btn hover_btn">Send Enquiry</span>
              </a>
            </div>
            <?php echo html_entity_decode(@$cat_res['category_description']); ?>
          </div>
        <?php } ?>

        <div class="clearfix"></div>
        <?php if (is_array($products) && !empty($products)) { ?>
           <div class="title_2">Related Products <!--(Showing <?= count($products); ?> results)--></div> 

          <section class="product_listing">

            <div class="row">

              <div class="col-lg-12 col-md-12 col-xs-12">
                <div class="product_right" id="prodListingContainer">
                  <div class="row">
                    <div class="clearfix"></div>
                    <?php
                    foreach ($products as $key => $val) {
                      $productImage = get_db_field_value("wps_products_media", "media", "WHERE products_id = '" . $val['products_id'] . "' and is_default='Y'");
                      //echo $productImage;
                      ?>
                      <div class="col-md-4 col-sm-4 col-lg-4 col-xs-6 listpager">
                        <div class="box_item">
                          <div class="item_images">
                            <a href="<?= base_url($val['friendly_url']); ?>" title="<?php echo $val['product_name']; ?>">
                              <img src="<?php echo get_image('products', $val['media'], 270, 270, 'AR'); ?>" alt="<?php echo $val['product_name']; ?>" title="<?php echo $val['product_name']; ?>">
                            </a>
                          </div>

                          <div class="description02">
                            <h4 class="tt"><a href="<?= base_url($val['friendly_url']); ?>" title="<?php echo $val['product_name']; ?>"><?php echo $val['product_name']; ?></a></h4>
                          </div>
                          <a href="<?= base_url($val['friendly_url']); ?>" class="service_read_more_btn hover_btn pad10x20" title="Read More">View More <i class="fa fa-caret-right"></i></a>
                          <a href="javascript:void(0);" data-toggle="modal" data-target="#getquote" class="service_read_more_btn hover_btn getQuote pad10x20" data-friendly_url="<?php echo site_url($val['friendly_url']); ?>" data-ProductID="<?php echo $val['products_id']; ?>" data-ProductName="<?php echo $val['product_name']; ?>" data-ProductImage="<?php echo get_image('products', $val['media'], '', '', 'AR'); ?>" data-id="<?php echo $val['products_id']; ?>">Send Enquiry <i class="fa fa-envelope-open-o"></i></a>
                        </div>
                      </div>
                    <?php } ?>

                  </div>
                </div>
              </div>

            </div>

          </section>

        <?php } ?>

      </div>

      <div class="col-md-3">
        <div class="sidebar">
          <h5 class="sidebar_title1">Our Products List</h5>
          <div class="sidebar_body">
            <div class="sidebar_group">
              <div class="sidebar_inner">
                <ul>
                  <?php
                  $category_ids = @$cat_res['category_id'];
                  $cat_listing = $this->db->query("SELECT * FROM wps_categories where status='1' and category_id!='$category_ids' and category_id!='32' order by category_name ASC")->result_array();

                  foreach ($cat_listing as $value) {
                    ?>
                    <li><a href="<?php echo site_url($value['friendly_url']); ?>" title="<?php echo imagealtTitle('', $value['category_name'], ''); ?>"><?php echo $value['category_name']; ?></a></li>
                  <?php } ?>
                </ul>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>

<?php $this->load->view("bottom"); ?>
<script src="<?php echo theme_url(); ?>js/jquery-ui.js"></script>
<script src="<?php echo theme_url(); ?>js/jquery.mCustomScrollbar.concat.min.js"></script>
<script>
  $(function () {
    $("#slider-range").slider({
      range: true,
      min: 0,
      max: 10000,
      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));
  });
</script>

<script>
  $(document).on('click', '.sidebar_heading a.sidebar_toggle', function (e) {
    var $this = $(this);
    if (!$this.hasClass('sidebar_collapsed')) {
      $this.parents('.sidebar_group').find('.sidebar_inner').slideUp();
      $this.addClass('sidebar_collapsed')

    } else {
      $this.parents('.sidebar_group').find('.sidebar_inner').slideDown();
      $this.removeClass('sidebar_collapsed')

    }
  })
</script>


<script>
  var page = 1;
  var triggeredPaging = 0;
  $(window).scroll(function () {
    $('#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 = '?stOffSet=' + page;
<?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) {
        triggeredPaging = 1;
        //alert(queryString);exit();
        $.ajax({
          type: "POST",
          url: "<?php echo base_url(); ?>products/ajax_load_product_view" + queryString,
          error: function (res) {
            //alert('Error');
            triggeredPaging = 0;
            $('#loadingdiv').hide();
            //console.log(arguments);
          },
          beforeSend: function (jqXHR, settings) {
            $('#loadingdiv').show();

          },
          success: function (res) {
            $('#loadingdiv').hide();
            $("#prodListingContainer").append(res);
            triggeredPaging = 0;
            console.log(res);
            // $('.listpager').fadeTo(1000, 0.5, function () {
            //   $(this).fadeTo(1000, 1.0);
            // });
            $('.listpager11').fadeTo(1000, 0.5, function () {
              $(this).fadeTo(1000, 1.0);
            });
          }
        });
      }
    }
  });

</script>
