<!-- 知识库文件列表 -->
<template>
  <el-table :data="listFiles" style="width: 100%" height="350">
    <el-table-column type="index" width="50">
    </el-table-column>
    <el-table-column prop="name" label="名称">
    </el-table-column>
    <el-table-column prop="createTime" label="创建时间">
    </el-table-column>
    <el-table-column prop="fileSize" label="文件大小">
    </el-table-column>
    <el-table-column prop="docsCount" label="切片数量">
    </el-table-column>

    <el-table-column label="操作" width="240px">
      <template slot-scope="scope">
        <el-button type="text" :size="defaultFormItemSize" @click="anew(scope.row)">重新切片</el-button>
        <el-button type="text" @click="searchDocs(scope.row)" :size="defaultFormItemSize">切片详情</el-button>
        <el-button type="text" @click="delFile(scope.row)" :size="defaultFormItemSize">删除</el-button>
        <el-button type="text" @click="down(scope.row)" :size="defaultFormItemSize">下载</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
import { KnowledgeManage } from '@/api/gptController.js'
import details from './details'

export default {
  data() {
    return {
      listFiles: [],
      sectionList: []
    }
  },
  props: ['knowledgeName', 'params'],
  components: {},

  computed: {},

  mounted() {
    this.getFiles()
  },

  methods: {
    getFiles() {
      let params = { knowledgeName: this.knowledgeName }
      KnowledgeManage.listFiles(this, params)
        .then((res) => {
          try {
            let data = JSON.parse(res.data)
            this.listFiles = data.map((item) => {
              return {
                name: item.file_name,
                createTime: item.create_time,
                fileSize: (item.file_size / 1024).toFixed(2) + 'KB',
                docsCount: item.docs_count
              }
            })
          } catch (error) {
            this.listFiles = []
          }
        })
        .catch((e) => {
          console.log(e)
        })
    },
    searchDocs(row) {
      // let params = { knowledge_base_name: this.knowledgeName, file_name: row.name }
      // KnowledgeManage.searchDocs(this, params)
      //   .then((res) => {
      //     try {
      //       this.sectionList = JSON.parse(res.data)
      //     } catch (error) {
      //       this.sectionList = []
      //     }

      //   })
      //   .catch((e) => {
      //     console.log(e)
      //   })
      this.$dialog
        .show(
          '切片详情',
          details,
          {
            area: ['100%', '100%']
          },
          { fileName: row.name, knowledgeName: this.knowledgeName }
        )
        .then((res) => {})
        .catch((e) => {})
    },
    delFile(row) {
      let params = {
        knowledge_base_name: this.knowledgeName,
        file_names: [row.name],
        delete_content: true,
        not_refresh_vs_cache: false
      }
      this.$confirm('是否确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        KnowledgeManage.deleteDocs(this, params)
          .then((res) => {
            if (res.success) {
              this.getFiles()
            }
          })
          .catch((e) => {
            console.log(e)
          })
      })
    },
    down(row) {
      // 下载文件
      let params = { knowledge_base_name: this.knowledgeName, file_name: row.name, boolean: false }
      this.download('/2api/knowledge_base/download_doc', params, row.name, 'get', false).then((res) => {})
    },
    anew(row) {
      // 重新切片

      if (
        this.KnowledgeImportFormat.getList().find((item) => {
          return item.symbol === row.name.split('.').pop().toLowerCase()
        }).id !== this.params.importFormat
      ) {
        this.$message.error('请选择对应格式')
        return
      }
      this.$confirm('是否重新切片', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        KnowledgeManage.updateDocs(this, { ...this.params, file_names: [row.name], knowledge_base_name: this.knowledgeName, not_refresh_vs_cache: false, doc: {} })
          .then((res) => {
            if (res.success) {
              this.getFiles()
              this.$message.success('重新切片成功')
            }
          })
          .catch((e) => {
            console.log(e)
          })
      })
    }
  }
}
</script>
<style scoped>
</style>
