<?php
/**
 * HuSNS - 圈子成员管理后台页面
 */
?>
<div class="admin-page">
    <div class="page-header">
        <h2><?php echo $this->escape($circle['name']); ?> - 成员管理</h2>
        <a href="<?php echo Helper::url('admin/circles'); ?>" class="btn">返回圈子列表</a>
    </div>
    
    <table class="data-table">
        <thead>
            <tr>
                <th>用户头像</th>
                <th>用户名</th>
                <th>角色</th>
                <th>加入时间</th>
                <th>最后活跃</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($members as $member): ?>
            <tr>
                <td>
                    <img src="<?php echo $member['avatar'] ? $this->uploadUrl($member['avatar']) : $this->asset('images/default-avatar.png'); ?>" 
                         alt="" style="width: 40px; height: 40px; border-radius: 50%;">
                </td>
                <td><?php echo $this->escape($member['username']); ?></td>
                <td>
                    <select onchange="updateRole(<?php echo $circle['id']; ?>, <?php echo $member['user_id']; ?>, this.value)">
                        <option value="0" <?php echo $member['role'] == 0 ? 'selected' : ''; ?>>普通成员</option>
                        <option value="1" <?php echo $member['role'] == 1 ? 'selected' : ''; ?>>管理员</option>
                        <option value="2" <?php echo $member['role'] == 2 ? 'selected' : ''; ?>>创始人</option>
                    </select>
                </td>
                <td><?php echo date('Y-m-d H:i', $member['joined_at']); ?></td>
                <td>
                    <?php echo $member['last_active_at'] ? date('Y-m-d H:i', $member['last_active_at']) : '-'; ?>
                </td>
                <td>
                    <?php if ($member['role'] != 2): ?>
                    <button class="btn btn-sm btn-danger" 
                            onclick="removeMember(<?php echo $circle['id']; ?>, <?php echo $member['user_id']; ?>)">
                        移除
                    </button>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    
    <?php if (empty($members)): ?>
    <div style="text-align: center; padding: 40px; color: #999;">
        <p>暂无成员</p>
    </div>
    <?php endif; ?>
    
    <?php if ($totalPages > 1): ?>
    <div class="pagination">
        <?php for ($i = 1; $i <= $totalPages; $i++): ?>
        <a href="?r=admin/circles/members&circle_id=<?php echo $circle['id']; ?>&page=<?php echo $i; ?>" class="<?php echo $i == $page ? 'active' : ''; ?>">
            <?php echo $i; ?>
        </a>
        <?php endfor; ?>
    </div>
    <?php endif; ?>
</div>

<script>
function updateRole(circleId, userId, role) {
    $.post('<?php echo Helper::url('admin/circles/updateRole'); ?>', {
        circle_id: circleId,
        user_id: userId,
        role: role,
        csrf_token: '<?php echo $_SESSION['csrf_token'] ?? ''; ?>'
    }, function(res) {
        if (!res.success) {
            alert(res.message);
        }
    }, 'json');
}

function removeMember(circleId, userId) {
    if (confirm('确定要移除该成员吗？')) {
        $.post('<?php echo Helper::url('admin/circles/removeMember'); ?>', {
            circle_id: circleId,
            user_id: userId,
            csrf_token: '<?php echo $_SESSION['csrf_token'] ?? ''; ?>'
        }, function(res) {
            if (res.success) {
                alert(res.message);
                window.location.reload();
            } else {
                alert(res.message);
            }
        }, 'json');
    }
}
</script>