<template>
  <sd-value-picker v-bind="{ ...pickerMixinProps, optionValue }" v-on="$listeners">
    <template v-slot="scope">
      <sd-picker-source-list title="全局角色" :load-list-data="loadGlobalRole" v-bind="scope" />
      <sd-picker-source-list
        v-if="showLocal"
        title="本地角色"
        :load-list-data="loadLocalRole"
        v-bind="scope"
      />
    </template>
  </sd-value-picker>
</template>

<script>
import axios from '@/common/services/axios-instance'
import pickerMixin from '@/common/components/sd-picker/picker-mixin'
import components from './_import-components/sd-roles-picker-import'

export default {
  name: 'SdRolesPicker',
  components,
  mixins: [pickerMixin],
  props: {
    optionValue: {
      type: String,
      default: 'code',
    },
    /**
     * 分级机构id
     */
    groupId: {
      type: String,
      required: true,
    },
    showLocal: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {}
  },
  computed: {
    queryParams() {
      return {
        startPosition: 0,
        maxResults: -1,
        groupId: this.groupId,
      }
    },
  },
  methods: {
    loadGlobalRole() {
      return axios({
        url: 'api/framework/v1/role-manager/query/roles',
        method: 'post',
        data: this.queryParams,
      }).then((res) => {
        return res.data.datas
      })
    },
    loadLocalRole() {
      return axios({
        url: 'api/framework/v1/role-manager/query/local-roles',
        method: 'post',
        data: this.queryParams,
      }).then((res) => {
        return res.data.datas
      })
    },
  },
}
</script>

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