<?php namespace App\Controllers\Admin;

use App\Models\Admin\SecureModel;

class Secure extends AdminController {    
	public function index()
	{	
		$data['message']="";	
		$data['footer_includes'] = ['secure_js'];		
		echo $this->view_login_template(['secure'],$data);
	}
	
	function login(){
		$data['error']=array();
		if($this->request->getMethod() === 'post'){
			$rules = $this->getLoginValidationRules();
			if ($this->validate($rules)) { 
				$mObj = new SecureModel('default');
				$user_dtl = $mObj->getUserDetail($this->request->getPost('username'),$this->request->getPost('password'));			
				
				if($user_dtl){						
					$accessLogID = $mObj->addAccessLog($user_dtl);
					$user_log = ['access_log_id' => $accessLogID,
								$this->project->session_admin_user_id => $user_dtl['user_id'],
								'name' => $user_dtl['name'],
								'user_type' => $user_dtl['user_type'],
								'is_logged' => true];					
					// Add language
					$default_lang = $this->cmObj->get_default_language();
					$user_log['language_code'] = $default_lang['code'];
					$this->session->set($user_log);					
				}else{
					$data['error'] = "Invalid Username / Password, Please try again.";
				}
			}else{
				$error_arr = $this->validator->getErrors();
				$data['error']= empty($error_arr) ? "" : implode(", ",$error_arr);
			}
			//print_r($data); die;
			echo json_encode($data);
		}
	}
	
	private function getLoginValidationRules(){
		$rules['username'] = [
					'label'=>'Username', 
					'rules'  => 'trim|required|min_length[3]'
				];
		$rules['password'] = [
					'label'=>'Password', 
					'rules'  => 'trim|required'
				];
		return $rules;
	}
	
	function forgot_password(){
		helper('text');
		$data['error']=array();
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_forgot_pwd_validation_rules();
			if ($this->validate($rules)) { 
				$mObj = new SecureModel('default');
				$user_dtl = $mObj->getNewPassword($this->request->getPost('email'));			
				if($user_dtl){	
					//$this->send_new_password($user_dtl);					
				}else{
					$data['error'] = "Invalid Email.";
				}
			}else{
				$error_arr = $this->validator->getErrors();
				$data['error']= empty($error_arr) ? "" : implode(", ",$error_arr);
			}
			//print_r($data); die;
			echo json_encode($data);
		}			
	}
	
	private function get_forgot_pwd_validation_rules(){
		$rules['email'] = [
					'label'=>'Email', 
					'rules'  => 'trim|required|valid_email'
				];
		return $rules;
	}
	
	private function send_new_password($user_dtl){
		$email = \Config\Services::email();
		$email->setTo($user_dtl['email_id']);

		$email->setSubject('Your New Password For '.$this->project->project_name);
		$email->setMessage('Testing the email class.');

		$email->send();
	}
	
	function logoff(){
		$access_log_id = $this->session->get('access_log_id');
		if($access_log_id){
			$mObj = new SecureModel('default');
			$mObj->update_access_log($access_log_id);
		}
		$this->session->destroy();
		return redirect()->to(get_admin_path('secure'));
	}
}