<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.List, 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<User> tenants = (List<User>) request.getAttribute("tenants");
    List<User> landlords = (List<User>) request.getAttribute("landlords");
    List<User> admins = (List<User>) request.getAttribute("admins");
    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: #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 {
            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; }
        }
        
        .user-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-active {
            background-color: #e8f5e9;
            color: #4CAF50;
        }
        
        .status-disabled {
            background-color: #ffebee;
            color: #f44336;
        }
        
        .status-pending {
            background-color: #fff3e0;
            color: #f39c12;
        }
        
        .btn {
            padding: 4px 12px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
            text-decoration: none;
            display: inline-block;
            margin: 0 3px;
        }
        
        .btn-enable {
            background-color: #4CAF50;
            color: white;
        }
        
        .btn-disable {
            background-color: #f44336;
            color: white;
        }
        
        .btn-delete {
            background-color: #999;
            color: white;
        }
        
        .btn-enable:hover {
            background-color: #45a049;
        }
        
        .btn-disable:hover {
            background-color: #d32f2f;
        }
        
        .btn-delete:hover {
            background-color: #777;
        }
        
        .empty-state {
            text-align: center;
            padding: 40px;
            color: #999;
        }
        
        .footer {
            background-color: #333;
            color: white;
            text-align: center;
            padding: 20px;
            margin-top: 40px;
        }
        
        .clearfix::after {
            content: "";
            clear: both;
            display: table;
        }
        
        .role-icon {
            font-size: 18px;
            margin-right: 5px;
        }
    </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" class="active">用户管理</a>
                <a href="${pageContext.request.contextPath}/admin/house/list">房源审核</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/user/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> 个注册用户
        </div>
        
        <!-- 租客列表 -->
        <div class="user-section">
            <div class="section-title">
                <span class="role-icon">👤</span> 租客列表
                <span style="font-size:14px; color:#999; margin-left:10px;">共 <%= tenants != null ? tenants.size() : 0 %> 人</span>
            </div>
            <table>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>用户名</th>
                        <th>真实姓名</th>
                        <th>手机号</th>
                        <th>状态</th>
                        <th>注册时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <%
                        if (tenants != null && !tenants.isEmpty()) {
                            for (User user : tenants) {
                    %>
                    <tr>
                        <td><%= user.getId() %></td>
                        <td><%= user.getUserName() %></td>
                        <td><%= user.getRealName() != null ? user.getRealName() : "-" %></td>
                        <td><%= user.getPhone() != null ? user.getPhone() : "-" %></td>
                        <td>
                            <span class="status-badge <%= "1".equals(user.getStatus()) ? "status-active" : "status-disabled" %>">
                                <%= "1".equals(user.getStatus()) ? "已认证" : "未认证/禁用" %>
                            </span>
                        </td>
                        <td><%= new java.text.SimpleDateFormat("yyyy-MM-dd").format(user.getCreateTime()) %></td>
                        <td>
                            <% if ("1".equals(user.getStatus())) { %>
                                <a href="${pageContext.request.contextPath}/admin/user/status?id=<%= user.getId() %>&action=disable" 
                                   class="btn btn-disable" onclick="return confirm('确定要禁用该用户吗？')">禁用</a>
                            <% } else { %>
                                <a href="${pageContext.request.contextPath}/admin/user/status?id=<%= user.getId() %>&action=enable" 
                                   class="btn btn-enable" onclick="return confirm('确定要启用该用户吗？')">启用</a>
                            <% } %>
                            <a href="${pageContext.request.contextPath}/admin/user/delete?id=<%= user.getId() %>" 
                               class="btn btn-delete" 
                               onclick="return confirm('确定要删除该用户吗？此操作不可恢复！')">删除</a>
                        </td>
                    </tr>
                    <%
                            }
                        } else {
                    %>
                    <tr>
                        <td colspan="7" class="empty-state">暂无租客数据</td>
                    </tr>
                    <%
                        }
                    %>
                </tbody>
            </table>
        </div>
        
        <!-- 房东列表 -->
        <div class="user-section">
            <div class="section-title">
                <span class="role-icon">🏠</span> 房东列表
                <span style="font-size:14px; color:#999; margin-left:10px;">共 <%= landlords != null ? landlords.size() : 0 %> 人</span>
            </div>
            <table>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>用户名</th>
                        <th>真实姓名</th>
                        <th>手机号</th>
                        <th>状态</th>
                        <th>注册时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <%
                        if (landlords != null && !landlords.isEmpty()) {
                            for (User user : landlords) {
                    %>
                    <tr>
                        <td><%= user.getId() %></td>
                        <td><%= user.getUserName() %></td>
                        <td><%= user.getRealName() != null ? user.getRealName() : "-" %></td>
                        <td><%= user.getPhone() != null ? user.getPhone() : "-" %></td>
                        <td>
                            <span class="status-badge <%= "1".equals(user.getStatus()) ? "status-active" : "status-disabled" %>">
                                <%= "1".equals(user.getStatus()) ? "已认证" : "未认证/禁用" %>
                            </span>
                        </td>
                        <td><%= new java.text.SimpleDateFormat("yyyy-MM-dd").format(user.getCreateTime()) %></td>
                        <td>
                            <% if ("1".equals(user.getStatus())) { %>
                                <a href="${pageContext.request.contextPath}/admin/user/status?id=<%= user.getId() %>&action=disable" 
                                   class="btn btn-disable" onclick="return confirm('确定要禁用该用户吗？')">禁用</a>
                            <% } else { %>
                                <a href="${pageContext.request.contextPath}/admin/user/status?id=<%= user.getId() %>&action=enable" 
                                   class="btn btn-enable" onclick="return confirm('确定要启用该用户吗？')">启用</a>
                            <% } %>
                            <a href="${pageContext.request.contextPath}/admin/user/delete?id=<%= user.getId() %>" 
                               class="btn btn-delete" 
                               onclick="return confirm('确定要删除该用户吗？此操作不可恢复！')">删除</a>
                        </td>
                    </tr>
                    <%
                            }
                        } else {
                    %>
                    <tr>
                        <td colspan="7" class="empty-state">暂无房东数据</td>
                    </tr>
                    <%
                        }
                    %>
                </tbody>
            </table>
        </div>
        
        <!-- 管理员列表 -->
        <div class="user-section">
            <div class="section-title">
                <span class="role-icon">👑</span> 管理员列表
                <span style="font-size:14px; color:#999; margin-left:10px;">共 <%= admins != null ? admins.size() : 0 %> 人</span>
            </div>
            <table>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>用户名</th>
                        <th>真实姓名</th>
                        <th>手机号</th>
                        <th>状态</th>
                        <th>注册时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <%
                        if (admins != null && !admins.isEmpty()) {
                            for (User user : admins) {
                    %>
                    <tr>
                        <td><%= user.getId() %></td>
                        <td><%= user.getUserName() %></td>
                        <td><%= user.getRealName() != null ? user.getRealName() : "-" %></td>
                        <td><%= user.getPhone() != null ? user.getPhone() : "-" %></td>
                        <td>
                            <span class="status-badge status-active">管理员</span>
                        </td>
                        <td><%= new java.text.SimpleDateFormat("yyyy-MM-dd").format(user.getCreateTime()) %></td>
                        <td>
                            <span style="color:#999;">不可操作</span>
                        </td>
                    </tr>
                    <%
                            }
                        } else {
                    %>
                    <tr>
                        <td colspan="7" class="empty-state">暂无管理员数据</td>
                    </tr>
                    <%
                        }
                    %>
                </tbody>
            </table>
        </div>
    </div>
    
    <div class="footer">
        <p>© 2026 租房系统 | 让租房更简单</p>
    </div>
</body>
</html>