<?php 
$this->load->view("top_application");
$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 ($this->input->get_post('keyword') != '') {
  $QryStringArr['keyword'] = $this->input->get_post('keyword');
}
if ($this->input->get_post('sort') != '') {
  $QryStringArr['sort'] = $this->input->get_post('sort');
}
//print_r($category_result);
?>
<section class="dir-alp dir-pa-sp-top">
  <div class="container">
    <div class="row">
      <div class="dir-alp-tit mobile_padding">
        <h1><?php echo $heading_title; ?></h1>
        <ol class="breadcrumb">
          <li><a href="<?php echo site_url(); ?>">Home ..</a> </li>
          <?php echo category_breadcrumbs($categoryId); ?>
        </ol>
      </div>
    </div>
    
    <div class="row">
      <div class="dir-alp-con">
        <?php //$this->load->view("category/category_left_view"); ?>
        <div class="col-md-12 dir-alp-con-right list-grid-rig-pad padding_normal">
          <div class="dir-alp-con-right-1">
            <div class="row">
              <!--LISTINGS-->
              <?php
              if (is_array($catList) && !empty($catList)) {
                ?>
                <div class="row span-none" id="prodListingContainer">

                  <?php
                  foreach ($catList as $val) {
                    $link_url = site_url($val['friendly_url']);
                    $categoryCount = count_category(" AND parent_id = '" . $val['category_id'] . "' ");
                    $productCount = count_products(" AND category_id = '" . $val['category_id'] . "' ");
                    if ($productCount > 0) {
                      $count = $productCount;
                      $catRes = array();
                    } else {
                      $count = $categoryCount;
                      $catRes = $this->db->query("SELECT * FROM wps_categories WHERE parent_id = '" . $val['category_id'] . "' AND status = '1' LIMIT 0, 3")->result_array();
                    }
                    ?>
                    <div class="col-sm-4 col-md-3 listpager">
                      <div class="listing01">
                        <div class="box-img">
                          <div class="images02">
                            <img src="<?php echo get_image('category', $val['category_image'], '415', '415', 'R'); ?>" alt="<?php imagealtTitle('', $val['category_name'], 'Service Provider'); ?>" title="<?php imagealtTitle('', $val['category_name'], 'Service Provider'); ?>">
                          </div>
                          <div class="caption">
                            <h2><a title="<?php imagealtTitle('', $val['category_name'], 'Service Provider'); ?>" href="<?php echo site_url($val['friendly_url']); ?>"><?php echo $val['category_name']; ?> (<?php echo $count; ?>)</a></h2>
                            <ul>
                              <?php
                              foreach ($catRes as $subCat) {
                                $catCount = count_category(" AND parent_id = '" . $subCat['category_id'] . "' ");
                                $proCount = count_products(" AND category_id = '" . $subCat['category_id'] . "' ");
                                if ($proCount > 0) {
                                  $subcount = $proCount;
                                } else {
                                  $subcount = $catCount;
                                }
                                ?>
                                <li><a href="<?php echo site_url($subCat['friendly_url']); ?>" title="<?php imagealtTitle('', $subCat['category_name'], 'Service Provider'); ?>"><?php echo $subCat['category_name']; ?> (<?php echo $subcount; ?>)</a></li>
                                <?php
                              }
                              ?>

                            </ul>
                            <div class="listing-bottom">
                              <a href="<?php echo site_url($val['friendly_url']); ?>" class="btn btn-common float-right" title="<?php imagealtTitle('', $val['category_name'], 'Service Provider'); ?>">View All</a>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                    <?php
                  }
                  ?>

                </div>
                <div class="clearfix"></div>

                <?php
              } else {
                ?>
                <div class="alert alert-warning" role="alert">
                  <strong>Oh snap!</strong> No Listing Found.
                </div>
                <div class="row">
                  <div class="input-field s12 text-center">
                    <div class="row">
                      <div class="input-field s4 ">
                        <?php
                        if ($this->session->userdata('user_id') > 0) {
                          ?>
                          <a href="<?php echo site_url(); ?>members/add-product" class="waves-effect waves-light btn-large" >Are your a Service Provider? Add New Listing Now!</a>
                          <?php
                        } else {
                          ?>
                          <a href="#" data-dismiss="modal" data-toggle="modal" data-target="#regis-ter" class="waves-effect waves-light btn-large" >Are your a Service Provider? Add New Listing Now!</a>
                          <?php
                        }
                        ?>
                      </div>
                    </div>
                  </div>
                </div>
                <?php
              }
              ?>
            </div>
          </div>
        </div>
      </div>
    </div>
   
    <?php
    if (is_array($category_result) && !empty($category_result) && $category_result['category_description']) {
      ?>
      <div class="row">
        <div class="full_content">
          <?php echo $category_result['category_description']; ?>
        </div>
      </div>
      <?php
    }
    ?>
  
  </div>
</section>
<!--MOBILE APP-->
<script type="text/javascript">
  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_rows; ?>;

      if (!triggeredPaging && page < actual_count) {
        triggeredPaging = 1;
        $.ajax({
          type: "POST",
          url: "<?php echo base_url(); ?>category/ajax_load_category_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);
            });
          }
        });
      }
    }
  });
</script>
<?php $this->load->view("bottom_application"); ?>