<?php if (!defined('BASEPATH'))    exit('No direct script access allowed');

class Mo_user extends CI_Model {

	public $table = 't_user';
	
    function fnGetData()
    {
        $this->db->order_by('f_user_id', 'DESC');
        return $this->db->get($this->table)->result();		
	}

	function getDataId($id)
	{
		$this->db->select('f_user_id, f_user_email, a.f_emp_id, (select f_emp_name from t_employee where f_emp_id=a.f_emp_id) f_emp_name, f_user_name, f_user_level, f_user_role,f_role_code, f_user_active, a.f_user_create_on,(select f_user_name from t_user where f_user_id=a.f_user_create_by) f_user_create_by, a.f_user_update_on,(select f_user_name from t_user where f_user_id=a.f_user_update_by) f_user_update_by',false);
		$this->db->join('t_role b','b.f_role_id=a.f_user_role');
		$sql = $this->db->get_where($this->table.' a',array('f_user_id'=> $id))->row_array();
		return $sql;
	}

	function saveData()
	{
		$data = array(
				'f_user_id' => $this->input->post('f_user_id'),
				'f_user_email' => $this->input->post('f_user_email'),
				'f_user_password' => sha1(md5($this->input->post('f_user_password'))),
				'f_user_name' => $this->input->post('f_user_name'),
				'f_user_level' => $this->input->post('f_user_level'),
				'f_user_role' => $this->input->post('f_user_role'),
				'f_user_active' => $this->input->post('f_user_active'),
				'f_user_create_on' => date('Y-m-d H:i:s'),
				'f_user_create_by' => $this->session->userdata('sId'),
		);
		
		//if($this->input->post('f_emp_id')) $data['f_emp_id'] = $this->input->post('f_emp_id');
		//else $this->db->set('f_emp_id', 'NULL', false); 
		
		$sql = $this->db->insert($this->table,$data);
		return $sql;
	}

	function updateData($id)
	{
		$data = array(
				'f_user_id' => $this->input->post('f_user_id'),
				'f_user_email' => $this->input->post('f_user_email'),
				'f_user_name' => $this->input->post('f_user_name'),
				'f_user_update_on' => date('Y-m-d H:i:s'),
				'f_user_update_by' => $this->session->userdata('sId'),
		);

		if($this->session->userdata('sLevel') != 3)
		{
			if($this->session->userdata('sLevel') == 2 && $this->input->post('f_user_level') <= 1) 
				$data['f_user_level'] = 2;
			else
				$data['f_user_level'] = $this->input->post('f_user_level');
				
			$data['f_user_role'] = $this->input->post('f_user_role');
			$data['f_user_active'] = $this->input->post('f_user_active');
		}
		
		if($this->input->post('f_user_password'))
		{
			$data['f_user_password'] = sha1(md5($this->input->post('f_user_password')));
		}

		//if($this->input->post('f_emp_id')) $data['f_emp_id'] = $this->input->post('f_emp_id');
		//else $this->db->set('f_emp_id', 'NULL', false); 
		
		$this->db->where('f_user_id',$id);
		$sql = $this->db->update($this->table,$data);
		return $sql;		
	}

	function deleteData($id)
	{
		$this->db->where('f_user_id',$id);
		$sql = $this->db->update($this->table,array('f_user_active'=>0));
//		$sql = $this->db->delete($this->table);
		return $sql;		
	}
	
	function comboData()
	{
		$sql = $this->db->get('t_user');
		if($sql)
		{
			foreach($sql->result() as $r):
				$list[$r->f_user_id] = $r->f_user_id;
			endforeach;
		}
		return $list;
	}

	function comboDataEmp()
	{
		$sql = $this->db->query('select f_emp_name,f_user_id from t_employee a,t_user b where a.f_emp_id=b.f_emp_id');
		if($sql)
		{
			foreach($sql->result() as $r):
				$list[$r->f_user_id] = $r->f_emp_name;
			endforeach;
		}
		return $list;
	}

	function remoteComboData()
	{
		if($this->input->get('term')) $this->db->like('f_user_id',$this->input->get('term')); // pencarian name
		if($this->input->get('id')) $this->db->where('f_user_id',$this->input->get('id')); // saat pencarian update
		
		$this->db->limit('10');
		$result = $this->db->get($this->table)->result();
		$DataJson = array();
		foreach($result as $row):
			$data['id']	= $row->f_user_id; // value
			$data['text']	= $row->f_user_id; // change display 
			array_push($DataJson,$data);
		endforeach;
		return $DataJson;
	}

	function remoteComboDataEmploye()
	{
		$this->db->select('f_emp_id');
		$this->db->where('f_emp_id >',0);
		$sql = $this->fnGetData();
		if($sql)
		{
			$i=0;
			foreach($sql as $r) {
				$emp_id[$i] = $r->f_emp_id;
				$i++;
			}
			$this->db->where_not_in('a.f_emp_id',$emp_id);
		}

		if($this->input->get('term')) $this->db->like('f_emp_name',$this->input->get('term')); // pencarian name
		if($this->input->get('term')) $this->db->or_like('f_emp_segment_name',$this->input->get('term')); // pencarian name
		if($this->input->get('id')) $this->db->where('f_emp_id',$this->input->get('id')); // saat pencarian update
		
		$this->db->limit('10');
		$this->db->select('a.f_emp_id,a.f_emp_name,a.f_emp_email,b.f_emp_segment_name',false);
		$this->db->join('t_employee_segment b','b.f_emp_segment_id=a.f_emp_segment_id');
		$result = $this->db->get('t_employee a')->result();
		$DataJson = array();
		foreach($result as $row):
			$data['id']	= $row->f_emp_id; // value
			$data['text']	= $row->f_emp_name; // change display 
			$data['segment']	= $row->f_emp_segment_name; // change display 
			$data['email']	= $row->f_emp_email; // change display 
			array_push($DataJson,$data);
		endforeach;
		return $DataJson;
	}
	
	function remoteComboDataUserEmp()
	{
		if($this->input->get('term')) $this->db->like('f_emp_name',$this->input->get('term')); // pencarian name
		if($this->input->get('id')) $this->db->where('f_user_id',$this->input->get('id')); // saat pencarian update
		
		//$this->db->where_in('c.f_emp_segment_id',array(6,7,8,11));
		$this->db->limit('10');
		$this->db->select('a.f_user_id,a.f_user_name,b.f_emp_id,b.f_emp_name,b.f_emp_email,c.f_emp_segment_name',false);
		$this->db->join('t_employee b','b.f_emp_id=a.f_emp_id');
		$this->db->join('t_employee_segment c','b.f_emp_segment_id=c.f_emp_segment_id');
		$result = $this->db->get($this->table.' a')->result();
		$DataJson = array();
		foreach($result as $row):
/*
			$data['id']	= $row->f_emp_id; // value
			$data['text']	= $row->f_emp_name; // change display 
			$data['segment']	= $row->f_emp_segment_name; // change display 
			$data['email']	= $row->f_emp_email; // change display
			*/ 
			$data[] = array(
				'id' => $row->f_user_id,
				'text' => $row->f_emp_name,
				'segment' => $row->f_emp_segment_name,
				'email' => $row->f_emp_email,
			); 
			//array_push($DataJson,$data);
		endforeach;
		//return $DataJson;
		return $data;
	}
	
}

/* End of file Mo_user.php */
/* Location: ./application/modules/User/models/Mo_user.php */	

	


