<?php
	if (isset($_POST['user']) && isset($_POST['password']))
	{
		$formulaire = '';
		$erreur = '';
		
		include 'connexion.php';
		include 'user_class.php';
		include 'escape_valeur.php';
		session_start();
		
		$requete = 'SELECT * FROM isoactif_t_usager_consult WHERE u_login = \'' . escape_valeur($_POST['user']) . '\' AND u_pwd = \'' . escape_valeur($_POST['password']) . '\'';
		if(!$resultat = $connexion->query($requete)){ die('There was an error running the query [' . $connexion->error . ']');}
		
		if ($resultat->num_rows == 0)
			header('Location: ../index.php?erreur=login');
		else
		{
			$ligne = $resultat->fetch_assoc();
			
			// Si usager pas admin toutes régions
			if ($ligne['u_region'] != 99)
			{
				$requeteRegion = 'SELECT * FROM t_arse WHERE a_id = ' . escape_valeur($ligne['u_region']);
				if(!$resultatRegion = $connexion->query($requeteRegion)){ die('There was an error running the query [' . $connexion->error . ']'); }
				$ligneRegion = $resultatRegion->fetch_assoc();
				$regionTXT = 'RSEQ ' . $ligneRegion['a_nom'];
			}
			else
				$regionTXT = 'RSEQ Provincial';
			
			
			
			$user = new Usager();
			$user->creerusager($ligne['u_id'], $ligne['u_nom'], $ligne['u_region'], $regionTXT);
			$_SESSION['usagerConsultIsoActif'] =  $user;
			
			header('Location: ../index.php');
		}
		
		$connexion->close();
	}
?>