<template>
  <div class="white pa-3">
    <m-filter :option="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"
      @pageHandleChange="pageHandleChange"
    >
      <template #name_zh="{ item }">
        <span class="defaultLink" @click="handleDetail(item)">{{ item.name_zh }}</span>
      </template>
      <template #status="{ item }">
        <v-chip small :color="item.status === 'active' ? 'success' : 'error'">
          {{ item.status === 'active' ? '启用' : '禁用' }}
        </v-chip>
      </template>
    </m-table>
  </div>
</template>

<script>
import MFilter from '@/components/Filter'
import MTable from '@/components/List/table.vue'
import { api } from '@/api/dataGovernance'
export default {
  name: 'dataBookDataProcess',
  components: { MFilter, MTable },
  data () {
    return {
      loading: false,
      show: false,
      filter: {
        list: [
          { type: 'textField', value: '', label: '关键词', key: 'title' }
        ]
      },
      queryData: {
        title: null
      },
      headers: [
        { text: '中文名称', align: 'start', value: 'name_zh' },
        { text: '英文名称', align: 'start', value: 'name_en' },
        { text: '状态', align: 'center', value: 'status' },
        { text: '分类', align: 'center', value: 'category' },
        { text: '作者', align: 'center', value: 'leader' },
        { text: '机构', align: 'center', value: 'organization' },
        { text: '描述', align: 'start', value: 'describe' },
        { text: '创建时间', align: 'start', value: 'created_at' },
        { text: '更新时间', align: 'start', value: 'updated_at' }
      ],
      items: [],
      orders: [],
      pageInfo: {
        size: 10,
        current: 1
      },
      total: 0
    }
  },
  created () {
    this.init()
  },
  methods: {
    async init () {
      this.loading = true
      try {
        const { data } = await api.getDataFlowList({
          page: this.pageInfo.current,
          page_size: this.pageInfo.size,
          search: this.queryData.title || undefined
        })
        this.items = data.list
        this.total = data.pagination.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()
    },
    handleDetail (item) {
      this.$router.push(`${this.$route.path}/details/${item.id}/${item.name_zh}`)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
