<template>
  <TableLayout
    height="300px"
    class="flex-1"
    style="width:50%"
    :column="column"
    :data="list"
    :pageInfo="pageInfo"
    @page-info-change="fetch"
  >
    <template v-slot:type="{ row }">{{acceptMap[row.type].name}}</template>
    <template v-slot:size="{ row }">{{row.size?.formatFileSize()}}</template>
    <template v-slot:createTime="{row}">{{new Date(row.createTime).format('yyyy-MM-dd hh:mm')}}</template>
    <template v-slot:action="{ row }">
      <el-button @click="toPreview(row)" type="text">预览</el-button>
    </template>
  </TableLayout>
</template>

<script>
import TableLayout from '@/components/layout/TableLayout.vue'
import { findStatisticDataApi } from '@/api/statistic'
import { ACCEPT_MAP } from '@/utils'

export default {
  components: { TableLayout },

  data: () => ({ list: [], pageInfo: { current: 1, size: 10, total: 0 } }),

  watch: {
    '$parent.activeType': {
      immediate: true,
      handler (t) {
        this.type = t
        this.pageInfo.current = 1
        this.fetch()
      }
    }
  },

  created () {
    this.acceptMap = ACCEPT_MAP
    this.column = [
      { prop: 'name', label: '文件名' },
      { prop: 'classify', label: '分类' },
      { prop: 'type', label: '文件类型' },
      { prop: 'size', label: '文件大小' },
      { prop: 'createTime', label: '创建时间', width: '120' },
      { prop: 'creator', label: '上传人' },
      { prop: 'action', label: '操作', width: '100' }
    ]
  },

  methods: {
    async fetch () {
      const { list, total } = await findStatisticDataApi('res-details-table', { type: this.type, pageInfo: this.pageInfo })
      this.list = list
      this.pageInfo.total = total
    },

    toPreview (row) {
      this.$store.showResourcePreviewDialog(row.resourceId)
    }
  }
}
</script>
