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

class Model_user_working_experience extends CI_Model {
	function __construct() {
		parent::__construct();
    }

    function find($id = 0, $profiling_id = 0)
    {
        $this->db->where('id',$id);
        if($profiling_id) $this->db->where('profiling_id',$profiling_id);
        $query = $this->db->get('user__working_experience');
        return ($query->num_rows() > 0)?$query->result():null;
    }

    function fetchWorkingExperience($staff_id = null, $latest = false, $profiling_id = null)
    {

        //inner query
        $this->db->select('MAX(profiling_id)');
        $this->db->from('user__working_experience ');
        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.*,
                        b.action_efective_date,
                        c.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)");
        if($profiling_id) $this->db->where('a.profiling_id',$profiling_id);

        $this->db->where('a.is_delete',0);
        $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__working_experience a');

        // if($this->session->userdata('user')['id'] == 304)
        // {
            // debug($this->db->last_query());
        //     die();
        // }
        
        return ($query->num_rows() > 0)?$query->result():null; 
    }

    function createWorkingExperience($input)
    {
    	$data = array(
            'profiling_id'              => $input['profiling_id'],
            'user_id'                   => $input['user_id'],
            'date_from'                 => date('Y-m-d',strtotime($input['date_from'])),
            'date_to'                   => date('Y-m-d',strtotime($input['date_to'])),
            'experience'                => $input['experience'],
            'nature_engagement'         => $input['nature_engagement'],
            'engagement_period_from'    => date('Y-m-d',strtotime($input['engagement_period_from'])),
            'engagement_period_to'      => date('Y-m-d',strtotime($input['engagement_period_to'])),
            'created_by'                => $input['created_by'],
    	);

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

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

    public function editWorkingExperience($input,$working_experience_id)
    {
        $data = array(
            'date_from'                 => date('Y-m-d',strtotime($input['date_from'])),
            'date_to'                   => date('Y-m-d',strtotime($input['date_to'])),
            'experience'                => $input['experience'],
            'nature_engagement'         => $input['nature_engagement'],
            'engagement_period_from'    => date('Y-m-d',$input['engagement_period_from']),
            'engagement_period_to'      => date('Y-m-d',$input['engagement_period_to']),
            'updated_by'                => $input['updated_by'],
            'updated_at'                => $input['updated_at'],
        );

        $this->db->set($data);
        $this->db->where('id',$working_experience_id);
        $this->db->update('user__working_experience');
        
        return true; 
    }

    public function removeWorkingExperience($working_experience_id)
    {
        $data = array(
            'is_delete'     => 1,
            'updated_by'    => $this->session->userdata('user')['id'],
            'updated_at'    => date('Y-m-d H:i:s'),
        );

        $this->db->set($data);
        $this->db->where('id',$working_experience_id);
        $this->db->update('user__working_experience');

        return true;
    }
}