<template>
  <div class="pa-3 white content">
    <m-filter :option="filter" @search="handleSearch" />
    <m-table
      class="mt-3"
      :loading="loading"
      :headers="headers"
      :items="items"
      :total="total"
      :page-info="pageInfo"
      :disable-sort="true"
      :show-select="false"
      :is-tools="false"
      @delete="handleDelete"
      @pageHandleChange="pageHandleChange"
      @sort="handleSort"
    >
      <template #business_organization="{ item }">
        <tooltip width="300" :text="item.business_organization" />
      </template>
      <template #business_name="{ item }">
        <tooltip width="300" :text="item.business_name" />
      </template>
      <template #keyword="{ item }">
        <tooltip width="300" :text="item.keyword" />
      </template>
      <template #actions="{ item }">
        <v-btn icon color="primary" small :disabled="!item.url" @click="handleFile(item)">
          <v-icon>
            {{ item.url ? 'mdi-file-document' : 'mdi-file-document-remove' }}
          </v-icon>
        </v-btn>
      </template>
    </m-table>
    <knowledge
      :type="switchValue ? 4 : 3"
      welcome="我是您的产品助手。请问您需要了解什么内容?"
      title="产品知识库"
    >
      <template #title>
        <v-switch
          v-model="switchValue"
          class="mt-0"
          hide-details
          label="RAG对比"
        ></v-switch>
      </template>
    </knowledge>
    <!-- <m-dialog :title="title" :visible.sync="show" @submit="handleSubmit">
      <edit-page v-if="show" ref="edit" :item-data="itemData"></edit-page>
    </m-dialog> -->
  </div>
</template>

<script>
import Tooltip from '@/components/Tooltip'
import MFilter from '@/components/Filter'
import MTable from '@/components/List/table.vue'
// import MDialog from '@/components/Dialog'
// import EditPage from './components/edit'
import Knowledge from '@/components/Knowledge'
import { api } from '@/api/dataGovernance'
import { minIOSrc } from '@/utils/preview'
export default {
  name: 'knowledge-base-product-list',
  components: {
    MFilter,
    MTable,
    // MDialog,
    // EditPage,
    Tooltip,
    Knowledge
  },
  data () {
    return {
      switchValue: false,
      loading: false,
      show: false,
      filter: {
        list: [
          { type: 'textField', value: '', label: '业务机构', key: 'business_organization' },
          { type: 'textField', value: '', label: '关键词', key: 'keyword' }
          // { type: 'autocomplete', value: null, label: '选择', key: 'type', items: [] }
        ]
      },
      queryData: {
        business_organization: null,
        keyword: null
      },
      headers: [
        { text: '业务机构', align: 'start', value: 'business_organization' },
        { text: '业务名称', align: 'start', value: 'business_name' },
        { text: '关键词', align: 'start', value: 'keyword' },
        { text: '时间', align: 'start', value: 'create_time' },
        { text: '附件', align: 'start', value: 'actions' }
      ],
      itemData: {},
      items: [],
      orders: [],
      pageInfo: {
        size: 10,
        current: 1
      },
      total: 0
    }
  },
  computed: {
    title () {
      return Object.keys(this.itemData).length ? '编辑' : '新增'
    }
  },
  created () {
    this.init()
  },
  methods: {
    async init () {
      this.loading = true
      try {
        const { data } = await api.productKnowledgeBaseList({ ...this.pageInfo, ...this.queryData })
        this.items = data.records
        this.total = data.total
      } catch (error) {
        this.$snackbar.error(error)
      } finally {
        this.loading = false
      }
    },
    handleSearch (val) {
      Object.assign(this.queryData, val)
      this.pageInfo.current = 1
      this.init()
    },
    handleAdd () {
      this.itemData = {}
      this.show = true
    },
    async handleEdit (item) {
      this.itemData = item
      this.show = true
    },
    handleDelete (ids) {
      if (Array.isArray(ids) && !ids.length) return this.$snackbar.warning('请选择要删除的选项')
      this.$confirm('提示', '是否删除该选项')
        .then(async () => {
          try {
            this.$snackbar.success('删除成功')
            this.init()
          } catch (error) {
            this.$snackbar.error('删除失败')
          }
        })
    },
    handleFile ({ url }) {
      window.open(minIOSrc(url), 'new', 'location=no, toolbar=no')
    },
    handleSubmit () {},
    pageHandleChange (page) {
      this.pageInfo.current = page
      this.init()
    },
    handleSort (val) {
      this.orders = val
      this.init()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
