package web.VehicleRelated;


import pojo.Vehicle;
import service.VehicleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/main/admin/Vehicles")
public class SelectVehicleServlet extends HttpServlet {
    VehicleService service = new VehicleService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Vehicle> vehicles = null;
        try {
            vehicles = service.getAllVehicles();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        req.setAttribute("vehicles", vehicles);
        req.getRequestDispatcher("/main/admin/adminVehicle.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String car_license = req.getParameter("car_license");
        String brand = req.getParameter("brand");
        String model = req.getParameter("model");
        String building = req.getParameter("buildnum");
        List<Vehicle> vehicles = null;
        try {
            vehicles = service.getVehiclesByCase(car_license, brand, model, building);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        req.setAttribute("vehicles", vehicles);
        req.getRequestDispatcher("/main/admin/adminVehicle.jsp").forward(req, resp);
    }
}
