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

class ForgetPassword extends CI_Controller {
	public function __construct() {
		parent::__construct();
		checkFront();
	}

	public function index() {
		$this->load->helper(array('form'));
		$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-en.js')
			->add_includes('js/validationengine/jquery.validationEngine.js');
		$data['msg_sent'] = FALSE;
		$this->layouts->view('forget_pwd', array(), $data, false);
	}

	function send_email() {
		$lg = $this->session->userdata('language');
		$lg  = ($lg) ? $lg:"en";
 		$this->lang->load("forget_pwd", $lg);
		$this->load->model('Usermaster');
		$result = $this->Usermaster->isEmail_exist($this->input->post('email'));
		if($result) {
			// SEND EMAIL
			$code = md5($result[0]->fldid . date("YmdHis"));
			$data = array("email" => $this->input->post('email'), "idUser" => $result[0]->fldid, "code" => $code);
			$res = $this->Usermaster->isEmail_exist($this->input->post('email'));
			if($res) {
				$this->db->query("Delete from temp_usermaster_reset_pwd where email = ?", $this->input->post('email'));
				$this->Usermaster->insert("temp_usermaster_reset_pwd", $data);
				$this->load->library('email');
				$email_from = $this->config->item('tekun_email_from');
				$this->email->from($email_from, 'TEKUN');
				$this->email->to($this->input->post('email'));

				$this->email->subject('Tekun Reset pwd');
				$link = base_url() . 'forgetPassword/reset_pwd/' . $code;
				$data['reset_link'] =$link;
				$this->email->message($this->load->view("frgtpwd_email_temp", $data, true));
				//$this->email->message('Resetting your password <a href="' . $link . '">Click here</a> or copy paste the link ' . $link);

				$this->email->send();
				// echo $this->email->print_debugger();
				// exit();
				$data['msg_sent'] = true;
				$this->layouts->view('forget_pwd', array(), $data, false);
			}
		}
		else {
			$data['msg_sent'] = FALSE;
			$data['msg_wrong_email'] = "true";
			$this->layouts->view('forget_pwd', array(), $data, false);
		}
	}
	

//Created By Fadhirul - 07/09/2016 	
	
	public function check_email(){
		
		$this->load->model('usermaster');
		
		$userid = $this->input->post('userid');
		
		$result = $this->usermaster->isEmail_exist2($userid);
		
		if($result){
			print json_encode(array("status"=>"success", "message"=>$result));
		}
		else{
			print json_encode(array("status"=>"failed"));
		}
	
	}
	
	
	public function verification(){
		
		$this->load->model('usermaster');
		
		$ID = $this->input->post('ID');
		$userid = $this->input->post('userid');
		
		$securityKey = rand(100000, 999999);
		
		
		$data = array(
					'userID' => $ID,
					'securityKey' => $securityKey,
				);
		
		$result = $this->usermaster->insertUsermaster('forget_password', $data);
		
		$getForgetID = $this->usermaster->getForgetPassword_byIDDesc();
		
		
		$to = $userid;
		$subject = "Reset Password";
		$txt = "Please click to reset your password : ".base_url()."forgetPassword/verifyUser/".$securityKey."/".$ID."/".$getForgetID[0]->ID;
		$headers = "From: support@spslite.my";

		mail($to,$subject,$txt,$headers);
		
		
		if($result){
			print json_encode(array("status"=>"success"));
		}
		else{
			print json_encode(array("status"=>"failed"));
		}
	
	}
	
	
//*************************************************************	
	
	

	public function reset_pwd() {
		$this->load->model('Usermaster');
		$code = $this->uri->segment(3);
		$res = $this->Usermaster->fgt_pwd_findCode($code);
		if(! $res) {
			echo "<script> alert('session expired, please resubmit'); window.location = '" . base_url() . "login';</script>";
		}
		else {
			$data["code"] = $code;
			$this->load->view('reset_pwd', $data);
		}
	}

	public function submit_reset_pwd() {
		$pwd = trim($this->input->post('password'));
		$pwd_conf = trim($this->input->post('confir_password'));
		$code = trim($this->input->post('reset_pwd_code'));
		if($pwd == $pwd_conf && strlen($pwd) > 4 && strlen($pwd) < 11) {
			$this->load->model('Usermaster');
			$res = $this->Usermaster->fgt_pwd_findCode($code);
			if(! $res) {
				echo "<script> alert('session expired, please resubmit'); window.location = '" . base_url() . "login';</script>";
			}
			else {
				$idUser = $res[0]->idUser;
				$upd = $this->Usermaster->update_user_pwd($idUser, array("password" => md5($pwd)));
				if($upd) {
					$upd = $this->Usermaster->delete_temuser_reset_pwd($code);
				}
				echo "<script> alert('Your password has been reset, try login'); window.location = '" . base_url() . "login';</script>";
			}
		}
		else {
			echo "<script> alert('Password invalide'); window.location = '" . base_url() . "forgetPassword/reset_pwd/" . $code . "';</script>";
		}
	}
	

//Created By Fadhirul - 07/09/2016
	
	public function verifyUser(){
		
		$this->load->model('usermaster');
		
		$securityKey = $this->uri->segment(3);
		$userID = $this->uri->segment(4);
		$forgetID = $this->uri->segment(5);
		
		$data['msg_sent'] = FALSE;
	
	
		if($securityKey == "" || $userID == "" || $forgetID == ""){
		
			echo"<script type='application/javascript'>alert('Incorrect link');window.location='".base_url()."login'</script>";
	
		}
		else{
			
			$currentDate = date("Y-m-d");
			$check = $this->usermaster->checkValidity($userID, $securityKey, $currentDate);
			
			if($check){
				
				$data['userID'] = $userID;
				$data['forgetID'] = $forgetID;
				
				$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-en.js')
					->add_includes('js/validationengine/jquery.validationEngine.js');
			
				$this->layouts->view('verifyUser', array(), $data, false);
			}
			else{
			
				echo"<script type='application/javascript'>alert('Please resubmit your email to reset password');window.location='".base_url()."forgetPassword'</script>";
			
			}
			
		
		}
	
	}
	
	
	public function updatePassword(){
		
		$this->load->model('usermaster');
		
		$password = $this->input->post('password');
		$userID = $this->input->post('userID');
		$forgetID = $this->input->post('forgetID');
		
		$data = array(
					'password' => md5($password),
				);
				
		$data2 = array(
					'updateAble' => 0,
				);
		
		$update = $this->usermaster->usermaster_edit($data, $userID);
		$update2 = $this->usermaster->changePassword('forget_password', $data2, $forgetID);
		
		if($update){
			print json_encode(array("status"=>"success"));
		}
		else{
			print json_encode(array("status"=>"failed"));
		}
		
		
	}


//***************************************************************************************
	

}

?>
