<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Brand extends Admin_Controller {
	
	   function __construct()
	   {
			parent::__construct();
			$this->load->model("mod_brand","brand");
	   }		

       public function index($page=1)
       {	
	   		$tab['activeTab'] = "brand";
		    $this->load->model("mod_brand");
			$data["brand"] = $this->mod_brand->getbrand();
			$data["brand"] = $data["brand"]->result_array(); 
			
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/brand/view',$data);
			$this->load->view('admin/footer');
       }
	
	   public function add()
       {	
	   		$tab['activeTab'] = "brand";
		    $this->load->model("mod_brand");
			$this->form_validation->set_rules('name', 'Name', 'required');
		    
			if ($this->form_validation->run() == TRUE)
			{
			//Altering Database
				$user_id = $this->session->userdata('userId');
				$data['name'] = trim($_POST['name']);
				$data['status'] = $_POST['status'];
				$data['detail'] = $_POST['detail'];
				$data['createdate'] = date('y-m-d');
				$data['createby'] = $user_id;
				
				$msg=$this->brand->add($data);
				//End Altering Database
				if($msg==1 || $msg==0){
					$this->session->set_flashdata('message',"New Brand is Successfully Added");
					redirect('admin/brand/add');
					
					}else{
					$this->session->set_flashdata('message',"New Brand is Failure Added!");
				    redirect('admin/brand/add');
					
				}
				
			}
				
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/brand/add');
			$this->load->view('admin/footer');
       }
	
		
	   public function edit()
	   {
			$tab['activeTab'] = "brand";
			$id = $this->uri->segment(4);
			$brand = $this->brand->getById($id);
			if(!$brand)
			{
				redirect("admin/brand");
			}
	
			if(@$_POST['update_brand'])
			{	
			    $user_id = $this->session->userdata('userId');
				$postdata['name'] = $_POST['name'];
				$postdata['detail'] = $_POST['detail'];
				$postdata['status'] = $_POST['status'];
				$postdata['modifydate'] = date('y-m-d');
				$postdata['modifyby'] = $user_id;
				
				$msg=$this->brand->update($postdata,$id);
				
				$this->session->set_flashdata('message',"Car Brand is Successfully Update");
				redirect("admin/brand");
			}
		
		    $data['brand'] = $brand;
		    $this->load->view('admin/header',$tab);
			$this->load->view('admin/brand/edit',$data);
			$this->load->view('admin/footer');	
	   }
	   
	   
	   function delete()
	   {
			$id = $this->uri->segment(4);
			$this->brand->delete($id);
			$this->session->set_flashdata('message',"Car Brand deleted successfully");
			redirect("admin/brand");
	   }
	   
		
}