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

class Login extends CI_Controller {
	function __Construct(){
		parent::__Construct();
		
		$this->load->library('ts_functions');
		$this->load->library('session');
	}
	public function index(){ 
	    if(isset($_SESSION['USERS_DATA'])){
			redirect(base_url("dashboard"));
		}
		$this->load->view('front/header');
		$this->load->view('front/login');
		$this->load->view('front/footer');
	}
	function check_login(){	
		if(isset($_POST['user_email']) & isset($_POST['password'])){
			$user_email = $_POST['user_email'];
			$checkUser = $this->my_model->select_data('*' , 'users' , array('status'=>1,'userType !='=>2,'userEmail' => $_POST['user_email'] , 'userPassword' => md5($_POST['password'])) , 1);
			if (!empty($checkUser)) {
				$userDetail = $checkUser[0]; 
			    $_SESSION['USERS_DATA'] = $userDetail;
				$resp = array('status' => 1 , 'redirect' => 'dashboard','msg'=>'Successfully login.');
				
				$remember = $this->input->post('remember_id');
						  
				if(isset($remember) && !empty($remember))
				   {   
				       $password5 = $_POST['password'];
						setcookie("cookie[user_email]", $user_email, time() + (86400 * 30), "/"); // 86400 = 1 day
						setcookie("cookie[password]", $password5 , time() + (86400 * 30), "/"); // 86400 = 1 day
				    }
			}else{
				$resp = array('status' => 0 , 'error' => 'Invalid email or password.');
			}
		} 
		echo json_encode($resp , JSON_UNESCAPED_SLASHES);
	}
	public function logout() {
        $this->session->sess_destroy();
			redirect(base_url());
			die;
    }
    public function forgot_password() {
        if(isset($_SESSION['USERS_DATA'])){
			redirect(base_url("dashboard"));
		}
		$data['title'] = 'Forgot password';
		$this->load->view('front/header',$data);
	
		if(!empty($_POST['email_otp'])){
		    $email = trim($_POST['user_email']);
		    $checkUser = $this->my_model->select_data('*' , 'users' , array('userEmail' => $email, 'userType !='=>2) , 1);
		    if(!empty($checkUser)){
		        $_SESSION['user_email'] = $email;
		        
		        $fourdigitrandom = rand(1000,9999); 
		        $this->my_model->update_data('users',array('verifyCode'=>$fourdigitrandom),array('userId'=>$checkUser[0]['userId']));
		        $resp = array('status' => 1 , 'redirect' => 'login/check_otp','msg'=>'Your password verification OTP has been sent to your email..');
		        //send otp email
                $to = $email;
        		
        		$msg=str_replace("[linktext]",$fourdigitrandom,str_replace("[break]","<br />",$this->ts_functions->getsettings('forgotps','text')));
        		$this->ts_functions->email2('forgotps',$to,$msg);
		        
		        echo json_encode($resp , JSON_UNESCAPED_SLASHES);
		        die();
		    }else{
		        $resp = array('status' => 0 , 'error' => 'Invalid email address.');
		        echo json_encode($resp , JSON_UNESCAPED_SLASHES);
		        die();
		    }
		}else{
		    $this->load->view('front/forgot_password');
        }
		$this->load->view('front/footer');
		
		
    }
    
    public function check_otp() {
        if(isset($_SESSION['USERS_DATA'])){
			redirect(base_url("dashboard"));
		}
		$data['title'] = 'Forgot password';
		$this->load->view('front/header' ,$data);
		
		if(!empty($_POST['email_otp'])){
		    $email =$_POST['user_email'];
		    $otp =$_POST['email_otp'];
		    $pwd =$_POST['npwd'];
		    $checkUser = $this->my_model->select_data('*' , 'users' , array('userEmail' => $email, 'userType !='=>2,'verify_code'=>$otp) , 1);
		    if(!empty($checkUser)){
		        
		        $this->my_model->update_data('users',array('verifyCode'=>'','userPassword'=>md5($pwd)),array('userId'=>$checkUser[0]['userId']));
		        $resp = array('status' => 1 , 'redirect' => 'login','msg'=>'password has been changed successfully.');
		        $this->session->sess_destroy();
		        echo json_encode($resp , JSON_UNESCAPED_SLASHES);
		        die();
		        
		    }else{
		        $resp = array('status' => 0 , 'error' => 'Invalid otp.');
		        	echo json_encode($resp , JSON_UNESCAPED_SLASHES);
		        	die();
		    }
		}else{
		    $this->load->view('front/check_otp');
        }
		$this->load->view('front/footer');
		
    }
	
}