<template>
  <schema-table :schema="schema">
    <div
      v-if="more"
      class="l-flex__none l-flex--row has-top-padding"
    >
      <div class="l-flex__auto" />
      <button
        class="l-flex__none c-sibling-item o-button"
        @click="onNext"
      >
        {{ btnMsg }}
      </button>
    </div>
    <preview-dialog ref="previewDialog" />
  </schema-table>
</template>

<script>
import {
  State,
  AssetType
} from '@/constant'
import {
  parseByte,
  parseDuration
} from '@/utils'
import mixin from './mixin'

export default {
  name: 'WorkflowReviewAssets',
  mixins: [mixin],
  data () {
    return {
      type: 'minio'
    }
  },
  computed: {
    schema () {
      return {
        condition: { pageSize: this.list.length },
        list: this.getList,
        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: 'ai', label: 'AI审核', type: 'tag', width: 100 },
          ...this.reviewCol
        ]
      }
    }
  },
  created () {
    this.list = this.transform(this.workflow.minios)
    if (!this.list.some(({ pass }) => !pass)) {
      this.$emit('next')
    }
  },
  methods: {
    transform (arr) {
      return arr.map(this.transformItem).sort((a, b) => {
        if (a.file.type === b.file.type) {
          return a.status - b.status
        }
        return a.file.type - b.file.type
      })
    },
    transformItem (asset) {
      let thumbnail
      if (asset.type === AssetType.IMAGE) {
        thumbnail = asset.keyName
      } else if (asset.type === AssetType.VIDEO && asset.screenshot !== 'analyzing') {
        thumbnail = asset.screenshot
      }
      return {
        pass: asset.status !== State.SUBMITTED,
        id: asset.keyName,
        typeName: [null, '图片', '视频', '音频', 'PPT', 'PDF'][asset.type],
        file: {
          type: asset.type,
          url: asset.keyName,
          thumbnail,
          files: (asset.childrenData || []).map(({ type, keyName }) => {
            return { type, url: keyName }
          })
        },
        duration: parseDuration(asset.duration),
        size: parseByte(asset.size),
        ai: this.getAIState(asset),
        status: asset.status
      }
    },
    getAIState ({ aiAuditState: status, aiAuditMsg: msg }) {
      switch (status) {
        case 1:
          return {
            type: 'danger',
            label: '不合规'
          }
        case 2:
          return {
            type: 'warning',
            label: '疑似',
            msg
          }
        case 3:
          return {
            type: 'info',
            label: '审核失败',
            msg
          }
        case 4:
        case 5:
        case 6:
          return {
            type: 'primmary',
            label: '审核中'
          }
        case 7:
          return {
            type: 'success',
            label: '通过'
          }
        case 8:
          return {
            type: 'info',
            label: '无法审核',
            msg
          }
        case 9:
          return {
            type: 'info',
            label: '未开启'
          }
        default:
          return null
      }
    },
    onViewAsset (asset) {
      this.$refs.previewDialog.show(asset)
    },
    onView ({ file }) {
      this.onViewAsset(file)
    }
  }
}
</script>
