<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Car extends Admin_Controller {
	
	   function __construct()
	   {
			parent::__construct();
			$this->load->model("mod_car","car");
			$this->load->model("mod_images","img");
			$this->load->helper(array('file','string','my_file'));

	   }		

        public function index()
        {
			$tab['activeTab'] = "car";
			$this->load->model("mod_car");
			$data["car"] = $this->mod_car->getcar();
			$data["car"] = $data["car"]->result_array(); 
			
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/car/view',$data);
			$this->load->view('admin/footer');
        }
		
		
		
		public function add()
        {
			$tab['activeTab'] = "car";
			$this->load->model("mod_car");
			$this->form_validation->set_rules('cartype', 'Cartype', 'required');
			$this->form_validation->set_rules('fullname', 'Fullname', 'required');
			$this->form_validation->set_rules('year', 'Only Number', 'required|numeric');
			//$this->form_validation->set_rules('image', 'Image', 'required');
			if (empty($_FILES['userfile']['name']))
		    {
			$this->form_validation->set_rules('userfile','Image Field', 'required');
		    }
			
			if ($this->form_validation->run() == TRUE)
			{
			//Start Upload Picture
				$fname = random_string('alnum', 16);
				$fExt=end(explode('.', $_FILES["userfile"]["name"]));
				$fExt=strtolower($fExt);
				$config['upload_path'] = './assets/img/slide/';
				$img_name=$config['file_name'] = $fname.".".$fExt;
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size'] = '5120';
				//$config['max_width'] = '1024';
				//$config['max_height'] = '768';
				$config['overwrite']  = "TRUE";
				
				$this->load->library('upload', $config);
				$this->upload->do_upload();
			//End Upload Picture	
			
			//Altering Database
				$user_id = $this->session->userdata('userId');
				$data['cartype'] = $_POST['cartype'];
				$data['fullname'] = trim($_POST['fullname']);
				$data['brandname'] = $_POST['brandname'];
				$data['year'] = $_POST['year'];
				$data['status'] = $_POST['status'];
				$data['slideshow'] = isset($_POST['slide']);
				$data['detail'] = $_POST['detail'];
				$data['images'] = $img_name;
				$data['createdate'] = date('y-m-d');
				$data['createby'] = $user_id;
				$msg=$this->car->add($data);
				
				//End Altering Database
				if($msg==1 || $msg==0){
					$this->session->set_flashdata('message',"New Car is Successfully Added");
					redirect('admin/car/add');
					
					}else{
					$this->session->set_flashdata('message',"New Car is Failure Added!");
				    redirect('admin/car/add');
					
				}
				
			}
			
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/car/add');
			$this->load->view('admin/footer');
        }
		
		public function edit()
		{
			$tab['activeTab'] = "car";
			$id = $this->uri->segment(4);
			$car = $this->car->getById($id);
			if(!$car)
			{
				redirect("admin/car");
			}
	
			if(@$_POST['update_car'])
			{	
				if (!empty($_FILES['userfile']['name']))
		    	{
					$fname = random_string('alnum', 16);
					$fExt=end(explode('.', $_FILES["userfile"]["name"]));
					$fExt=strtolower($fExt);
					$config['upload_path'] = './assets/img/slide/';
					$img_name=$config['file_name'] = $fname.".".$fExt;
					$config['allowed_types'] = 'gif|jpg|png';
					$config['max_size'] = '5120';
					$config['overwrite']  = "TRUE";
					$this->load->library('upload', $config);
					$this->upload->do_upload();
					//$data['images'] = $img_name;
		    	}
				
			    $user_id = $this->session->userdata('userId');
				$postdata['cartype'] = trim($_POST['cartype']);
				$postdata['fullname'] = $_POST['fullname'];
				$postdata['brandname'] = $_POST['brandname'];
				$postdata['year'] = $_POST['year'];
				$postdata['status'] = $_POST['status'];
				$postdata['slideshow'] = isset($_POST['slide']);
				$postdata['detail'] = $_POST['detail'];
				if (!empty($_FILES['userfile']['name']))
		    	{
					$postdata['images'] = $img_name;
				}
				$postdata['modifydate'] = date('y-m-d');
				$postdata['modifyby'] = $user_id;
				
				
				
				$msg=$this->car->update($postdata,$id);
				$this->session->set_flashdata('message',"Car is Successfully Update");
				redirect("admin/car");
			}
		
		    $data['car'] = $car;
		   
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/car/edit',$data);
			$this->load->view('admin/footer');	
		}
		
		function delete()
	   {
			$id = $this->uri->segment(4);
			$this->car->delete($id);
			$this->session->set_flashdata('message',"Car deleted successfully");
			redirect("admin/car");
	   }
		
	   
	   public function upload_multiple_files()
       {
        	if ($this->input->post()) {
            	$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();
			//print_r($uploaded[3]['file_name']);
           
			$arrlength = count($uploaded);
			for($x = 0; $x < $arrlength; $x++) {
				print_r($uploaded[$x]['file_name']);
    			echo "<br>";
			}
            exit();
			
        }
        $this->load->view('admin/car/upload_form');
       }
	   
	   
}