<?php 
// Include main file
include_once('../libs/main.php');

// Object Creation 
$adminObj = new admin();

$successMsg =  isset($_REQUEST['successMsg']) ? $_REQUEST['successMsg'] : '';
$failedMsg = isset($_REQUEST['failedMsg']) ? $_REQUEST['failedMsg'] : '';

// Logical section of page
if(isset($_POST['submit'])){
	
    $adminName = $_POST['email_address'];
    $password = $_POST['password'];

    if($adminName!='' && $password!=''){

    	$where = array('admin_email_address=' => $adminName, 'admin_password=' => md5($password), 'admin_status=' => '1');
    	$data=array('where'=>$where);
        $result = $adminObj->fromFields($data);
        
        if($result){
            $_SESSION['admin_id'] = $result->admin_id;
            $_SESSION['admin_fname'] = $result->admin_fname;
            $_SESSION['admin_lname'] = $result->admin_lname;
            $_SESSION['admin_type'] = $result->admin_type;
            $_SESSION['admin_email_address'] = $result->admin_email_address;
            $_SESSION['admin_access'] = $result->admin_access;

            $id=$result->admin_id;
            $data=array('admin_last_login_ip'=>$_SERVER['REMOTE_ADDR'],'admin_last_login_date'=>date('Y-m-d H:i:s'));
            $rs=$adminObj->updateRecords($data,$id);
            header('location:index.php');
            exit;
        }else{
            $failedMsg = base64_encode("Invalid Username or Password");
        }
    }else{
        $failedMsg = base64_encode("Username and Password are required");
    }
}


if(isset($_REQUEST['act']) && $_REQUEST['act']=='logout'){
        unset($_SESSION['admin_id']);
	unset($_SESSION['admin_fname']);
	unset($_SESSION['admin_lname']);
	unset($_SESSION['admin_lname']);
	unset($_SESSION['admin_email_address']);
	unset($_SESSION['admin_access']);
	$successMsg =  base64_encode("You are successfully logged out.");
}
// Include Template 
include_once('../libs/routs.php')

?>