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

class Product extends CI_Controller {
	
	public function index()
	{                                                              
                //$data['current']  = "home";   
                //$data['sliders']  = $this->general->getAllRecordsWhere(SLIDER , array("is_active" => 1),"id" , "asc"); 
		//$this->load->view('home',$data);
	}
        
        public function categories($slug= "",$sortby="date_added",$sort="desc")
        {
            $data['current']  = "$slug"; 
            $pagetitle = "";
            $catdetails = "";
            $where = "";
            $field = "CategoryID";
            $sort_by_cat = $this->input->post("filchoices");
            $catfilter = $this->session->userdata("catfilter");
            $pricefilter = $this->input->post("filpricesranges");
            $prifilter = $this->session->userdata("pricechoices");
            
            $allfillchoices = $this->preg_grep_keys('/^AllFilterChoices_/', $_POST);
            $allfillchoicesvalues = $this->session->userdata("AllFilterChoicesValues");
            
            
            if(isset($_POST) && Count($_POST) > 0)
            {       
                
                
                if(Count($allfillchoices)  == 0)
                    $this->session->unset_userdata('AllFilterChoicesValues');
                
                // unset variables ================================
                if(!isset($_POST["filchoices"]))
                        $this->session->unset_userdata('catfilter');
                
                if(!isset($_POST["filpricesranges"]))
                        $this->session->unset_userdata('pricechoices');
                                                
                
                        
                
                // set variables ===================================
                if(isset($_POST["filchoices"])) {
                     $newdata = array(
                                           'catfilter'         => $sort_by_cat                                           
                                           );                
                        $this->session->set_userdata($newdata);  
                }       
                
                if(isset($_POST["clearprice"]))
                {     $this->session->unset_userdata('pricechoices');$prifilter = $this->session->userdata("pricechoices");$pricefilter = "";}
                elseif(isset($_POST["filpricesranges"])) {
                    $newdata = array(                                           
                                           'pricechoices'     => $pricefilter[0]
                                           );                
                        $this->session->set_userdata($newdata); 
                }    
                
                                                               
                if(isset($allfillchoices)) {                       
                    if(Count($allfillchoices) > 0)
                    {
                        foreach($allfillchoices as $key => $value)
                        {                                                                                    
                            $resultval = explode("_",$value);                            
                            $sessionfiltereddata = $this->input->post("allclear_".$resultval[0]);                                                        
                            if($sessionfiltereddata != "")
                            {
                                // clear session array raw from post data                                
                                unset($allfillchoices[$key]);                                                               
                            }
                        }
                                           
                        $newdata = array(
                                           'AllFilterChoicesValues' => $allfillchoices                                           
                                           );                
                        $this->session->set_userdata($newdata);                          
                    }
                }
                
                
            }
            else
            {
                if(isset($catfilter))
                {
                    $sort_by_cat = $catfilter;
                }                
                if(isset($prifilter))
                {
                    $pricefilter[0] = $prifilter;                   
                }                
                if(isset($allfillchoicesvalues))
                {
                    $allfillchoices = $allfillchoicesvalues;
                }   
            }
            
            if($slug != "" && $sort_by_cat == "")
            {                                              
                $catdetails = $this->general->getRecordWhere(CATEGORIES,array("slug"=>$slug));                 
                $where = array("$field"=>$catdetails->CategoryID);
                $pagetitle = $catdetails->CategoryName;    
                $field = "";
                $catids = array();                
                
                $data["filteroptions"] = $this->general->getFilterChoices($catdetails->CategoryID);
                //print_r($data["filteroptions"]);
            }            
            else if($sort_by_cat != ""){    
                $catids = $sort_by_cat;
                $catdetails = $this->general->getAllRecordsWhereINWhere(CATEGORIES,$field,$sort_by_cat,array());  
                $where = false;
                $pagetitle = "Filtered Products";                
            }
            else {       
                $field = "";
                $catids = array();
                $catdetails = $this->general->getAllRecordsWhere(CATEGORIES,array());  
                $where = false;
                $pagetitle = "All Products";
            }
            
            $sort_by_url = $this->input->post("sort");                       
            $sortsession = $this->session->userdata("sortoption");
            
            if($sort_by_url == NULL && $sortsession != NULL)                        
            {$sort_by_url = $sortsession;}
            
            if($sort_by_url != NULL) 
            {
                // save in session 
                $newdata = array(
                                   'sortoption'         => $sort_by_url,                                                                  
                                   );                
                $this->session->set_userdata($newdata);
                
                if($sort_by_url == "recent")
                {
                    $sortby = "date_added";
                    $sort = "desc";                   
                }
                else if($sort_by_url == "pricelow")
                {
                    $sortby = "UnitPrice";
                    $sort = "asc";                    
                }
                else if($sort_by_url == "pricehigh")
                {
                    $sortby = "UnitPrice";
                    $sort = "desc";                   
                }
                else if($sort_by_url == "offer")
                {
                    $sortby = "DiscountAvailable";
                    $sort = "desc";
                    //$where = array("CategoryID"=>$catdetails->CategoryID,"DiscountAvailable"=>1);
                    $where["DiscountAvailable"] = 1;
                    //print_r($where);
                }                                
            }
            else {                              
                //$this->session->unset_userdata('sortoption');                
                $sortby = "date_added";
                    $sort = "desc";                   
            }
            
            
            $data["ranges"] = $this->general->getPriceRages(PRODUCTS,"UnitPrice",$where);
            
            // price ranges
            if($pricefilter != "")
            {                
                $resultprice = explode("_",$pricefilter[0]);
                $where["UnitPrice >="] = $resultprice[0];
                $where["UnitPrice <="] = $resultprice[1];                               
            }
            
           
            
            // query for all filter values 
            $advancedfilter = FALSE;
            if(isset($allfillchoices)) {                       
                    if(Count($allfillchoices) > 0)
                    {
                        $advancedfilter = TRUE;
                    }            
            }
            
            // pagination case
            $per_page = $this->input->get("per_page");
                     
            $this->load->library('pagination');
            $config['base_url'] = base_url()."/categories/".$slug;
                        
            //get total rows 
            if($advancedfilter)
            $totalrows = $this->general->getCountWhereINWhere2($where,$field,$catids,$allfillchoices);            
            else                    
            $totalrows = $this->general->getCountWhereINWhere(PRODUCTS,$where,$field,$catids);
            
            $config['total_rows'] = $totalrows;
            $config['per_page'] = 1;
            $config['page_query_string'] = TRUE;
            $config['full_tag_open'] = '<ul class="pagination paginationfloat">';
            $config['full_tag_close'] = '</ul>';
            $config['first_tag_open'] = '<li class="page-item">';
            $config['first_tag_close'] = '</li>';
            $config['last_tag_open'] = '<li class="page-item">';
            $config['last_tag_close'] = '</li>';
            $config['next_tag_open'] = '<li class="page-item">';
            $config['next_tag_close'] = '</li>';
            $config['prev_tag_open'] = '<li class="page-item">';
            $config['prev_tag_close'] = '</li>';
            $config['cur_tag_open'] = '<li class="page-item page-link current-link">';
            $config['cur_tag_close'] = '</li>';
            $config['num_tag_open'] = '<li class="page-item">';
            $config['num_tag_close'] = '</li>';
            $config['attributes'] = array('class' => 'page-link');
            $this->pagination->initialize($config);
            $data['paginationstring'] = $this->pagination->create_links();   

            if($advancedfilter)     
            $data['productsdetails'] = $this->general->getAllRecordsWhereINWhere2($field,$catids,$where,$sortby,$sort,$config['per_page'],$per_page,$allfillchoices);            
            else    
            $data['productsdetails'] = $this->general->getAllRecordsWhereINWhere(PRODUCTS,$field,$catids,$where,$sortby,$sort,$config['per_page'],$per_page);            
            
                      print_r($this->db->last_query());
                        
            $data['pagetitle']  = $pagetitle;                                                   
            $this->load->view('category',$data);
        }
        
        public function productdetails($slug,$proid)
        {
            die($slug.$proid);
        }
        
        function preg_grep_keys($pattern, $input, $flags = 0) {
            return array_intersect_key(
                $input,
                array_flip(preg_grep(
                   $pattern,
                   array_keys($input),
                   $flags
                ))
            );
        }
       
}
