<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Employee_Model extends MY_Model {

    function __construct() {
        parent::__construct();
    }

    public function get_employee_list($limit, $offset, $search, $count) {
        $this->db->select('E.*, D.name AS department, DE.name AS designation, R.name as role, U.role_id, U.device_id');
        $this->db->from('employees AS E');
        $this->db->join('departments AS D', 'D.id = E.department_id', 'left');
        $this->db->join('designations AS DE', 'DE.id = E.designation_id', 'left');
        $this->db->join('users AS U', 'U.id = E.user_id', 'left');
        $this->db->join('roles AS R', 'R.id = U.role_id', 'left');
        if ($this->session->userdata('role_id') == ADMIN) {
            $this->db->where('U.role_id !=', 1);
        }
        
        if ($search) {
            $this->db->group_start();
            $this->db->like('E.phone', $search);
            $this->db->or_like('E.name', $search, 'both');
            $this->db->or_like('E.bn_name', $search, 'both');
            $this->db->or_like('E.gender', $search);
            $this->db->or_like('E.religion', $search);
            $this->db->or_like('E.blood_group', $search);
            $this->db->or_like('E.marital_status', $search);
            $this->db->or_like('E.present_address', $search, 'both');
            $this->db->or_like('E.permanent_address', $search, 'both');
            $this->db->or_like('D.name', $search, 'both');
            $this->db->or_like('DE.name', $search, 'both');
             $this->db->group_end();
        }
        $this->db->where('U.role_id !=', DOCTOR);
        $this->db->where('U.role_id !=', VD);
        $this->db->order_by('E.id', 'DESC');
        if ($count) {
            return $this->db->count_all_results();
        } else {
            $this->db->limit($limit, $offset);
            $query = $this->db->get();

            if ($query->num_rows() > 0) {
                return $query->result();
            }
        }
        return array();
    }

    public function get_single_employee($user_id) {
        $this->db->select('E.*, D.name AS department, DE.name AS designation, U.username, U.temp_password, U.device_id, U.sbiz_id, U.role_id');
        $this->db->from('employees AS E');
        $this->db->join('departments AS D', 'D.id = E.department_id', 'left');
        $this->db->join('designations AS DE', 'DE.id = E.designation_id', 'left');
        $this->db->join('users AS U', 'U.id = E.user_id', 'left');
        $this->db->join('roles AS R', 'R.id = U.role_id', 'left');
        if ($this->session->userdata('role_id') == ADMIN) {
            $this->db->where('U.role_id !=', 1);
        }
        $this->db->where('E.user_id', $user_id);
        return $this->db->get()->row();
    }

    function duplicate_check($username, $id = null) {
        if ($id) {
            $this->db->where_not_in('id', $id);
        }
        $this->db->where('username', $username);
        return $this->db->get('users')->num_rows();
    }
}
