<template>
  <schema-table
    ref="table"
    :schema="schema"
  >
    <preview-dialog ref="previewDialog" />
  </schema-table>
</template>

<script>
import {
  getAssets,
  resolveAsset
} from '@/api/asset'
import {
  parseByte,
  parseDuration
} from '@/utils'
import { State } from '@/constant'
import mixin from './mixin'

export default {
  name: 'ReviewAsset',
  mixins: [mixin],
  data () {
    return {
      schema: {
        condition: { status: State.REVIEW },
        list: getAssets,
        transform: this.transform,
        cols: [
          { prop: 'typeName', label: '类型', align: 'center', width: 80 },
          { prop: 'file', type: 'asset', on: this.onViewAsset },
          { prop: 'originalName', label: '' },
          { prop: 'duration', label: '时长' },
          { prop: 'size', label: '文件大小' },
          { prop: 'createBy', label: '申请人' },
          { prop: 'createTime', label: '提交时间', 'min-width': 100 },
          { type: 'invoke', width: 160, render: [
            { label: '查看', on: this.onView },
            { label: '通过', on: this.onResolve },
            { label: '驳回', on: this.onReject }
          ] }
        ]
      }
    }
  },
  methods: {
    resolve: resolveAsset,
    transform (asset) {
      const { type } = asset
      asset.typeName = [null, '图片', '视频', '音频'][type]
      asset.file = {
        type: asset.type,
        url: asset.keyName,
        thumbnail: asset.thumbnail
      }
      asset.duration = parseDuration(asset.duration)
      asset.size = parseByte(asset.size)
      asset.createBy = asset.userName || asset.createBy
      return asset
    },
    onView ({ file }) {
      this.onViewAsset(file)
    },
    onViewAsset (asset) {
      this.$refs.previewDialog.show(asset)
    }
  }
}
</script>
