<?php 

	if ($_SERVER["REQUEST_METHOD"] == "POST") {
		import_file(DIR_MODELS."Model.class.php");

		$email = filter_input(INPUT_POST, "email_usuario", FILTER_SANITIZE_EMAIL);
		$senha = filter_input(INPUT_POST, "senha_usuario", FILTER_SANITIZE_STRING);

		$model = new Model();

		$result = $model->query("
			SELECT * FROM usuario 
			WHERE 
				email_usuario = :email AND 
				senha_usuario = :senha AND 
				status_usuario = 'S'
		", [
			":email" => $email, 
			":senha" => md5($senha)
		]);

		if ($result->rowCount() == 1) {
			$dados = $result->fetch();
			Session::set(
				[
					"id_usuario" => $dados->id_usuario,
					"nome_usuario" => $dados->nome_usuario,
					"email_usuario" => $dados->email_usuario,
					"tipo_usuario" => $dados->id_tipo_usuario,
					"id_indicador" => $dados->id_indicador,
					"usuario_ativo" => $dados->usuario_ativo,
					"id_estado" => $dados->id_estado,
					"id_cidade" => $dados->id_cidade
				],
				[
					FILTER_SANITIZE_NUMBER_INT,
					FILTER_SANITIZE_STRING,
					FILTER_SANITIZE_EMAIL,
					FILTER_SANITIZE_NUMBER_INT,
					FILTER_SANITIZE_NUMBER_INT,
					FILTER_SANITIZE_STRING,
					FILTER_SANITIZE_NUMBER_INT,
					FILTER_SANITIZE_NUMBER_INT
				]
			);
			
			echo json_encode(["status" => true]);
		}
		else{
			echo json_encode(["status" => false]);
		}
	}
	else{
		show_403();
	}

?>