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

class Login extends CI_Controller {

	public function __construct(){
		parent::__construct();

		if(isset($_REQUEST['action']) && $_REQUEST['action']=='logout'){
			$this->session->unset_userdata('isUserLoggedIn');
		}

		if(isUserLoggedIn()==true){
			redirect(base_url('/dashboard'));
		}

		$this->load->model('LoginModel');
	}

	public function index()
	{
		$pageData = array(
			'header'=>array('pageTitle'=>'Login'),
		);

		if(isset($_REQUEST['passwordUpdate'])){
			/* Process of update passsoword */
			$this->db->update('hotels',array('fsPassword'=>md5($_REQUEST['fsPassword'])),array('Id'=>$_REQUEST['userId']));
			$pageData['body']['isError'] = "Password updated successfully.";
		}else{
			if($this->input->post('fsEmail')!=""){
				$userDetails = $this->LoginModel->checkLogin($this->input->post('fsEmail'),$this->input->post('fsPassword'));
				if(count($userDetails) > 0){
					$this->session->set_userdata('isUserLoggedIn',$userDetails);
					redirect(base_url('/dashboard'));
				}else{
					$pageData['body']['isError'] = "The email & password is not matched.";
				}
			}
		}
		
		loadView('login/login',$pageData,true);
	}



	/*=================================================================================
									Check Email
	==================================================================================*/
		
		function check_admin_email_exist()
		{
			
			$email = $this->input->post('fsEmail');
			
			$query =  $this->db->select('*')->from('hotels')->where("fsEmail",$email)->get();
			$res =  $query->result_array();  
					
			if($res)
			{
				//print_r($res); die;
				$to = $email;

				$subject = 'QUEST COMPENDIUM | Forgot Password';

				$headers = "From: no-reply@newsnytro.com\r\n";
				$headers .= "MIME-Version: 1.0\r\n";
				$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

				$resetLink = base_url().'Login/?action=update_password&token='.base64_encode($res[0]['Id'].'---'.$email);
				$message = '<html><body>';
				$message .= '<div>Bellow is link of your forgot passoword.<br/> If you need to reset your passsword, Click on bellow link.</div>';
				$message .= '<div><a href="'.$resetLink.'" style="">Reset Password</div>';
				$message .= '</body></html>';

				mail($to,$subject,$message,$headers);

				echo "<span style='color:green'>Sent you email on your email id, Please check inbox or spam folder.</span>";
			}
			else
			{
				echo "<span style='color:red'>Email address is not registered with us.</span>";
			}
		}

}
