package com.hrs.controller.landlord;

import com.hrs.dao.ReservationDao;
import com.hrs.dao.impl.ReservationDaoImpl;
import com.hrs.model.entity.User;

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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;

/**   
* Copyright: Copyright (c) 2026 chinasofti
* 
* @ClassName: ReservationHandleController.java
* @Description: 房东处理预约控制器
*
* @version: v1.0.0
* @author: 李鑫
* @date: 2026年4月13日 上午9:01:42 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2026年4月13日     李鑫           v1.0.0               新建文件
*/
@WebServlet("/landlord/reservation/handle")
public class ReservationHandleController extends HttpServlet {
    
    private ReservationDao reservationDao = new ReservationDaoImpl();
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        // 获取登录用户
        HttpSession session = request.getSession(false);
        if (session == null) {
            response.sendRedirect(request.getContextPath() + "/user/login");
            return;
        }
        
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null || !"1".equals(loginUser.getRoleType())) {
            response.sendRedirect(request.getContextPath() + "/user/login");
            return;
        }
        
        // 获取参数
        String idStr = request.getParameter("id");
        String action = request.getParameter("action"); // confirm 或 reject
        
        if (idStr == null || action == null) {
            response.sendRedirect(request.getContextPath() + "/landlord/reservation/list");
            return;
        }
        
        Long id = Long.parseLong(idStr);
        String status = "confirm".equals(action) ? "1" : "2";
        
        boolean success = reservationDao.updateStatus(id, status);
        
        if (success) {
            String msg = "confirm".equals(action) ? "已确认预约" : "已拒绝预约";
            String encodedMsg = URLEncoder.encode(msg, "UTF-8");
            response.sendRedirect(request.getContextPath() + "/landlord/reservation/list?msg=" + encodedMsg);
        } else {
            String encodedError = URLEncoder.encode("操作失败", "UTF-8");
            response.sendRedirect(request.getContextPath() + "/landlord/reservation/list?error=" + encodedError);
        }
    }
}