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;
import java.util.Map;

@WebServlet(urlPatterns = "/main/admin/UserOrder")
public class OrderUserServlet extends HttpServlet {
    UserService service = new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String choice=req.getParameter("statistic");
        Map<String, Integer> mp = null;
        try {
            if(choice!=null){
                if(choice.equals("totalUsers")){
                    mp = service.CntUserBytotalUser();
                }
                else if(choice.equals("gender")){
                    mp = service.CntUserByGender();
                }
                else{
                    mp = service.CntUserByUserType();
                }
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        req.setAttribute("mp", mp);
        req.getRequestDispatcher("/main/admin/adminUser.jsp?count=1").forward(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        String choice=req.getParameter("sort");
        List<User> userList = null;
        try {
            if(choice!=null){
                if(choice.equals("userId")){
                    userList = service.OrderUserByUserId();
                }
                else if(choice.equals("parking_spot_id")){
                    userList = service.OrderUserByParkId();
                }
                else{
                    userList = service.OrderUserByName();
                }
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        req.setAttribute("users", userList);
        req.getRequestDispatcher("/main/admin/adminUser.jsp").forward(req, resp);
    }
}


