<template>
  <div class="pa-3 white">
    <m-filter :filter="['name_zh', 'name_en', 'category']" categoryValue="DataOps" @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"
      @pageHandleChange="pageHandleChange"
      @sort="handleSort"
    >
      <template #group="{ item }">
        {{ item.group ? '是' : '否' }}
      </template>
      <template #name_zh="{item}">
        <div class="defaultLink" @click="handleDetails(item)">{{ item.name_zh }}</div>
      </template>
      <template #status="{ item }">
        <v-chip
          :color="item.status ? 'success' : 'error'"
          small
        >
          {{ item.status ? '已启用' : '已禁用'}}
        </v-chip>
      </template>
    </m-table>
  </div>
</template>

<script>
import MFilter from '../../dataGovernance/components/Filter'
import MTable from '@/components/List/table.vue'

import { api } from '@/api/dataGovernance'

export default {
  name: 'data-book-label',
  components: { MFilter, MTable },
  data () {
    return {
      loading: false,
      show: false,
      queryData: {
        name_zh: null,
        category: 'DataOps'
      },
      headers: [
        { text: '名称', value: 'name_zh' },
        { text: '分类', value: 'category' },
        { text: '适用范围', value: 'scope' },
        { text: '描述', value: 'describe' },
        { text: '标注数量', value: 'number' },
        { text: '是否业务域', value: 'group', align: 'center' },
        { text: '状态', value: 'status' },
        { text: '创建时间', value: 'create_time' }
      ],
      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.dataLabelList({
          ...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()
    },
    pageHandleChange (page) {
      this.pageInfo.current = page
      this.init()
    },
    handleSort (val) {
      this.orders = val
      this.init()
    },
    handleDetails (item) {
      this.$router.push(`${this.$route.path}/details/${item.id}/${encodeURIComponent(item.name_zh)}`)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
