<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.List, com.hrs.model.entity.House, com.hrs.model.entity.User" %>
<%
    User loginUser = (User) session.getAttribute("loginUser");
    if (loginUser == null || !"0".equals(loginUser.getRoleType())) {
        response.sendRedirect(request.getContextPath() + "/user/login");
        return;
    }
    
    List<House> houseList = (List<House>) request.getAttribute("houseList");
    Integer totalCount = (Integer) request.getAttribute("totalCount");
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>房源列表 - 租房系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: Arial, "微软雅黑", sans-serif;
            background-color: #f5f5f5;
        }
        
        /* 头部导航 */
        .header {
            background-color: #4CAF50;
            color: white;
            padding: 15px 0;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        .container {
            width: 1200px;
            margin: 0 auto;
            overflow: hidden;
        }
        
        .logo {
            float: left;
            font-size: 24px;
            font-weight: bold;
        }
        
        .nav {
            float: right;
            margin-top: 5px;
        }
        
        .nav a {
            color: white;
            text-decoration: none;
            margin-left: 20px;
            padding: 5px 10px;
        }
        
        .nav a:hover {
            background-color: #45a049;
            border-radius: 4px;
        }
        
        .user-info {
            float: right;
            margin-right: 20px;
            font-size: 14px;
        }
        
        /* 搜索栏 */
        .search-bar {
            background-color: white;
            padding: 20px 0;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        
        .search-form {
            display: flex;
            gap: 15px;
            align-items: flex-end;
            flex-wrap: wrap;
        }
        
        .search-group {
            display: flex;
            flex-direction: column;
        }
        
        .search-group label {
            font-size: 12px;
            color: #666;
            margin-bottom: 5px;
        }
        
        .search-group input, .search-group select {
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            width: 150px;
        }
        
        .search-btn {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 8px 20px;
            border-radius: 4px;
            cursor: pointer;
        }
        
        .search-btn:hover {
            background-color: #45a049;
        }
        
        .reset-btn {
            background-color: #999;
            color: white;
            border: none;
            padding: 8px 20px;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
        }
        
        /* 房源列表 */
        .house-list {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .house-card {
            background-color: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            transition: transform 0.3s, box-shadow 0.3s;
        }
        
        .house-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
        }
        
        .house-image {
            height: 200px;
            background-color: #e0e0e0;
            background-size: cover;
            background-position: center;
            position: relative;
        }
        
        .house-image .no-image {
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100%;
            color: #999;
            font-size: 14px;
        }
        
        .house-info {
            padding: 15px;
        }
        
        .house-title {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 10px;
            color: #333;
        }
        
        .house-title a {
            color: #333;
            text-decoration: none;
        }
        
        .house-title a:hover {
            color: #4CAF50;
        }
        
        .house-detail {
            color: #666;
            font-size: 14px;
            margin-bottom: 8px;
        }
        
        .house-price {
            color: #f44336;
            font-size: 22px;
            font-weight: bold;
            margin: 10px 0;
        }
        
        .house-price span {
            font-size: 14px;
            font-weight: normal;
        }
        
        .house-tags {
            margin: 10px 0;
        }
        
        .tag {
            display: inline-block;
            background-color: #f0f0f0;
            padding: 2px 8px;
            border-radius: 4px;
            font-size: 12px;
            margin-right: 5px;
            color: #666;
        }
        
        .house-footer {
            border-top: 1px solid #eee;
            padding-top: 10px;
            margin-top: 10px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .landlord-info {
            font-size: 12px;
            color: #999;
        }
        
        .btn {
            padding: 5px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
            text-decoration: none;
            display: inline-block;
        }
        
        .btn-primary {
            background-color: #4CAF50;
            color: white;
        }
        
        .btn-primary:hover {
            background-color: #45a049;
        }
        
        .btn-outline {
            background-color: transparent;
            border: 1px solid #4CAF50;
            color: #4CAF50;
        }
        
        .btn-outline:hover {
            background-color: #4CAF50;
            color: white;
        }
        
        /* 统计信息 */
        .stats {
            background-color: white;
            padding: 10px 15px;
            border-radius: 4px;
            margin-bottom: 20px;
            color: #666;
        }
        
        /* 空状态 */
        .empty-state {
            text-align: center;
            padding: 60px;
            background-color: white;
            border-radius: 8px;
            color: #999;
        }
        
        .empty-state p {
            margin-top: 10px;
        }
        
        /* 页脚 */
        .footer {
            background-color: #333;
            color: white;
            text-align: center;
            padding: 20px;
            margin-top: 40px;
        }
        
        .clearfix::after {
            content: "";
            clear: both;
            display: table;
        }
    </style>
</head>
<body>
    <!-- 头部导航 -->
    <div class="header">
        <div class="container clearfix">
            <div class="logo">🏠 租房系统</div>
            <div class="user-info">
                欢迎，<%= loginUser.getRealName() %> |
                <a href="${pageContext.request.contextPath}/user/logout" style="color:white;">退出</a>
            </div>
            <div class="nav">
                <a href="${pageContext.request.contextPath}/user/house/list">首页</a>
                <a href="${pageContext.request.contextPath}/user/collection/list">我的收藏</a>
                <a href="${pageContext.request.contextPath}/user/reservation/list">我的预约</a>
                <a href="${pageContext.request.contextPath}/user/order/list">我的订单</a>
            </div>
        </div>
    </div>
    
    <!-- 搜索栏 -->
    <div class="search-bar">
        <div class="container">
            <form class="search-form" action="${pageContext.request.contextPath}/user/house/list" method="get">
                <div class="search-group">
                    <label>区域</label>
                    <select name="area">
                        <option value="">全部区域</option>
                        <option value="朝阳区" <%= "朝阳区".equals(request.getAttribute("searchArea")) ? "selected" : "" %>>朝阳区</option>
                        <option value="海淀区" <%= "海淀区".equals(request.getAttribute("searchArea")) ? "selected" : "" %>>海淀区</option>
                        <option value="东城区" <%= "东城区".equals(request.getAttribute("searchArea")) ? "selected" : "" %>>东城区</option>
                        <option value="西城区" <%= "西城区".equals(request.getAttribute("searchArea")) ? "selected" : "" %>>西城区</option>
                        <option value="丰台区" <%= "丰台区".equals(request.getAttribute("searchArea")) ? "selected" : "" %>>丰台区</option>
                    </select>
                </div>
                <div class="search-group">
                    <label>最低租金(元)</label>
                    <input type="number" name="minPrice" placeholder="不限" value="<%= request.getAttribute("minPrice") != null ? request.getAttribute("minPrice") : "" %>">
                </div>
                <div class="search-group">
                    <label>最高租金(元)</label>
                    <input type="number" name="maxPrice" placeholder="不限" value="<%= request.getAttribute("maxPrice") != null ? request.getAttribute("maxPrice") : "" %>">
                </div>
                <button type="submit" class="search-btn">搜索</button>
                <a href="${pageContext.request.contextPath}/user/house/list" class="reset-btn">重置</a>
            </form>
        </div>
    </div>
    
    <div class="container">
        <!-- 统计信息 -->
        <div class="stats">
            📊 共找到 <strong><%= totalCount %></strong> 套房源
        </div>
        
        <!-- 房源列表 -->
        <div class="house-list">
            <%
                if (houseList != null && !houseList.isEmpty()) {
                    for (House house : houseList) {
            %>
            <div class="house-card">
                <div class="house-image" style="background-image: url('/HRS/<%= house.getFirstImage() != null ? house.getFirstImage() : "" %>');">
                    <% if (house.getFirstImage() == null) { %>
                        <div class="no-image">🏠 暂无图片</div>
                    <% } %>
                </div>
                <div class="house-info">
                    <div class="house-title">
                        <a href="${pageContext.request.contextPath}/user/house/detail?id=<%= house.getId() %>">
                            <%= house.getTitle() %>
                        </a>
                    </div>
                    <div class="house-detail">
                        📍 <%= house.getArea() %> | <%= house.getHouseType() %> | <%= house.getRentTypeText() %>
                    </div>
                    <div class="house-price">
                        ¥<%= house.getRentPrice() %><span>/月</span>
                    </div>
                    <div class="house-tags">
                        <span class="tag">#<%= house.getHouseType() %></span>
                        <span class="tag">#<%= house.getArea() %></span>
                    </div>
                    <div class="house-footer">
                        <div class="landlord-info">
                            👤 <%= house.getLandlordName() != null ? house.getLandlordName() : "房东" %>
                        </div>
                        <a href="${pageContext.request.contextPath}/user/house/detail?id=<%= house.getId() %>" class="btn btn-primary">
                            查看详情
                        </a>
                    </div>
                </div>
            </div>
            <%
                    }
                } else {
            %>
            <div class="empty-state">
                <div style="font-size: 48px;">🏠</div>
                <p>暂无房源，请稍后再来</p>
            </div>
            <%
                }
            %>
        </div>
    </div>
    
    <!-- 页脚 -->
    <div class="footer">
        <p>© 2026 租房系统 | 让租房更简单</p>
    </div>
</body>
</html>