<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of expencecategorymodel
 *
 * @author BusyMan
 */
class EmployeeModel extends CI_Model {
    //put your code here
    private $_tbl_user = 'tbl_user';
    private $_tbl_admin = 'tbl_admin';
    private $_tbl_leaves = 'tbl_leaves';

//income search by employee
    public function getAllEmployee() {
        $query=$this->db->or_where_in('userType',array(1,2,3))
                    ->where('reigning_date IS NULL')
        			->get($this->_tbl_user);
        $result=$query->result_array();
        return $result;
    }

    public function getAllInactiveEmployee(){
        $query=$this->db->or_where_in('userType',array(1,2,3))
            ->where('reigning_date IS NOT NULL')
            ->get($this->_tbl_user);
        $result=$query->result_array();
        return $result;
    }

    public function getAllEmployeeId()
    {
        $query=$this->db->select('userId')
                    ->or_where_in('userType',array(1,2,3))
                    ->get($this->_tbl_user);
        $result=$query->result_array();
        if(!empty($result))
        {
            return $result;
        }
    }

    public function getAlladminsId()
    {
        $query=$this->db->select('employeeId')
                    ->or_where_in('admin_level',array(1,2,3))
                    ->get($this->_tbl_admin);
        $result=$query->result_array();
        if(!empty($result))
        {
            return $result;
        }
    }

    public function getAdminById($data)
    {
        $query=$this->db->select('*')
                    ->where('employeeId',$data)->where_in('admin_level',array(1,2,3))
                    ->get($this->_tbl_admin);         
        $result=$query->row_array();
        if(!empty($result))
        {
            return $result;
        }
    }
    // public function getAdminById($receiverid){
    //     $this->db->select('admin_name');
    //     $this->db->select('userName');
    //     $query=$this->db->get_where($this->_tbl_admin,array('employeeId'=>$receiverid));
    //     $result=$query->row_array();
    //     if(!empty($result)){
    //         return $result;
    //   }
    // }

    public function getEmployeeById($userId)
    {
        $query=$this->db->select('*')
                    ->where('userId',$userId)->where_in('userType',array(1,2,3)) //($i=1;$j=2;)  ->where('userType BETWEEN "'. $i. '" and "'. $j.'"')
                    ->get($this->_tbl_user);         
    	$result=$query->row_array();
    	if(!empty($result))
    	{
    		return $result;
    	}
    }

    /*public function updateEmployeeById($data)
    {
    	$query=$this->db->update($this->_tbl_user,$data,array('userId'=>$data['userId']));
       
        	if($data['userType']==1){ $admin_name='super_admin';$admin_level=1;}elseif($data['userType']==2){ $admin_name='employee';$admin_level=2;}else{$admin_name='Director';$admin_level=3;}
        	if($data['userType']==1){ $userName='administrator';}elseif($data['userType']==2){ $userName='employee';}else{$userName='subadmin';}
        	$datax=array(
        		'employeeId'=>$data['userId'],
        		'admin_email'=>$data['email'],
        		'admin_name'=>$admin_name,
        		'admin_level'=>$admin_level,
        		'userName'=>$userName
        		);
        	$query2=$this->db->update($this->_tbl_admin,$datax,array('employeeId'=>$datax['employeeId']));
        	//echo $query2; die();
        if(!empty($query) || !empty($query2)){return $this->db->affected_rows();}
        elseif(!empty($query) && !empty($query2)){return $this->db->affected_rows();}
        else{return false;}
        
    }*/
    public function updateEmployeeById($userId,$data)
     {
	     $this->db->where('userId', $userId); 
	     $this->db->update($this->_tbl_user,$data);
	     $dataaffect1=$this->db->affected_rows(); 
	     if($data['userType']==1){ $admin_name='super_admin';$admin_level=1;}elseif($data['userType']==2){ $admin_name='employee';$admin_level=2;}else{$admin_name='Director';$admin_level=3;}
        	if($data['userType']==1){ $userName='administrator';}elseif($data['userType']==2){ $userName='employee';}else{$userName='subadmin';}
	     $datax=array(
        		//'employeeId'=> $data['userId'],
        		'admin_email' => $data['email'],
        		'admin_name'  => $admin_name,
        		'admin_level' => $admin_level,
        		'userName'    => $userName
        		); 
        	 $this->db->where('employeeId',$userId); 
	   	$this->db->update($this->_tbl_admin,$datax); 
	   	 $dataaffect2=$this->db->affected_rows();
	   	return  $result=array($dataaffect1,$dataaffect2); 
	}
    

    public function deleteemployeeById($userId)
    {
        $query=$this->db->delete($this->_tbl_user,array('userId'=>$userId));
        if(!empty($query))
        {
            $query2=$this->db->delete($this->_tbl_admin,array('employeeId'=>$userId));
            return $this->db->affected_rows(); 
        }
    }

    public function updateOwnProfile($data){
        $query=$this->db->update($this->_tbl_user,$data,array('userId' =>$data['userId']));
        $datax=array(
                'employeeId'=>$data['userId'],
                'admin_email'=>$data['email']
                );
        $query2=$this->db->update($this->_tbl_admin,$datax,array('employeeId'=>$datax['employeeId']));
        // if($query || $query2){return 1;}
        // else if($query && $query2){return $this->db->affected_rows();}
        // else{return null;}
        if(!empty($query) || !empty($query2)){return $this->db->affected_rows();}
        else if(!empty($query) && !empty($query2)){return $this->db->affected_rows();}
        else{return false;}
    }

    public function makeUserActiveById($id){
        $this->db->where('userId', $id);
        $this->db->update($this->_tbl_user, array('reigning_date'=>NULL));
        return $id;
    }
    
        public function insert_leaves($data) {
        //$data['payment_date']=date('Y-m-d',strtotime($data['payment_date']));
        $this->db->insert($this->_tbl_leaves, $data);
        return $this->db->insert_id();
             
        }
        
        public function getLeavesById($leaveid)
        {
            $query=$this->db->select('*')
                        ->where('lv_id',$leaveid)
                        ->get($this->_tbl_leaves);         
        	$result=$query->row_array();
        	if(!empty($result))
        	{
        		return $result;
        	}
        }
    
        
        public function getEmployeeAllLeaves(){
        $query = $this->db->select('*')->get($this->_tbl_leaves);
        $result=$query->result_array();
        if(!empty($result)){
            return $result;
            }
        }
        
        public function getEmployeeLeaves($userId){
        $query = $this->db->select('*')->get_where($this->_tbl_leaves, array('lv_employee'=>$userId));
        $result=$query->result_array();
        if(!empty($result)){
            return $result;
            }
        }
        
        public function updateLeaveById($lv_id,$data)
         {
         $this->db->where('lv_id', $lv_id); 
         $this->db->update($this->_tbl_leaves,$data);
         return $this->db->affected_rows(); 
    	}
        
        public function deleteleaveById($leaveid)
        {
        $query=$this->db->delete($this->_tbl_leaves,array('lv_id'=>$leaveid));
        return $this->db->affected_rows(); 
        // if(!empty($query))
        // {
        //   return $this->db->affected_rows(); 
        // }
    }
}