<template>
  <audit-group-picker
    v-bind="$attrs"
    :single="!multiple"
    :read-only="false"
    :selectchecked="true"
    :selectcheckfn="getData"
    @change="change"
  />
</template>

<script>
import pickValues from '@/common/services/pick-values'
import auditGroupPicker from '@product/iam/components/picker/audit-group-picker.vue'
import components from './_import-components/xm-select-modal-render-import'

export default {
  name: 'XmSelectModalRender',
  metaInfo: {
    title: 'XmSelectModalRender',
  },
  components: {
    auditGroupPicker,
    ...components,
  },
  props: {
    selectField: {
      type: String,
      default: '',
    },
    groupIdField: {
      type: String,
      default: '',
    },
    groupCodeField: {
      type: String,
      default: '',
    },
    groupNameField: {
      type: String,
      default: '',
    },
    multiple: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      selectcheckfw: [],
      defaultValue: this.$attrs?.value || null,
      readOnly: this.$attrs ? this.$attrs['read-only'] : false,
    }
  },
  inject: {
    SdFormContext: { default: () => {} },
  },
  methods: {
    getnamedata(data) {
      let name = ''
      if (JSON.parse(data) === 'object') {
        JSON.parse(data).forEach((i) => {
          name = name + i.name + ','
        })
      }
      if (name.length > 0) {
        if (name.substring(name.length - 1, name.length) === ',')
          name = name.substring(0, name.length - 1)
      }
      return name
    },
    getData() {
      const getdata = new Promise((resolve) => {
        if (this.selectField !== '') {
          resolve(this.SdFormContext.getFieldValue(this.selectField))
        } else {
          resolve([])
        }
      })
      return getdata
    },
    change(values) {
      this.$emit('input', values)
      let ids = '' // ids
      let codes = '' // code
      let names = '' // name
      values?.forEach((item) => {
        // 人员id
        if (ids === '') {
          ids = item.code
        } else {
          ids = ids + ',' + item.code
        }
        // 人员account
        if (codes === '') {
          codes = item.code
        } else {
          codes = codes + ',' + item.code
        }
        // 人员name
        if (names === '') {
          names = item.name
        } else {
          names = names + ',' + item.name
        }
      })
      if (this.groupIdField !== '') {
        this.SdFormContext.setFieldValue(this.groupIdField, ids)
      }
      if (this.groupCodeField !== '') {
        this.SdFormContext.setFieldValue(this.groupCodeField, codes)
      }
      if (this.groupNameField !== '') {
        this.SdFormContext.setFieldValue(this.groupNameField, names)
      }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.xmselinput {
  :global .ant-input {
    width: 70%;
    margin-right: 10px;
  }
  :global .ant-btn {
    width: auto;
    padding: 0 10px;
  }
}
</style>
