<template>
  <sd-list-picker
    v-bind="pickerMixinProps"
    :load-list-data="loadListData"
    :option-value="optionValue"
    v-on="$listeners"
  >
    <a-icon slot="suffixIcon" type="sd-user" />
  </sd-list-picker>
</template>

<script>
import GroupUserService from '@/addressbook/group-user-service'
import pickerMixin from './sd-picker/picker-mixin'
import components from './_import-components/sd-role-picker-import'
/**
 * 角色选择器
 * @displayName SdRolePicker 角色选择器
 */
export default {
  name: 'SdRolePicker',
  metaInfo: {
    title: 'SdRolePicker',
  },
  components,
  mixins: [pickerMixin],
  props: {
    optionValue: {
      type: String,
      default: 'code',
    },
    /**
     * 分级机构id，获取指定分级下的角色时传此参数
     * @since 8.1.0.23
     */
    belongedOrgId: {
      type: String,
      default: undefined,
    },
  },
  data() {
    return {
      roleInfo: [],
      innerOptionValue: this.optionValue,
    }
  },
  mounted() {
    const params = {}
    if (this.belongedOrgId) {
      params.belongedOrgId = this.belongedOrgId
    }
    GroupUserService.getRoles(params).then((val) => {
      val.map((item) => {
        if (this.innerOptionValue === 'id') {
          this.roleInfo.push({
            id: item.code,
            name: item.name,
          })
          return
        }
        this.roleInfo.push({
          type: 'Role',
          code: item.code,
          name: item.name,
          attrNames: [],
        })
      })
    })
  },
  methods: {
    loadListData() {
      return this.roleInfo
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
