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

class User_model extends CI_Model {
 
	public function create($data = array())
	{
		return $this->db->insert('user', $data);
	}

	public function createEmp($data = array())
	{
		return $this->db->insert('employee_history', $data);
	}

	public function createRole($data = array()){
		return $this->db->insert('sec_user_access_tbl', $data);
	}

	public function read()
	{
		return $this->db->select("
				user.*, 
				CONCAT_WS(' ', firstname, lastname) AS fullname 
			")
			->from('user')
			->order_by('id', 'desc')
			->get()
			->result();
	}

	public function single($id = null)
	{
		return $this->db->select('*')
			->from('user')
			->where('id', $id)
			->get()
			->row();
	}

	public function update($data = array())
	{
		return $this->db->where('id', $data["id"])
			->update("user", $data);
	}

	public function academicUpdate($data = array()){
		return $this->db->where('id', $data["id"])
		->update("academic", $data);
	}

	public function professionalUpdate($data = array()){
		return $this->db->where('id', $data["id"])
		->update("professional", $data);
	}

	public function delete($id = null)
	{
		return $this->db->where('id', $id)
			->where_not_in('is_admin',1)
			->delete("user");
	}

	public function dropdown()
	{
		$data = $this->db->select("id, CONCAT_WS(' ', firstname, lastname) AS fullname")
			->from("user")
			->where('status', 1)
			->where_not_in('is_admin', 1)
			->get()
			->result();
		$list[''] = display('select_option');
		if (!empty($data)) {
			foreach($data as $value)
				$list[$value->id] = $value->fullname;
			return $list;
		} else {
			return false; 
		}
	}
 
    public function employee(){
    	$data = $this->db->select("employee_id, CONCAT_WS(' ', first_name, last_name) AS fullname")
    			->from("employee_history")
    			->where('employee_status',1)
    			->get()
    			->result();
    		$list[''] = display('select_option');
    		if (!empty($data)) {
    			foreach($data as $value)
    				$list[$value->employee_id] = $value->fullname;
    			return $list;
    		} else {
    			return false; 
    		}
    }
    // employee info
    public function empinfo($email){
    return $result = $this->db->select('first_name,last_name,employee_id')->from('employee_history')->where('email',$email)->get()->row();
    }
    
    // user info
    public function userinfo($email){
    	return $result = $this->db->select('firstname,lastname,id,email')->from('user')->where('email',$email)->get()->row();
    }

	public function get_user_by_token($token) {
        $query = $this->db->get_where('user', array('token' => $token));
        return $query->row();
    }
	public function get_user_exp_by_id($id) {
        $query = $this->db->get_where('user_emp_exp', array('emp_per_id' => $id));
        return $query->row();
    }

    public function verify_user($id) {
		$this->db->set('status', 1);
        $this->db->set('token', NULL);
        $this->db->set('token_expires_at', NULL);
        $this->db->where('id', $id);
        return $this->db->update('user');
      
    }

	public function get_user_by_email($email) {
        $query = $this->db->get_where('user', array('email' => $email));
        return $query->row();
    }
    
     public function update_user($user_id, $data) {
        $this->db->where('id', $user_id);
        return $this->db->update('user', $data);
    }

	public function get_user_by_reset_token($token) {
		$query = $this->db->get_where('user', array('password_reset_token' => $token));
		return $query->row();
	}
}
