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


class Ajx_functions extends CI_Controller{
	
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('Db_model');
		$this->load->library('cart');
		
		$this->load->helper('basic_helper');
		$ic=0;
		foreach ($this->cart->contents() as $items):
		$ic++;
		
		endforeach;
		$this->cart_qty = ''.$ic.'';
		//$this->cart_qty = ''.$this->cart->total_items().'';
		
		if($this->session->userdata('shop_user2'))
		{
			$this->load->library('encrypt') ;
			$this->user = $this->encrypt->decode($this->session->userdata('shop_user2'));
			$this->logged = 1;
		}
			
	}
	
	private function _isValidEmail($email)
	{
		return preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", $email);
	}
	
	
	
	
	
	public function get_ajax($category_id='')
	{
	 
		$category_id = $this->uri->segment(3);
		$cat_array = $this->Db_model->get_where_by_order('shop_subcategory',array('category_id'=>$category_id),'title','asc') ;
		if($cat_array!==FALSE)
		{
			echo '<option value="0">--All --</option>' ;
			
			foreach($cat_array as $row)
			{
				echo '<option value="'.$row['id'].'">'.$row['title'].'</option>' ;
			}
		}
		
		
	}
	
	
	
	public function dis_change($dist_id='')
	{
		
		
		
	 
		$dist_id = $this->uri->segment(3);

		$cat_array = $this->Db_model->get_where_by_order('pincodes',array('dis_id'=>$dist_id),'officename','asc') ;
		
		if($cat_array!==FALSE)
		{
			echo '<option value="">Select Postoffice</option>' ;
			
			foreach($cat_array as $row)
			{
				echo '<option value="'.$row['id'].'">'.$row['officename'].'</option>' ;
			}
		}
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	public function city_change($dist_id='')
	{
		
		
		
	 
		$dist_id = $this->uri->segment(3);

		$cat_array = $this->Db_model->get_where_by_order('city',array('dist_id'=>$dist_id),'title','asc') ;
		
		if($cat_array!==FALSE)
		{
			echo '<option value="0">--All--</option>' ;
			
			foreach($cat_array as $row)
			{
				echo '<option value="'.$row['id'].'">'.$row['title'].'</option>' ;
			}
		}
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	
	
	
public function sub_categories($sub_categories='')
{

$sub_categories = $this->uri->segment(3);
$sub_categories_products= $this->Db_model->get_where_by_order('shop_product',array('subcategory_id'=>$sub_categories),'aid','asc') ;
if($sub_categories_products!==FALSE)
{
echo '<option value="0">Select Product</option>' ;
foreach($sub_categories_products as $row)
{
echo '<option value="'.$row['aid'].'">'.$row['title'].'</option>' ;
}

}
else
{
	echo '<option>No Products in This category</option>' ;
}



}






	
public function sub_categories_products($sub_categories_products='')
{
$product_id = $this->uri->segment(3);
$product_ds = $this->Db_model->get_where_by_order('shop_product',array('aid'=>$product_id),'aid','asc') ;
if($product_ds==FALSE){
	echo '<span >Select A Product</span>' ;	
}
else
{
foreach($product_ds as $row2)
{



$sql ="SELECT * FROM shop_unit where id=".$row2['unit_id']."";

$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
foreach($query->result() as $row) {
$pro_quty=$row->title; 
}
}

		
echo '<span>Rs : '.$row2['price'].'/'.$pro_quty.'</span>' ;
}
}

//echo '<span >majo'.$product_id.'</span>' ;	
}
	
	
	
public function pro_qty($pro_qty='',$product_id='')
{
$pro_qty = $this->uri->segment(3);
$product_id = $this->uri->segment(4);


$sql ="SELECT * FROM shop_product where aid=".$product_id."";

$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
foreach($query->result() as $row) {
$price=$row->price; 
}
}

$protamount=$pro_qty * $price;

//echo $protamount;
echo '<span>Total Rs : '.$protamount.'</span>' ;
//echo '<input type="text" value="'.$protamount.'"   />';
//echo '<span>Product Qty'.$pro_qty.'Product id'.$product_id.'Product price'.$price.'Product Amount'.$protamount.'</span>' ;
}

	
	
function delete_item()
{ 


$row_id = $this->uri->segment(3);
$data = array(
               'rowid' => $row_id,
               'qty'   => 0
            );

$this->cart->update($data);
echo '<span>Total Rs'.$row_id.' </span>' ;
}	
	
	
	
	
		public function date_vali($dates_pik='')
	{
	 


$site_array = $this->Db_model->get_all('site',array()) ;
foreach($site_array as $row){ $deltime1=$row['dvt1']; $deltime2=$row['dvt2']; }


if(function_exists('date_default_timezone_set')) {
date_default_timezone_set("Asia/Kolkata");
}

   $date2 = str_replace('/', '-', $dates_pik);
   $datedeli=date('Y-m-d', strtotime($date2));$date_now = date("Y-m-d");
   $time =  date("H:i ");
   $datetime = new DateTime('tomorrow');
   $datetime5= $datetime->format('Y-m-d');
 //Our YYYY-MM-DD date string.

 
//Get the day of the week using PHP's date function.
$dayOfWeek = date("l", strtotime($datedeli));
 

	 
	    
	if ($date_now == $datedeli ) {
        if($time < "13:00") { echo '<option value="'.$deltime2.'">'.$deltime2.'</option>' ; }
		else{ echo '<option value="">This date is not Avilable</option>' ; }
    }
	elseif ($date_now > $datedeli ) {
      	echo '<option value="">This date is not Avilable</option>' ;
    }
	
    else{
	
	if($dayOfWeek=="Sunday")
	{
	 echo '<option value="">No Delivery in this date</option>' ;
	}
	
	elseif($dayOfWeek=="Monday")
	{
	echo '<option value="'.$deltime2.'">'.$deltime2.'</option>' ;
	}
	
	elseif($datedeli == $datetime5){ 
	
	
	if($time >= "19:00") { echo '<option value="'.$deltime2.'">'.$deltime2.'</option>' ; }
	else { echo '<option value="'.$deltime1.'">'.$deltime1.'</option>' ;
		echo '<option value="'.$deltime2.'">'.$deltime2.'</option>' ;}
}
	
	else
	{
      	echo '<option value="'.$deltime1.'">'.$deltime1.'</option>' ;
		echo '<option value="'.$deltime2.'">'.$deltime2.'</option>' ;
		}
    }
    
	
	
		
			
	
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public function user_exist($username='')
	{
		if($username!='')
		{
			
			if( strlen($username)>=6 && strlen($username)<=15)
			{
				
				$status = $this->Db_model->get_one('shop_checkoutusers',array('username'=>$username)) ;
		
				if($status!==FALSE)
				{
					echo '<span class="fontphp2">Username Exist.</span><br />' ;
				}
				else
				{
					echo '<span class="fontphp3">Username Available.</span><br />' ;
				}
			
			}
			else
			{
				echo '<span class="fontphp2">Username length b/w 6 to 15 .</span><br />' ;
			}
		}
		
	}
	
	public function email_exist()
	{
		
		if(isset($_POST['email']) && $_POST['email']!='')
		{
			$email = $_POST['email'] ;
		
			$status = $this->Db_model->get_one('shop_checkoutusers',array('email'=>$email)) ;
	
			if($status!==FALSE)
			{
				echo '<span class="fontphp2">Email Id Exist.</span><br />' ;
			}
			else
			{
				if( !$this->_isValidEmail($email) )
				{
					echo '<span class="fontphp2">Please enter a valid Email Id.</span><br />' ;
				}
				else
				{
					echo '<span class="fontphp3">Email Id Available.</span><br />' ;				
				}
				
			}
		
		}
		
		
		
	}
	

	
	


}