package com.hrs.controller.user;

import com.hrs.dao.UserDao;
import com.hrs.dao.impl.UserDaoImpl;
import com.hrs.model.entity.User;
import com.hrs.service.UserService;
import com.hrs.service.impl.UserServiceImpl;
import com.hrs.util.MD5Util;

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.net.URLEncoder;

/**   
* Copyright: Copyright (c) 2026 chinasofti
* 
* @ClassName: RegisterController.java
* @Description: 注册控制器
*
* @version: v1.0.0
* @author: 李鑫
* @date: 2026年4月13日 上午9:08:06 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2026年4月13日     李鑫           v1.0.0               新建文件
*/
@WebServlet("/user/register")
public class RegisterController extends HttpServlet {
    
    private UserDao userDao = new UserDaoImpl();
    private UserService userService = new UserServiceImpl();
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        // GET请求：显示注册页面
        request.getRequestDispatcher("/jsp/user/register.jsp").forward(request, response);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        // POST请求：处理注册表单提交
        request.setCharacterEncoding("UTF-8");
        
        // 1. 获取表单参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String confirmPassword = request.getParameter("confirmPassword");
        String realName = request.getParameter("realName");
        String phone = request.getParameter("phone");
        String idCard = request.getParameter("idCard");
        String roleType = request.getParameter("roleType");
        
        // 2. 创建用户对象
        User user = new User();
        user.setUserName(username);
        user.setPassword(password);  // 密码将在Service层加密
        user.setRealName(realName);
        user.setPhone(phone);
        user.setIdCard(idCard);
        user.setRoleType(roleType);  // 0-租客 1-房东
        user.setStatus("0");  // 默认未认证状态
        
        // 3. 调用UserService进行注册验证和保存
        UserService.RegisterResult result = userService.register(user, confirmPassword);
        
        if (result.isSuccess()) {
            // 注册成功，跳转到登录页面（使用重定向避免表单重复提交）
            String encodedMsg = URLEncoder.encode(result.getMessage(), "UTF-8");
            response.sendRedirect(request.getContextPath() + "/user/login?message=" + encodedMsg);
        } else {
            // 注册失败，留在注册页面显示错误
            request.setAttribute("error", result.getMessage());
            request.getRequestDispatcher("/jsp/user/register.jsp").forward(request, response);
        }
    }
}