<?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->library('email');
		$this->load->model('LoginModel');

	}

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

		if(isset($_REQUEST['passwordUpdate'])){
			/* Process of update passsoword */
			if(isset($_REQUEST['isUser']) && $_REQUEST['isUser']=='no'){
				$this->db->update('tbl_admin',array('fsPassword'=>md5($_REQUEST['fsPassword'])),array('ID'=>$_REQUEST['userId']));
			}else{
				$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);
	}

/*Email integration Using SMTP*/	
	function check_admin_email_exist()
		{
			$this->load->library('email');
			$email = $this->input->post('fsEmail');
			
			$query =  $this->db->select('*')->from('hotels')->where("fsEmail",$email)->get();
			$res =  $query->result_array();  

			$adminQuery =  $this->db->select('*')->from('tbl_admin')->where("fsEmail",$email)->get();
			$fsAdmin =  $adminQuery->result_array();  
					
			if(count($res) > 0 || count($fsAdmin) > 0)
			{
				$isUser = 'yes';
				if(count($fsAdmin) > 0){
					$isUser = 'no';
					$res[0]['Id'] = $fsAdmin[0]['ID'];
				}
				//print_r($res); die;
				$to = $email;

				$subject = 'Forgot Password | HOTEL COMPENDIUM';

				$headers = "From: HOTEL COMPENDIUM <no-reply@hotelcompendium.com.au>\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&isUser='.$isUser.'&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>';

				/*SMTP*/
				$this->email->initialize(array(
				  'protocol' => 'smtp',
				  'smtp_host' => 'smtp.sendgrid.net',
				  'smtp_user' => 'apikey',
				  'smtp_pass' => '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',
				  'smtp_port' => 587,
				  'crlf' => "\r\n",
				  'newline' => "\r\n"
				));

				$this->email->set_mailtype("html");
				$this->email->set_newline("\r\n");
				$this->email->from('support@technomate.com.au', 'Hotel Compendium');
				$this->email->to($to);$this->email->subject($subject);
   				$this->email->message($message);
				$this->email->send();

				
				/*echo $this->email->print_debugger();*/

				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>";
			}
		}
/*END Email integration Using SMTP*/


}
