import { ref } from 'vue'
import { getRoleList as fetchRoleList } from '@/apis/system/role'
import type { LabelValueState } from '@/types/global'

/** 角色模块 */
export function useRole(options?: { onSuccess?: () => void }) {
  const loading = ref(false)
  const roleList = ref<LabelValueState[]>([])

  const getRoleList = async () => {
    try {
      loading.value = true
      const res = await fetchRoleList()
      
      // 将新的角色数据格式转换为表单需要的 LabelValueState 格式
      if (res && res.data) {
        roleList.value = (res.data || []).map(role => ({
          label: role.roleName,
          value: role.roleId,
          disabled: role.status !== '1' // 假设状态为1表示启用
        }))
      }
      
      options?.onSuccess && options.onSuccess()
    } finally {
      loading.value = false
    }
  }
  return { roleList, getRoleList, loading }
}
