<template>
  <el-drawer title="知识库管理" :visible.sync="drawer" :wrapperClosable="false" ref="drawer" size="700px"
    :before-close="handleDrawerClose" class="JNPF-common-drawer" append-to-body>
    <div class="JNPF-flex-main">
      <div class="JNPF-common-head">
        <topOpts @refresh="getKnowledgeBaseList()" @add="addOrUpdateHandle()">
        </topOpts>
        <div class="JNPF-common-head-right">
          <el-tooltip effect="dark" :content="$t('common.refresh')" placement="top">
            <el-link icon="icon-ym icon-ym-Refresh
              JNPF-common-head-icon" :underline="false" @click="reset()" />
          </el-tooltip>
        </div>
      </div>
      <JNPF-table v-loading="listLoading" :data="tableData" row-key="id" default-expand-all
        :tree-props="{children: 'children', hasChildren: ''}">
        <el-table-column prop="name" label="名称" />
        <el-table-column label="操作" width="130">
          <template slot-scope="scope" v-if="scope.row.id!='0'&&scope.row.id!='1'">
            <tableOpts @edit="addOrUpdateHandle(scope.row.id)" @del="handleDel(scope.row.id)">
            </tableOpts>
          </template>
        </el-table-column>
      </JNPF-table>
      <TypeForm v-if="typeFormVisible" ref="TypeForm" @refreshDataList="getKnowledgeBaseList" />
    </div>
  </el-drawer>
</template>
<script>
import {
  getKnowledgeBase,
  delKnowledgeBase
} from '@/api/governmentCloud/knowledgeBase/knowledgeBase'
import TypeForm from './Form'

export default {
  components: {
    TypeForm
  },
  data() {
    return {
      drawer: false,
      listLoading: false,
      btnLoading: false,
      typeFormVisible: false,
      tableData: []
    }
  },
  methods: {
    init() {
      this.drawer = true
      this.$nextTick(() => {
        this.getKnowledgeBaseList()
      })
    },
    getKnowledgeBaseList() {
      this.listLoading = true
      getKnowledgeBase().then(res => {
        this.tableData = res.data
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    addOrUpdateHandle(id) {
      this.typeFormVisible = true
      this.$nextTick(() => {
        this.$refs.TypeForm.init(id)
      })
    },
    handleDel(id) {
      this.$confirm(this.$t('common.delTip'), this.$t('common.tipTitle'), {
        type: 'warning'
      }).then(() => {
        delKnowledgeBase(id).then(res => {
          this.$message({
            type: 'success',
            message: res.msg,
            duration: 1500,
            onClose: () => {
              this.getKnowledgeBaseList()
            }
          })
        })
      }).catch(() => { })
    },
    reset() {
      this.getKnowledgeBaseList()
    },
    handleDrawerClose(done) {
      done();
      this.$emit('refreshDataList')
    }
  }
}
</script>
