<template>
  <CzrDialog
    :show="show"
    title="选择知识库"
    @onClose="$emit('update:show', false)"
    @onSubmit="onSubmit"
    width="62.5rem"
    height="auto"
    :loading="state.loading"
  >
    <div class="bm-form">
      <template v-for="item in state.list">
        <div
          class="knowledge-item __hover"
          :class="{ active: item.id == state.active.id }"
          @click="() => (state.active = item)"
        >
          <img src="@/assets/images/answer.png" />
          <div v-title>{{ item.name }}</div>
        </div>
      </template>
    </div>
  </CzrDialog>
</template>

<script setup lang="ts">
import {
  computed,
  getCurrentInstance,
  nextTick,
  reactive,
  ref,
  watch,
} from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useDialogStore, useDictionaryStore } from '@/stores'
import { useRouter } from 'vue-router'

const router = useRouter()
const DictionaryStore = useDictionaryStore()
const DialogStore = useDialogStore()
const emit = defineEmits(['update:show', 'refresh'])
const { proxy } = getCurrentInstance()
const props = defineProps({
  show: { default: false },
  transfer: <any>{},
})
const state: any = reactive({
  loading: false,
  list: [],
  active: '',
})
watch(
  () => props.show,
  (n) => {
    if (n) {
      initDictionary()
    }
  },
)
const initDictionary = () => {
  const arr: any = []
  for (let i = 1; i <= 100; i++) {
    arr.push({
      id: i,
      name: '部门知识库-新_' + i,
    })
  }
  state.list = arr
}
const onSubmit = () => {
  let tips = '请确认是否将'
  if (props.transfer.row) {
    tips += props.transfer.row.name
  } else {
    tips += props.transfer.list.length + '个'
    if (props.transfer.type === 'text') {
      tips += '文档'
    } else if (props.transfer.type === 'qa') {
      tips += '问答'
    } else if (props.transfer.type === 'stage') {
      tips += '分段'
    }
  }
  tips += `迁移至${state.active.name}？`
  if (state.active.id) {
    DialogStore.confirm({
      content: tips,
      onSubmit: () => {
        state.loading = true
        ElMessage.success(`迁移成功！`)
        emit('update:show', false)
        emit('refresh')
        state.loading = false
      },
    })
  } else {
    ElMessage.warning(`请选择知识库！`)
  }
}
</script>

<style lang="scss" scoped>
.bm-form {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1rem;
  max-height: 36rem;
  overflow-y: auto;
  .knowledge-item {
    height: 5.25rem;
    box-shadow: 0rem 0.25rem 0.63rem 0rem rgba(40, 83, 247, 0.05);
    border-radius: 0.63rem;
    border: var(--czr-border);
    display: flex;
    align-items: center;
    padding: 0 1.5rem;
    font-weight: bold;
    font-size: 1.25rem;
    color: #2e3238;
    gap: 1rem;
    > img {
      width: 2.13rem;
      height: 2.12rem;
    }
    &.active {
      background: #f3f5fe;
      box-shadow: 0rem 0.25rem 0.63rem 0rem rgba(40, 83, 247, 0.05);
      border-color: var(--czr-main-color);
    }
  }
}
</style>
