<template>
  <ContentContainer>
    <template #header>
      <slot name="header"> </slot>
    </template>
    <template #search>
      <div class="flex">
        <div class="complex-search">
          <el-select
            class="complex-search__left"
            v-model="search_type"
            style="width: 90px"
            @change="search_type_change"
          >
            <el-option :label="$t('common.creator')" value="create_user" />

            <el-option :label="$t('common.name')" value="name" />
          </el-select>
          <el-input
            v-if="search_type === 'name'"
            v-model="search_form.name"
            @change="searchHandle"
            :placeholder="$t('common.searchBar.placeholder')"
            style="width: 190px"
            clearable
          />
          <el-select
            v-else-if="search_type === 'create_user'"
            v-model="search_form.create_user"
            @change="searchHandle"
            filterable
            clearable
            style="width: 190px"
          >
            <el-option v-for="u in user_options" :key="u.id" :value="u.id" :label="u.nick_name" />
          </el-select>
        </div>
        <span
          class="ml-8"
          v-if="!isShared && (permissionPrecise.batchMove() || permissionPrecise.batchDelete())"
        >
          <el-button @click="batchSelectedHandle(true)" v-if="isBatch === false">
            <AppIcon iconName="app-batch-delete" class="mr-4" />
            {{ $t('views.paragraph.setting.batchSelected') }}
          </el-button>
          <el-button @click="batchSelectedHandle(false)" v-if="isBatch === true">
            <AppIcon iconName="app-batch-delete" class="mr-4" />
            {{ $t('views.paragraph.setting.cancelSelected') }}
          </el-button>
        </span>
        <div v-if="isBatch === false">
          <el-button
            class="ml-8"
            v-show="false"
            v-if="!isShared && permissionPrecise.create()"
            @click="openTemplateStoreDialog()"
          >
            <AppIcon iconName="app-template-center" class="mr-4" />
            {{ $t('workflow.setting.templateCenter') }}
          </el-button>
          <el-dropdown trigger="click" v-if="!isShared && permissionPrecise.create()">
            <el-button type="primary" class="ml-8">
              {{ $t('common.create') }}
              <el-icon class="el-icon--right">
                <arrow-down />
              </el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu class="create-dropdown">
                <el-dropdown-item @click="openCreateDialog(CreateKnowledgeDialog)">
                  <div class="flex">
                    <el-avatar class="avatar-blue mt-4" shape="square" :size="32">
                      <img src="@/assets/knowledge/icon_document.svg" style="width: 58%" alt="" />
                    </el-avatar>
                    <div class="pre-wrap ml-8">
                      <div class="lighter">
                        {{ $t('views.knowledge.knowledgeType.generalKnowledge') }}
                      </div>
                      <el-text type="info" size="small" class="color-secondary"
                        >{{ $t('views.knowledge.knowledgeType.generalInfo') }}
                      </el-text>
                    </div>
                  </div>
                </el-dropdown-item>
                <el-dropdown-item @click="openCreateDialog(CreateWebKnowledgeDialog)">
                  <div class="flex">
                    <el-avatar class="avatar-purple mt-4" shape="square" :size="32">
                      <img src="@/assets/knowledge/icon_web.svg" style="width: 58%" alt="" />
                    </el-avatar>
                    <div class="pre-wrap ml-8">
                      <div class="lighter">
                        {{ $t('views.knowledge.knowledgeType.webKnowledge') }}
                      </div>
                      <el-text type="info" size="small" class="color-secondary"
                        >{{ $t('views.knowledge.knowledgeType.webInfo') }}
                      </el-text>
                    </div>
                  </div>
                </el-dropdown-item>
                <el-dropdown-item
                  @click="openCreateDialog(CreateLarkKnowledgeDialog)"
                  v-if="user.isPE() || user.isEE()"
                >
                  <div class="flex">
                    <el-avatar
                      class="avatar-purple mt-4"
                      shape="square"
                      :size="32"
                      style="background: none"
                    >
                      <img src="@/assets/knowledge/logo_lark.svg" alt="" />
                    </el-avatar>
                    <div class="pre-wrap ml-8">
                      <div class="lighter">
                        {{ $t('views.knowledge.knowledgeType.larkKnowledge') }}
                      </div>
                      <el-text type="info" size="small" class="color-secondary"
                        >{{ $t('views.knowledge.knowledgeType.larkInfo') }}
                      </el-text>
                    </div>
                  </div>
                </el-dropdown-item>
                <el-dropdown-item @click="openCreateDialog(CreateWorkflowKnowledgeDialog)">
                  <div class="flex">
                    <el-avatar class="avatar-orange mt-4" shape="square" :size="32">
                      <img src="@/assets/workflow/logo_workflow.svg" style="width: 60%" alt="" />
                    </el-avatar>
                    <div class="pre-wrap ml-8">
                      <div class="lighter">
                        {{ $t('views.knowledge.knowledgeType.workflowKnowledge') }}
                      </div>
                      <el-text type="info" size="small" class="color-secondary"
                        >{{ $t('views.knowledge.knowledgeType.workflowInfo') }}
                      </el-text>
                    </div>
                  </div>
                </el-dropdown-item>
                <el-dropdown-item @click="openSampleCenterDialog">
                  <div class="flex">
                    <el-avatar class="avatar-green mt-4" shape="square" :size="32">
                      <AppIcon iconName="app-template-center" style="font-size: 20px"></AppIcon>
                    </el-avatar>
                    <div class="pre-wrap ml-8">
                      <div class="lighter">样本中心知识库</div>
                      <el-text type="info" size="small" class="color-secondary"
                        >从样本中心导入专业知识库</el-text
                      >
                    </div>
                  </div>
                </el-dropdown-item>
                <el-upload
                  ref="importKnowledgeUploadRef"
                  :file-list="[]"
                  action="#"
                  multiple
                  :auto-upload="false"
                  :show-file-list="false"
                  :limit="1"
                  accept=".zip"
                  :on-change="(file: any) => importKnowledgeBundle(file)"
                  class="import-button"
                >
                  <el-dropdown-item>
                    <div class="flex align-center w-full">
                      <el-avatar shape="square" :size="32" style="background: none">
                        <img src="@/assets/icon_import.svg" alt="" />
                      </el-avatar>
                      <div class="pre-wrap ml-8">
                        <div class="lighter">{{ $t('common.importCreate') }}</div>
                      </div>
                    </div>
                  </el-dropdown-item>
                </el-upload>
                <el-dropdown-item @click="openCreateFolder" divided v-if="apiType === 'workspace'">
                  <div class="flex align-center">
                    <AppIcon iconName="app-folder" style="font-size: 32px"></AppIcon>
                    <div class="pre-wrap ml-4">
                      <div class="lighter">
                        {{ $t('components.folder.addFolder') }}
                      </div>
                    </div>
                  </div>
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </template>

    <div
      v-loading.fullscreen.lock="paginationConfig.current_page === 1 && loading"
      style="max-height: calc(100vh - 120px)"
    >
      <InfiniteScroll
        :size="knowledge.knowledgeList.length"
        :total="paginationConfig.total"
        :page_size="paginationConfig.page_size"
        v-model:current_page="paginationConfig.current_page"
        @load="getList"
        :loading="loading"
      >
        <el-checkbox-group v-model="multipleSelection" @change="handleCheckedChatChange">
          <el-row v-if="knowledge.knowledgeList.length > 0" :gutter="15" class="w-full">
            <template v-for="(item, index) in knowledge.knowledgeList" :key="index">
              <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="6" class="mb-16">
                <CardBox
                  :title="item.name"
                  :description="item.desc"
                  class="cursor"
                  @click="toDocument(item)"
                  :disabled="isBatch"
                >
                  <template #icon>
                    <KnowledgeIcon :type="item.type" />
                  </template>
                  <template #subTitle>
                    <el-text class="color-secondary lighter flex align-center" size="small">
                      <span
                        :title="i18n_name(item.nick_name)"
                        class="ellipsis"
                        style="max-width: 90px"
                      >
                        {{ i18n_name(item.nick_name) }}
                      </span>
                      <span class="ml-4 mr-4"> {{ $t('common.createdIn') }}</span>
                      <span> {{ dateFormat(item.create_time) }}</span>
                    </el-text>
                  </template>
                  <template #tag>
                    <el-checkbox :value="item.id" v-if="isBatch" @change="checkboxChange(item)" />
                    <div v-else>
                      <el-tag
                        v-if="isShared || isSystemShare"
                        size="small"
                        type="info"
                        class="info-tag"
                      >
                        {{ $t('views.shared.title') }}
                      </el-tag>
                    </div>
                  </template>
                  <template #footer>
                    <div class="footer-content flex-between">
                      <div>
                        <span class="bold mr-4">{{ item?.document_count || 0 }}</span>
                        <span class="color-secondary">{{
                          $t('views.knowledge.document_count')
                        }}</span>
                        <el-divider direction="vertical" />
                        <span class="bold mr-4">{{ numberFormat(item?.char_length) || 0 }}</span>
                        <span class="color-secondary">{{ $t('common.character') }}</span>
                      </div>
                    </div>
                  </template>
                  <template #mouseEnter>
                    <div @click.stop v-if="!isShared">
                      <el-dropdown trigger="click">
                        <el-button text @click.stop v-if="MoreFilledPermission(item)">
                          <AppIcon iconName="app-more" class="color-secondary"></AppIcon>
                        </el-button>
                        <template #dropdown>
                          <el-dropdown-menu>
                            <el-dropdown-item
                              @click.stop="syncKnowledge(item)"
                              v-if="item.type === 1 && permissionPrecise.sync(item.id)"
                            >
                              <AppIcon iconName="app-sync" class="color-secondary"></AppIcon>

                              {{ $t('views.knowledge.setting.sync') }}
                            </el-dropdown-item>
                            <el-dropdown-item
                              @click.stop="reEmbeddingKnowledge(item)"
                              v-if="permissionPrecise.vector(item.id)"
                            >
                              <AppIcon
                                iconName="app-vectorization"
                                class="color-secondary"
                              ></AppIcon>
                              {{ $t('views.knowledge.setting.vectorization') }}
                            </el-dropdown-item>

                            <el-dropdown-item
                              @click.stop="openGenerateDialog(item)"
                              v-if="permissionPrecise.generate(item.id)"
                            >
                              <AppIcon
                                iconName="app-generate-question"
                                class="color-secondary"
                              ></AppIcon>

                              {{ $t('views.document.generateQuestion.title') }}
                            </el-dropdown-item>
                            <el-dropdown-item
                              v-if="isSystemShare"
                              @click.stop="openAuthorizedWorkspaceDialog(item)"
                            >
                              <AppIcon iconName="app-lock" class="color-secondary"></AppIcon>
                              {{ $t('views.shared.authorized_workspace') }}</el-dropdown-item
                            >
                            <el-dropdown-item
                              @click.stop="openAuthorization(item)"
                              v-if="apiType === 'workspace' && permissionPrecise.auth(item.id)"
                            >
                              <AppIcon
                                iconName="app-resource-authorization"
                                class="color-secondary"
                              ></AppIcon>
                              {{ $t('views.system.resourceAuthorization.title') }}
                            </el-dropdown-item>
                            <el-dropdown-item
                              text
                              @click.stop="openResourceMappingDrawer(item)"
                              v-if="permissionPrecise.relate_map(item.id)"
                            >
                              <AppIcon
                                iconName="app-resource-mapping"
                                class="color-secondary"
                              ></AppIcon>
                              {{ $t('views.system.resourceMapping.title') }}
                            </el-dropdown-item>
                            <el-dropdown-item
                              @click.stop="openMoveToDialog(item)"
                              v-if="permissionPrecise.edit(item.id) && apiType === 'workspace'"
                            >
                              <AppIcon iconName="app-migrate" class="color-secondary"></AppIcon>
                              {{ $t('common.moveTo') }}
                            </el-dropdown-item>

                            <el-dropdown-item
                              @click.stop="
                                router.push({
                                  path: `/knowledge/${item.id}/${folder.currentFolder.id || 'shared'}/${item.type}/setting`,
                                })
                              "
                              v-if="permissionPrecise.edit(item.id)"
                            >
                              <AppIcon iconName="app-setting" class="color-secondary"></AppIcon>
                              {{ $t('common.setting') }}
                            </el-dropdown-item>
                            <el-dropdown-item
                              divided
                              @click.stop="exportKnowledge(item)"
                              v-if="permissionPrecise.export(item.id)"
                            >
                              <AppIcon iconName="app-export" class="color-secondary"></AppIcon
                              >{{ $t('views.document.setting.exportDocument') }} Excel
                            </el-dropdown-item>
                            <el-dropdown-item
                              @click.stop="exportZipKnowledge(item)"
                              v-if="permissionPrecise.export(item.id)"
                            >
                              <AppIcon iconName="app-export" class="color-secondary"></AppIcon
                              >{{
                                $t('views.document.setting.exportDocument')
                              }}
                              ZIP</el-dropdown-item
                            >
                            <el-dropdown-item
                              @click.stop="exportKnowledgeBundle(item)"
                              v-if="permissionPrecise.export(item.id)"
                            >
                              <AppIcon iconName="app-export" class="color-secondary"></AppIcon>
                              {{ $t('views.document.setting.exportKnowledge') }}
                            </el-dropdown-item>

                            <el-dropdown-item
                              divided
                              type="danger"
                              @click.stop="deleteKnowledge(item)"
                              v-if="permissionPrecise.delete(item.id)"
                            >
                              <AppIcon iconName="app-delete" class="color-secondary"></AppIcon>
                              {{ $t('common.delete') }}</el-dropdown-item
                            >
                          </el-dropdown-menu>
                        </template>
                      </el-dropdown>
                    </div>
                  </template>
                </CardBox>
              </el-col>
            </template>
          </el-row>
          <el-empty :description="$t('common.noData')" v-else />
        </el-checkbox-group>
      </InfiniteScroll>
    </div>
    <!-- 批量操作拦 -->
    <div class="mul-operation border-t w-full flex align-center" v-if="isBatch">
      <el-checkbox
        v-model="checkAll"
        :indeterminate="isIndeterminate"
        @change="handleCheckAllChange"
      >
        {{ $t('common.allCheck') }}
      </el-checkbox>
      <el-button
        class="ml-16"
        :disabled="multipleSelection.length === 0"
        @click="openMoveToDialog()"
        v-if="permissionPrecise.batchMove()"
      >
        {{ $t('common.moveTo') }}
      </el-button>

      <el-button
        :disabled="multipleSelection.length === 0"
        @click="deleteMulKnowledge"
        v-if="permissionPrecise.batchDelete()"
      >
        {{ $t('common.delete') }}
      </el-button>
      <span class="color-secondary ml-24 mr-16">
        {{ $t('common.selected') }} {{ multipleSelection.length }}/{{ paginationConfig.total }}
        {{ $t('views.document.items') }}
      </span>

      <el-button link type="primary" @click="batchSelectedHandle(false)">
        {{ $t('views.paragraph.setting.cancelSelected') }}
      </el-button>
    </div>
  </ContentContainer>

  <component :is="currentCreateDialog" ref="CreateKnowledgeDialogRef" v-if="!isShared" />
  <CreateFolderDialog ref="CreateFolderDialogRef" v-if="!isShared" @refresh="refreshFolder" />
  <GenerateRelatedDialog ref="GenerateRelatedDialogRef" :apiType="apiType" />
  <SyncWebDialog ref="SyncWebDialogRef" v-if="!isShared" />
  <AuthorizedWorkspace
    ref="AuthorizedWorkspaceDialogRef"
    v-if="isSystemShare"
  ></AuthorizedWorkspace>
  <MoveToDialog
    ref="MoveToDialogRef"
    :source="SourceTypeEnum.KNOWLEDGE"
    @refresh="refreshKnowledgeList"
    v-if="apiType === 'workspace'"
  />
  <ResourceAuthorizationDrawer
    :type="SourceTypeEnum.KNOWLEDGE"
    ref="ResourceAuthorizationDrawerRef"
    v-if="apiType === 'workspace'"
  />
  <TemplateStoreDialog ref="templateStoreDialogRef" :api-type="apiType" @refresh="getList" />
  <ResourceMappingDrawer ref="resourceMappingDrawerRef"></ResourceMappingDrawer>
  <CreateSampleCenterKnowledgeDialog ref="sampleCenterDialogRef" @select="handleSampleCenterSelect" />
</template>

<script lang="ts" setup>
import { onMounted, ref, reactive, shallowRef, nextTick, computed, watch } from 'vue'
import { useRouter, useRoute, onBeforeRouteLeave } from 'vue-router'
import { cloneDeep, get } from 'lodash'
import type { CheckboxValueType } from 'element-plus'
import CreateKnowledgeDialog from '@/views/knowledge/create-component/CreateKnowledgeDialog.vue'
import CreateWebKnowledgeDialog from '@/views/knowledge/create-component/CreateWebKnowledgeDialog.vue'
import CreateLarkKnowledgeDialog from '@/views/knowledge/create-component/CreateLarkKnowledgeDialog.vue'
import CreateWorkflowKnowledgeDialog from '@/views/knowledge/create-component/CreateWorkflowKnowledgeDialog.vue'
import CreateSampleCenterKnowledgeDialog from '@/views/knowledge/create-component/CreateSampleCenterKnowledgeDialog.vue'
import SyncWebDialog from '@/views/knowledge/component/SyncWebDialog.vue'
import CreateFolderDialog from '@/components/folder-tree/CreateFolderDialog.vue'
import MoveToDialog from '@/components/folder-tree/MoveToDialog.vue'
import GenerateRelatedDialog from '@/components/generate-related-dialog/index.vue'
import AuthorizedWorkspace from '@/views/system-shared/AuthorizedWorkspaceDialog.vue'
import ResourceAuthorizationDrawer from '@/components/resource-authorization-drawer/index.vue'
import TemplateStoreDialog from '@/views/knowledge/template-store/TemplateStoreDialog.vue'
import ResourceMappingDrawer from '@/components/resource_mapping/index.vue'
import { MsgSuccess, MsgConfirm } from '@/utils/message'
import { numberFormat, i18n_name } from '@/utils/common'
import { dateFormat } from '@/utils/time'
import { SourceTypeEnum } from '@/enums/common'
import { loadSharedApi } from '@/utils/dynamics-api/shared-api'
import permissionMap from '@/permission'
import useStore from '@/stores'
import { t } from '@/locales'
const resourceMappingDrawerRef = ref<InstanceType<typeof ResourceMappingDrawer>>()

const openResourceMappingDrawer = (knowledge: any) => {
  resourceMappingDrawerRef.value?.open('KNOWLEDGE', knowledge)
}
const router = useRouter()
const route = useRoute()
const { folder, user, knowledge } = useStore()
onBeforeRouteLeave((to, from) => {
  knowledge.setKnowledgeList([])
})

const emit = defineEmits(['refreshFolder'])

const apiType = computed(() => {
  // 工作空间普通用户的共享是share。系统的共享是shared
  if (route.path.includes('shared')) {
    return 'systemShare'
  } else if (route.path.includes('resource-management')) {
    return 'systemManage'
  } else {
    return 'workspace'
  }
})
const permissionPrecise = computed(() => {
  return permissionMap['knowledge'][apiType.value]
})

const isShared = computed(() => {
  return folder.currentFolder.id === 'share'
})
const isSystemShare = computed(() => {
  return apiType.value === 'systemShare'
})

const MoreFilledPermission = (item: any) => {
  return (
    (item.type === 1 && permissionPrecise.value.sync(item.id)) ||
    permissionPrecise.value.vector(item.id) ||
    permissionPrecise.value.generate(item.id) ||
    (permissionPrecise.value.edit(item.id) && apiType.value) === 'workspace' ||
    permissionPrecise.value.export(item.id) ||
    permissionPrecise.value.auth(item.id) ||
    permissionPrecise.value.delete(item.id) ||
    permissionPrecise.value.relate_map(item.id) ||
    isSystemShare.value
  )
}

const loading = ref(false)

const search_type = ref('name')
const search_form = ref<any>({
  name: '',
  create_user: '',
})

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

const paginationConfig = reactive({
  current_page: 1,
  page_size: 30,
  total: 0,
})

// 批量操作
const isBatch = ref(false)
const multipleSelection = ref<any[]>([])
const checkAll = ref(false)
const isIndeterminate = computed(() => {
  return (
    multipleSelection.value.length > 0 &&
    multipleSelection.value.length < knowledge.knowledgeList.length
  )
})
function batchSelectedHandle(bool: boolean) {
  isBatch.value = bool
  multipleSelection.value = []
  checkAll.value = false
}

const handleCheckAllChange = (val: CheckboxValueType) => {
  let bool
  if (isIndeterminate.value) {
    bool = true
  } else {
    bool = val as boolean
  }
  multipleSelection.value = bool ? knowledge.knowledgeList.map((v) => v.id) : []
  checkAll.value = bool as boolean
}
const handleCheckedChatChange = (value: CheckboxValueType[]) => {
  const checkedCount = value.length
  checkAll.value = checkedCount === knowledge.knowledgeList.length
}

const checkboxChange = (data?: any) => {
  const index = multipleSelection.value.indexOf(data?.id)
  if (index === -1) {
    multipleSelection.value.push(data?.id)
  } else {
    multipleSelection.value.splice(index, 1)
  }
  checkAll.value = multipleSelection.value.length === knowledge.knowledgeList.length
}

function deleteMulKnowledge() {
  MsgConfirm(
    `${t('views.document.delete.confirmTitle1')} ${multipleSelection.value.length} ${t('views.knowledge.delete.confirmTitle2')}`,
    t('views.paragraph.delete.confirmMessage'),
    {
      confirmButtonText: t('common.confirm'),
      confirmButtonClass: 'danger',
    },
  )
    .then(() => {
      loadSharedApi({ type: 'knowledge', systemType: apiType.value })
        .delMulKnowledge(multipleSelection.value, loading)
        .then(() => {
          batchSelectedHandle(false)
          paginationConfig.current_page = 1
          knowledge.setKnowledgeList([])
          getList()
          MsgSuccess(t('views.document.delete.successMessage'))
        })
    })
    .catch(() => {})
}

function toDocument(item: any) {
  if (isBatch.value) {
    const index = multipleSelection.value.indexOf(item?.id)
    if (index === -1) {
      multipleSelection.value.push(item?.id)
    } else {
      multipleSelection.value.splice(index, 1)
    }
    checkAll.value = multipleSelection.value.length === knowledge.knowledgeList.length
    return
  }

  router.push({
    path: `/knowledge/${item.id}/${folder.currentFolder.id ? (folder.currentFolder.id !== 'share' ? item.folder_id : 'share') : 'shared'}/${item.type}/document`,
  })
}
const ResourceAuthorizationDrawerRef = ref()
function openAuthorization(item: any) {
  ResourceAuthorizationDrawerRef.value.open(item.id)
}

const MoveToDialogRef = ref()
function openMoveToDialog(data?: any) {
  let obj
  if (isBatch.value) {
    obj = {
      id_list: multipleSelection.value,
    }
  } else {
    // 仅2个参数就行
    obj = {
      id: data.id,
      folder_id: data.folder,
    }
  }

  MoveToDialogRef.value?.open(obj)
}

function refreshKnowledgeList(row: any) {
  if (row) {
    // 不是根目录才会移除
    if (folder.currentFolder?.parent_id) {
      const list = cloneDeep(knowledge.knowledgeList)
      const index = list.findIndex((v) => v.id === row.id)
      list.splice(index, 1)
      knowledge.setKnowledgeList(list)
    }
  } else {
    batchSelectedHandle(false)
    paginationConfig.current_page = 1
    knowledge.setKnowledgeList([])
    getList()
  }
}

const CreateKnowledgeDialogRef = ref()
const currentCreateDialog = shallowRef<any>(null)

function openCreateDialog(data: any) {
  currentCreateDialog.value = data
  nextTick(() => {
    CreateKnowledgeDialogRef.value.open(folder.currentFolder)
  })
}

const sampleCenterDialogRef = ref<InstanceType<typeof CreateSampleCenterKnowledgeDialog>>()

function openSampleCenterDialog() {
  sampleCenterDialogRef.value?.open()
}

function handleSampleCenterSelect(data: any) {
  // TODO: 实现从样本中心创建知识库的逻辑
  console.log('Selected sample center KB:', data)
  MsgSuccess('样本中心知识库选择成功，待实现入库逻辑')
}

function reEmbeddingKnowledge(row: any) {
  loadSharedApi({ type: 'knowledge', systemType: apiType.value })
    .putReEmbeddingKnowledge(row.id)
    .then(() => {
      MsgSuccess(t('common.submitSuccess'))
    })
}

const SyncWebDialogRef = ref()

function syncKnowledge(row: any) {
  SyncWebDialogRef.value.open(row.id)
}

const search_type_change = () => {
  search_form.value = { name: '', create_user: '' }
}

const exportKnowledgeBundle = (item: any) => {
  loadSharedApi({ type: 'knowledge', systemType: apiType.value })
    .exportKnowledgeBundle(item.name, item.id, loading)
    .then(() => {
      MsgSuccess(t('common.exportSuccess'))
    })
}

const importKnowledgeUploadRef = ref()

function importKnowledgeBundle(file: any) {
  const formData = new FormData()
  formData.append('file', file.raw)
  formData.append('folder_id', folder.currentFolder.id || user.getWorkspaceId())
  importKnowledgeUploadRef.value.clearFiles()

  loadSharedApi({ type: 'knowledge', systemType: apiType.value })
    .importKnowledgeBundle(formData, loading)
    .then(async (res: any) => {
      if (res?.data) {
        const knowledgeId = res.data.knowledge_id
        const knowledgeType = res.data.type
        const folderId = folder.currentFolder.id || user.getWorkspaceId()
        await user.profile()
        router.push({
          path: `/knowledge/${knowledgeId}/${folderId}/${knowledgeType}/document`,
          query: { imported: 'true' },
        })
      }
    })
    .catch((e: any) => {
      if (e.code === 400) {
        MsgConfirm(t('common.tip'), t('views.application.tip.professionalMessage'), {
          cancelButtonText: t('common.confirm'),
          confirmButtonText: t('common.professional'),
        }).then(() => {
          window.open('https://maxkb.cn/pricing.html', '_blank')
        })
      }
    })
}

const GenerateRelatedDialogRef = ref<InstanceType<typeof GenerateRelatedDialog>>()
function openGenerateDialog(row: any) {
  if (GenerateRelatedDialogRef.value) {
    GenerateRelatedDialogRef.value.open([], 'knowledge', row)
  }
}

const exportKnowledge = (item: any) => {
  loadSharedApi({ type: 'knowledge', systemType: apiType.value })
    .exportKnowledge(item.name, item.id, loading)
    .then(() => {
      MsgSuccess(t('common.exportSuccess'))
    })
}
const exportZipKnowledge = (item: any) => {
  loadSharedApi({ type: 'knowledge', systemType: apiType.value })
    .exportZipKnowledge(item.name, item.id, loading)
    .then(() => {
      MsgSuccess(t('common.exportSuccess'))
    })
}

function deleteKnowledge(row: any) {
  MsgConfirm(
    `${t('views.knowledge.delete.confirmTitle')}${row.name} ?`,
    row.resource_count > 0
      ? t('views.knowledge.delete.resourceCountMessage', row.resource_count)
      : '',
    {
      confirmButtonText: t('common.confirm'),
      confirmButtonClass: 'danger',
    },
  )
    .then(() => {
      loadSharedApi({ type: 'knowledge', systemType: apiType.value })
        .delKnowledge(row.id, loading)
        .then(() => {
          const list = cloneDeep(knowledge.knowledgeList)
          const index = list.findIndex((v) => v.id === row.id)
          list.splice(index, 1)
          knowledge.setKnowledgeList(list)

          MsgSuccess(t('common.deleteSuccess'))
        })
    })
    .catch(() => {})
}

const AuthorizedWorkspaceDialogRef = ref()
function openAuthorizedWorkspaceDialog(row: any) {
  if (AuthorizedWorkspaceDialogRef.value) {
    AuthorizedWorkspaceDialogRef.value.open(row)
  }
}

// 文件夹相关
const CreateFolderDialogRef = ref()
function openCreateFolder() {
  CreateFolderDialogRef.value.open(SourceTypeEnum.KNOWLEDGE, folder.currentFolder.id)
}
watch(
  () => folder.currentFolder,
  (newValue) => {
    if (newValue && newValue.id && !isSystemShare.value) {
      batchSelectedHandle(false)
      paginationConfig.current_page = 1
      knowledge.setKnowledgeList([])
      getList()
    }
  },
  { deep: true, immediate: true },
)

function getList() {
  const params: any = {
    folder_id: folder.currentFolder?.id || user.getWorkspaceId(),
    scope: apiType.value === 'systemShare' ? 'SHARED' : 'WORKSPACE',
  }
  if (search_form.value[search_type.value]) {
    params[search_type.value] = search_form.value[search_type.value]
  }
  loadSharedApi({ type: 'knowledge', isShared: isShared.value, systemType: apiType.value })
    .getKnowledgeListPage(paginationConfig, params, loading)
    .then((res: any) => {
      paginationConfig.total = res.data?.total
      knowledge.setKnowledgeList([...knowledge.knowledgeList, ...res.data.records])
    })
}

function searchHandle() {
  paginationConfig.current_page = 1
  knowledge.setKnowledgeList([])
  getList()
}

function refreshFolder() {
  emit('refreshFolder')
}

const templateStoreDialogRef = ref()
function openTemplateStoreDialog() {
  templateStoreDialogRef.value?.open(folder.currentFolder.id)
}

onMounted(() => {
  if (apiType.value !== 'workspace') {
    folder.setCurrentFolder({
      id: '',
    })
    getList()
  }
  loadSharedApi({ type: 'workspace', isShared: isShared.value, systemType: apiType.value })
    .getAllMemberList(user.getWorkspaceId(), loading)
    .then((res: any) => {
      user_options.value = res.data
    })
})
</script>

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