<template>
  <sd-tree-picker
    v-bind="pickerMixinProps"
    :load-tree-data="loadTreeData"
    :search-tree-data="searchTreeData"
    title="选择业务类型"
    v-on="$listeners"
  />
</template>

<script>
import businesstypeService from '@/business-type/businesstype-service'
import pickerMixin from './sd-picker/picker-mixin'
import components from './_import-components/sd-business-type-picker-import'

/**
 * input框，通过弹出对话框选择业务类型
 * @displayName SdBusinessTypePicker 业务类型选择器
 */
export default {
  name: 'SdBusinessTypePicker',
  components,
  mixins: [pickerMixin],
  props: {
    /**
     * 需要根据code（业务类型分类编码）查找分类树时传此参数
     * @deprecated 0.23，请使用新参数appId
     */
    parentCode: {
      type: String,
      default: null,
    },
    /**
     * 需要获取指定应用下的业务类型分类树时传此参数（应用id）
     * @since 8.1.0.24
     */
    appId: {
      type: String,
      default: null,
    },
    /**
     * 分级机构id，获取指定分级下的业务类型时传此参数
     * @since 8.1.0.23
     */
    belongedOrgId: {
      type: String,
      default: undefined,
    },
  },
  methods: {
    loadTreeData(id, originalValue) {
      let params
      if (id) {
        params = { appId: originalValue.props?.appId, parentId: id }
      } else {
        params = this.appId ? { appId: this.appId } : {}
        if (this.parentCode) {
          params.parentCode = this.parentCode
        }
      }
      if (this.belongedOrgId) {
        params.belongedOrgId = Number(this.belongedOrgId)
      }
      return businesstypeService.getYwlxTree(params).then((res) => {
        return (res.data || []).map((item) => {
          return {
            id: item.id,
            name: item.text,
            checkable: item.props?.isChoice === 'true',
            isLeaf: item.leaf,
            props: item.props,
          }
        })
      })
    },
    searchTreeData(text) {
      const params = {
        appId: this.appId,
        belongedOrgId: Number(this.belongedOrgId),
        name: text,
      }
      return businesstypeService.searchBusiType(params).then((res) => {
        return (res.data || []).map((item) => {
          return {
            id: item.id,
            name: item.text,
            checkable: item.props?.isChoice === 'true',
            isLeaf: item.leaf,
            props: item.props,
          }
        })
      })
    },
  },
}
</script>

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

<docs>
```vue
<template>
  <form>
    选择业务类型：
    <sd-business-type-picker v-model='value' />
  </form>
</template>

<script>
export default {
  data() {
    return {
      value:[]
    }}
}
</script>
```
</docs>
