<?php
/**
 * HuSNS - 角色管理后台页面
 */
?>
<div class="admin-content">
    <div class="admin-header">
        <h1>角色管理</h1>
        <button class="btn btn-primary" data-toggle="modal" data-target="#addModal">
            <i class="fa fa-plus"></i> 添加角色
        </button>
    </div>
    
    <!-- 角色列表 -->
    <div class="admin-table">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>角色名称</th>
                    <th>描述</th>
                    <th>权限数量</th>
                    <th>状态</th>
                    <th>创建时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($roles as $role): ?>
                <tr>
                    <td><?php echo htmlspecialchars($role['name']); ?></td>
                    <td><?php echo htmlspecialchars($role['description']); ?></td>
                    <td><?php echo count($role['permissions']); ?></td>
                    <td>
                        <?php if ($role['status']): ?>
                        <span class="badge badge-success">启用</span>
                        <?php else: ?>
                        <span class="badge badge-secondary">禁用</span>
                        <?php endif; ?>
                    </td>
                    <td><?php echo date('Y-m-d H:i', $role['created_at']); ?></td>
                    <td>
                        <div class="btn-group">
                            <button class="btn btn-sm btn-default" onclick="editRole(<?php echo $role['id']; ?>)">
                                <i class="fa fa-edit"></i> 编辑
                            </button>
                            <?php if ($role['id'] != 1): ?>
                            <button class="btn btn-sm btn-danger" onclick="deleteRole(<?php echo $role['id']; ?>)">
                                <i class="fa fa-trash"></i> 删除
                            </button>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        
        <?php if (empty($roles)): ?>
        <div class="empty-state">
            <i class="fa fa-users"></i>
            <p>暂无角色数据</p>
        </div>
        <?php endif; ?>
    </div>
</div>

<!-- 添加/编辑弹窗 -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="modalTitle">添加角色</h5>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">
                <form id="roleForm">
                    <input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
                    <input type="hidden" name="id" id="roleId">
                    
                    <div class="form-group">
                        <label>角色名称 *</label>
                        <input type="text" name="name" id="roleName" class="form-control" required>
                    </div>
                    
                    <div class="form-group">
                        <label>角色描述</label>
                        <textarea name="description" id="roleDescription" class="form-control" rows="2"></textarea>
                    </div>
                    
                    <div class="form-group">
                        <label>权限设置</label>
                        <div class="permission-tree">
                            <?php foreach ($permissions as $category => $data): ?>
                            <div class="permission-category">
                                <label>
                                    <input type="checkbox" class="category-checkbox" data-category="<?php echo $category; ?>">
                                    <?php echo $data['name']; ?>
                                </label>
                                <div class="permission-items">
                                    <?php foreach ($data['permissions'] as $key => $name): ?>
                                    <label>
                                        <input type="checkbox" name="permissions[]" value="<?php echo $key; ?>">
                                        <?php echo $name; ?>
                                    </label>
                                    <?php endforeach; ?>
                                </div>
                            </div>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="saveRole()">保存</button>
            </div>
        </div>
    </div>
</div>

<script>
function editRole(id) {
    $.get('<?php echo Helper::url('admin/roles/get'); ?>?id=' + id, function(res) {
        if (res.success) {
            $('#roleId').val(res.data.id);
            $('#roleName').val(res.data.name);
            $('#roleDescription').val(res.data.description);
            
            // 清除所有权限选中状态
            $('input[name="permissions[]"]').prop('checked', false);
            
            // 设置权限选中状态
            if (res.data.permissions && res.data.permissions.length > 0) {
                res.data.permissions.forEach(function(perm) {
                    $('input[name="permissions[]"][value="' + perm + '"]').prop('checked', true);
                });
            }
            
            $('#modalTitle').text('编辑角色');
            $('#addModal').modal('show');
        } else {
            alert(res.message);
        }
    }, 'json');
}

function saveRole() {
    $.post('<?php echo Helper::url('admin/roles/save'); ?>', $('#roleForm').serialize(), function(res) {
        if (res.success) {
            alert(res.message);
            window.location.reload();
        } else {
            alert(res.message);
        }
    }, 'json');
    
    $('#addModal').modal('hide');
}

function deleteRole(id) {
    if (!confirm('确定要删除这个角色吗？')) {
        return;
    }
    
    $.post('<?php echo Helper::url('admin/roles/delete'); ?>', {
        id: id,
        csrf_token: '<?php echo $_SESSION['csrf_token']; ?>'
    }, function(res) {
        if (res.success) {
            alert(res.message);
            window.location.reload();
        } else {
            alert(res.message);
        }
    }, 'json');
}

$(function() {
    // 全选/取消全选分类权限
    $('.category-checkbox').change(function() {
        var category = $(this).data('category');
        var checked = $(this).prop('checked');
        
        $(this).closest('.permission-category').find('input[name="permissions[]"]').prop('checked', checked);
    });
    
    // 点击权限项时同步分类全选状态
    $('input[name="permissions[]"]').change(function() {
        var categoryDiv = $(this).closest('.permission-category');
        var allChecked = categoryDiv.find('input[name="permissions[]"]').length === 
                         categoryDiv.find('input[name="permissions[]"]:checked').length;
        
        categoryDiv.find('.category-checkbox').prop('checked', allChecked);
    });
    
    $('#addModal').on('hidden.bs.modal', function() {
        $('#roleId').val('');
        $('#roleName').val('');
        $('#roleDescription').val('');
        $('input[name="permissions[]"]').prop('checked', false);
        $('.category-checkbox').prop('checked', false);
        $('#modalTitle').text('添加角色');
    });
});
</script>
