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

class Admin_Login extends MY_Controller {

	public function __construct() { 
		parent::__construct();
		$this->is_logged_in();
	}

	function is_logged_in()
	{
		$is_admin_logged_in = $this->session->userdata('is_admin_logged_in');
		if(!isset($is_admin_logged_in) || $is_admin_logged_in != true)
		{
		}else{
			redirect(base_url(A));
		}
	}

	public function index()
	{
		// $this->loadAdminHtml("login","Login Page");
		$this->load->view("login","Login Page");
	}

	public function adminLoginPage()
	{
		// $this->loadAdminHtml("login","Login Page");
		$this->load->view("login","Login Page");
	}

	public function getAdminList($value='')
	{
		$res = $this->common->getData('admin');
		// echo "<pre>";
		// print_r ($res);
		// echo "</pre>";
	}

	public function doLogin()
	{
		if(isset($_POST['email']) && isset($_POST['password']))
			{
				//print_r($_POST); die;
				if($data = $this->common->getData('admin',array('email'=>$_POST['email'],'password'=>md5($_POST['password']))))
				{
					//print_r($data); die;
					if(($data[0]['email']!=$_POST['email']) || ($data[0]['password']!=md5($_POST['password'])))
					{
						$this->session->set_flashdata('emessage','User name or Password Wrong');
						redirect(base_url(A."-login"));
					}else{
						$this->loginRedirect($_POST['email'],$data[0]['adminid']);
					}
				}else{
					$this->session->set_flashdata('emessage','User name or Password Wrong');
					redirect(base_url(A."-login"));
				}
			}
	}

	public function loginRedirect($email='',$adminid='')
	{
		$sdata = array(
			'admin_email' => $email,
			'adminid' => $adminid,
			'is_admin_logged_in' => true
		);
		$this->session->set_userdata($sdata);
		redirect(base_url(A));
	}

	public function forgotAdminPassword()
	{
		$admin = $this->common->getsData('admin',array('id'=>1));
		$list= array();
		$characters = '0123456789';
		$length = 4;
	    $charactersLength = strlen($characters);
	    $randomString = '';
	    for ($i = 0; $i < $length; $i++) {
	        $randomString .= $characters[rand(0, $charactersLength - 1)];
	    }
	    $this->common->updateData('admin',array('password'=>md5($randomString),'plain_password'=>$randomString),array('id'=>1));
	    $email_message = "Dear Admin ,It is Your Auto Generate password ".$randomString." Please login with this password Do not share it with any one. ".base_url('admin-login');
	    //echo $email_message."<br>";
	    //echo $email."<br>";
		// $this->email->from('ben@websentials.com.sg',author);
		// $this->email->to($admin['email']);
		// $this->email->subject('Forgot Password');
		// $this->email->message($email_message);
		// $this->email->send();

		$res = $this->common->sendMailBySendGrid(admin_email,'',from_email,author,$subject,$umessage);

		$this->session->set_flashdata('message',"Password Sent To Your Email. Please Check Your Email.");
		redirect($_SERVER['HTTP_REFERER']);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */