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

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