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

class Model_job_history extends CI_Model {

	public function fetchJobHistory($staff_id = null,$latest = false,$profiling_id = null)
	{
		//inner query
		$this->db->select('MAX(a.profiling_id)');
		$this->db->from('user__employment_history a');
		$this->db->where('a.is_delete',0);
		if($staff_id) $this->db->where('a.user_id',$staff_id);
		$max = $this->db->get_compiled_select();


		//outer query
		$this->db->select('a.*,
							b.action_efective_date,
							c.type as action_type');
		$this->db->where('a.is_delete',0);

		if($staff_id) $this->db->where('a.user_id',$staff_id);
		if($latest) $this->db->where("a.profiling_id IN ($max) ");
		if($profiling_id) $this->db->where('a.profiling_id',$profiling_id); 

		$this->db->join('user__profiling b','b.id = a.profiling_id','left');
		$this->db->join('_ref__action_type c','c.id = b.action_type_id','left');

		$query = $this->db->get('user__employment_history a');
		return ($query->num_rows() > 0 )?$query->result() : null;
	}

	public function createJobs($input)
	{
		$this->db->insert('user__employment_history',$input);
		
		return $this->db->insert_id();
	}

	public function editJobs($input,$job_id)
	{
		$this->db->set($input);
		$this->db->where('id',$job_id);
		$this->db->update('user__employment_history');
	}
}
?>