package com.hrs.controller.admin;

import com.hrs.dao.HouseDao;
import com.hrs.dao.impl.HouseDaoImpl;
import com.hrs.model.entity.House;
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.util.List;

/**   
* Copyright: Copyright (c) 2026 chinasofti
* 
* @ClassName: AdminHouseController.java
* @Description: 管理员房源审核控制器
*
* @version: v1.0.0
* @author: 李鑫
* @date: 2026年4月13日 上午8:51:26 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2026年4月13日     李鑫           v1.0.0               新建文件
*/
@WebServlet("/admin/house/list")
public class AdminHouseController 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;
        }
        
        // 获取不同状态的房源
        List<House> pendingList = houseDao.findPendingHouses();
        List<House> publishedList = houseDao.findPublishedHousesForAdmin();
        List<House> offlineList = houseDao.findOfflineHouses();
        
        request.setAttribute("pendingList", pendingList);
        request.setAttribute("publishedList", publishedList);
        request.setAttribute("offlineList", offlineList);
        request.setAttribute("pendingCount", pendingList.size());
        request.setAttribute("publishedCount", publishedList.size());
        request.setAttribute("offlineCount", offlineList.size());
        request.setAttribute("loginUser", loginUser);
        
        request.getRequestDispatcher("/jsp/admin/house_list.jsp").forward(request, response);
    }
}