<template>
  <div class="pa-3 white" :class="{content: switchShow}">
    <m-filter switchover :filter="['name_zh', 'name_en', 'category', 'tag', 'level']" @search="handleSearch" v-model="switchShow"/>
    <m-table
      v-if="!switchShow"
      class="mt-3"
      :loading="loading"
      :headers="headers"
      :items="items"
      :total="total"
      :page-info="pageInfo"
      :show-select="false"
      :is-tools="false"
      @pageHandleChange="pageHandleChange"
      @sort="handleSort"
    >
      <template #status="{ item }">
        <v-chip
          :color="item.status ? 'success' : 'error'"
          small
        >
          {{ item.status ? '已启用' : '已禁用'}}
        </v-chip>
      </template>
      <template #tag="{ item }">
        <m-graph-drill
          :id="item.tag?.id"
          :title="`${item.tag?.name_zh}血缘图谱`"
        >{{ item.tag?.name_zh }}</m-graph-drill>
      </template>
      <template #blood_count="{ item }">
        <m-graph-drill
          :id="item.id"
          :title="`${item.name_zh}血缘图谱`"
          :to-api="api.getModelGraph"
        >{{ item.blood_count }}</m-graph-drill>
      </template>
      <template #name_zh="{ item }">
        <div class="defaultLink" @click="handleDetails(item)">{{ item.name_zh }}</div>
      </template>
      <template #alias="item">
        <v-chip v-for="chip in item.alias" :key="chip">{{ chip }}</v-chip>
      </template>
    </m-table>
    <div v-else style="flex: 1" class="mt-3">
      <m-graph-list ref="graph" :to-api="api.getModelListToGraph" download-image-file-name="数据模型图谱"></m-graph-list>
    </div>
  </div>
</template>

<script>
import MFilter from '../../dataGovernance/components/Filter'
import MTable from '@/components/List/table.vue'
import MGraphDrill from '../components/mGraphDrill'
import MGraphList from '../components/mGraphList'
import { api } from '@/api/dataGovernance'
import {
  frequency,
  sensitivity
} from '@/utils/dataGovernance'
export default {
  name: 'data-book-model',
  components: { MFilter, MTable, MGraphDrill, MGraphList },
  data () {
    return {
      api,
      switchShow: false,
      loading: false,
      show: false,
      queryData: {
        name_zh: null
      },
      headers: [
        { text: '中文名', value: 'name_zh' },
        { text: '英文名', value: 'name_en' },
        { text: '分类', value: 'category' },
        { text: '标签', value: 'tag' },
        { text: '层级', value: 'level' },
        { text: '描述', value: 'describe' },
        { text: '状态', value: 'status' },
        { text: '血缘关系数量', value: 'blood_count', align: 'center' },
        { text: '创建时间', value: 'create_time' }
      ],
      itemData: {},
      items: [],
      // orders: [],
      pageInfo: {
        size: 10,
        current: 1
      },
      total: 0
    }
  },
  created () {
    this.init()
  },
  methods: {
    handleClose () {
      this.show = false
      this.init()
    },
    async init () {
      this.loading = true
      api.getModelList({
        ...this.pageInfo,
        ...this.queryData
      })
        .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) {
      if (this.switchShow) {
        this.$nextTick(() => {
          this.$refs.graph.init(val)
        })
        return
      }
      Object.assign(this.queryData, val)
      this.pageInfo.current = 1
      this.init()
    },
    handleDetails (item) {
      this.$router.push(`${this.$route.path}/details/${item.id}/${item.name_zh}`)
    },
    pageHandleChange (page) {
      this.pageInfo.current = page
      this.init()
    },
    handleSort (val) {
      this.orders = val
      this.init()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
