<template>
  <!-- 元数据 -->
  <div class="pa-3 white">
    <filter-list @search="handleSearch" />
    <table-list
      class="mt-3"
      :loading="loading"
      :headers="headers"
      :items="items"
      :total="total"
      :page-info="pageInfo"
      :is-tools="false"
      :show-select="false"
      @pageHandleChange="pageHandleChange"
      @sort="handleSort"
    >
      <template #status="{ item }">
        <v-chip
          :color="item.status ? 'success' : 'error'"
          small
        >
          {{ item.status ? '已启用' : '已禁用'}}
        </v-chip>
      </template>
      <template #tag="{ item }">
        {{ item.tag.map(e => e.name_zh).join(',') }}
      </template>
      <template v-slot:navBtn>
        <v-btn class="elevation-5 buttons mr-3" color="primary" rounded @click="handleAdd">
          <v-icon left>mdi-plus</v-icon>
          新增元数据
        </v-btn>
      </template>
      <template #actions="{ item }">
        <v-btn
          text
          :disabled="loading"
          color="primary"
          @click="handleEdit(item)">编辑</v-btn>
        <v-btn
          text
          :disabled="loading"
          color="error"
          @click="handleDelete(item.id)">删除</v-btn>
        <v-btn
          text
          :disabled="loading"
          :color="item.status ? 'error' : 'success'"
          @click="handleChangeStatus(item)"
        >
          {{ item.status ? '禁用' : '启用' }}
        </v-btn>
      </template>
    </table-list>

    <edit-dialog :visible.sync="show" :title="title" @submit="handleSubmit">
      <edit v-if="show" v-loading="submitLoading" ref="form" class="mt-5" :item-data="itemData"></edit>
    </edit-dialog>
  </div>
</template>

<script>
import FilterList from '../components/Filter'
import TableList from '@/components/List/table'
import EditDialog from '@/components/Dialog'
import Edit from './components/edit'

import { api } from '@/api/dataGovernance'
export default {
  name: 'meta-data',
  components: { FilterList, TableList, EditDialog, Edit },
  data () {
    return {
      submitLoading: false,
      show: false,
      loading: false,
      headers: [
        { text: '中文名', value: 'name_zh' },
        { text: '英文名', value: 'name_en' },
        { text: '分类', value: 'category', sortable: false },
        { text: '描述', value: 'describe', sortable: false },
        { text: '标签', value: 'tag', sortable: false },
        { text: '状态', value: 'status' },
        { text: '血缘关系数量', value: 'blood_count', align: 'center' },
        { text: '创建时间', value: 'create_time' },
        { text: '操作', value: 'actions', sortable: false }
      ],
      items: [],
      total: 0,
      pageInfo: {
        size: 10,
        current: 1
      },
      query: {},
      orders: [],
      itemData: {}
    }
  },
  computed: {
    title () {
      return this.itemData.id ? '编辑元数据' : '新增元数据'
    }
  },
  created () {
    this.init()
  },
  methods: {
    async init () {
      this.loading = true
      try {
        const { data } = await api.getMetaDataList({ ...this.pageInfo, ...this.query })
        this.total = data.total
        this.items = data.records
      } catch (error) {
        this.$snackbar.error(error)
      } finally {
        this.loading = false
      }
    },
    handleAdd () {
      this.itemData = {}
      this.show = true
    },
    async handleEdit ({ id }) {
      try {
        const { data } = await api.getMetadataDetails({ id })
        if (!data.length) {
          this.$snackbar.error('找不到元数据信息')
          return
        }
        this.itemData = {
          id,
          ...data.pop()
        }
        this.show = true
      } catch (error) {
        this.$snackbar.error(error)
      }
    },
    handleDelete (id) {
      this.$confirm('提示', '是否确定删除该项元数据').then(async () => {
        try {
          await api.deleteMetadata({ id })
          this.$snackbar.success('删除成功')
          this.init()
        } catch (error) {
          this.$snackbar.error(error)
        }
      })
    },
    async handleChangeStatus (item) {
      this.loading = true
      try {
        const { status, ...obj } = item
        await api.metadataUpdate({
          ...obj,
          status: 1 ^ status
        })
        this.$snackbar.success('操作成功')
        this.init()
      } catch (error) {
        this.loading = false
        this.$snackbar.error(error)
      }
    },
    async handleSubmit () {
      const obj = this.$refs.form.getValue()
      if (!obj) {
        return
      }
      try {
        this.submitLoading = true
        if (obj.id) {
          await api.metadataUpdate(obj)
        } else {
          await api.metadataAdd(obj)
        }
        this.$snackbar.success('操作成功')
        this.show = false
        this.init()
      } catch (error) {
        this.$snackbar.error(error)
      } finally {
        this.submitLoading = false
      }
    },
    handleSort (val) {
      this.orders = val
      this.init()
    },
    handleSearch (obj) {
      Object.assign(this.query, obj)
      this.pageInfo.current = 1
      this.init()
    },
    pageHandleChange (index) {
      this.pageInfo.current = index
      this.init()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
