<template>
  <div class="pa-3 white">
    <m-filter @search="handleSearch" />
    <m-table
      class="mt-3"
      :loading="loading"
      :headers="headers"
      :items="items"
      :total="total"
      :page-info="pageInfo"
      :show-select="false"
      :is-tools="false"
      @edit="handleEdit"
      @delete="handleDelete"
      @pageHandleChange="pageHandleChange"
      @sort="handleSort"
    >
      <template #type="{ item }">
        {{ item.type === 'structure' ? '结构化' : '非结构化' }}
      </template>
      <template #status="{ item }">
        <v-chip
          :color="item.status ? 'success' : 'error'"
          small
        >
          {{ item.status ? '已启用' : '已禁用'}}
        </v-chip>
      </template>
      <template #navBtn>
        <v-btn
          v-for="item in fileItems"
          :key="item.value"
          class="elevation-5 mr-2 buttons"
          rounded
          color="primary"
          @click="handleAdd(item.value)"
        >
          <v-icon left>mdi-plus</v-icon>
          {{ item.text }}
        </v-btn>
      </template>
      <template #tag_info="{item}">
        {{ item.tag_info?.name_zh }}
      </template>
    </m-table>
    <m-dialog
      :title="title"
      :visible.sync="show"
      :fullscreen="true"
      body-style="display: flex;padding: 0;"
      :footer="false"
    >
      <component
        v-if="show"
        :is="path"
        :type="path"
        ref="edit"
        :item-data="itemData"
        @close="handleClose"
      ></component>
    </m-dialog>
  </div>
</template>

<script>
import MFilter from '../components/Filter'
import MTable from '@/components/List/table.vue'
import MDialog from '@/components/Dialog'
import Structure from './components/Structure'
import Unstructured from './components/Unstructured'
import Database from './components/Database'
import { api } from '@/api/dataGovernance'
import {
  frequency,
  sensitivity
} from '@/utils/dataGovernance'
export default {
  name: 'data-resource',
  components: {
    MFilter,
    MTable,
    MDialog,
    Structure,
    Unstructured,
    Database
  },
  data () {
    return {
      loading: false,
      path: null,
      // documentType: null,
      fileItems: [
        { text: 'DDL解析', value: 'database' },
        { text: '结构化文档', value: 'structure' },
        { text: '非结构化文档', value: 'unstructured' }
      ],
      show: false,
      queryData: {
        name_zh: null
      },
      headers: [
        { text: '中文名', value: 'name_zh' },
        { text: '英文名', value: 'name_en' },
        { text: '分类', value: 'category' },
        { text: '描述', value: 'describe' },
        { text: '标签', value: 'tag_info' },
        { text: '状态', value: 'status' },
        { text: '血缘关系数量', value: 'blood_count', align: 'center' },
        { text: '更新频率', value: 'frequency' },
        { text: '数据敏感度', value: 'data_sensitivity' },
        { text: '创建时间', value: 'create_time' },
        { text: '操作', value: 'actions' }
      ],
      itemData: {},
      items: [],
      orders: [],
      pageInfo: {
        size: 10,
        current: 1
      },
      total: 0
    }
  },
  computed: {
    title () {
      if (!this.path) return ''
      const list = {
        structure: '结构化数据资源',
        unstructured: '非结构化数据资源',
        database: 'DDL数据资源'
      }
      return Object.keys(this.itemData).length
        ? `编辑${list[this.path]} - ` + this.itemData.name_zh
        : `新增${list[this.path]}`
    }
  },
  created () {
    this.init()
  },
  methods: {
    handleClose () {
      this.show = false
      this.init()
    },
    async init () {
      this.loading = true
      api.getResourceList({
        ...this.pageInfo,
        ...this.queryData,
        type: 'all'
      })
        .then(({ data }) => {
          this.items = data.records.map(e => {
            e.frequency = frequency.find(_e => _e.value === e.frequency)?.label
            e.data_sensitivity = sensitivity.find(_e => _e.value === e.data_sensitivity)?.label
            return e
          })
          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 (path) {
      this.itemData = {}
      this.path = path
      this.show = true
    },
    async handleEdit (item) {
      const { id } = item
      try {
        const { data } = await api.getResourceDetails({ id })
        // this.path = data.url ? data.type : 'database'
        this.path = data.type
        this.itemData = { ...data, id }
        this.show = true
      } catch (error) {
        this.$snackbar.error(error)
      }
    },
    handleDelete (ids) {
      if (Array.isArray(ids) && !ids.length) return this.$snackbar.warning('请选择要删除的选项')
      this.$confirm('提示', '是否删除该选项')
        .then(async () => {
          try {
            await api.deleteSource({ id: ids })
            this.$snackbar.success('删除成功')
            this.init()
          } catch (error) {
            this.$snackbar.error('删除失败')
          }
        })
    },
    pageHandleChange (page) {
      this.pageInfo.current = page
      this.init()
    },
    handleSort (val) {
      this.orders = val
      this.init()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
