package com.landaiqing.servlet;



import com.landaiqing.entity.AdminUserEntity;
import com.landaiqing.service.AdminUserService;
import com.landaiqing.utils.RandomValidateCode;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.List;

/**
 * 管理员登录
 */
@WebServlet("/login")
public class AdminLoginServlet 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 {
        // 转发login页面
        req.getRequestDispatcher("login.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 点击登录的时候 获取到用户的参数
        String userName = req.getParameter("username");
        if (StringUtils.isEmpty(userName)) {
            //转发到错误页面
            req.setAttribute("errorMsg", "用户名称不能够是为空!");
            req.getRequestDispatcher("error.jsp").forward(req, resp);
            return;
        }
        String userPwd = req.getParameter("password");
        // 参数验证
        if (StringUtils.isEmpty(userPwd)) {
            //转发到错误页面
            req.setAttribute("errorMsg", "userPwd不能够是为空!");
            req.getRequestDispatcher("error.jsp").forward(req, resp);
            return;
        }
        String userCode = req.getParameter("code"); // 用户输入的图形验证码
        // 从session中获取图形验证码
        HttpSession session = req.getSession();
        String sessionCode = (String) session.getAttribute(RandomValidateCode.RANDOMVALIDATECODE);
        if (!sessionCode.equalsIgnoreCase(userCode)) {
            req.setAttribute("errorMsg", "图形验证码不正确，请重新输入!");
            req.getRequestDispatcher("error.jsp").forward(req, resp);
            return;
        }
        // 在调用业务逻辑层
        List<AdminUserEntity> adminUserEntity = adminUserService.login(userName, userPwd);
        if (adminUserEntity == null) {
            // 用户名称或者密码错误!
            req.setAttribute("errorMsg", "用户名称或者是密码错误!");
            req.getRequestDispatcher("error.jsp").forward(req, resp);
            return;
        }
        // 判断用户是否记住密码
        String rememberPassword = req.getParameter("remember");
        if ("on".equals(rememberPassword)) {
            // 如果有记住密码则 将密码保存在cookie中
            Cookie userNameCookie = new Cookie("userName", userName);
            Cookie userPwdCookie = new Cookie("userPwd", userPwd);
            resp.addCookie(userNameCookie);
            resp.addCookie(userPwdCookie);
        }
        // 能够db中查询到对象 登录成功了  将用户数据存放在session中
        session = req.getSession();
        session.setAttribute("user", adminUserEntity);
        // 在转发到首页（重定向到首页）
//        req.getRequestDispatcher("index.jsp").forward(req, resp);
        resp.sendRedirect("./System/index.jsp");
    }
}

