<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Filter extends CI_Controller {

	function __construct() 
	{
       error_reporting(0);
        parent::__construct();
        $this->load->model('CommonModel');
        $this->load->helper('Security');
        $this->load->library('form_validation');
        $this->load->library('cart');
       
    }

  public function sort_filter()
	{
    $input_data = $this->input->post();
    $input_data = $this->security->xss_clean($input_data);
    if ($input_data['sorttype']=='Sort by latest') {
        $prd_cate = $this->db->select('*')->like("p_title",'')->where('status','1')->where("p_description LIKE '%$this->input->get('data')%'")->order_by('prd_id','desc')->get('product')->result_array();
        //$prd_cate = $this->db->select('*')->like("p_title",'')->where("p_description LIKE '%$this->input->get('data')%'")->order_by('prd_id','desc')->get('product')->result_array();
        foreach($sub_cat as $get_prd){
          if($get_prd['discount_type']==''){
                $discount='';
            } else{
                if($get_prd['discount_type']=='Price'){
                    $discount='₹'.$get_prd['discount'].' Off';
                }else{
                    $discount=$get_prd['discount'].'% Off';
                }
            }
                        ?>

                            <div class="grid-item col">
                                <div class="product">
                                    <div class="product-thumb">
                                        <a href="<?php echo base_url().'Product/product_details/'.md5($get_prd['prd_id']).'/?mode='.md5($get_prd['prd_id']); ?>" class="image">
                                            <img src="<?php echo base_url(); ?>uploads/product/thumbnail/<?php echo $get_prd['primary_image'];?>" alt="<?php echo $get_prd['p_title'];?>">
                                        </a>
                                        <a href="#" class="add-to-wishlist hintT-left <?php if($get_prd['p_quantity']>0) { ?> add_cart <?php } ?>" data-productid="<?php echo $get_prd['prd_id'];?>" data-productname="<?php echo $get_prd['p_title'];?>" data-productprice="<?php echo $get_prd['p_sell_price'];?>" <?php if($get_prd['p_quantity']<1) { ?> disabled <?php } ?> data-hint="Add to Cart"><i class="far fa-shopping-cart"></i></a>
                                    </div>
                                    <div class="product-info">
                                        <h6 class="title"><a href="<?php echo base_url().'Product/product_details/'.md5($get_prd['prd_id']).'/?mode='.md5($get_prd['prd_id']); ?>"><?php echo $get_prd['p_title'];?></a></h6>
                                        <span class="price">
                                            <span class="new">₹<?php echo $get_prd['p_sell_price'];?></span>
                                            <span class="DisCnt"><?php echo $discount;?></span>
                                        </span>
                                    </div>
                                </div>
                            </div>
                        <?php }
    }
    

    }
  public function price_filter()
    {
    $input_data = $this->input->post();
    $input_data = $this->security->xss_clean($input_data);

                    $food_preference="";
                    $discount="";
                    $eprice="";

                    $food_preference = isset($input_data['food_preference'])?$input_data['food_preference']:"";
                    $discount = isset($input_data['discount'])?$input_data['discount']:"";
                    $sprice = isset($input_data['sprice'])?$input_data['sprice']:"";
                    $eprice = isset($input_data['eprice'])?$input_data['eprice']:"";

                    $query = "select * from product where sub_cat_id = '".$input_data['sub_category']."'";
                    $query = "SELECT p_title, p_mrp, p_sell_price,sub_cat_id,p_code,product_size,product_unit,discount,ingredients,disclaimer,p_mrp,p_sell_price,p_quantity,p_description,p_img,primary_image,special_offer,food_preference, (round( (p_mrp-p_sell_price)/p_mrp * 100 )) AS percentage FROM product where sub_cat_id = '".$input_data['sub_category']."'"; 
         
                      if(!empty($food_preference)){
                        $food_preference_size=sizeof($food_preference);
                          $colordata =implode("','",$food_preference);
                          foreach ($food_preference as $food_preference_key => $food_preferencevalue) {
                            $position_value=$food_preference_key+1;
                            if ($food_preference_key==0) {
                                $query  .= " and ( food_preference = '".$food_preferencevalue."'";
                            }
                            else
                            {
                                $query  .= " or food_preference = '".$food_preferencevalue."'";
                            }
                            if ($position_value==$food_preference_size) {
                                $query  .= " ) ";
                            }
                          } 
                      }
                      $discount_percentage=0;
                      //print_r($discount);
                      if(!empty($discount)){
                        
                        $discount_percentage=min($discount);
                      }
                      if(!empty($sprice) && !empty($eprice)){
                        $query  .= " and p_sell_price between $sprice and $eprice "; 
                      }
                      if(empty($sprice) && !empty($eprice)){
                        $query  .= " and p_sell_price between 0 and $eprice "; 
                      }
                      if(!empty($sprice) && empty($eprice)){
                        $query  .= " and p_sell_price between $sprice and 2000 "; 
                      }
                     $res_query=$this->db->query($query); 
                     $num_rows1 = $res_query->num_rows();
                     if($num_rows1<1){
                        ?>
                        <div class="agileinfo-ads-display col-md-9 w3l-rightpro">

                          <img src="<?php echo base_url();?>images/no-product-found.png" class="img-responsive">

                        </div>
                        <?php
                        }
                        else{
                     ?>
                 <div class="agileinfo-ads-display col-md-9 w3l-rightpro">
        <div class="wrapper">
          <!-- first section -->
          <div class="product-sec1">
            <?php 
            //$discount_percentage = str_replace('%', '', $discount_percentage);
                    foreach ($res_query->result_array() as $row_product){
                            if ($discount_percentage<$row_product['percentage']) {
                            //echo $discount_percentage;
                            $img=explode(",", $row_product['p_img']);
                            $discount_price=($row_product['p_mrp']-$row_product['p_sell_price'])*100/$row_product['p_mrp'];
                            $discount_price=ceil($discount_price);

                            ?>
                        <div class="col-xs-4 product-men">
                            <div class="men-pro-item simpleCart_shelfItem">
                                <div class="men-thumb-item">
                                    <img src="<?php echo base_url(); ?>uploads/product/thumbnail/<?php echo $row_product['primary_image'];?>" alt="">
                                    <div class="men-cart-pro">
                                        <div class="inner-men-cart-pro">
                                            <a href="<?php echo base_url().'Product/product_details/'.md5($row_product['prd_id']).'/?mode='.md5($row_product['prd_id']); ?>" class="link-product-add-cart">More Details</a>
                                        </div>
                                    </div>
                                    <span class="product-new-top">New</span>

                                </div>
                                <div class="item-info-product">
                                    <h4>
                                        <a href="<?php echo base_url().'Product/product_details/'.md5($row_product['prd_id']).'/?mode='.md5($row_product['prd_id']); ?>"><?php echo $row_product['p_title'];?></a>
                                    </h4>
                                    <div class="info-product-price">
                                        <span class="item_price">Rs.<?php echo $row_product['p_sell_price'];?></span>
                                        <del>Rs.<?php echo $row_product['p_mrp'];?></del><br>
                    <span style="font-size:11px;color:#666;">Todays Price</span>
                                    </div>
                                    <div class="snipcart-details top_brand_home_details item_add single-item hvr-outline-out">
                                    <input type="submit" name="submit" value="Add to cart" class="button <?php if($row_product['p_quantity']>0) { ?> add_cart <?php } ?>" data-productid="<?php echo $row_product['prd_id'];?>" data-productname="<?php echo $row_product['p_title'];?>" data-productprice="<?php echo $row_product['p_sell_price'];?>" <?php if($row_product['p_quantity']<1) { ?> disabled <?php } ?>/>
                                    </div>

                                </div>
                            </div>
                        </div>
                        <?php } else { ?>
                          <img src="<?php echo base_url();?>images/no-product-found.png" class="img-responsive">
                     <?php } } ?>
            <div class="clearfix"></div>
          </div>
          <!-- //first section -->
          <!-- 2nd section) -->
          <!-- //2nd section  -->
          

        </div>
      </div> 
                        <?php
                    }
            
	}
}
?>