<?php
require "functions.php";

if (isset($_GET['do'])){
  if ($_GET['do'] == 'logout'){
    require_once "connect.php";
    //dbw_query($db_conn, "UPDATE USERS SET SessionID = NULL WHERE SessionID='$_COOKIE[SessionID]'");
    session_destroy();
    header('Location: index.php');
  }
}

if (isset($_SESSION['UserID'])){ //Ya está logueado
  header('Location: panel.php');
}

if (!isset($_POST['nick'])){
  echo $twig->render('login.twig');
}
if (isset($_POST['nick'])){
    $nick=$_POST['nick'];
    $pass= hash("sha256",$_POST['pass']);
    $resql = dbw_query_fetch_array($db_conn,"SELECT ID_U,`passw` FROM `USERS` WHERE nick ='$nick'");
    $passcomprobar = $resql['passw'];
	if ($pass == $passcomprobar)
	{
    $_SESSION['UserID'] = $resql['ID_U'];
    header('Location: panel.php');
	}else{
		echo $twig->render('login.twig', array('status' => 'error'));
	}
	
}
?>