<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.List, java.text.SimpleDateFormat, com.hrs.model.entity.House, com.hrs.model.entity.User" %>
<%
    User loginUser = (User) session.getAttribute("loginUser");
    if (loginUser == null || !"2".equals(loginUser.getRoleType())) {
        response.sendRedirect(request.getContextPath() + "/user/login");
        return;
    }
    
    List<House> pendingList = (List<House>) request.getAttribute("pendingList");
    List<House> publishedList = (List<House>) request.getAttribute("publishedList");
    List<House> offlineList = (List<House>) request.getAttribute("offlineList");
    Integer pendingCount = (Integer) request.getAttribute("pendingCount");
    Integer publishedCount = (Integer) request.getAttribute("publishedCount");
    Integer offlineCount = (Integer) request.getAttribute("offlineCount");
    String msg = request.getParameter("msg");
    String error = request.getParameter("error");
    
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
%>
<!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: #2196F3;
            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: #1976D2;
            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);
        }
        
        .stats-row {
            display: flex;
            gap: 20px;
            margin-bottom: 20px;
        }
        
        .stat-card {
            background-color: white;
            padding: 15px 25px;
            border-radius: 8px;
            flex: 1;
            text-align: center;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .stat-number {
            font-size: 32px;
            font-weight: bold;
            margin-bottom: 5px;
        }
        
        .stat-label {
            color: #666;
            font-size: 14px;
        }
        
        .stat-pending .stat-number { color: #f39c12; }
        .stat-published .stat-number { color: #4CAF50; }
        .stat-offline .stat-number { color: #f44336; }
        
        .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; }
        }
        
        .house-section {
            background-color: white;
            border-radius: 8px;
            margin-bottom: 30px;
            overflow: hidden;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .section-title {
            background-color: #f5f5f5;
            padding: 15px 20px;
            font-size: 18px;
            font-weight: bold;
            border-bottom: 2px solid #2196F3;
        }
        
        table {
            width: 100%;
            border-collapse: collapse;
        }
        
        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }
        
        th {
            background-color: #fafafa;
            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-approve {
            background-color: #4CAF50;
            color: white;
        }
        
        .btn-reject {
            background-color: #f44336;
            color: white;
        }
        
        .btn-offline {
            background-color: #ff9800;
            color: white;
        }
        
        .btn-online {
            background-color: #2196F3;
            color: white;
        }
        
        .btn-view {
            background-color: #999;
            color: white;
        }
        
        .btn-approve:hover { background-color: #45a049; }
        .btn-reject:hover { background-color: #d32f2f; }
        .btn-offline:hover { background-color: #fb8c00; }
        .btn-online:hover { background-color: #1976D2; }
        .btn-view:hover { background-color: #777; }
        
        .empty-state {
            text-align: center;
            padding: 40px;
            color: #999;
        }
        
        .price {
            color: #f44336;
            font-weight: bold;
        }
        
        .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}/jsp/admin/index.jsp" >首页</a>
                <a href="${pageContext.request.contextPath}/admin/user/list">用户管理</a>
                <a href="${pageContext.request.contextPath}/admin/house/list" class="active">房源审核</a>
                <a href="${pageContext.request.contextPath}/admin/order/list">订单管理</a>
            </div>
        </div>
    </div>
    
    <div class="container">
        <div class="breadcrumb">
            <a href="${pageContext.request.contextPath}/admin/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-row">
            <div class="stat-card stat-pending">
                <div class="stat-number"><%= pendingCount %></div>
                <div class="stat-label">待审核房源</div>
            </div>
            <div class="stat-card stat-published">
                <div class="stat-number"><%= publishedCount %></div>
                <div class="stat-label">已上架房源</div>
            </div>
            <div class="stat-card stat-offline">
                <div class="stat-number"><%= offlineCount %></div>
                <div class="stat-label">已下架/违规房源</div>
            </div>
        </div>
        
        <!-- 待审核房源 -->
        <div class="house-section">
            <div class="section-title">
                ⏳ 待审核房源
                <span style="font-size:14px; color:#999; margin-left:10px;">共 <%= pendingCount %> 条</span>
            </div>
            <table>
                <thead>
                    <tr>
                        <th>房源图片</th>
                        <th>房源信息</th>
                        <th>房东信息</th>
                        <th>租金</th>
                        <th>发布时间</th>
                        <th>操作</th>
                    </thead>
                <tbody>
                    <%
                        if (pendingList != null && !pendingList.isEmpty()) {
                            for (House house : pendingList) {
                    %>
                    <tr>
                        <td style="width:80px;">
                            <div style="width:70px; height:50px; background-color:#e0e0e0; background-size:cover; background-position:center; border-radius:4px; 
                                <% if (house.getFirstImage() != null) { %>background-image:url('/HRS/<%= house.getFirstImage() %>');<% } %>">
                                <% if (house.getFirstImage() == null) { %>
                                    <div style="display:flex;align-items:center;justify-content:center;height:100%;font-size:12px;">无图</div>
                                <% } %>
                            </div>
                        </td>
                        <td>
                            <strong><%= house.getTitle() %></strong><br>
                            <span style="font-size:12px;color:#999;"><%= house.getArea() %> | <%= house.getHouseType() %></span>
                        </td>
                        <td>
                            <%= house.getLandlordName() != null ? house.getLandlordName() : "未知" %><br>
                            <span style="font-size:12px;color:#999;"><%= house.getLandlordPhone() != null ? house.getLandlordPhone() : "" %></span>
                        </td>
                        <td class="price">¥<%= house.getRentPrice() %>/月</td>
                        <td><%= sdf.format(house.getCreateTime()) %></td>
                        <td>
                            <a href="${pageContext.request.contextPath}/admin/house/audit?id=<%= house.getId() %>&action=approve" 
                               class="btn btn-approve" onclick="return confirm('确定通过审核吗？房源将立即上架。')">通过</a>
                            <a href="${pageContext.request.contextPath}/admin/house/audit?id=<%= house.getId() %>&action=reject" 
                               class="btn btn-reject" onclick="return confirm('确定拒绝该房源吗？')">拒绝</a>
                            <a href="${pageContext.request.contextPath}/user/house/detail?id=<%= house.getId() %>" 
                               class="btn btn-view" target="_blank">预览</a>
                         </tr>
                    <%
                            }
                        } else {
                    %>
                    <tr><td colspan="6" class="empty-state">暂无待审核房源</td></tr>
                    <%
                        }
                    %>
                </tbody>
            </table>
        </div>
        
        <!-- 已上架房源 -->
        <div class="house-section">
            <div class="section-title">
                ✅ 已上架房源
                <span style="font-size:14px; color:#999; margin-left:10px;">共 <%= publishedCount %> 条</span>
            </div>
            <table>
                <thead>
                    <tr>
                        <th>房源图片</th>
                        <th>房源信息</th>
                        <th>房东信息</th>
                        <th>租金</th>
                        <th>上架时间</th>
                        <th>操作</th>
                    </thead>
                <tbody>
                    <%
                        if (publishedList != null && !publishedList.isEmpty()) {
                            for (House house : publishedList) {
                    %>
                    <tr>
                        <td style="width:80px;">
                            <div style="width:70px; height:50px; background-color:#e0e0e0; background-size:cover; background-position:center; border-radius:4px; 
                                <% if (house.getFirstImage() != null) { %>background-image:url('/HRS/<%= house.getFirstImage() %>');<% } %>">
                                <% if (house.getFirstImage() == null) { %>
                                    <div style="display:flex;align-items:center;justify-content:center;height:100%;font-size:12px;">无图</div>
                                <% } %>
                            </div>
                        </td>
                        <td>
                            <strong><%= house.getTitle() %></strong><br>
                            <span style="font-size:12px;color:#999;"><%= house.getArea() %> | <%= house.getHouseType() %></span>
                        </td>
                        <td>
                            <%= house.getLandlordName() != null ? house.getLandlordName() : "未知" %>
                        </td>
                        <td class="price">¥<%= house.getRentPrice() %>/月</td>
                        <td><%= sdf.format(house.getCreateTime()) %></td>
                        <td>
                            <a href="${pageContext.request.contextPath}/admin/house/offline?id=<%= house.getId() %>" 
                               class="btn btn-offline" onclick="return confirm('确定要下架该房源吗？')">下架</a>
                            <a href="${pageContext.request.contextPath}/user/house/detail?id=<%= house.getId() %>" 
                               class="btn btn-view" target="_blank">预览</a>
                        </td>
                    </tr>
                    <%
                            }
                        } else {
                    %>
                    <tr><td colspan="6" class="empty-state">暂无已上架房源</td></tr>
                    <%
                        }
                    %>
                </tbody>
            </table>
        </div>
        
        <!-- 已下架/违规房源 -->
        <div class="house-section">
            <div class="section-title">
                📥 已下架/违规房源
                <span style="font-size:14px; color:#999; margin-left:10px;">共 <%= offlineCount %> 条</span>
            </div>
            <table>
                <thead>
                    <tr>
                        <th>房源图片</th>
                        <th>房源信息</th>
                        <th>房东信息</th>
                        <th>租金</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <%
                        if (offlineList != null && !offlineList.isEmpty()) {
                            for (House house : offlineList) {
                    %>
                    <tr>
                        <td style="width:80px;">
                            <div style="width:70px; height:50px; background-color:#e0e0e0; background-size:cover; background-position:center; border-radius:4px; 
                                <% if (house.getFirstImage() != null) { %>background-image:url('/HRS/<%= house.getFirstImage() %>');<% } %>">
                                <% if (house.getFirstImage() == null) { %>
                                    <div style="display:flex;align-items:center;justify-content:center;height:100%;font-size:12px;">无图</div>
                                <% } %>
                            </div>
                        </td>
                        <td>
                            <strong><%= house.getTitle() %></strong><br>
                            <span style="font-size:12px;color:#999;"><%= house.getArea() %> | <%= house.getHouseType() %></span>
                        </td>
                        <td>
                            <%= house.getLandlordName() != null ? house.getLandlordName() : "未知" %>
                        </td>
                        <td class="price">¥<%= house.getRentPrice() %>/月</td>
                        <td>
                            <span class="status-badge status-offline"><%= house.getStatusText() %></span>
                        </td>
                        <td>
                            <a href="${pageContext.request.contextPath}/admin/house/online?id=<%= house.getId() %>" 
                               class="btn btn-online" onclick="return confirm('确定要重新上架该房源吗？')">重新上架</a>
                            <a href="${pageContext.request.contextPath}/user/house/detail?id=<%= house.getId() %>" 
                               class="btn btn-view" target="_blank">预览</a>
                        </td>
                    </tr>
                    <%
                            }
                        } else {
                    %>
                    <tr><td colspan="6" class="empty-state">暂无已下架房源</td></tr>
                    <%
                        }
                    %>
                </tbody>
            </table>
        </div>
    </div>
    
    <div class="footer">
        <p>© 2026 租房系统 | 让租房更简单</p>
    </div>
</body>
</html>