<template>
  <!-- 元数据 -->
  <div class="pa-3 white">
    <filter-list :filter="['name_zh', 'name_en', 'category', 'tag']" @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>
        <v-btn class="elevation-5 buttons" color="primary" rounded @click="showCombination = true">
          <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" :footer="false" :fullscreen="true">
      <edit v-if="show" v-loading="submitLoading" ref="form" :item-data="itemData" @close="handleClose"></edit>
    </edit-dialog>

    <edit-dialog
      :visible.sync="showCombination"
      title="新增组合业务域"
      :footer="true"
      widthType="1"
      @close="showCombination = false"
      @submit="handleCombinationSubmit"
    >
      <CombinationForm v-if="showCombination" ref="combinationFormRef"></CombinationForm>
    </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 CombinationForm from './components/combination'

import { api } from '@/api/dataGovernance'
export default {
  name: 'businessDomain',
  components: { FilterList, TableList, EditDialog, Edit, CombinationForm },
  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: {},
      showCombination: false
    }
  },
  computed: {
    title () {
      return this.itemData.id ? '编辑业务域' : '新增业务域'
    }
  },
  created () {
    this.init()
  },
  methods: {
    handleClose () {
      this.show = false
      this.init()
    },
    async init () {
      this.loading = true
      try {
        const { data } = await api.getBusinessDomainList({ ...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.getBusinessDomainDetail({ id })
        if (!data || !Object.keys(data).length) {
          this.$snackbar.error('找不到业务域信息')
          return
        }
        this.itemData = data
        this.show = true
      } catch (error) {
        this.$snackbar.error(error)
      }
    },
    handleDelete (id) {
      this.$confirm('提示', '是否确定删除该项业务域').then(async () => {
        try {
          await api.deleteBusinessDomain({ id })
          this.$snackbar.success('删除成功')
          this.init()
        } catch (error) {
          this.$snackbar.error(error)
        }
      })
    },
    async handleChangeStatus ({ id }) {
      this.loading = true
      try {
        const { data } = await api.getBusinessDomainDetail({ id })
        if (!data || !Object.keys(data).length) {
          this.$snackbar.error('业务域不存在')
          return
        }
        const { status, ...obj } = data
        await api.updateBusinessDomain({
          ...obj,
          status: 1 ^ status
        })
        this.$snackbar.success('操作成功')
        this.init()
      } catch (error) {
        this.loading = false
        this.$snackbar.error(error)
      }
    },
    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()
    },
    // 组合业务域提交
    async handleCombinationSubmit () {
      const form = this.$refs.combinationFormRef.getValue()
      if (!form) return this.$snackbar.warning('请将表单内容填写完整!')
      if (!form.id_list.length) return this.$snackbar.warning('请选择要组合的元数据项!')
      try {
        await api.combinationCreateBusinessDomain(form)
        this.$snackbar.success('组合业务域创建成功')
        this.showCombination = false
        this.init()
      } catch (error) {
        this.$snackbar.error(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
