package web.UserRelated;


import pojo.User;
import service.UserService;

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/User")
public class selectUserServlet extends HttpServlet {
    UserService service = new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<User> userList = null;
        try {
            userList = service.getAllUser();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        req.setAttribute("users", userList);
        req.getRequestDispatcher("/main/admin/adminUser.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        String parkingSpotIdParam = req.getParameter("parking_spot_id");
        int parking_spot_id = 0; // 默认值
        if (parkingSpotIdParam != null && !parkingSpotIdParam.isEmpty()) {
            try {
                parking_spot_id = Integer.parseInt(parkingSpotIdParam);
            } catch (NumberFormatException e) {

            }
        }
        String address = req.getParameter("address");

        List<User> userList = null;
        try {
            userList = service.getUserByNPA(name,parking_spot_id, address);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        req.setAttribute("users", userList);
        req.getRequestDispatcher("/main/admin/adminUser.jsp").forward(req, resp);
    }
}



