session_start();
require_once 'api/AbzuDB.php';
$db = new PDO("sqlite:storage/data.db");
$AbzuDB = new AbzuDB($db);
//	var_dump($AbzuDB);
if (isset($_POST['btn-login'])) {
    $user_name = trim($_POST['username']);
    $user_password = trim($_POST['password']);
    //	var_dump($user_name);
    //	var_dump($user_password);
    //	$password = md5($user_password);
    try {
        //	$stmt = $db_con->prepare("SELECT * FROM tbl_users WHERE user_email=:email");
        //	$stmt->execute(array(":email"=>$user_name));
        //	$row = $stmt->fetch(PDO::FETCH_ASSOC);
        //	$count = $stmt->rowCount();
        // var_dump($user_name);
        // var_dump($user_password);
        // echo $AbzuDB->check_credentials($user_name, $user_password);
        if ($AbzuDB->check_credentials($user_name, $user_password) == 'True') {
            echo "ok";
            // log in
            $_SESSION['user_session'] = $user_name;
        } else {
            echo "Username or password does not exist.";
            // wrong details
        }
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}
    include 'api/AbzuDB.php';
    $AbzuDB = new AbzuDB($db);
    $user_username = $_POST['username'];
    $user_pass = $_POST['pass'];
    /*
    	$check_user="******";
    
    	$run=mysqli_query($dbcon,$check_user);
    
    	if(mysqli_num_rows($run))
    	{
    		echo "<script>window.open('welcome.php','_self')</script>";
    
    		$_SESSION['username']=$user_username;//here session is used and value of $user_username store in $_SESSION.
    
    	}
    	else
    	{
    		echo "<script>alert('username or password is incorrect!')</script>";
    	}
    */
    $user_pass_valid = $AbzuDB->check_credentials($user_username, $user_pass);
    echo $user_pass_valid;
    if ($user_pass_valid = true) {
        echo "<script>window.open('index.html','_self')</script>";
        $_SESSION['username'] = $user_username;
        //here session is used and value of $user_username store in $_SESSION.
    } else {
        echo "<script>alert('username or password is incorrect!')</script>";
    }
}