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

class Model_staff_action extends CI_Model {

	public function fetchAction($id = null, $user_id = null,$date = null)
	{
		$this->db->select('
			a.*,
			b.type,b.icon,b.color,
			c.name,
			d.title as user_title,
			e.name as religion,
			f.race,
			g.country as issue_country,
			h.country as nationality');

		if($id) $this->db->where('a.id',$id);
		if($user_id) $this->db->where('a.user_id',$user_id);
		if($date) $this->db->where('a.action_efective_date >',$date);

		$this->db->where('a.is_delete',0);

		$this->db->join('_ref__action_type b','b.id = a.action_type_id','left');
		$this->db->join('user c','c.id = a.user_id','left');
		$this->db->join('_ref__title d','d.id = a.title_id','left');
		$this->db->join('_ref__religion e','e.id = a.religion_id','left');
		$this->db->join('_ref__race f','f.id = a.race_id','left');
		$this->db->join('_ref__country g','g.id = a.place_issue','left');
		$this->db->join('_ref__country h','h.id = a.citizen_id','left');


		$this->db->order_by('a.action_efective_date,a.id','desc');
		$query = $this->db->get('user__profiling a');

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

	public function fetchType()
	{
		$this->db->select('*');

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

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

	public function registerProfiling($data)
	{
		$this->db->insert('user__profiling',$data);
		return $this->db->insert_id();
	}

	public function updateProfiling($input = array(),$id)
	{
		$this->db->where('id',$id);
		$this->db->update('user__profiling',$input);
	}

	public function removeAction($id)
	{
		$this->db->set('is_delete',1);
		$this->db->where('id',$id);
		$this->db->update('user__profiling');
	}

	public function getActionByUserID($id = 0, $user_id = null,$date = null)
	{
		$this->db->select('
			a.*,
			b.type,b.icon,b.color,
			c.name,
			d.title as user_title,
			e.name as religion,
			f.race,
			g.country as issue_country,
			h.country as nationality');

		if($id > 0) $this->db->where('a.id',$id);
		else return null; 
		if($user_id) $this->db->where('a.user_id',$user_id);
		if($date) $this->db->where('a.action_efective_date >',$date);

		$this->db->where('a.is_delete',0);

		$this->db->join('_ref__action_type b','b.id = a.action_type_id','left');
		$this->db->join('user c','c.id = a.user_id','left');
		$this->db->join('_ref__title d','d.id = a.title_id','left');
		$this->db->join('_ref__religion e','e.id = a.religion_id','left');
		$this->db->join('_ref__race f','f.id = a.race_id','left');
		$this->db->join('_ref__country g','g.id = a.place_issue','left');
		$this->db->join('_ref__country h','h.id = a.citizen_id','left');


		$this->db->order_by('a.action_efective_date,a.id','desc');
		$query = $this->db->get('user__profiling a');

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

	
}
?>