<script>
import businesstypeService from './businesstype-service'
import components from './_import-components/sd-app-ywlx-mixin-import'

export default {
  name: 'SdAppYwlxMixin',
  components,
  props: {
    appId: {
      type: String,
      default: '',
    },
    appNodeId: {
      type: String,
      default: undefined,
    },
    editable: {
      type: Boolean,
      default: false,
    },
    appModuleInfo: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {}
  },
  computed: {
    localAppId() {
      return this.category?.props?.appId || ''
    },
    appModuleId() {
      return this.category?.props?.appModuleId || ''
    },
  },
  methods: {
    loadTreeData(parentCatId) {
      const params = { appId: this.appId }
      if (this.appNodeId) {
        // 有appNodeId，说明是公共应用
        if (this.editable) {
          // 能编辑，说明是个性化业务类型
          params.appId = this.appNodeId
        } else {
          // 不能编辑，说明是公共的（下放的）
          params.appNodeId = this.appNodeId
        }
      }
      if (parentCatId) {
        // 非根节点
        params.parentId = parentCatId
      }
      return businesstypeService.getYwlxCategoryTree(params).then((res) =>
        res.data.map((d) => {
          const appModuleId = d.props?.appModuleId
          // 有模块id，记录当前模块的信息
          if (appModuleId) {
            if (!this.appModuleInfo[appModuleId]) {
              this.$set(this.appModuleInfo, appModuleId, { usePermission: d.props?.usePermission })
            }
          }
          return {
            ...d,
            permission: {
              create: this.editable && !!appModuleId, // 当前节点允许新建子分类（有模块id）
              update: this.editable && d.props?.isUpdate === 'true', // 当前节点允许修改
              delete: this.editable && d.props?.isUpdate === 'true', // 当前节点允许删除
            },
          }
        })
      )
    },
    getFormTreeData(parentCatId, appModuleId) {
      const params = { appId: this.localAppId, appModuleId: appModuleId || this.appModuleId }
      if (parentCatId) {
        // 非根节点
        params.parentId = parentCatId
      }
      return businesstypeService.getYwlxFormCategoryTree(params).then((res) => res.data)
    },
    getTreePickerData(parentCatId, { disableNodes = [], appModuleId } = {}) {
      return this.getFormTreeData(parentCatId, appModuleId).then((treeData) =>
        this.transformTreePickerData(treeData, disableNodes)
      )
    },
    transformTreePickerData(treeData, disableNodes) {
      // 过滤掉禁用的节点(含其子节点)
      return treeData
        .filter((t) => !disableNodes.includes(t.id))
        .map((d) => {
          const { children } = d
          const node = {
            id: d.id,
            name: d.text,
            checkable: true,
            isLeaf: d.leaf,
          }
          if (!d.leaf && children?.length) {
            node.children = this.transformTreePickerData(children, disableNodes)
          }
          return node
        })
    },
  },
}
</script>

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