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

class Staff_auth extends CI_Controller {

	public function index()
	{
		$this->load->view('staff_login');
	}
	public function login()
	{
		if($this->input->post("email")){
			$email = $this->input->post("email");
			$pass = $this->input->post("pass");
			$return = $this->login_model->staff_login( $email , md5($pass));
			if($return[0] == 0){
				$this->session->set_userdata(array('email' => $email,'usertype' => $return[3],'logged_in'=>TRUE,'id'=>$return[2]) );
			}
			echo json_encode($return);
		}else{
			redirect(base_url('staff-login'));
		}
	}
	
		public function change_pass()
	{
	     $data['title'] = 'Change Password';
	    $this->load->view('includes/header',$data);
		$this->load->view('includes/sidebar');
      	$this->load->view('staff_change_password');
        $this->load->view('includes/footer');
	
	}
	
		public function change_pass_check()
	{
		if($this->input->post("currentpass")){
		    $this->form_validation->set_rules('pass', 'Password', 'required');
		    $this->form_validation->set_rules('cpass', 'cPassword', 'required|matches[pass]');

			$currentpass = $this->input->post("currentpass");
			$pass = $this->input->post("pass");
				$cpass = $this->input->post("cpass");
				
				if($pass == $cpass ){ 
				    
        			$return = $this->login_model->staff_pass_change($currentpass,$pass,$cpass);
        		
        			    
        			     echo json_encode($return);
				            
        			
				}
				else{
				    
				    echo json_encode("Password and Confirm Password does not match");
				}
		
			
		}else{
// 			redirect(base_url());
		}
	}

	//forgot password
	public function forgot_password()
	{
			$data['title'] = 'Forgot password';
			$this->load->view('staff_forgot_password',$data);
	
	}

	public function generateResetPasswordOtp(){
		if(isset($_POST['email']) && $_POST['email']!==''){
			$check_existing_record = $this->model->getRowData('staff',['emailid'=>$_POST['email']]);
			$otp=mt_rand(100000, 999999);
			if($check_existing_record){
				if($check_existing_record['is_suspended'] == 0){
					if($this->model->edit('staff',['reset_otp'=>$otp],['id'=>$check_existing_record['id']]))
					{
						echo json_encode(['return'=>true,'msg'=>$otp]);
						// if($this->model->send_otp($_POST['email'],$otp))
						// {
						//     echo json_encode(['return'=>true,'msg'=>'']);
						// }
						
					}
					else
					{
						echo json_encode(['return'=>false,'msg'=>'Otp not generated']);
					}
				}else{
					echo json_encode(['return'=>false,'msg'=>'You are suspended!!']);
				}
				
			}
			else
			{
				echo json_encode(['return'=>false,'msg'=>'Email not exists']);
			}
		}
		else
		{
			echo json_encode(['return'=>false,'msg'=>'Email not received']);
		}
	}
	public function verifyResetPasswordOtp(){
		if(isset($_POST['email']) && $_POST['email']!=='' && isset($_POST['otp']) && $_POST['otp']!==''){
			$countRecord = $this->model->countData('staff',['emailid'=>$_POST['email'],'reset_otp'=>$_POST['otp']]);
			if($countRecord !== 0)
			{
				echo json_encode(['return'=>true,'msg'=>'Otp verified']);
			}
			else
			{
				echo json_encode(['return'=>false,'msg'=>'Incorrect otp']);
			}
		}
		else
		{
			echo json_encode(['return'=>false,'msg'=>'otp is required']);
		}
	}
	public function resetPassword(){
		if(isset($_POST['email']) && $_POST['email']!=='' && isset($_POST['new_password']) && $_POST['new_password']!==''){
			if($_POST['new_password'] == $_POST['confirm_password']){
				if($this->model->edit('staff',['password'=>md5($_POST['new_password'])],['emailid'=>$_POST['email']])){
					echo json_encode(['return'=>true,'msg'=>'Otp verified']);
				}
			}
			else
			{
				echo json_encode(['return'=>false,'msg'=>'new password and confirm password does not match!!']);
			}
		}
		else
		{
			echo json_encode(['return'=>false,'msg'=>'email,password are required']);
		}
	}
	public function setResetSuccessMessage() {
		$this->session->set_flashdata('reset_success', $this->input->post('message'));
	}
	
	
}
