<%@ 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 || !"1".equals(loginUser.getRoleType())) {
        response.sendRedirect(request.getContextPath() + "/user/login");
        return;
    }
    
    List<House> houseList = (List<House>) request.getAttribute("houseList");
    Integer totalCount = (Integer) request.getAttribute("totalCount");
    String msg = request.getParameter("msg");
    String error = request.getParameter("error");
%>
<!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: #ff9800;
            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, .nav a.active {
            background-color: #fb8c00;
            border-radius: 4px;
        }
        
        .user-info {
            float: right;
            margin-right: 20px;
            font-size: 14px;
        }
        
        .breadcrumb {
            background-color: white;
            padding: 12px 0;
            margin-bottom: 20px;
            border-radius: 4px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        
        .breadcrumb a {
            color: #ff9800;
            text-decoration: none;
        }
        
        .stats {
            background-color: white;
            padding: 10px 15px;
            border-radius: 4px;
            margin-bottom: 20px;
            color: #666;
        }
        
        .message {
            background-color: #e8f5e9;
            color: #2e7d32;
            padding: 12px;
            border-radius: 4px;
            margin-bottom: 20px;
            text-align: center;
            animation: fadeOut 3s ease forwards;
        }
        
        .error-message {
            background-color: #ffebee;
            color: #c62828;
            padding: 12px;
            border-radius: 4px;
            margin-bottom: 20px;
            text-align: center;
            animation: fadeOut 3s ease forwards;
        }
        
        @keyframes fadeOut {
            0% { opacity: 1; }
            70% { opacity: 1; }
            100% { opacity: 0; display: none; }
        }
        
        .add-btn {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            text-decoration: none;
            border-radius: 4px;
            display: inline-block;
            margin-bottom: 20px;
        }
        
        .add-btn:hover {
            background-color: #45a049;
        }
        
        .house-table {
            background-color: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        table {
            width: 100%;
            border-collapse: collapse;
        }
        
        th, td {
            padding: 15px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }
        
        th {
            background-color: #f5f5f5;
            font-weight: bold;
            color: #333;
        }
        
        tr:hover {
            background-color: #f9f9f9;
        }
        
        .status-badge {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: bold;
        }
        
        .status-pending {
            background-color: #fff3e0;
            color: #f39c12;
        }
        
        .status-published {
            background-color: #e8f5e9;
            color: #4CAF50;
        }
        
        .status-offline {
            background-color: #ffebee;
            color: #f44336;
        }
        
        .btn {
            padding: 4px 12px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
            text-decoration: none;
            display: inline-block;
            margin: 0 3px;
        }
        
        .btn-edit {
            background-color: #2196F3;
            color: white;
        }
        
        .btn-edit:hover {
            background-color: #0b7dda;
        }
        
        .btn-delete {
            background-color: #f44336;
            color: white;
        }
        
        .btn-delete:hover {
            background-color: #d32f2f;
        }
        
        .empty-state {
            text-align: center;
            padding: 60px;
            background-color: white;
            border-radius: 8px;
            color: #999;
        }
        
        .empty-state p {
            margin-top: 20px;
        }
        
        .footer {
            background-color: #333;
            color: white;
            text-align: center;
            padding: 20px;
            margin-top: 40px;
        }
        
        .clearfix::after {
            content: "";
            clear: both;
            display: table;
        }
        
        .house-image {
            width: 80px;
            height: 60px;
            background-color: #e0e0e0;
            background-size: cover;
            background-position: center;
            border-radius: 4px;
            position: relative;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .no-image {
            font-size: 20px;
            text-align: center;
            color: #999;
        }
        
        .price {
            color: #f44336;
            font-weight: bold;
        }
    </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}/landlord/house/list" class="active">房源管理</a>
                <a href="${pageContext.request.contextPath}/landlord/reservation/list">预约管理</a>
                <a href="${pageContext.request.contextPath}/landlord/order/list">订单管理</a>
            </div>
        </div>
    </div>
    
    <div class="container">
        <div class="breadcrumb">
            <a href="${pageContext.request.contextPath}/landlord/house/list">首页</a> &gt;
            <span>房源管理</span>
        </div>
        
        <% if (msg != null) { %>
            <div class="message"><%= msg %></div>
        <% } %>
        <% if (error != null) { %>
            <div class="error-message"><%= error %></div>
        <% } %>
        
        <div class="stats">
            📊 共有 <strong><%= totalCount %></strong> 套房源
            <a href="${pageContext.request.contextPath}/landlord/house/add" class="add-btn" style="float: right;">+ 发布新房源</a>
        </div>
        
        <div class="house-table">
            <%
                if (houseList != null && !houseList.isEmpty()) {
            %>
            <table>
                <thead>
                    <tr>
                        <th>房源图片</th>
                        <th>房源信息</th>
                        <th>租金</th>
                        <th>状态</th>
                        <th>发布时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <%
                        for (House house : houseList) {
                    %>
                    <tr>
                        <td>
                            <div class="house-image" 
                                 style="background-image: url('${pageContext.request.contextPath}/<%= house.getFirstImage() != null ? house.getFirstImage() : "" %>');">
                                <% if (house.getFirstImage() == null) { %>
                                    <div class="no-image">🏠 暂无图片</div>
                                <% } %>
                            </div>
                        </td>
                        <td>
                            <strong><%= house.getTitle() %></strong><br>
                            <span style="font-size:12px;color:#999;"><%= house.getArea() %> | <%= house.getHouseType() %></span>
                        </td>
                        <td class="price">¥<%= house.getRentPrice() %>/月</td>
                        <td>
                            <span class="status-badge status-<%= house.getStatus().equals("0") ? "pending" : house.getStatus().equals("1") ? "published" : "offline" %>">
                                <%= house.getStatusText() %>
                            </span>
                        </td>
                        <td><%= new java.text.SimpleDateFormat("yyyy-MM-dd").format(house.getCreateTime()) %></td>
                        <td>
                            <a href="${pageContext.request.contextPath}/landlord/house/edit?id=<%= house.getId() %>" class="btn btn-edit">编辑</a>
                            <a href="${pageContext.request.contextPath}/landlord/house/delete?id=<%= house.getId() %>" 
                               class="btn btn-delete" 
                               onclick="return confirm('确定要删除这套房源吗？删除后不可恢复！')">删除</a>
                        </td>
                    </tr>
                    <%
                        }
                    %>
                </tbody>
            </table>
            <%
                } else {
            %>
            <div class="empty-state">
                <div style="font-size: 64px;">🏠</div>
                <p>还没有发布任何房源</p>
                <p><a href="${pageContext.request.contextPath}/landlord/house/add">立即发布新房源 &gt;&gt;</a></p>
            </div>
            <%
                }
            %>
        </div>
    </div>
    
    <div class="footer">
        <p>© 2026 租房系统 | 让租房更简单</p>
    </div>
</body>
</html>