<template>
  <div class="pa-3 white">
    <m-filter @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"
      @add="handleAdd"
      @edit="handleEdit"
      @delete="handleDelete"
      @pageHandleChange="pageHandleChange"
      @sort="handleSort"
    >
      <template #status="{ item }">
        <v-chip
          :color="item.status ? 'success' : 'error'"
          small
        >
          {{ item.status ? '已启用' : '已禁用'}}
        </v-chip>
      </template>
      <template #navBtn>
        <v-btn class="elevation-5 mr-2 buttons" rounded color="primary" @click="handleAdd">
          <v-icon left>mdi-plus</v-icon>
          新增指标
        </v-btn>
      </template>
      <template #tag="{ item }">
        {{ item.tag?.name_zh }}
      </template>
    </m-table>
    <m-dialog
      :visible.sync="show"
      :title="title"
      :fullscreen="true"
      body-style="display: flex;padding: 0;"
      :footer="false"
    >
      <edit-page v-if="show" ref="edit" :item-data="itemData" @close="handleClose"></edit-page>
    </m-dialog>
  </div>
</template>

<script>
import MFilter from '../components/Filter'
import MTable from '@/components/List/table.vue'
import MDialog from '@/components/Dialog'
import EditPage from './components/edit'

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

export default {
  name: 'data-indicator',
  components: {
    MFilter,
    MTable,
    MDialog,
    EditPage
  },
  data () {
    return {
      loading: false,
      show: false,
      queryData: {
        name_zh: null
      },
      headers: [
        { text: '名称', value: 'name_zh' },
        { text: '英文名称', value: 'name_en' },
        { text: '分类', value: 'category' },
        { text: '所属机构', value: 'organization' },
        { text: '负责人', value: 'leader' },
        { text: '更新频率', value: 'frequency' },
        { text: '数据敏感度', value: 'data_sensitivity' },
        { text: '标签', value: 'tag' },
        { text: '描述', value: 'describe' },
        { text: '数据模型', value: 'data_model' },
        { text: '指标计算规则', value: 'metricRulesText' },
        { text: '状态', value: 'status' },
        { text: '创建时间', value: 'create_time' },
        { text: '操作', value: 'actions' }
      ],
      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.dataIndicatorList({ ...this.pageInfo, ...this.queryData })
        const parser = new DOMParser()
        this.items = data.records.map(e => {
          const doc = parser.parseFromString(e.metric_rules, 'text/html')
          e.metricRulesText = doc.body.innerText
          return e
        })
        this.total = data.total
      } catch (error) {
        this.$snackbar.error(error)
      } finally {
        this.loading = false
      }
    },
    handleClose () {
      this.show = false
      this.init()
    },
    handleSearch (val) {
      Object.assign(this.queryData, val)
      this.pageInfo.current = 1
      this.init()
    },
    handleAdd () {
      this.itemData = {}
      this.show = true
    },
    async handleEdit (item) {
      // this.itemData = item
      // this.show = true
      try {
        const { data } = await api.dataIndicatorDetails({ id: item.id })
        this.itemData = { ...data.data_metric, id: item.id }
        this.show = true
      } catch (error) {
        this.$snackbar.error(error)
      }
    },
    handleDelete (ids) {
      if (Array.isArray(ids) && !ids.length) return this.$snackbar.warning('请选择要删除的选项')
      this.$confirm('提示', '是否删除该选项')
        .then(async () => {
          try {
            await api.deleteDataIndicator({ id: ids })
            this.$snackbar.success('删除成功')
            this.init()
          } catch (error) {
            this.$snackbar.error('删除失败')
          }
        })
    },
    pageHandleChange (page) {
      this.pageInfo.current = page
      this.init()
    },
    handleSort (val) {
      this.orders = val
      this.init()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
