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

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