<template>
  <LayoutContainer showCollapse class="tool-manage">
    <template #left>
      <h4 class="p-12-16 pb-0 mt-12">{{ $t('views.tool.title') }}</h4>
      <folder-tree
        :source="SourceTypeEnum.TOOL"
        :data="folderList"
        :currentNodeKey="folder.currentFolder?.id"
        @handleNodeClick="folderClickHandle"
        @refreshTree="refreshFolder"
        :shareTitle="$t('views.shared.shared_tool')"
        :showShared="permissionPrecise['is_share']()"
        class="p-8"
      />
    </template>
    <ToolListContainer>
      <template #header>
        <FolderBreadcrumb :folderList="folderList" @click="folderClickHandle" />
      </template>
    </ToolListContainer>
  </LayoutContainer>
</template>

<script lang="ts" setup>
import { onMounted, ref, reactive, computed } from 'vue'
import ToolListContainer from '@/views/tool/component/ToolListContainer.vue'
import { SourceTypeEnum } from '@/enums/common'
import permissionMap from '@/permission'
import { useRoute } from 'vue-router'
import useStore from '@/stores'
const route = useRoute()
const { folder, tool } = useStore()

const apiType = computed(() => {
  if (route.path.includes('shared')) {
    return 'systemShare'
  } else if (route.path.includes('resource-management')) {
    return 'systemManage'
  } else {
    return 'workspace'
  }
})
const permissionPrecise = computed(() => {
  return permissionMap['tool'][apiType.value]
})

const loading = ref(false)

const folderList = ref<any[]>([])

function getFolder(bool?: boolean) {
  const params = {}
  folder.asyncGetFolder(SourceTypeEnum.TOOL, params, loading).then((res: any) => {
    folderList.value = res.data
    if (bool) {
      // 初始化刷新
      folder.setCurrentFolder(res.data?.[0] || {})
    }
  })
}

function folderClickHandle(row: any) {
  if (row.id === folder.currentFolder?.id) {
    return
  }
  folder.setCurrentFolder(row)
  tool.setToolList([])
}

function refreshFolder() {
  getFolder()
}

onMounted(() => {
  getFolder(true)
})
</script>

<style lang="scss" scoped></style>
