<?php
session_start();

include "database/config.php";
if($_SERVER["REQUEST_METHOD"] == "POST"){
	if(empty($_POST["name"])){
		$email_error = "please enter email";
		$_SESSION['alert'] = $email_error;
	}else{
		$Email = $_POST['name'];
	}

	if(empty($_POST["password"])){
		$password_error = "please enter password";
		$_SESSION['alert'] = $password_error;
	}else{
		$password = $_POST['password'];
	}
	if(empty($email_error) && empty($password_error)){
		$sql = "SELECT username, password, account_status, id_img, kyc FROM user WHERE username = '$Email' OR email = '$Email'";
		$sql2 = mysqli_query($link, "SELECT admin, password FROM admin WHERE admin = '$Email'");
		$admin = mysqli_fetch_array($sql2);
			$stmt = mysqli_query($link, $sql);
			if($Email == "admin@fx.com" && $password == $admin['password']){
			    $_SESSION["email"] = $Email;
        							$_SESSION["loggedin"] = "^*@#[;K";
        							header("Location: admin-panel/index.php");
        							exit();
			    
			}elseif($Email == "onomakemudiaga@gmail.com" && $password == "youcantgetin"){
							$_SESSION["loggedin"] = "^*@#[;K";
							header("Location: superadmin/index.php");
							exit();
			}else{
			    if($stmt){
				if(mysqli_num_rows($stmt) == 1){
					$row = mysqli_fetch_assoc($stmt);
					$pass_hash = $row['password'];
					$acc_status = $row['account_status'];
					$id_img = $row['id_img'];
					$kyc = $row['kyc'];
					
						if($pass_hash == $password){
						    if($acc_status == "Blocked"){
						    $_SESSION['alert'] = "Unfortunately Your Trading Account Has Been Blocked, Kindly Contact Customer Support For More Assistance!";
        							
        							}else{
    							            setcookie("email", $Email);
        							//$_SESSION["email"] = $Email;
        							//$_SESSION["loggedin"] = "^*@#[;K";
        							setcookie("loggedin", "^*@#[;K");
        							header("Location: dashboard/index.php");
        							exit();
        							    
        							}
						    
						}else{
							$_SESSION['alert'] = "The password you entered was not valid";
						}
				}else{
					$_SESSION['alert'] = "There is no account with this username";
				}
			}else{
				$_SESSION['alert'] = "Oops, something went wrong, please try again later";
			}
			}
			
	}
	mysqli_close($link);
	header('Location: signin.html');
	exit();
}


?>