<template>
  <SdCategoryTree
    ref="categoryTree"
    form-id="weAppBusinessTypeGroup"
    page-id="app/appbusinesstypegroup/weAppBusinessTypeGroup"
    :load-tree-data="loadTreeData"
    :before-modal-show="beforeModalShow"
    @treeSelect="(n) => $emit('treeSelect', n)"
  >
    <sd-form-item name="name" />
    <a-form-model-item
      label="上级分组"
      :rules="[
        {
          required: true,
          message: '不能为空',
          trigger: ['change', 'blur'],
        },
      ]"
      prop="parentId"
    >
      <sd-tree-picker
        v-model="parentSel"
        single
        :load-tree-data="loadTreePickerData"
        @change="parentChange"
      />
    </a-form-model-item>
    <sd-form-item name="sortNumber" />
    <sd-form-item name="appModuleId" hidden :input-props="{ defaultValue: appModuleId }" />
    <sd-form-item name="parentId" hidden :input-props="{ defaultValue: parent && parent.id }" />
    <sd-form-item name="appId" hidden :input-props="{ defaultValue: localAppId }" />
  </SdCategoryTree>
</template>

<script>
import SdCategoryTree from '@/application-manage/components/sd-category-tree.vue'
import SdAppYwlxMixin from './sd-app-ywlx-mixin.vue'
import components from './_import-components/sd-app-ywlx-tree-import'

export default {
  name: 'SdAppYwlxTree',
  components: {
    ...components,
    SdCategoryTree,
  },
  mixins: [SdAppYwlxMixin],
  data() {
    return {
      parent: null,
      parentSel: [],
      category: {},
    }
  },
  methods: {
    beforeModalShow(parent) {
      this.category = parent
      this.parent = { id: parent.id, name: parent.text }
      this.parentSel = [{ ...this.parent }]
      return Promise.resolve(true)
    },
    parentChange(data) {
      this.$refs.categoryTree.getDetailModal().setFieldValue('parentId', data[0]?.id || '')
    },
    loadTreePickerData(parentCatId) {
      const curNodeId = this.$refs.categoryTree.getDetailModal().getFieldValue('id')
      return this.getTreePickerData(parentCatId, { disableNodes: [curNodeId] })
    },
  },
}
</script>

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