<?php
	//start PHP session
	session_start();

	//check if login form is submitted
	if(isset($_POST['submit1'])){
		//assign variables to post values
		$email = $_POST['usernm'];
		$password = $_POST['pword'];

		//include our database connection
		include 'connlg.php';

		//get the user with email
		$stmt = $pdo->prepare('SELECT * FROM airtime_customer WHERE username = :email and user_type="normal"');
		
		try{
			$stmt->execute(['email' => $email]);

			//check if email exist
			if($stmt->rowCount() > 0){
				//get the row
				$user = $stmt->fetch();

				//validate inputted password with $user password
				if(password_verify($password, $user['password'])){
					//action after a successful login
					//for now just message a successful login
					$_SESSION['id'] = $user['customer_id'];
							$_SESSION['email'] = $email;
					$_SESSION['password'] = $password;
	                $_SESSION['log_id'] = $user['password']; 
					$_SESSION['error'] = 'Incorrect password';
				    $_SESSION['log_id'] = $logid; 
					header('location: homepage.php'); exit();
					$_SESSION['success'] = 'User verification successful';
				
				}
				else{
					//return the values to the user
					$_SESSION['email'] = $email;
					$_SESSION['password'] = $password;
	                $_SESSION['log_id'] = $user['password']; 
					$_SESSION['error'] = 'Incorrect password';
					header('location: indexd.php');
				}

			}
			else{
				//return the values to the user
				$_SESSION['email'] = $email;
				$_SESSION['password'] = $password;  

				$_SESSION['error'] = 'No account associated with the username';
			}
			
		}
		catch(PDOException $e){
			$_SESSION['error'] = $e->getMessage();
		}
		
	}
	else{
		$_SESSION['error'] = 'Fill up login form first';
	}

	header('location: lg.php');
?>