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

       public function index($page=1)
       {	
	   		$tab['activeTab'] = "management";
		    $this->load->model("mod_management");
			$data["management"] = $this->mod_management->getuser();
			$data["management"] = $data["management"]->result_array(); 
			
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/management/view',$data);
			$this->load->view('admin/footer');
       }
	
	   public function add()
       {	
	   		$tab['activeTab'] = "management";
			$this->form_validation->set_rules('name', 'Name', 'required');
		    
			if ($this->form_validation->run() == TRUE)
			{
			//Altering Database
				//$user_id = $this->session->userdata('userId');
				$data['username'] = trim($_POST['name']);
				$data['password'] = sha1($_POST['passwd']);
				$data['email'] = $_POST['email'];
				$data['role_id'] = $_POST['position'];
				$data['banned'] = $_POST['status'];
				$data['createdate'] = date('y-m-d');
				
				$msg=$this->management->add($data);
				//End Altering Database
				if($msg==1 || $msg==0){
					$this->session->set_flashdata('message',"New User is Successfully Added");
						redirect('admin/management/add');
					
					}else{
					$this->session->set_flashdata('message',"New User is Failure Added!");
				    	redirect('admin/management/add');
					
				}
				
			}
				
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/management/add');
			$this->load->view('admin/footer');
       }
	
		
	   public function edit()
	   {
			$tab['activeTab'] = "management";
			$id = $this->uri->segment(4);
			$management = $this->management->getById($id);
			if(!$management)
			{
				redirect("admin/management");
			}
	
			if(@$_POST['update_management'])
			{	
			    //$user_id = $this->session->userdata('userId');
				
				$postdata['username'] = trim($_POST['name']);
				//$postdata['password'] = sha1($_POST['passwd']);
				$postdata['email'] = trim($_POST['email']);
				$postdata['banned'] = $_POST['status'];
				$postdata['role_id'] = $_POST['position'];
				$postdata['modifydate'] = date('y-m-d');
			
				$msg = $this->management->update($postdata,$id);
				
				$this->session->set_flashdata('message',"User is Successfully Update");
				redirect("admin/management");
			}
		
		    $data['management'] = $management;
		    $this->load->view('admin/header',$tab);
			$this->load->view('admin/management/edit',$data);
			$this->load->view('admin/footer');	
	   }
	   
	   
	   function delete()
	   {
			$id = $this->uri->segment(4);
			$this->management->delete($id);
			$this->session->set_flashdata('message',"User deleted successfully");
			redirect("admin/management");
	   }
	   
		
}