<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Images extends Admin_Controller {
	
	function __construct()
	{
			parent::__construct();
			$this->load->model("mod_images","images");
			$this->load->helper(array('file','string'));
	}	
	
	public function index()
    {
		$tab['activeTab'] = "images";
		$data["images"] = $this->images->getimages();
		$data["images"] = $data["images"]->result_array(); 
		$this->load->view('admin/header',$tab);
		$this->load->view('admin/images/view',$data);
		$this->load->view('admin/footer');
    }
	
	private function set_upload_options()
	{   
    	//upload an image options
    	$config = array();
    	$config['upload_path'] = './assets/img/car/';
    	$config['allowed_types'] = 'gif|jpg|png';
    	$config['max_size']      = '2048';
    	$config['overwrite']     = FALSE;
		//$config['encrypt_name']	=  TRUE;

    	return $config;
	}
		
	public function add()
        {
			$tab['activeTab'] = "images";
			$this->load->model("mod_car");
			$this->form_validation->set_rules('title', 'Title', 'required');
			//$this->form_validation->set_rules('image', 'Image', 'required');
			if (empty($_FILES['uploadedimages']['name']))
		    {
				$this->form_validation->set_rules('uploadedimages','Image Field', 'required');
		    }
			
			if ($this->form_validation->run() == TRUE)
			{
			$total = count($_FILES["uploadedimages"]["name"]);
			echo $total;
			//Start Upload Picture
				$config = array(
                	'upload_path' => './assets/img/car/',
                	'allowed_types' => 'gif|jpg|png',
                	'max_size' => '2048',
					'overwrite' => FALSE,
					'encrypt_name' => TRUE
					//'file_name' => $fname,
            	);
				
            	// load Upload library
				$this->load->library('upload', $config);
            	$this->upload->do_upload('uploadedimages');
            	$uploaded = $this->upload->data();
				$arrlength = count($uploaded);
				if($total == 1){
						$img_name = $uploaded['file_name'];
						$data['title'] = $_POST['title'];
						$data['name'] = $img_name;
						$data['car_id'] = $_POST['carname'];
						$data['create_date'] = date('y-m-d');
						$data['upload_by'] = $this->session->userdata('userId');
						$msg = $this->images->add($data);
				}else{
					for($x = 0; $x < $arrlength; $x++) {
						$img_name = $uploaded[$x]['file_name'];
						$data['title'] = $_POST['title'];
						$data['name'] = $img_name;
						$data['car_id'] = $_POST['carname'];
						$data['create_date'] = date('y-m-d');
						$data['upload_by'] = $this->session->userdata('userId');
						$msg = $this->images->add($data);
					}
            		//exit();	
				}
				
			//End Upload Picture	
				if($msg==1 || $msg==0){
					$this->session->set_flashdata('message',"New Images is Successfully Added");
					redirect('admin/images/add');
					
					}else{
					$this->session->set_flashdata('message',"New Images is Failure Added!");
				    redirect('admin/images/add');
					
				}
				
			}
			
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/images/add');
			$this->load->view('admin/footer');
        }
		
	   public function edit()
	   {
			$tab['activeTab'] = "images";
			$id = $this->uri->segment(4);
			$img = $this->images->getById($id);
			if(!$img)
			{
				redirect("admin/images");
			}
	
			if(@$_POST['update_images'])
			{	
			    $user_id = $this->session->userdata('userId');
				$postdata['title'] = $_POST['title'];
				$postdata['car_id'] = $_POST['carname'];
				$postdata['modify_date'] = date('y-m-d');
				$postdata['modify_by'] = $this->session->userdata('userId');
				
				$msg=$this->images->update($postdata,$id);
				
				$this->session->set_flashdata('message',"Images is Successfully Update");
				redirect("admin/images");
			}
		
		    $data['img'] = $img;
		    $this->load->view('admin/header',$tab);
			$this->load->view('admin/images/edit',$data);
			$this->load->view('admin/footer');	
	   }
	
	
	   function delete()
	   {
			$id = $this->uri->segment(4);
			$this->images->delete($id);
			$this->session->set_flashdata('message',"Images deleted successfully");
			redirect("admin/images");
	   }
	
	
	
}