<?php namespace App\Controllers;

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


class Gallery  extends BaseController
{
		
		protected $id;
		protected $portfolioId ;
        protected $directory="galleryImages";
	    
	    public function __construct() 
				    {
						parent::__construct();
						$this->myTime = parent::getTime();
						$this->myDate= date("d/m/Y",$this->myTime); 	
					
			       }
		
		public function displayGallery()
				{
			      	 $handle = new GalleryModel();	
					 $mycount= $handle->isEmptyGallery();
					 $count= $mycount["COUNT(*)"];
					
					
					
					
					
					
							 if(($count ==0)OR ($count==null))
							 {
							$data = [
							'title'  => 'info ',
							'date'=>$this->myDate,
							'info'=>'currently gallery is empty'
									];	 
								echo view('info',$data);
							  die();
							 }
							
					else
					{
						 $data = [
						 'title'=>'paginate',
						'result' => $handle->paginate(5),
						'pager' => $handle->pager,
						'date'=>$this->myDate
					];
					echo view('displayGallery',$data);
		
		
					}
				}
		
		
		
		
		public function addGalleryDo()
  
				{
	
	               $utilityHandle = new Utility();
					$title=$this->request->getVar('title'); 
					
					$cleanTitle= $utilityHandle->badCharsRemoveAll($title);
					
					$slug=url_title($cleanTitle);
					$file =   $this->request->getFile('galleryImage');
					$nameImgFile= $file->getName();   
					$imgHandle= new  ImgPreCheck();
					$handle= new GalleryModel();
					$logic= $imgHandle->extCheck($nameImgFile);
	  
						 if($logic == 0)
						
						{
						
							$data =[		'title'=>'Gallery ',
							'info'=>'that is not an image file',
							'date'=>$this->myDate
							];
							echo view('addGalleryForm',$data);
							exit();
						
						}
	 
	
					$Logic2= $logic= $imgHandle->allReadyExists($this->directory,$nameImgFile);
	  
		
		
		
							if( $logic==true)
								{
								  $data = [
											'title'  => 'any ',
											'date'=>$this->myDate,
											'info'=>'that image already exists,'
								];
									
								echo view('addGalleryForm',$data);
								exit();
									
								}
				
					$size= $imgHandle->getFileSize($file);
	
					if($size > 380000)
					
								{
								$data =[
									'title'=>'gallery',
									'info'=>'thats too large an image file',
									'date'=>$this->myDate
									];
											
										
									echo view('addGalleryForm',$data);
									
								 exit();
					
					}
		
	              //move image and insert into db now since everything else ok with try block 
	              try{
	
	
					$galleryModelHandle = new GalleryModel();
                    $galleryModelHandle->insertGallery($nameImgFile,$cleanTitle,$slug);
	                @chmod(ROOTPATH.'public/galleryImages', 0777 );
					$file->move(ROOTPATH.'public/galleryImages', $nameImgFile);		
  
				}
  
  //catch here 
                        catch ( \Exception  $e)
						 {
						 $data = [
									'title'  => 'info',
								  
							   'info'=>'somwething went wrong '.$e->getMessage(),
							   'date'=>$this->myDate
							   
								 ];

							
							echo view('info', $data);
							die();
						 //above try catch works 
					   }
		
  
  
  
			 $data =[
				'title'=>'gallery',
				'info'=>'looks like new gallery was added ',
				'date'=>$this->myDate
				];
		  echo view('info',$data);
  
				}//end addGallery method
	
				public function galleryAddForm()
				
				{
				$data = [
												'title'  => 'info',
											  
										   'info'=>'',
										   'date'=>$this->myDate
										   
											 ];	
					
					
				echo view('addGalleryForm',$data);
				
				}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	 public function delGalleryForm()
  
  {
		$handle = new GalleryModel;
	     $result= $handle->getAllDo2();
		
	$data = [
                'title'  => ' ',
               'date'=>$this->myDate,
                'result'=>$result,
                'info'=>''
		];  
	  echo view('removeGalleryForm',$data);
  }

 public function delGalleryDo()
 
 {

	 $galleryId= $this->request->getVar('galleryId'); 
	echo $galleryId;
    $handle= new GalleryModel();
    $result= $handle->getOneToDel2($galleryId);
 
  $Id= $result['Id'];
   $image=  $result['image'];//works so now use name image to unlink and dlete from db
  unlink(ROOTPATH.'public/galleryImages/'.$image);
   $handle->delOneGallery($Id);
 
 $data = [
                'title'  => 'removev gallery ',
               'date'=>$this->myDate,
               
                'info'=>'seems image was deleted'
		];  
	  echo view('info',$data);
 
 
 
 }


		
	    
	    
   
			   
    
			
			  
    
}//class end
