package com.landaiqing.servlet.system;

import com.landaiqing.entity.AdminUserEntity;
import com.landaiqing.service.AdminUserService;
import com.mysql.cj.util.StringUtils;
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 java.io.IOException;

@WebServlet("/updateAdmin")
public class GetAdminByID extends HttpServlet {
    private AdminUserService adminUserService;

    {
        try {
            adminUserService = new AdminUserService();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String idStr = req.getParameter("id");
        if (idStr==null || idStr==""){
            req.setAttribute("errorMsg","ID的值不能为空!!!");
            req.getRequestDispatcher("error.jsp").forward(req,resp);
            return;
        }
        try {
            Integer id = Integer.parseInt(idStr);
            AdminUserEntity adminUserEntity = adminUserService.getAdminByID(id);
            if (adminUserEntity==null){
                req.setAttribute("errorMsg","id不存在!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }
            req.setAttribute("adminByID",adminUserEntity);
            req.getRequestDispatcher("./System/adminManage.jsp").forward(req,resp);
        }catch (Exception e){
            req.setAttribute("errorMsg","系统异常!!!");
            req.getRequestDispatcher("error.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String idStr = req.getParameter("id");
            if (StringUtils.isNullOrEmpty(idStr)){
                req.setAttribute("errorMsg","id的值不能为空!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }
            Integer id = Integer.parseInt(idStr);

            String adminUserName = req.getParameter("adminUserName");
            if (StringUtils.isNullOrEmpty(adminUserName)){
                req.setAttribute("errorMsg","adminUserName 的值不能为空!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }
            String adminPassword = req.getParameter("adminPassword");
            if (StringUtils.isNullOrEmpty(adminPassword)){
                req.setAttribute("errorMsg","adminPassword 的值不能为空!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }
            String isValid = req.getParameter("isValid");
            if (StringUtils.isNullOrEmpty(idStr)){
                req.setAttribute("errorMsg","isValid 的值不能为空!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }


            AdminUserEntity adminUserEntity=new AdminUserEntity();
            adminUserEntity.setAdminId(id);
            adminUserEntity.setAdminUserName(adminUserName);
            adminUserEntity.setAdminPassword(adminPassword);
            adminUserEntity.setIsValid(Integer.valueOf(isValid));

            int result = adminUserService.updateAdmin(adminUserEntity);
            if (result<=0){
                req.setAttribute("errorMsg","修改失败!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }
            resp.sendRedirect("./System/adminManage.jsp");
        } catch (Exception e) {
            req.setAttribute("errorMsg","系统异常!!!");
            req.getRequestDispatcher("error.jsp").forward(req,resp);
            throw new RuntimeException(e);
        }
    }
}
