package com.hrs.controller.landlord;

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.math.BigDecimal;
import java.net.URLEncoder;


/**   
* Copyright: Copyright (c) 2026 chinasofti
* 
* @ClassName: HouseEditController.java
* @Description: 编辑房源控制器
*
* @version: v1.0.0
* @author: 李鑫
* @date: 2026年4月13日 上午8:56:25 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2026年4月13日     李鑫           v1.0.0               新建文件
*/
@WebServlet("/landlord/house/edit")
public class HouseEditController 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 || !"1".equals(loginUser.getRoleType())) {
            response.sendRedirect(request.getContextPath() + "/user/login");
            return;
        }
        
        // 获取房源ID
        String idStr = request.getParameter("id");
        if (idStr == null || idStr.trim().isEmpty()) {
            response.sendRedirect(request.getContextPath() + "/landlord/house/list");
            return;
        }
        
        Long houseId = Long.parseLong(idStr);
        House house = houseDao.findById(houseId);
        
        // 验证是否是自己的房源
        if (house == null || !house.getLandlordId().equals(loginUser.getId())) {
            response.sendRedirect(request.getContextPath() + "/landlord/house/list");
            return;
        }
        
        request.setAttribute("house", house);
        request.setAttribute("loginUser", loginUser);
        request.getRequestDispatcher("/jsp/landlord/house_edit.jsp").forward(request, response);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        request.setCharacterEncoding("UTF-8");
        
        // 获取登录用户
        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 title = request.getParameter("title");
        String area = request.getParameter("area");
        String address = request.getParameter("address");
        String houseType = request.getParameter("houseType");
        String rentPriceStr = request.getParameter("rentPrice");
        String rentType = request.getParameter("rentType");
        String facility = request.getParameter("facility");
        String description = request.getParameter("description");
        String imgUrl = request.getParameter("imgUrl");
        
        if (idStr == null || title == null || rentPriceStr == null) {
            response.sendRedirect(request.getContextPath() + "/landlord/house/list");
            return;
        }
        
        Long houseId = Long.parseLong(idStr);
        
        // 创建房源对象
        House house = new House();
        house.setId(houseId);
        house.setTitle(title);
        house.setArea(area);
        house.setAddress(address);
        house.setHouseType(houseType);
        house.setRentPrice(new BigDecimal(rentPriceStr));
        house.setRentType(rentType);
        house.setFacility(facility);
        house.setDescription(description);
        house.setImgUrl(imgUrl);
        
        boolean success = houseDao.update(house);
        
        if (success) {
            String encodedMsg = URLEncoder.encode("房源修改成功", "UTF-8");
            response.sendRedirect(request.getContextPath() + "/landlord/house/list?msg=" + encodedMsg);
        } else {
            request.setAttribute("error", "修改失败，请稍后重试");
            doGet(request, response);
        }
    }
}