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

class model_user_case_emergency extends CI_Model {

	public function get_case_of_emergency($staff_id=0,$latest=true,$profiling_id=null, $emergency_id = null)
	{
//inner query
        $this->db->select('MAX(profiling_id)');
        $this->db->from('user__case_of_emergency ');
        if($staff_id) $this->db->where('user_id',$staff_id);
        $this->db->where('is_delete',0);
        $max = $this->db->get_compiled_select();

//outer query
        $this->db->select('a.*,
                        d.action_efective_date,
                        e.type as action_type');

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

        $this->db->where('a.is_delete',0);
        $this->db->join('user__profiling d','d.id = a.profiling_id','left');
        $this->db->join('_ref__action_type e','e.id = d.action_type_id','left');

        $query = $this->db->get('user__case_of_emergency a');

        
        return (($query) && $query->num_rows() > 0)?$query->result():null; 
	}

	public function createCaseEmergency($input)
	{
		$data['profiling_id'] 	= $input['profiling_id'];
		$data['user_id'] 		= $input['user_id'];
		$data['name'] 			= $input['name'];
		$data['relation'] 		= $input['relation'];
		$data['phone_no'] 		= $input['phone_no'];
		$data['email'] 			= $input['email'];
		$data['address']		= $input['address'];

		$this->db->insert('user__case_of_emergency',$data);

		return $this->db->insert_id();
	}

	public function editCaseEmergency($input, $user__case_of_emergency = null, $profiling_id = null)
	{
        if($profiling_id) $this->db->where('profiling_id',$profiling_id);
		$this->db->set($input);
		$this->db->where('id',$user__case_of_emergency);
		$this->db->update('user__case_of_emergency');

		return true;
	}
}
?>