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

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

    public function fetchMembership($staff_id = null, $latest = false,$profiling_id = null)
    {
        //inner query
        $this->db->select('MAX(a.profiling_id)');
        $this->db->from('user__profesional_membership 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,
                            e.type as membership_type_name');
        
    	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__profesional_membership_attachment b','b.professional_membership_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');
        $this->db->join('_ref__type_of_membership e','e.id = a.membership_type','left');

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

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

    public function createMembership($input)
    {
        $data['profiling_id']       = $input['profiling_id'];
    	$data['user_id'] 			= $input['user_id'];
    	$data['professional_body']	= $input['professional_body'];
    	$data['certificate']		= $input['certificate'];
        // $data['membership']         = $input['membership'];
    	$data['membership_type']    = $input['membership_type'];
        $data['membership_no']      = $input['membership_no'];
        // $data['year']               = $input['year'];
        $data['date_of_admitted']   = $input['date_of_admitted'];
    	$data['is_delete']			= 0;


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

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

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

        return true;
    }

    public function fetchMembershipType($ID = null)
    {
        if($ID) $this->db->where('a.id',$ID);

        $query = $this->db->where('a.is_delete',0)
                        ->get('_ref__type_of_membership a');

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