<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org" th:replace="~{layout :: layout(~{::title}, ~{::content})}">
<head>
    <title>用户管理</title>
</head>
<body>
    <div th:fragment="content">
        <!-- Page Actions -->
        <div th:fragment="page-actions">
            <a th:href="@{/users/new}" class="btn btn-primary">
                <i class="fas fa-plus me-2"></i>新增用户
            </a>
        </div>
        
        <!-- Users Table -->
        <div class="card">
            <div class="card-header">
                <div class="row align-items-center">
                    <div class="col">
                        <h5 class="mb-0">
                            <i class="fas fa-users me-2"></i>用户列表
                        </h5>
                    </div>
                    <div class="col-auto">
                        <div class="input-group" style="max-width: 300px;">
                            <input type="text" class="form-control" placeholder="搜索用户..." id="searchInput">
                            <button class="btn btn-outline-secondary" type="button">
                                <i class="fas fa-search"></i>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0" id="usersTable">
                        <thead>
                            <tr>
                                <th width="80">ID</th>
                                <th>用户名</th>
                                <th>邮箱</th>
                                <th width="120">角色</th>
                                <th width="120">状态</th>
                                <th width="200">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr th:each="u : ${users}" class="user-row">
                                <td th:text="${u.id}">1</td>
                                <td>
                                    <div class="d-flex align-items-center">
                                        <div class="avatar-circle me-2">
                                            <i class="fas fa-user"></i>
                                        </div>
                                        <span th:text="${u.username}">user</span>
                                    </div>
                                </td>
                                <td th:text="${u.email}">email</td>
                                <td>
                                    <span th:if="${u.role == 'ROLE_USER'}" class="badge bg-secondary">普通用户</span>
                                    <span th:if="${u.role == 'ROLE_MEMBER'}" class="badge bg-info">会员用户</span>
                                    <span th:if="${u.role == 'ROLE_ADMIN'}" class="badge bg-danger">管理员</span>
                                    <span th:if="${u.role != 'ROLE_USER' and u.role != 'ROLE_MEMBER' and u.role != 'ROLE_ADMIN'}" 
                                          class="badge bg-warning" th:text="${u.role}">其他</span>
                                </td>
                                <td>
                                    <span class="badge bg-success">正常</span>
                                </td>
                                <td>
                                    <div class="btn-group" role="group">
                                        <a th:href="@{|/users/${u.id}/edit|}" class="btn btn-sm btn-outline-primary">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <button type="button" class="btn btn-sm btn-outline-info" 
                                                th:onclick="'showUserDetails(' + ${u.id} + ')'">
                                            <i class="fas fa-eye"></i>
                                        </button>
                                        <form th:action="@{|/users/${u.id}/delete|}" method="post" class="d-inline">
                                            <button type="submit" class="btn btn-sm btn-outline-danger" 
                                                    th:onclick="'return confirmDelete(\'' + ${u.username} + '\')'">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
            
            <div class="card-footer">
                <div class="row align-items-center">
                    <div class="col">
                        <small class="text-muted">
                            共 <span th:text="${users.size()}">0</span> 个用户
                        </small>
                    </div>
                    <div class="col-auto">
                        <nav aria-label="用户分页">
                            <ul class="pagination pagination-sm mb-0">
                                <li class="page-item disabled">
                                    <span class="page-link">上一页</span>
                                </li>
                                <li class="page-item active">
                                    <span class="page-link">1</span>
                                </li>
                                <li class="page-item disabled">
                                    <span class="page-link">下一页</span>
                                </li>
                            </ul>
                        </nav>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- User Details Modal -->
        <div class="modal fade" id="userDetailsModal" tabindex="-1">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">
                            <i class="fas fa-user me-2"></i>用户详情
                        </h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>
                    <div class="modal-body" id="userDetailsContent">
                        <!-- User details will be loaded here -->
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <style>
        .avatar-circle {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 0.875rem;
        }
        
        .user-row:hover {
            background-color: #f8f9fa;
        }
        
        .btn-group .btn {
            margin-right: 0.25rem;
        }
        
        .btn-group .btn:last-child {
            margin-right: 0;
        }
        
        .table th {
            border-top: none;
            font-weight: 600;
            color: #495057;
        }
        
        .card-header {
            background-color: #f8f9fa;
            border-bottom: 1px solid #dee2e6;
        }
        
        .card-footer {
            background-color: #f8f9fa;
            border-top: 1px solid #dee2e6;
        }
    </style>
    
    <script>
        // Search functionality
        document.getElementById('searchInput').addEventListener('input', function() {
            const searchTerm = this.value.toLowerCase();
            const rows = document.querySelectorAll('.user-row');
            
            rows.forEach(function(row) {
                const username = row.cells[1].textContent.toLowerCase();
                const email = row.cells[2].textContent.toLowerCase();
                
                if (username.includes(searchTerm) || email.includes(searchTerm)) {
                    row.style.display = '';
                } else {
                    row.style.display = 'none';
                }
            });
        });
        
        // Show user details
        function showUserDetails(userId) {
            // In a real application, you would fetch user details via AJAX
            const modal = new bootstrap.Modal(document.getElementById('userDetailsModal'));
            const content = document.getElementById('userDetailsContent');
            
            // For demo purposes, show static content
            content.innerHTML = `
                <div class="row">
                    <div class="col-md-4">
                        <div class="text-center">
                            <div class="avatar-circle mx-auto mb-3" style="width: 80px; height: 80px; font-size: 2rem;">
                                <i class="fas fa-user"></i>
                            </div>
                            <h6>用户ID: ${userId}</h6>
                        </div>
                    </div>
                    <div class="col-md-8">
                        <table class="table table-sm">
                            <tr>
                                <td><strong>用户名:</strong></td>
                                <td>demo_user</td>
                            </tr>
                            <tr>
                                <td><strong>邮箱:</strong></td>
                                <td>demo@example.com</td>
                            </tr>
                            <tr>
                                <td><strong>角色:</strong></td>
                                <td><span class="badge bg-secondary">普通用户</span></td>
                            </tr>
                            <tr>
                                <td><strong>注册时间:</strong></td>
                                <td>2024-01-01 10:00:00</td>
                            </tr>
                            <tr>
                                <td><strong>最后登录:</strong></td>
                                <td>2024-01-15 14:30:00</td>
                            </tr>
                        </table>
                    </div>
                </div>
            `;
            
            modal.show();
        }
        
        // Confirm delete
        function confirmDelete(username) {
            return confirm(`确定要删除用户 "${username}" 吗？此操作不可撤销。`);
        }
        
        // Auto-refresh table every 30 seconds
        setInterval(function() {
            // In a real application, you would refresh the table data
            console.log('Auto-refreshing user table...');
        }, 30000);
    </script>
</body>
</html>



