package projet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet("/SupprimerBloc")
public class SupprimerBloc extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public SupprimerBloc() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Utilisateur u = (Utilisateur) session.getAttribute("utilisateur");
		
		if(u != null) {
			response.sendRedirect("AfficherPage");
		}else {
			response.sendRedirect("/Projet/");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Utilisateur u = (Utilisateur) session.getAttribute("utilisateur");
		
		if(u != null) {
			String idBlocStr = request.getParameter("blocId");
			
			if(idBlocStr == null || idBlocStr.isEmpty()) {
				response.sendRedirect("AfficherPage");	
			} else {
				int idBloc = Integer.parseInt(idBlocStr);
				Bloc.supprimerBloc(idBloc, u.getId());
				response.sendRedirect("AfficherPage");	
			}
			
		} else {
			response.sendRedirect("/Projet/");
		}
	}
}
