<?php 
class Component extends Admin_Controller{
	
	function __construct()
	{
		parent::__construct();
		$this->load->model("mod_component","component");
	}		

	public function index($page=1)
    {
		$tab['activeTab'] = "component";
		$this->load->model("mod_component");
		$data["component"] = $this->mod_component->getcomponent();
		$data["component"] = $data["component"]->result_array(); 
			
		$this->load->view('admin/header',$tab);
		$this->load->view('admin/component/view',$data);
		$this->load->view('admin/footer');
    }
	   
	public function add()
    {
		$tab['activeTab'] = "component";
		$this->load->model("mod_component");
		$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['value'] = $_POST['value'];
			$data['car_id'] = $_POST['carname'];
			$data['status'] = $_POST['status'];
			$data['createdate'] = date('y-m-d');
			$data['createby'] = $user_id;
				
			$msg=$this->component->add($data);
			//End Altering Database
			if($msg==1 || $msg==0){
				$this->session->set_flashdata('message',"New Component is Successfully Added");
					redirect('admin/component/add');
					
				}else{
					$this->session->set_flashdata('message',"New Component is Failure Added!");
				    redirect('admin/component/add');
					
				}
				
			}
				
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/component/add');
			$this->load->view('admin/footer');
       }   
	   
	   public function edit()
	   {
			$tab['activeTab'] = "component";
			$id = $this->uri->segment(4);
			$component = $this->component->getById($id);
			if(!$component)
			{
				redirect("admin/component");
			}
	
			if(@$_POST['update_component'])
			{	
			    $user_id = $this->session->userdata('userId');
				$postdata['name'] = $_POST['name'];
				$postdata['value'] = $_POST['value'];
				$postdata['car_id'] = $_POST['carname'];
				$postdata['status'] = $_POST['status'];
				$postdata['modifydate'] = date('y-m-d');
				$postdata['modifyby'] = $user_id;
				
				$msg=$this->component->update($postdata,$id);
				
				$this->session->set_flashdata('message',"Component is Successfully Update");
				redirect("admin/component");
			}
		
		    $data['component'] = $component;
		    $this->load->view('admin/header',$tab);
			$this->load->view('admin/component/edit',$data);
			$this->load->view('admin/footer');	
	   }
	
	
	   function delete()
	   {
			$id = $this->uri->segment(4);
			$this->component->delete($id);
			$this->session->set_flashdata('message',"Component deleted successfully");
			redirect("admin/component");
	   }
	
	   
	
}