package com.hrs.controller.admin;

import com.hrs.dao.HouseDao;
import com.hrs.dao.impl.HouseDaoImpl;
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: AdminHouseOfflineController.java
* @Description: 管理员下架房源控制器
*
* @version: v1.0.0
* @author: 李鑫
* @date: 2026年4月13日 上午8:52:08 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2026年4月13日     李鑫           v1.0.0               新建文件
*/
@WebServlet("/admin/house/offline")
public class AdminHouseOfflineController extends HttpServlet {
    
    private HouseDao houseDao = new HouseDaoImpl();
    
    @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 || !"2".equals(loginUser.getRoleType())) {
            response.sendRedirect(request.getContextPath() + "/user/login");
            return;
        }
        
        String houseIdStr = request.getParameter("id");
        if (houseIdStr == null) {
            response.sendRedirect(request.getContextPath() + "/admin/house/list");
            return;
        }
        
        Long houseId = Long.parseLong(houseIdStr);
        boolean success = houseDao.updateStatus(houseId, "2"); // 2-已下架
        
        if (success) {
            String encodedMsg = URLEncoder.encode("房源已下架", "UTF-8");
            response.sendRedirect(request.getContextPath() + "/admin/house/list?msg=" + encodedMsg);
        } else {
            String encodedError = URLEncoder.encode("操作失败", "UTF-8");
            response.sendRedirect(request.getContextPath() + "/admin/house/list?error=" + encodedError);
        }
    }
}