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

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

    public function fetchProfessionalAcademic($staff_id = null, $latest = false,$profiling_id = null, $professional_academic_id = null, $academic_type = null)
    {
        //inner query
        $this->db->select('MAX(a.profiling_id)');
        $this->db->from('user__professional_academic 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.id as attachment_id,b.attachment_name,b.location,b.file_type,b.file_ext,b.mask_id,
                            c.action_efective_date,
                            d.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);
        if($professional_academic_id) $this->db->where('a.id',$professional_academic_id);
        if($academic_type > -1) $this->db->where('a.type',$academic_type);

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

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

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

    public function createProfessionalAcademic($input)
    {
        $data['profiling_id']      = $input['profiling_id'];
    	$data['user_id']           = $input['user_id'];
    	$data['type']              = $input['type'];
    	$data['level']		       = $input['level'];
    	$data['associate_name']    = $input['associate_name'];
        $data['program']           = $input['program'];
        $data['date_finish']       = date('Y-m-d',strtotime($input['date_finish']));
        $data['result']            = $input['result'];
    	$data['created_by']		   = $input['created_by'];


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

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

    public function editProfessionalAcademic($input,$id)
    { 
        $this->db->set($input);
        $this->db->where('id',$id);
        $this->db->update('user__professional_academic');

        return true;
    }
}
?>