package locadora.servlets;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import locadora.modelo.DAO.UserDAO;
import locadora.modelo.user.Cliente;



/**
 * Servlet implementation class Login
 */
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{

		HttpSession session = request.getSession(true);
			
		String email = (String)request.getParameter("email"); 
		String senha = (String)request.getParameter("senha"); 
		String url = null;

	     
	   try {  
		 UserDAO userDao = new UserDAO();
		 Cliente novoCliente= userDao.verificaLogin(email,senha);
	     if(novoCliente!=null){
	    	//Seta a sessao de login
				session.putValue("cliente", novoCliente);
				session.putValue("nome_autor", novoCliente.getNome()); 
				url="index.jsp?msg_sucesso=Olá "+novoCliente.getNome();   
								
				response.sendRedirect(url); 

				
	     }
	     else{
	    	 session.putValue("cliente", novoCliente);
		     url="index.jsp?msg_erro=Login ou senha incorreta";   
			 
		     response.sendRedirect(url); 


	     }
	   
	    } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();  
		}
	}

}
