<template>
  <div class="pa-3 white">
    <m-filter :option="filter" @search="handleSearch" />
    <m-table
      class="mt-3"
      :loading="loading"
      :headers="headers"
      :items="items"
      :total="total"
      :show-select="false"
      :is-tools="false"
      :page-info="pageInfo"
      @edit="handleEdit"
      @delete="handleDelete"
      @pageHandleChange="pageHandleChange"
      @sort="handleSort"
    >
      <!-- <template #name="{ item }">
        <span class="defaultLink" @click="handleDetails(item)">{{ item.name }}</span>
      </template> -->
      <template #navBtn>
        <v-btn rounded color="primary" class="buttons" elevation="5" @click="handleAdd">
          <v-icon left>mdi-plus</v-icon>
          新增资产
        </v-btn>
      </template>
    </m-table>
    <m-dialog :title="title" :visible.sync="show" @submit="handleSubmit">
      <edit-page v-if="show" ref="edit" :item-data="itemData"></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'
export default {
  name: 'data-asset',
  components: { MFilter, MTable, MDialog, EditPage },
  data () {
    return {
      loading: false,
      show: false,
      filter: {
        list: [
          { type: 'textField', value: null, label: '分类', key: 'type' },
          { type: 'datePicker', value: null, label: '时间', key: 'time' }
        ]
      },
      queryData: {
        type: null,
        time: null
      },
      headers: [
        { text: '资产名称', align: 'start', value: 'name' },
        { text: '分类', align: 'start', value: 'type' },
        { text: '制作单位', align: 'start', value: 'institution' },
        { text: '制作时间', align: 'start', value: 'time' },
        { text: '评估机构', align: 'start', value: 'organization' },
        { text: '估值（万元）', align: 'start', value: 'amount' },
        { text: '操作', align: 'start', value: 'actions' }
      ],
      itemData: {},
      items: [
        {
          id: '1',
          name: '最近五年市场销售数据集',
          type: '市场营销',
          institution: '市场部',
          time: '2024年08月11日',
          organization: '广州数据交易所',
          amount: '100'
        }
      ],
      orders: [],
      pageInfo: {
        size: 10,
        current: 1
      },
      total: 1
    }
  },
  computed: {
    title () {
      return Object.keys(this.itemData).length ? '编辑数据资产' : '新增数据资产'
    }
  },
  created () {
    this.init()
  },
  methods: {
    async init () {
      try {
        this.loading = true
      } catch (error) {

      } finally {
        this.loading = false
      }
    },
    handleDetails ({ id, name }) {
      this.$router.push(`${this.$route.path}/details/${id}/${name}`)
    },
    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
    },
    handleSubmit () {},
    handleDelete (ids) {
      if (Array.isArray(ids) && !ids.length) return this.$snackbar.warning('请选择要删除的选项')
      this.$confirm('提示', '是否删除该选项')
        .then(async () => {
          try {
            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>
