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

class Admin extends CI_Controller {

	function __construct() 
	{
        parent::__construct();
         $met= $this->router->method; 
        if($met!='index' && $met!='login' && $met !='' && $met !='logout' && $met!='profile' && $met!='update_profile' && $met!='forgot_password' && $met!='notification' && $met!='notification_company') 
        {
        	$data1=$this->session->userdata('secret_admin');
    		if($data1['role']!=1)
    		{
    			return redirect(base_url('admin'),'refresh');
    		}
        }
    	
    }
	public function index()
	{
		if(check_login())
    	{
    			$data1=$this->session->userdata('secret_admin');
				if($data1['role']==1)
				{
					$this->load->view('admin/dashboard');
				}
				elseif($data1['role']==2)
				{
					$this->load->view('subadmin/dashboard');
				}
				elseif($data1['role']==3)
				{
					$this->load->view('consultant/dashboard');
				}
				elseif($data1['role']==4)
				{
					$this->load->view('company/dashboard');
				}
    	}
    	else
    	{
    		$this->load->view('admin/login');
    	}
	}
	
	
	public function login()
	{
		$email = $_POST['email'];
		if (!filter_var($email, FILTER_VALIDATE_EMAIL)) 
		{
			$_POST['password']=md5($_POST['password']);
			$_POST['user_id']=$_POST['email'];
			
			
			
			unset($_POST['email']);
			if(!empty($this->db->where($_POST)->get('user')->row_array()))
			{
				$data=$this->db->where($_POST)->get('user')->row_array();
				if($data['role']==1)
				{
					$this->session->set_userdata('secret_admin', $data );
					$this->load->view('admin/dashboard');
				}
				elseif($data['role']==2)
				{
					$this->session->set_userdata('secret_admin', $data );
					$this->load->view('subadmin/dashboard');
				}
				elseif($data['role']==3)
				{
					$this->session->set_userdata('secret_admin', $data );
					$this->load->view('consultant/dashboard');
				}
				elseif($data['role']==4)
				{
					$this->session->set_userdata('secret_admin', $data );
					$this->load->view('company/dashboard');
				}
			}
			else
			{
				if(isset($_POST['role']))
				{
					if($_POST['role']==3)
					{
						$this->session->set_flashdata('errl', 'Only Consultant Can Login From Here');
						return redirect(base_url('consultant_login'),'refresh');
					}
					if($_POST['role']==4)
					{
						$this->session->set_flashdata('errl', 'Only Company Can Login From Here');
						return redirect(base_url('company_login'),'refresh');						
					}
				}
				else
				{
					$this->session->set_flashdata('errl', 'Enter a valid Username and password.');
					return redirect(base_url('admin'),'refresh');
				}
			}
		}
		else
		{
			$_POST['password']=md5($_POST['password']);
			//$_POST['status']=1;
			if(!empty($this->db->where($_POST)->get('user')->row_array()))
			{
				$data=$this->db->where($_POST)->get('user')->row_array();
				if($data['role']==1)
				{
					$this->session->set_userdata('secret_admin', $data );
					$this->load->view('admin/dashboard');
				}
				elseif($data['role']==2)
				{
					$this->session->set_userdata('secret_admin', $data );
					$this->load->view('subadmin/dashboard');
				}
				elseif($data['role']==3)
				{
					$this->session->set_userdata('secret_admin', $data );
					$this->load->view('consultant/dashboard');
				}
				elseif($data['role']==4)
				{
					$this->session->set_userdata('secret_admin', $data );
					$this->load->view('company/dashboard');
				}
			}
			else
			{
				if(isset($_POST['role']))
				{
					if($_POST['role']==3)
					{
						$this->session->set_flashdata('errl', 'Only Consultant Can Login From Here');
						return redirect(base_url('consultant_login'),'refresh');
					}
					if($_POST['role']==4)
					{
						$this->session->set_flashdata('errl', 'Only Company Can Login From Here');
						return redirect(base_url('company_login'),'refresh');						
					}
				}
				else
				{
				$this->session->set_flashdata('errl', 'Enter a valid Username and password.');
				return redirect(base_url('admin'),'refresh');
				}
			}
		}
	}
	public function profile()
	{
		if(!check_login())
    	{
    		return redirect(base_url(),'refresh');
    	}
		$data1=$this->session->userdata('secret_admin');
		$data['profile']=$this->db->where('id',$data1['id'])->get('user')->row_array();
		if($data1['role']==1)
		{
			$this->load->view('admin/profile',$data);
		}
		elseif($data1['role']==2)
		{
			$this->load->view('subadmin/profile',$data);
		}
		elseif($data1['role']==3)
		{
			$this->load->view('consultant/profile',$data);
		}
		elseif($data1['role']==4)
		{
			$this->load->view('company/profile',$data);
		}
	}
	
	
	public function update_profile()
	{

	    $this->db->where('id',$_POST['id'])->update('user',$_POST);
		$data['profile']=$this->db->where('id',$_POST['id'])->get('user')->row_array();
		
		$this->session->set_flashdata('msg', 'Profile SUccessfully updated');
		if($data['profile']['role']==1)
		{	
            	
			$this->load->view('admin/profile',$data);
		}
		elseif($data['profile']['role']==2)
		{
			
			$this->load->view('subadmin/profile',$data);
		}
		elseif($data['profile']['role']==3)
		{
			
			$this->load->view('consultant/profile',$data);
		}
		elseif($data['profile']['role']==4)
		{
			
			$this->load->view('company/profile',$data);
		}	
	}
	public function subadmin()
	{
		if($this->input->server('REQUEST_METHOD') == 'POST')
		{
			$this->form_validation->set_rules('name', 'Name', 'required');
			$this->form_validation->set_rules('email', 'Email', 'required|is_unique[user.email]');
			$this->form_validation->set_rules('phone', 'Phone', 'required');
			$this->form_validation->set_rules('password', 'Password', 'required');
			if ($this->form_validation->run() == FALSE)
			{
				$this->load->view('admin/subadmin');
			}
			else
			{
				if(isset($_POST['id']))
				{
					unset($_POST['id']);
				}
				 $maxid=$this->db->select_max('id')->get('user')->row_array()['id']+1;
				 $max='HR'.substr(time(),-4).$maxid;
				 $_POST['user_id']=$max;
				 $_POST['permission']=implode(',',$_POST['permission']);
				 $_POST['confirm_password']=$_POST['password'];
				 $_POST['password']=md5($_POST['password']);
				 $this->db->insert('user',$_POST);
				 $this->session->set_flashdata('msg', 'Sub Admin Successfully Created');
				 return redirect(base_url('admin/subadmin'),'refresh');
			}
		}
		else
		{
			$page =(isset($_GET['page'])) ? $_GET['page'] : 0;
			$query=$this->db->select('user.*')->from('user')->where('role',2)->order_by('id','desc')->limit(5,($page*5))->get();
			$num_rows=$this->db->where('role',2)->order_by('id', 'desc')->get('user')->num_rows();
			$data['user']=$query->result_array();
			$data['links']=$this->pagi->pagination1('admin/subadmin',$num_rows,5);
			$this->load->view('admin/subadmin',$data);
		}
	}

	public function subadmin_update($id='')
	{
		$data=[];
		foreach($_POST['form_val'] as $f)
		{
			$data[$f['nam']]=$f['vall'];
		}
		unset($_POST);
		$_POST=$data;
		if(isset($data['name']))
		{
			$this->form_validation->set_rules('name', 'Name', 'required');
		}
		if(isset($data['email']))
		{
			$this->form_validation->set_rules('email', 'Email', 'required');
		}
		if(isset($data['phone']))
		{
			$this->form_validation->set_rules('phone', 'Phone', 'required');
		}
		if(isset($data['password']))
		{
			$this->form_validation->set_rules('password', 'Password', 'required');
			$data['password']=md5($data['password']);
		}
			if ($this->form_validation->run() == FALSE)
			{
				$val['error']=validation_errors();
				echo json_encode($val);
				exit;
			}	
		if($this->db->where('id',$data['id'])->update('user',$data))
		{
			echo json_encode(1);
		}
		else
		{
			echo json_encode(0);
		}
	}

	public function add_position()
	{
		if($this->input->server('REQUEST_METHOD') == 'POST')
		{
						$this->form_validation->set_rules('position', 'position', 'required');
			$this->form_validation->set_rules('position_no', 'Position No', 'required|numeric');
			$this->form_validation->set_rules('location', 'Location', 'required');
			$this->form_validation->set_rules('exp', 'Exp', 'required');
			$this->form_validation->set_rules('ctc', 'CTC', 'required');
			$this->form_validation->set_rules('tat', 'TAT', 'required');
			if ($this->form_validation->run() == FALSE)
			{
				$this->load->view('admin/add_position');
			}
			else
			{
				$journalName = str_replace(' ', '_', $_FILES['jd']['name']);
				$config['upload_path']   = './uploads/position/'; 
				 $config['allowed_types'] = '*'; 
				$config['file_name']  			= time().$journalName;
				 $this->load->library('upload', $config);
					
				 if ( ! $this->upload->do_upload('jd')) 
				 {
				    $error = array('error' => $this->upload->display_errors()); 

				    $this->load->view('admin/add_position', $error); 
				    exit;
				 }
					
				 else 
				 { 
				    $_POST['jd']=$config['file_name'];
				 }
				 $maxid=$this->db->select_max('id')->get('position')->row_array()['id']+1;
				 $max='JOB'.substr(time(),-4).$maxid;
				 $_POST['job_id']=$max;
				 $user=$this->session->userdata('secret_admin');
				 $_POST['added_by']=$user['id'];
				 $_POST['notification']='0,';
				 $_POST['status']='Open';
				 $_POST['admin_status']='Inactive';
				 if($this->db->insert('position',$_POST))
				 {
				 	$this->session->set_flashdata('msg', 'Position Successfully Added');
				 	return redirect(base_url('admin/add_position'),'refresh');
				 }
				 else
				 {
				 	$this->session->set_flashdata('err', 'Please try Again After SomeTime');
				 	return redirect(base_url('admin/add_position'),'refresh');
				 }
			}
		}
		else
		{
			$user=$this->session->userdata('secret_admin');
			$data['position']=$this->db->where('added_by',$user['id'])->order_by('id','desc')->limit(5)->get('position')->result_array();
			$this->load->view('admin/add_position',$data);
		}
	}

	public function position()
	{
		$where=[];
		$user=$this->session->userdata('secret_admin');
		$page =(isset($_GET['page'])) ? $_GET['page'] : 0;
		if(isset($_GET['search']))
		{
			 $query='select * from position where (job_id like "%'.$_GET['search'].'%" or position like "%'.$_GET['search'].'%" or position_no like "%'.$_GET['search'].'%" or location like "%'.$_GET['search'].'%" or exp like "%'.$_GET['search'].'%" or ctc like "%'.$_GET['search'].'%" or tat like "%'.$_GET['search'].'%") order by id DESC limit '.($page*20).' , 20';
			 $num_rows=count($this->db->query('select * from position where (job_id like "%'.$_GET['search'].'%" or position like "%'.$_GET['search'].'%" or position_no like "%'.$_GET['search'].'%" or location like "%'.$_GET['search'].'%" or exp like "%'.$_GET['search'].'%" or ctc like "%'.$_GET['search'].'%" or tat like "%'.$_GET['search'].'%")')->result_array());
			 $data['links']=$this->pagi->pagination1('admin/position',$num_rows,20);
			 $data['position']=$this->db->query($query)->result_array();
			 $this->load->view('admin/position',$data);
		}
		else
		{
		$query=$this->db->select('position.*')->from('position')->order_by('id','desc')->limit(20,($page*20))->get();
		$num_rows=$this->db->order_by('id', 'desc')->get('position')->num_rows();
		$data['position']=$query->result_array();
		$data['links']=$this->pagi->pagination1('admin/position',$num_rows,20);
		$this->load->view('admin/position',$data);
		}
	}

	public function change_position_status()
	{
		 
		$this->db->where('id',$_POST['id'])->update('position',$_POST);
		$this->session->set_flashdata('msg', 'Successfully Updated Status');
		return redirect(base_url('admin/position'),'refresh');
	}

	public function add_candidate()
	{
		if($this->input->server('REQUEST_METHOD') == 'POST')
		{
			$this->form_validation->set_rules('position', 'position', 'required');
			$this->form_validation->set_rules('job_location', 'Job Location', 'required');
			$this->form_validation->set_rules('current_location', 'Location', 'required');
			$this->form_validation->set_rules('email', 'Email', 'required');
			$this->form_validation->set_rules('ctc', 'CTC', 'required');
			$this->form_validation->set_rules('expected_ctc', 'Expected CTC', 'required');
			$this->form_validation->set_rules('current_company', 'Current Company', 'required');
			$this->form_validation->set_rules('notice_period', 'Notice Period', 'required');
			
			if ($this->form_validation->run() == FALSE)
			{
			$user=$this->session->userdata('secret_admin');
			$data['candidate']=$this->db->where('added_by',$user['id'])->order_by('id','desc')->limit(5)->get('candidate')->result_array();

			$this->load->view('admin/add_candidate',$data);
			}
			else
			{
				
				$journalName = str_replace(' ', '_', $_FILES['cv']['name']);
				$config['upload_path']   = './uploads/position/'; 
				 $config['allowed_types'] = '*'; 
				$config['file_name']  			= time().$journalName;
				 $this->load->library('upload', $config);
					
				 if ( ! $this->upload->do_upload('cv')) 
				 {
				    $error = array('error' => $this->upload->display_errors()); 
				    $this->session->set_flashdata('err', $error);
				   $user=$this->session->userdata('secret_admin');
			$data['candidate']=$this->db->where('added_by',$user['id'])->order_by('id','desc')->limit(5)->get('candidate')->result_array();

			$this->load->view('admin/add_candidate',$data); 
				    exit;
				 }
					
				 else 
				 { 
				    $_POST['cv']=$config['file_name'];
				 }
				 $maxid=$this->db->select_max('id')->get('position')->row_array()['id']+1;
				 $max='CDT'.substr(time(),-4).$maxid;
				 $_POST['candidate_id']=$max;
				 $user=$this->session->userdata('secret_admin');
				 $_POST['added_by']=$user['id'];
				 $_POST['notification']='0,';
				 $_POST['status']='Profile feedback pending';
				 $_POST['admin_status']='Inactive';
				 if($this->db->insert('candidate',$_POST))
				 {
				 	$this->session->set_flashdata('msg', 'Candidate Successfully Added');
				 	return redirect(base_url('admin/add_candidate'),'refresh');
				 }
				 else
				 {
				 	$this->session->set_flashdata('err', 'Please try Again After SomeTime');
				 	return redirect(base_url('admin/add_candidate'),'refresh');
				 }
			}
		}
		else
		{
			$user=$this->session->userdata('secret_admin');
			$data['candidate']=$this->db->where('added_by',$user['id'])->order_by('id','desc')->limit(5)->get('candidate')->result_array();

			$this->load->view('admin/add_candidate',$data);
		}
	}

	public function forgot_password()
	{
		 
		 if($this->input->server('REQUEST_METHOD') == 'POST')
		{
			
			$this->form_validation->set_rules('email', 'Email', 'required');
			if ($this->form_validation->run() == FALSE)
			{
				$this->load->view('admin/forgot_password');
			}
			else
			{
				if($this->db->where('email',$_POST['email'])->get('user')->row_array())
				{
					
				
				
					$config = Array(
				 // change it to yours
				  'mailtype' => 'html',
				  'charset' => 'iso-8859-1',
				  'wordwrap' => TRUE
				);
                      $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
				$charactersLength = strlen($characters);
				$randomString = '';
				for ($i = 0; $i < 6; $i++) 
				{
				$randomString .= $characters[rand(0, $charactersLength - 1)];
				}
				
				$data['password']=md5($randomString);
				$data['confirm_password']=$randomString;
					 $config['newline'] = "\r\n";
					 $message="<table>
								<tr><td><h3>Your New Password is this=".$randomString."</h3></td></tr>
					 </table>
					
					 ";
					$this->load->library('email',$config);

					$this->email->from('manas@kingdigital.in');
					$this->email->to($_POST['email'], 'Myrecruiter.in');
                      
					$this->email->subject('MYrecruiter.in');
					$this->email->message($message);

					$this->email->send();
					unset($_POST['subject']);
					
					$this->db->where('email',$_POST['email'])->update('user',array('password'=>$data['password'],'confirm_password'=>$data['confirm_password']));
					

					$this->session->set_flashdata('msg', 'Your Request is Succeessfully Submiited , Please check your Email');
					

					return redirect(base_url('admin/forgot_password'),'refresh');
				}
				else
				{
					$this->session->set_flashdata('err', 'User does not exist.');
					return redirect(base_url('admin/forgot_password'),'refresh');
				}
			}
		}	 
		else
		{
			$this->load->view('admin/forgot_password');
	    }
	}
	
	public function candidate()
	{
		$where=[];
		$user=$this->session->userdata('secret_admin');
		$page =(isset($_GET['page'])) ? $_GET['page'] : 0;
		if(isset($_GET['search']))
		{
			 $query='select * from candidate where (candidate.job_id like "%'.$_GET['search'].'%" or candidate.candidate_id like "%'.$_GET['search'].'%" or candidate.position like "%'.$_GET['search'].'%" or candidate.job_location like "%'.$_GET['search'].'%" or candidate.email like "%'.$_GET['search'].'%" or candidate.name like "%'.$_GET['search'].'%" or candidate.phone like "%'.$_GET['search'].'%" or candidate.ctc like "%'.$_GET['search'].'%" or candidate.expected_ctc like "%'.$_GET['search'].'%") order by id DESC limit '.($page*20).' , 20';
			 $num_rows=count($this->db->query('select * from candidate where and (candidate.job_id like "%'.$_GET['search'].'%" or candidate.candidate_id like "%'.$_GET['search'].'%" or candidate.position like "%'.$_GET['search'].'%" or candidate.job_location like "%'.$_GET['search'].'%" or candidate.email like "%'.$_GET['search'].'%" or candidate.name like "%'.$_GET['search'].'%" or candidate.phone like "%'.$_GET['search'].'%" or candidate.ctc like "%'.$_GET['search'].'%" or candidate.expected_ctc like "%'.$_GET['search'].'%")')->result_array());
			 $data['links']=$this->pagi->pagination1('admin/candidate',$num_rows,20);
			 $data['candidate']=$this->db->query($query)->result_array();
			 $this->load->view('admin/candidate',$data);
		}
		else
		{
		$query=$this->db->select('candidate.*')->from('candidate')->order_by('id','desc')->limit(20,($page*20))->get();
		$num_rows=$this->db->order_by('id', 'desc')->get('candidate')->num_rows();
		$data['candidate']=$query->result_array();
		$data['links']=$this->pagi->pagination1('admin/candidate',$num_rows,20);
		$this->load->view('admin/candidate',$data);
		}
	}

	public function opening()
	{
		$where=[];
		$user=$this->session->userdata('secret_admin');
		$page =(isset($_GET['page'])) ? $_GET['page'] : 0;
		if(isset($_GET['search']))
		{
			 $query='select * from position where status="Open" and (job_id like "%'.$_GET['search'].'%" or position like "%'.$_GET['search'].'%" or position_no like "%'.$_GET['search'].'%" or location like "%'.$_GET['search'].'%" or exp like "%'.$_GET['search'].'%" or ctc like "%'.$_GET['search'].'%" or tat like "%'.$_GET['search'].'%") order by id DESC limit '.($page*20).' , 20';
			 $num_rows=count($this->db->query('select * from position where status="Open" and (job_id like "%'.$_GET['search'].'%" or position like "%'.$_GET['search'].'%" or position_no like "%'.$_GET['search'].'%" or location like "%'.$_GET['search'].'%" or exp like "%'.$_GET['search'].'%" or ctc like "%'.$_GET['search'].'%" or tat like "%'.$_GET['search'].'%")')->result_array());
			 $data['links']=$this->pagi->pagination1('admin/opening',$num_rows,20);
			 $data['position']=$this->db->query($query)->result_array();
			 $this->load->view('admin/opening',$data);
		}
		else
		{
		$query=$this->db->select('position.*')->from('position')->where('status','Open')->order_by('id','desc')->limit(20,($page*20))->get();
		$num_rows=$this->db->where('status','Open')->order_by('id', 'desc')->get('position')->num_rows();
		$data['position']=$query->result_array();
		$data['links']=$this->pagi->pagination1('admin/opening',$num_rows,20);
		$this->load->view('admin/opening',$data);
		}
	}


	public function cv_feedback_pending()
	{
		$page =(isset($_GET['page'])) ? $_GET['page'] : 0;
		$user=$this->session->userdata('secret_admin');
		if(isset($_GET['search']))
		{
			 $query='select candidate.* from candidate where candidate.status="Profile feedback pending" and (candidate.job_id like "%'.$_GET['search'].'%" or candidate.position like "%'.$_GET['search'].'%" or candidate.job_location like "%'.$_GET['search'].'%" or candidate.email like "%'.$_GET['search'].'%" or candidate.name like "%'.$_GET['search'].'%" or candidate.phone like "%'.$_GET['search'].'%" or candidate.ctc like "%'.$_GET['search'].'%" or candidate.expected_ctc like "%'.$_GET['search'].'%") order by id DESC limit '.($page*20).' , 20';
			 $num_rows=count($this->db->query('select candidate.* from candidate where candidate.status="Profile feedback pending" and (candidate.job_id like "%'.$_GET['search'].'%" or candidate.position like "%'.$_GET['search'].'%" or candidate.job_location like "%'.$_GET['search'].'%" or candidate.email like "%'.$_GET['search'].'%" or candidate.name like "%'.$_GET['search'].'%" or candidate.phone like "%'.$_GET['search'].'%" or candidate.ctc like "%'.$_GET['search'].'%" or candidate.expected_ctc like "%'.$_GET['search'].'%")')->result_array());
			 $data['links']=$this->pagi->pagination1('admin/cv_feedback_pending',$num_rows,20);
			 $data['candidate']=$this->db->query($query)->result_array();
			 $this->load->view('admin/cv_feedback_pending',$data);
		}
		else
		{
			$data['candidate']=$this->db->select('candidate.*')->from('candidate')->where('candidate.status','Profile feedback pending')->order_by('id','desc')->limit(20,($page*20))->get()->result_array();
			$num_rows=$this->db->select('candidate.*')->from('candidate')->where('candidate.status','Profile feedback pending')->order_by('id','desc')->get()->num_rows();
			$data['links']=$this->pagi->pagination1('admin/cv_feedback_pending',$num_rows,20);
			$this->load->view('admin/cv_feedback_pending',$data);
		}		
	}

	public function shortlisted_for_interview()
	{
		$page =(isset($_GET['page'])) ? $_GET['page'] : 0;
		$user=$this->session->userdata('secret_admin');
		if(isset($_GET['search']))
		{
			 $query='select candidate.* from candidate where candidate.status="Profile Shortlist For Interview" and (candidate.job_id like "%'.$_GET['search'].'%" or candidate.position like "%'.$_GET['search'].'%" or candidate.job_location like "%'.$_GET['search'].'%" or candidate.email like "%'.$_GET['search'].'%" or candidate.name like "%'.$_GET['search'].'%" or candidate.phone like "%'.$_GET['search'].'%" or candidate.ctc like "%'.$_GET['search'].'%" or candidate.expected_ctc like "%'.$_GET['search'].'%") order by id DESC limit '.($page*20).' , 20';
			 $num_rows=count($this->db->query('select candidate.* from candidate where candidate.status="Profile Shortlist For Interview" and (candidate.job_id like "%'.$_GET['search'].'%" or candidate.position like "%'.$_GET['search'].'%" or candidate.job_location like "%'.$_GET['search'].'%" or candidate.email like "%'.$_GET['search'].'%" or candidate.name like "%'.$_GET['search'].'%" or candidate.phone like "%'.$_GET['search'].'%" or candidate.ctc like "%'.$_GET['search'].'%" or candidate.expected_ctc like "%'.$_GET['search'].'%")')->result_array());
			 $data['links']=$this->pagi->pagination1('company/shortlisted_for_interview',$num_rows,20);
			 $data['candidate']=$this->db->query($query)->result_array();
			 $this->load->view('admin/shortlisted_for_interview',$data);
		}
		else
		{
			$data['candidate']=$this->db->select('candidate.*')->from('candidate')->where('candidate.status','Profile Shortlist For Interview')->order_by('id','desc')->limit(20,($page*20))->get()->result_array();
			$num_rows=$this->db->select('candidate.*')->from('candidate')->where('candidate.status','Profile Shortlist For Interview')->order_by('id','desc')->get()->num_rows();
			$data['links']=$this->pagi->pagination1('company/shortlisted_for_interview',$num_rows,20);
			$this->load->view('admin/shortlisted_for_interview',$data);
		}
	}

	public function schedule_interview()
	{
		$_POST['status']='Schedule Interview';
		$this->db->where(['candidate_id'=> $_POST['candidate_id'],'job_id'=> $_POST['job_id']])->update('candidate',$_POST);
		$this->session->set_flashdata('msg', 'Interview Scheduled Succeessfully');
		return redirect(base_url('company/shortlisted_for_interview'),'refresh');
	}

	public function interview_scheduled()
	{
		$page =(isset($_GET['page'])) ? $_GET['page'] : 0;
		$user=$this->session->userdata('secret_admin');
		if(isset($_GET['search']))
		{
			 $query='select candidate.* from candidate where candidate.status="Schedule Interview" and (candidate.job_id like "%'.$_GET['search'].'%" or candidate.position like "%'.$_GET['search'].'%" or candidate.job_location like "%'.$_GET['search'].'%" or candidate.email like "%'.$_GET['search'].'%" or candidate.name like "%'.$_GET['search'].'%" or candidate.phone like "%'.$_GET['search'].'%" or candidate.ctc like "%'.$_GET['search'].'%" or candidate.expected_ctc like "%'.$_GET['search'].'%") order by id DESC limit '.($page*20).' , 20';
			 $num_rows=count($this->db->query('select candidate.* from candidate where candidate.status="Schedule Interview" and (candidate.job_id like "%'.$_GET['search'].'%" or candidate.position like "%'.$_GET['search'].'%" or candidate.job_location like "%'.$_GET['search'].'%" or candidate.email like "%'.$_GET['search'].'%" or candidate.name like "%'.$_GET['search'].'%" or candidate.phone like "%'.$_GET['search'].'%" or candidate.ctc like "%'.$_GET['search'].'%" or candidate.expected_ctc like "%'.$_GET['search'].'%")')->result_array());
			 $data['links']=$this->pagi->pagination1('admin/interview_scheduled',$num_rows,20);
			 $data['candidate']=$this->db->query($query)->result_array();
			 $this->load->view('admin/interview_scheduled',$data);
		}
		else
		{
			$data['candidate']=$this->db->select('candidate.*')->from('candidate')->where('candidate.status','Schedule Interview')->order_by('id','desc')->limit(20,($page*20))->get()->result_array();
			$num_rows=$this->db->select('candidate.*')->from('candidate')->where('candidate.status','Schedule Interview')->order_by('id','desc')->get()->num_rows();
			$data['links']=$this->pagi->pagination1('admin/interview_scheduled',$num_rows,20);
			$this->load->view('admin/interview_scheduled',$data);
		}
	
	}

	public function update_remark()
	{
		if($this->db->where(['job_id'=>$_POST['job_id'],'candidate_id'=>$_POST['candidate_id']])->update('candidate',$_POST))
		{
			echo json_encode("Updated SuccessFully");
		}
		else
		{
			echo json_encode("Try Again");
		}
	}

	public function final_selected()
	{
		$page =(isset($_GET['page'])) ? $_GET['page'] : 0;
		$user=$this->session->userdata('secret_admin');
		if(isset($_GET['search']))
		{
			 $query='select candidate.* from candidate where candidate.status="Final Select" and (candidate.job_id like "%'.$_GET['search'].'%" or candidate.position like "%'.$_GET['search'].'%" or candidate.job_location like "%'.$_GET['search'].'%" or candidate.email like "%'.$_GET['search'].'%" or candidate.name like "%'.$_GET['search'].'%" or candidate.phone like "%'.$_GET['search'].'%" or candidate.ctc like "%'.$_GET['search'].'%" or candidate.expected_ctc like "%'.$_GET['search'].'%") order by id DESC limit '.($page*20).' , 20';
			 $num_rows=count($this->db->query('select candidate.* from candidate where candidate.status="Final Select" and (candidate.job_id like "%'.$_GET['search'].'%" or candidate.position like "%'.$_GET['search'].'%" or candidate.job_location like "%'.$_GET['search'].'%" or candidate.email like "%'.$_GET['search'].'%" or candidate.name like "%'.$_GET['search'].'%" or candidate.phone like "%'.$_GET['search'].'%" or candidate.ctc like "%'.$_GET['search'].'%" or candidate.expected_ctc like "%'.$_GET['search'].'%")')->result_array());
			 $data['links']=$this->pagi->pagination1('admin/final_selected',$num_rows,20);
			 $data['candidate']=$this->db->query($query)->result_array();
			 $this->load->view('admin/final_selected',$data);
		}
		else
		{
			$data['candidate']=$this->db->select('candidate.*')->from('candidate')->where('candidate.status','Final Select')->order_by('id','desc')->limit(20,($page*20))->get()->result_array();
			$num_rows=$this->db->select('candidate.*')->from('candidate')->where('candidate.status','Final Select')->order_by('id','desc')->get()->num_rows();
			$data['links']=$this->pagi->pagination1('admin/final_selected',$num_rows,20);
			$this->load->view('admin/final_selected',$data);
		}
	
	}
	
	public function view_employer()
	{
	
	    $page =(isset($_GET['page'])) ? $_GET['page'] : 0;
		$query=$this->db->select('user.*')->from('user')->where('role',4)->order_by('id','desc')->limit(10,($page*10))->get();
		$num_rows=$this->db->where('role',4)->order_by('id', 'desc')->get('user')->num_rows();
		$data['user']=$query->result_array();
		$data['links']=$this->pagi->pagination1('admin/view_employer',$num_rows,10);
	    $data['employer']=$this->db->where('role',4)->order_by('id','desc')->get('user')->result_array();
		$this->load->view('admin/view_employer',$data);
	}
	public function add_employer()
	{
		if($this->input->server('REQUEST_METHOD') == 'POST')
		{
			$this->form_validation->set_rules('name', 'Name', 'required');
			$this->form_validation->set_rules('email', 'Email', 'required|is_unique[user.email]');
			$this->form_validation->set_rules('phone', 'Phone', 'required');
			$this->form_validation->set_rules('company_name', 'Company Name', 'required');
			$this->form_validation->set_rules('company_address', 'comapany address', 'required');
			$this->form_validation->set_rules('password', 'password', 'required');
			if ($this->form_validation->run() == FALSE)
			{
				$this->load->view('admin/add_employer');
				
			}
		  $user=$this->session->userdata('secret_admin');
		  if($user['role']==1){
			  
			    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
				$charactersLength = strlen($characters);
				$randomString = '';
				for ($i = 0; $i < 6; $i++) 
				{
				$randomString .= $characters[rand(0, $charactersLength - 1)];
				}
				 $maxid=$this->db->select_max('id')->get('user')->row_array()['id']+1;
				 $max='EM'.substr(time(),-4).$maxid;
				 $_POST['user_id']=$max;
				$data=$_POST;
				$data=$_POST;
				$data['added_by']=$user['id'];
				$data['role']=4;
				$data['status']=1;
				$data['confirm_password']=$_POST['password'];
				$data['password']=md5($randomString);
				$this->db->insert('user',$data);
				$this->session->set_flashdata('msg', 'User Added Successfully');
				return redirect(base_url('admin/add_employer'),'refresh');
			  
		  }
				
				
		}
		else
		{
			
			$this->load->view('admin/add_employer');
			
		}
	}
	
	public function notification()
	{
		    
			$user=$this->session->userdata('secret_admin');
			$id=$user['id'];
			
			 $previous_week = strtotime("-1 week +1 day");

			$start_week = strtotime("last sunday midnight",$previous_week);
			$end_week = strtotime("next saturday",$start_week);
			$present_day = date("Y-m-d");
			$end_week = date("Y-m-d",$end_week);
          			
            $query="SELECT * FROM `position` WHERE `date` > '$end_week 00:00' AND `date` <  '$present_day 23:59' AND `notification` NOT LIKE '%$id,%' AND `status`='Open' AND admin_status='Active'";

			$data1=$this->db->query($query)->result_array(); 
			
			
		    echo json_encode($data1);
			
			
	}
	
	public function admin_status_candidate()
	{
		$this->db->where('id',$_POST['id'])->update('candidate',$_POST);
		return redirect(base_url('admin/candidate'),'refresh');
		
	}
	
	public function admin_status_position()
	{
		$this->db->where('id',$_POST['id'])->update('position',$_POST);
		return redirect(base_url('admin/position'),'refresh');
	}

	
	
	public function notification_company()
	{
		    $user=$this->session->userdata('secret_admin');
			$id=$user['id'];
			$role=$user['role'];
			
			 $previous_week = strtotime("-1 week +1 day");

			$start_week = strtotime("last sunday midnight",$previous_week);
			$end_week = strtotime("next saturday",$start_week);
			$present_day = date("Y-m-d");
			$end_week = date("Y-m-d",$end_week);
          			
            if($role==4)
			{
				$query="SELECT candidate.*, position.added_by FROM `candidate` INNER JOIN position ON candidate.job_id=position.job_id WHERE position.added_by=$id AND candidate.notification NOT LIKE  '%$id,%' AND candidate.date > '$end_week 00:00' AND candidate.date <  '$present_day 23:59' AND candidate.admin_status='Active'";
			}
			elseif($role==1 || $role==2)
			{
				$query="SELECT * FROM `candidate` WHERE `date` > '$end_week 00:00' AND `date` <  '$present_day 23:59' AND `notification` NOT LIKE '%$id,%' ";
		    }

			$data1=$this->db->query($query)->result_array(); 
			
			
		    echo json_encode($data1);
		
	}
		public function positions($id)
	{
		
		$user=$this->session->userdata('secret_admin');
		$active_id=$user['id'];
		/* print_r($data);
		die(); */
	
		if(isset($_GET['msg'])){
			    $user=$this->session->userdata('secret_admin');
				$data=$this->db->select('notification')->from('position')->where('id',$id)->get()->row_array();
				
				$this->db->like('notification',$active_id.',');
				$isid=$this->db->get_where('position',array('id'=>$id))->num_rows();
		
				if($isid<1)
				{
				$notification=$data['notification'].$active_id.',';
				
				$data1=array('notification'=>$notification);
				$this->db->where('id',$id)->update('position',$data1);
				}
				
			}
			$data['position']=$this->db->where('id',$id)->get('position')->row_array();
			$this->load->view('admin/positions',$data);
		
	}
	
	 public function candidates($id)
	 {
		$user=$this->session->userdata('secret_admin');
		$active_id=$user['id'];
		/* print_r($data);
		die(); */
		if(isset($_GET['msg'])){
			    $user=$this->session->userdata('secret_admin');
				$data=$this->db->select('notification')->from('candidate')->where('id',$id)->get()->row_array();
				
				$this->db->like('notification',$active_id.',');
				$isid=$this->db->get_where('candidate',array('id'=>$id))->num_rows();
		
				if($isid<1)
				{
				$notification=$data['notification'].$active_id.',';
				
				$data1=array('notification'=>$notification);
				$this->db->where('id',$id)->update('candidate',$data1);
				}
				
			}
			$data['candidate']=$this->db->where('id',$id)->get('candidate')->row_array();
			$this->load->view('admin/candidates',$data);
		 
	 }
	
	public function view_cosultant()
	{
		$page =(isset($_GET['page'])) ? $_GET['page'] : 0;
		$query=$this->db->select('user.*')->from('user')->where('role',3)->order_by('id','desc')->limit(10,($page*10))->get();
		$num_rows=$this->db->where('role',3)->order_by('id', 'desc')->get('user')->num_rows();
		$data['user']=$query->result_array();
		$data['links']=$this->pagi->pagination1('admin/view_consultant',$num_rows,10);
		$data['consultant']=$this->db->where('role',3)->get('user')->result_array();
		$this->load->view('admin/view_consultant',$data);
	}
	
	function get_job()
	{
		$x=$this->db->where('job_id',$_POST['job_id'])->get('position')->row_array();
		echo json_encode($x);
	}

	
	public function estatus_update()
	{
		
		 $this->db->where('id',$_POST['id'])->update('user',array('status'=>$_POST['status']));
		redirect(base_url('admin/view_employer'));
	}
	public function cstatus_update()
	{
		$this->db->where('id',$_POST['id'])->update('user',array('status'=>$_POST['status']));
		redirect(base_url('admin/view_cosultant'));
		
	}
	public function add_consultant()
	{
		if($this->input->server('REQUEST_METHOD') == 'POST')
		{
			$this->form_validation->set_rules('name', 'Name', 'required');
			$this->form_validation->set_rules('email', 'Email', 'required|is_unique[user.email]');
			$this->form_validation->set_rules('phone', 'Phone', 'required');
			$this->form_validation->set_rules('company_name', 'Company Name', 'required');
			$this->form_validation->set_rules('company_address', 'comapany address', 'required');
			$this->form_validation->set_rules('password', 'password', 'required');
			if ($this->form_validation->run() == FALSE)
			{
				$this->load->view('admin/add_consultant');
				
			}
		  $user=$this->session->userdata('secret_admin');
		  if($user['role']==1){
			  
			    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
				$charactersLength = strlen($characters);
				$randomString = '';
				for ($i = 0; $i < 6; $i++) 
				{
				$randomString .= $characters[rand(0, $charactersLength - 1)];
				}
				 $maxid=$this->db->select_max('id')->get('user')->row_array()['id']+1;
				 $max='CON'.substr(time(),-4).$maxid;
				 $_POST['user_id']=$max;
				$data=$_POST;
				$data['added_by']=$user['id'];
				$data['role']=3;
				$data['status']=1;
				$data['confirm_password']=$_POST['password'];
				$data['password']=md5($randomString);
				$this->db->insert('user',$data);
				$this->session->set_flashdata('msg', 'User Added Successfully');
				return redirect(base_url('admin/add_consultant'),'refresh');
			  
		  }
				
				
		}
		else
		{
			
			$this->load->view('admin/add_consultant');
			
		}
	}

	public function joining_info()
	{
		$this->db->where(['candidate_id'=> $_POST['candidate_id'],'job_id'=> $_POST['job_id']])->update('candidate',$_POST);
		$this->session->set_flashdata('msg', 'Joining Info Succeessfully');
		return redirect(base_url('company/interview_scheduled'),'refresh');	
	}


	public function logout()
	{
		$this->session->sess_destroy();
		return redirect(base_url('admin'),'refresh');
	}
}
