<?php namespace App\Controllers;

use CodeIgniter\Controller;
use \App\Andymodel;
use \App\Andy\ImgPreCheck;
use \App\Andy\Utility;
use CodeIgniter\I18n\Time;
use \App\Models\ProductModel;


class Product   extends BaseController
{
		protected $id;
		protected $portfolioId ;
		protected $productImages="productImages";
		
		
			public function __construct() 
						{
							parent::__construct();
							$this->myTime = parent::getTime();
							$this->myDate= date("d/m/Y",$this->myTime); 	
						
					   }
		  
			public function productAddForm()
				   {
					   $data = [
								'title'  => 'any ',
								'date'=>$this->myDate,
								'info'=>' '
						];
						echo view('addProductForm',$data);
				  }
  
  
			  public function addProductDo()
			  
				{
					
				     $utilityHandle = new Utility(); 
				    
				    $info=$this->request->getVar('info'); 
					$info=$utilityHandle->removeScript($info);
					$info=$utilityHandle->convertQuotes($info);
					$info= $utilityHandle->newRegex($info);
					$title=$this->request->getVar('title'); 
					$title= $utilityHandle->badCharsRemoveAll($title);
					$slug=url_title($title);
					$category=  $this->request->getVar('category'); 
					$price=  $this->request->getVar('price'); 
					$price= $utilityHandle->badCharsRemoveAll($price);
					$file =   $this->request->getFile('productImage');
					$nameImgFile= $file->getName(); 
				
				
					$image = new ImgPreCheck();
					$handle= new ProductModel();
					
					//first check file suffix to make sure image 
					$logic= $image->extCheck($nameImgFile);
					if( $logic===0)
							{
							  $data = [
										'title'  => 'any ',
										'date'=>$this->myDate,
										'info'=>'thats not   a valid image '
								];
								
							echo view('addProductForm',$data);
								  exit();
								
							}
					//now check to make sure image doresnt already exist
					$logic= $image->allReadyExists($this->productImages,$nameImgFile);
					
					if( $logic==true)
							{
							  $data = [
										'title'  => 'any ',
										'date'=>$this->myDate,
										'info'=>'that image already exists,'
								];
								
							echo view('addProductForm',$data);
								  exit();
								
							}
							
							
					//check fiel size is not overly large
					
					$imageSize=  $image->getFileSize($file);
					
					if($imageSize >= 300000)
					{
							  $data = [
										'title'  => 'any ',
										'date'=>$this->myDate,
										'info'=>'that image is too large '
								];
								
							echo view('addProductForm',$data);
							exit();
								
							}
							
				try{
				
				 $file->move(ROOTPATH.'public/productImages',$nameImgFile);
				 $handle = new ProductModel();
				$outcome= $handle->insertProduct($nameImgFile,$title,$slug,$category,$info,$price);
				 
				
				
			}

			 catch ( \Exception  $e)
								
										   {
											
											$data = [
										'title'  => 'any ',
										'date'=>$this->myDate,
										'info'=>'something went wrong'.$e->getMessage()
								];
											

												echo view('info', $data);
												
											 
					  }
				
				
				$data= [
										'title'  => 'any ',
										'date'=>$this->myDate,
										'info'=>'looks like product was added '
								];
											

												echo view('info', $data);
					
				
				
				} //end add product do 
	//start of new product function 		  
  public function getCategory($theCategory)
		 {
		
		
		
			 //start
		$handle= new ProductModel();
		$dbEntries = $handle->getCountByCategory($theCategory); 
		
		
		if  ( ($dbEntries==0)OR  ($dbEntries==null))
				  {
				  $data= [
							
							'title'    => 'products ',
							'category'  =>$theCategory,
							'date'    =>$this->myDate,
							'info'=>'no products in this category at the monment '
										];
				echo view('infoProduct', $data);	
		        die();
			  }
		 
		 
		 
		 else   {
				$result= $handle->useCategory($theCategory);
				$data = [
								 
								 'result'=>$result,
								 'title'=>'paginate',
								'portfolio' => $handle->paginate(5),
								'pager' => $handle->pager,
								'category'=>$theCategory,
								'date'=>$this->myDate
							];
							
				   
				   echo view('displayProducts',$data);
				 } 
				 
				
				 
				 
	  }//end new product category function
  
		  
	 
	 
	
	 
  
 
  
  
  
  
  
  

  
  public function prodInfo($Id)
	
	{
	
	
		$handle = new ProductModel;
	$singleEntry= $handle->getOne($Id);
		
	$data = [
                'title'  => 'home page ',
               'date'=>$this->myDate,
                'prodInfo'=>$singleEntry
		];
			
			
			
			
			
			
			
			
		
        echo view('productInfo',$data);
	
	
	
	
	}	
	
  
  
  public function displayProdsToEdit()
  {
	 $handle= new  ProductModel();
	  $result= $handle->getAll();
	
	$data = [
                'title'  => ' ',
               'date'=>$this->myDate,
                'products'=>$result
		];
			
				
			
		
        echo view('editProductForm',$data);
	
	
	}
  
  
  public function editOneProduct($id)
  
  {
	  
	  $handle= new ProductModel();
      $result= $handle->getOne($id);
     
      $data = [
                'title'  => 'edit ',
               'date'=>$this->myDate,
                'product'=>$result,
                'info'=>''
		];
			
		echo view('editProductFormDo', $data);		
  }
  
  
  public function processEditProduct()
  {
	
  
        $info=$this->request->getVar('info'); 
      	$imageTitle=$this->request->getVar('title'); 
	 	$slug=url_title($imageTitle);
	  	$category=  $this->request->getVar('category'); 
	  	$price=  $this->request->getVar('price'); 
		$Id= $this->request->getVar('Id'); 
        $handle= new ProductModel();
         $logic= $handle->amendProduct($Id,$imageTitle,$slug,$category,$info,$price);
        
					if($logic == 1)
					
					{
						$data = [
							'title'  => 'product Edit ',
						   'date'=>$this->myDate,
								   'info'=>'looks like edit of product was successful'
					];   
					echo view('info',$data);
					exit();
				}
					else
					{
					$data = [
							'title'  => 'product Edit ',
						   'date'=>$this->myDate,
								   'info'=>'something went wrong with edit'
								   
									];   
					echo view('info',$data);
					exit();	
						
					}
	  	 
  }//end of processing product edit 
  
  public function delProdForm()
  
  {
		$handle = new ProductModel;
	     $result= $handle->getAll();
		
	$data = [
                'title'  => ' ',
               'date'=>$this->myDate,
                'prods'=>$result,
                'info'=>''
		];  
	  echo view('removeProdForm',$data);
  }



public function delProductDo()
{

	$productId= $this->request->getVar('prodId'); 
	$handle= new ProductModel();
	$result= $handle->getOne($productId);
	$image= $result['image'];
	
	
	
	try
	{
	chmod(ROOTPATH.'public/productImages/'.$image,0777);
	unlink(ROOTPATH.'public/productImages/'.$image);
	$handle->deleteOne($productId);
}


 catch ( \Exception  $e)
			 {
			$error= $e->getMessage();
			 $data = [
						'title'  => 'info',
					  
				   'info'=>'somwething went wrong with deleting blog'.$error,
				   'date'=>$this->myDate
					 ];

				
				echo view('info', $data);
				
				  
		   
			 die();
			 //above try catch works 
		   }
	  
	   $data = [
						'title'  => 'info',
					  
				   'info'=>'looks like product was deleted ',
				   'date'=>$this->myDate
					 ];

				
				echo view('info', $data);



	//above works now need to check id typed in exitst, delete from database and then unlink image


}

  
}//end class 
