{% extends 'admin/master.html' %}

{% block body %}
<div class="user-detail-container">
    <!-- 返回按钮 -->
    <div class="back-nav">
        <a href="{{ url_for('admin_users') }}" class="btn-back">
            <span class="material-symbols-outlined">arrow_back</span>
            返回用户列表
        </a>
    </div>

    <!-- 用户基本信息卡片 -->
    <div class="card mb-4">
        <div class="card-header">
            <h5 class="mb-0">基本信息</h5>
        </div>
        <div class="card-body">
            <div class="user-profile">
                <div class="user-avatar">
                    {% if user.avatar %}
                    <img src="{{ user.avatar }}" alt="{{ user.username }}">
                    {% else %}
                    <div class="avatar-placeholder">
                        <span class="material-symbols-outlined">person</span>
                    </div>
                    {% endif %}
                </div>
                <div class="user-info-grid">
                    <div class="info-item">
                        <label>用户ID</label>
                        <div class="info-value">{{ user.id }}</div>
                    </div>
                    <div class="info-item">
                        <label>用户名</label>
                        <div class="info-value">
                            <strong>{{ user.username }}</strong>
                            <button class="btn-icon" onclick="showEditUsernameModal()" title="修改昵称">
                                <span class="material-symbols-outlined">edit</span>
                            </button>
                        </div>
                    </div>
                    <div class="info-item">
                        <label>邮箱</label>
                        <div class="info-value">
                            {% if user.email %}
                            {{ user.email }}
                            {% if user.email_verified %}
                            <span class="badge badge-success-sm">
                                <span class="material-symbols-outlined">verified</span>
                                已验证
                            </span>
                            {% else %}
                            <span class="badge badge-warning-sm">未验证</span>
                            {% endif %}
                            {% else %}
                            <span class="text-muted">未设置</span>
                            {% endif %}
                        </div>
                    </div>
                    <div class="info-item">
                        <label>注册时间</label>
                        <div class="info-value">{{ user.created_at.strftime('%Y-%m-%d %H:%M:%S') if user.created_at else '-' }}</div>
                    </div>
                    <div class="info-item">
                        <label>最后登录</label>
                        <div class="info-value">{{ user.last_login.strftime('%Y-%m-%d %H:%M:%S') if user.last_login else '从未登录' }}</div>
                    </div>
                    <div class="info-item">
                        <label>账户状态</label>
                        <div class="info-value">
                            {% if user.is_active %}
                            <span class="badge badge-success">正常</span>
                            {% else %}
                            <span class="badge badge-secondary">已禁用</span>
                            {% endif %}
                        </div>
                    </div>
                    <div class="info-item">
                        <label>个人简介</label>
                        <div class="info-value">{{ user.bio if user.bio else '暂无' }}</div>
                    </div>
                    <div class="info-item">
                        <label>资料公开</label>
                        <div class="info-value">
                            {% if user.is_public_profile %}
                            <span class="badge badge-info-sm">公开</span>
                            {% else %}
                            <span class="badge badge-secondary-sm">私密</span>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 管理操作卡片 -->
    <div class="card mb-4">
        <div class="card-header">
            <h5 class="mb-0">管理操作</h5>
        </div>
        <div class="card-body">
            <div class="action-buttons">
                <button class="btn btn-warning" onclick="showResetPasswordModal()">
                    <span class="material-symbols-outlined">lock_reset</span>
                    重置密码
                </button>
            </div>
        </div>
    </div>

    <!-- 收藏统计卡片 -->
    <div class="row mb-4">
        <div class="col-md-6">
            <div class="stat-card">
                <div class="stat-icon" style="background: rgba(0, 82, 217, 0.1); color: #0052D9;">
                    <span class="material-symbols-outlined">bookmark</span>
                </div>
                <div class="stat-info">
                    <div class="stat-value">{{ collections_count }}</div>
                    <div class="stat-label">收藏的工具</div>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="stat-card">
                <div class="stat-icon" style="background: rgba(0, 168, 112, 0.1); color: #00A870;">
                    <span class="material-symbols-outlined">folder</span>
                </div>
                <div class="stat-info">
                    <div class="stat-value">{{ folders_count }}</div>
                    <div class="stat-label">收藏分组</div>
                </div>
            </div>
        </div>
    </div>

    <!-- 收藏分组列表 -->
    {% if folders %}
    <div class="card mb-4">
        <div class="card-header">
            <h5 class="mb-0">收藏分组 ({{ folders_count }})</h5>
        </div>
        <div class="card-body">
            <div class="folders-grid">
                {% for folder in folders %}
                <div class="folder-item">
                    <div class="folder-icon">{{ folder.icon }}</div>
                    <div class="folder-info">
                        <div class="folder-name">{{ folder.name }}</div>
                        <div class="folder-meta">
                            <span>{{ folder.collections.count() }} 个工具</span>
                            {% if folder.is_public %}
                            <span class="badge badge-info-sm">公开</span>
                            {% endif %}
                        </div>
                    </div>
                </div>
                {% endfor %}
            </div>
        </div>
    </div>
    {% endif %}

    <!-- 最近收藏 -->
    {% if recent_collections %}
    <div class="card">
        <div class="card-header">
            <h5 class="mb-0">最近收藏 (最多显示10条)</h5>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table mb-0">
                    <thead>
                        <tr>
                            <th>工具名称</th>
                            <th>所属分组</th>
                            <th>收藏时间</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for collection in recent_collections %}
                        <tr>
                            <td>
                                <div class="site-info">
                                    {% if collection.site.logo %}
                                    <img src="{{ collection.site.logo }}" alt="{{ collection.site.name }}" class="site-logo">
                                    {% endif %}
                                    <a href="/site/{{ collection.site.code }}" target="_blank">{{ collection.site.name }}</a>
                                </div>
                            </td>
                            <td>
                                {% if collection.folder %}
                                <span class="folder-badge">{{ collection.folder.icon }} {{ collection.folder.name }}</span>
                                {% else %}
                                <span class="text-muted">未分组</span>
                                {% endif %}
                            </td>
                            <td>{{ collection.created_at.strftime('%Y-%m-%d %H:%M') }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    {% endif %}
</div>

<!-- 重置密码弹窗 -->
<div id="resetPasswordModal" class="modal">
    <div class="modal-content">
        <div class="modal-header">
            <h5>重置用户密码</h5>
            <button class="close-btn" onclick="closeResetPasswordModal()">
                <span class="material-symbols-outlined">close</span>
            </button>
        </div>
        <div class="modal-body">
            <p class="text-muted mb-3">为用户 <strong>{{ user.username }}</strong> 设置新密码</p>
            <div class="form-group">
                <label>新密码 <span class="text-danger">*</span></label>
                <input type="password" id="newPassword" class="form-control" placeholder="至少6位字符">
            </div>
            <div class="form-group">
                <label>确认密码 <span class="text-danger">*</span></label>
                <input type="password" id="confirmPassword" class="form-control" placeholder="再次输入新密码">
            </div>
            <div id="resetPasswordError" class="alert alert-danger" style="display: none;"></div>
        </div>
        <div class="modal-footer">
            <button class="btn btn-secondary" onclick="closeResetPasswordModal()">取消</button>
            <button class="btn btn-warning" onclick="submitResetPassword()">确认重置</button>
        </div>
    </div>
</div>

<!-- 修改昵称弹窗 -->
<div id="editUsernameModal" class="modal">
    <div class="modal-content">
        <div class="modal-header">
            <h5>修改用户昵称</h5>
            <button class="close-btn" onclick="closeEditUsernameModal()">
                <span class="material-symbols-outlined">close</span>
            </button>
        </div>
        <div class="modal-body">
            <div class="form-group">
                <label>当前昵称</label>
                <input type="text" class="form-control" value="{{ user.username }}" disabled>
            </div>
            <div class="form-group">
                <label>新昵称 <span class="text-danger">*</span></label>
                <input type="text" id="newUsername" class="form-control" placeholder="2-50个字符" value="{{ user.username }}">
            </div>
            <div id="editUsernameError" class="alert alert-danger" style="display: none;"></div>
        </div>
        <div class="modal-footer">
            <button class="btn btn-secondary" onclick="closeEditUsernameModal()">取消</button>
            <button class="btn btn-primary" onclick="submitEditUsername()">确认修改</button>
        </div>
    </div>
</div>

<script>
// 重置密码弹窗
function showResetPasswordModal() {
    document.getElementById('resetPasswordModal').style.display = 'flex';
    document.getElementById('newPassword').value = '';
    document.getElementById('confirmPassword').value = '';
    document.getElementById('resetPasswordError').style.display = 'none';
}

function closeResetPasswordModal() {
    document.getElementById('resetPasswordModal').style.display = 'none';
}

function submitResetPassword() {
    const newPassword = document.getElementById('newPassword').value.trim();
    const confirmPassword = document.getElementById('confirmPassword').value.trim();
    const errorDiv = document.getElementById('resetPasswordError');

    if (!newPassword) {
        errorDiv.textContent = '请输入新密码';
        errorDiv.style.display = 'block';
        return;
    }

    if (newPassword.length < 6) {
        errorDiv.textContent = '密码至少需要6位字符';
        errorDiv.style.display = 'block';
        return;
    }

    if (newPassword !== confirmPassword) {
        errorDiv.textContent = '两次输入的密码不一致';
        errorDiv.style.display = 'block';
        return;
    }

    fetch('/api/admin/users/{{ user.id }}/reset-password', {
        method: 'POST',
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({ new_password: newPassword })
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            alert(data.message);
            closeResetPasswordModal();
        } else {
            errorDiv.textContent = data.message;
            errorDiv.style.display = 'block';
        }
    })
    .catch(error => {
        errorDiv.textContent = '操作失败，请重试';
        errorDiv.style.display = 'block';
    });
}

// 修改昵称弹窗
function showEditUsernameModal() {
    document.getElementById('editUsernameModal').style.display = 'flex';
    document.getElementById('editUsernameError').style.display = 'none';
}

function closeEditUsernameModal() {
    document.getElementById('editUsernameModal').style.display = 'none';
}

function submitEditUsername() {
    const newUsername = document.getElementById('newUsername').value.trim();
    const errorDiv = document.getElementById('editUsernameError');

    if (!newUsername) {
        errorDiv.textContent = '请输入新昵称';
        errorDiv.style.display = 'block';
        return;
    }

    if (newUsername.length < 2 || newUsername.length > 50) {
        errorDiv.textContent = '昵称长度需要在2-50个字符之间';
        errorDiv.style.display = 'block';
        return;
    }

    fetch('/api/admin/users/{{ user.id }}/update-username', {
        method: 'POST',
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({ new_username: newUsername })
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            alert(data.message);
            location.reload();
        } else {
            errorDiv.textContent = data.message;
            errorDiv.style.display = 'block';
        }
    })
    .catch(error => {
        errorDiv.textContent = '操作失败，请重试';
        errorDiv.style.display = 'block';
    });
}

window.onclick = function(event) {
    const resetModal = document.getElementById('resetPasswordModal');
    const editModal = document.getElementById('editUsernameModal');
    if (event.target === resetModal) closeResetPasswordModal();
    if (event.target === editModal) closeEditUsernameModal();
}
</script>

<style>
.user-detail-container {
    max-width: 1200px;
}

.back-nav {
    margin-bottom: 20px;
}

.btn-back {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    background: #F5F7FA;
    border: 1px solid #DCDFE6;
    border-radius: 4px;
    color: #606266;
    text-decoration: none;
    font-size: 14px;
    transition: all 0.2s;
}

.btn-back:hover {
    background: #ECF2FE;
    border-color: #0052D9;
    color: #0052D9;
    text-decoration: none;
}

.btn-back .material-symbols-outlined {
    font-size: 18px;
}

.card {
    background: white;
    border: 1px solid #DCDFE6;
    border-radius: 6px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, .05);
}

.card-header {
    padding: 16px 20px;
    border-bottom: 1px solid #F0F0F0;
}

.card-header h5 {
    font-size: 16px;
    font-weight: 600;
    color: #000000;
    margin: 0;
}

.card-body {
    padding: 20px;
}

.mb-4 {
    margin-bottom: 24px;
}

.user-profile {
    display: flex;
    gap: 24px;
}

.user-avatar {
    flex-shrink: 0;
}

.user-avatar img {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    object-fit: cover;
}

.avatar-placeholder {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    background: #F5F7FA;
    display: flex;
    align-items: center;
    justify-content: center;
}

.avatar-placeholder .material-symbols-outlined {
    font-size: 48px;
    color: #C0C4CC;
}

.user-info-grid {
    flex: 1;
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
}

.info-item label {
    display: block;
    font-size: 13px;
    color: #909399;
    margin-bottom: 6px;
}

.info-value {
    font-size: 14px;
    color: #303133;
    display: flex;
    align-items: center;
    gap: 8px;
}

.btn-icon {
    background: none;
    border: none;
    padding: 4px;
    cursor: pointer;
    color: #909399;
    display: inline-flex;
    align-items: center;
    border-radius: 4px;
    transition: all 0.2s;
}

.btn-icon:hover {
    background: #F5F7FA;
    color: #0052D9;
}

.btn-icon .material-symbols-outlined {
    font-size: 18px;
}

.text-muted {
    color: #909399;
}

.badge {
    padding: 4px 8px;
    font-size: 12px;
    border-radius: 4px;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

.badge-success {
    background: rgba(0, 168, 112, 0.1);
    color: #00A870;
}

.badge-success-sm {
    background: rgba(0, 168, 112, 0.1);
    color: #00A870;
    padding: 2px 6px;
}

.badge-success-sm .material-symbols-outlined {
    font-size: 14px;
}

.badge-warning-sm {
    background: rgba(227, 115, 24, 0.1);
    color: #E37318;
    padding: 2px 6px;
}

.badge-secondary {
    background: #F5F5F5;
    color: #606266;
}

.badge-secondary-sm {
    background: #F5F5F5;
    color: #606266;
    padding: 2px 6px;
}

.badge-info-sm {
    background: rgba(0, 82, 217, 0.1);
    color: #0052D9;
    padding: 2px 6px;
}

.row {
    display: flex;
    gap: 16px;
    margin: 0 -8px;
}

.col-md-6 {
    flex: 1;
    padding: 0 8px;
}

.stat-card {
    background: white;
    border: 1px solid #DCDFE6;
    border-radius: 6px;
    padding: 20px;
    display: flex;
    align-items: center;
    gap: 16px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, .05);
}

.stat-icon {
    width: 56px;
    height: 56px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.stat-icon .material-symbols-outlined {
    font-size: 28px;
}

.stat-info {
    flex: 1;
}

.stat-value {
    font-size: 28px;
    font-weight: 600;
    color: #000000;
    line-height: 1;
    margin-bottom: 4px;
}

.stat-label {
    font-size: 14px;
    color: #606266;
}

.action-buttons {
    display: flex;
    gap: 12px;
}

.btn {
    padding: 10px 20px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    transition: all 0.2s;
}

.btn-warning {
    background: #E37318;
    color: white;
}

.btn-warning:hover {
    background: #C96316;
}

.btn .material-symbols-outlined {
    font-size: 18px;
}

.folders-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 12px;
}

.folder-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    background: #F5F7FA;
    border: 1px solid #DCDFE6;
    border-radius: 6px;
}

.folder-icon {
    font-size: 24px;
}

.folder-info {
    flex: 1;
}

.folder-name {
    font-size: 14px;
    font-weight: 500;
    color: #303133;
    margin-bottom: 4px;
}

.folder-meta {
    font-size: 12px;
    color: #909399;
    display: flex;
    align-items: center;
    gap: 8px;
}

.p-0 {
    padding: 0;
}

.table-responsive {
    overflow-x: auto;
}

.table {
    width: 100%;
}

.table thead th {
    background: #F5F7FA;
    color: #606266;
    font-weight: 600;
    font-size: 14px;
    padding: 12px 16px;
    border-bottom: 1px solid #DCDFE6;
}

.table tbody td {
    padding: 12px 16px;
    border-bottom: 1px solid #F0F0F0;
    font-size: 14px;
    color: #303133;
}

.table tbody tr:hover {
    background: #F5F7FA;
}

.site-info {
    display: flex;
    align-items: center;
    gap: 8px;
}

.site-logo {
    width: 24px;
    height: 24px;
    border-radius: 4px;
    object-fit: cover;
}

.site-info a {
    color: #0052D9;
    text-decoration: none;
}

.site-info a:hover {
    text-decoration: underline;
}

.folder-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 8px;
    background: #F5F7FA;
    border-radius: 4px;
    font-size: 13px;
    color: #606266;
}

/* 弹窗样式 */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    align-items: center;
    justify-content: center;
}

.modal-content {
    background-color: white;
    border-radius: 8px;
    width: 90%;
    max-width: 500px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    border-bottom: 1px solid #F0F0F0;
}

.modal-header h5 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    color: #000000;
}

.close-btn {
    background: none;
    border: none;
    padding: 4px;
    cursor: pointer;
    color: #909399;
    display: flex;
    align-items: center;
    border-radius: 4px;
    transition: all 0.2s;
}

.close-btn:hover {
    background: #F5F7FA;
    color: #303133;
}

.modal-body {
    padding: 20px;
}

.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    font-size: 14px;
    color: #303133;
    margin-bottom: 8px;
    font-weight: 500;
}

.form-control {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #DCDFE6;
    border-radius: 4px;
    font-size: 14px;
    box-sizing: border-box;
}

.form-control:focus {
    outline: none;
    border-color: #0052D9;
}

.form-control:disabled {
    background: #F5F7FA;
    color: #909399;
    cursor: not-allowed;
}

.text-danger {
    color: #D54941;
}

.alert {
    padding: 12px;
    border-radius: 4px;
    font-size: 14px;
    margin-top: 12px;
}

.alert-danger {
    background: rgba(213, 73, 65, 0.1);
    color: #D54941;
    border: 1px solid rgba(213, 73, 65, 0.2);
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    padding: 16px 20px;
    border-top: 1px solid #F0F0F0;
}

.btn-secondary {
    background: #F5F5F5;
    color: #606266;
}

.btn-secondary:hover {
    background: #E5E5E5;
}

.btn-primary {
    background: #0052D9;
    color: white;
}

.btn-primary:hover {
    background: #0041A8;
}
</style>
{% endblock %}
