<?php
	session_start();

	include "connect.php";

	$login = $_POST['login'];
	$senha = $_POST['senha'];
	//echo $login;
	//echo $senha;
	$buscausuario = "select * from sgcws_usuarios where ds_email = '$login' and ds_senha = '$senha'";
	$sql = "select id, ds_email, st_code from sgcws_usuarios where ds_email = ? and ds_senha = ?";
	//echo "<pre>$sql</pre>" . PHP_EOL;
	//echo "<pre>$buscausuario</pre>" . PHP_EOL;
	$stmt = mysqli_prepare($conexao, $sql) or die(mysqli_error($conexao));
	mysqli_stmt_bind_param($stmt, 'ss', $login, $senha) or die(mysqli_error($conexao));
	//echo "<pre>$login/$senha</pre>" . PHP_EOL;
	mysqli_stmt_execute($stmt) or die(mysqli_error($conexao));
	//$resultado = mysqli_query($conexao, $buscausuario) or die(mysqli_error($conexao));
	//if (mysqli_num_rows($resultado) == 1) {
	$registros = fetch_assoc_stmt($stmt);
	//echo "<pre>" . print_r($registros, true) . "</pre>";
	if (count($registros) == 1) {
		$registro = $registros[0];
		//while (mysqli_stmt_fetch($stmt)){
			$_SESSION['usuario'] = $registro['ds_email'];
			$_SESSION['id_usuario'] = $registro['id'];
			$_SESSION['st_code'] = $registro['st_code'];
			//echo "<pre>id=" . $registro['id'] . "</pre>" . PHP_EOL;
			//echo "<pre>usuario=" . $registro['ds_email'] . "</pre>" . PHP_EOL;
			//echo "<pre>st_code=" . $registro['st_code'] . "</pre>" . PHP_EOL;
			header('Location:principal.php');
		//}
	}
	else{
		echo "<div class='loginwrapper'>
	<div class='loginwrap zindex100 animate2 bounceInDown'>
	<h1 class='logintitle'><span class='icon-ban-circle'></span> ACESSO NEGADO <span class='subtitle'>Senha/Usuario Inválidos</span></h1>
        <div class='loginwrapperinner'>
            <form id='loginform' action='index.php' method='post'>
                
                <p class='animate6 bounceIn'><button class='btn btn-default btn-block'>VOLTAR</button></p>
                
            </form>
        </div>
    </div>
    <div class='loginshadow animate3 fadeInUp'></div>
</div>";
	  
	}
	
	
?>

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Logando</title>
<link rel="stylesheet" href="css/style.default.css" type="text/css" />
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="js/jquery-migrate-1.1.1.min.js"></script>
</head>

<body class="loginbody">
</body>
</html>
