<template>
  <el-dialog
    :visible.sync="showDetail"
    custom-class="c-preview"
    @click.native="showDetail = false"
    v-on="$listeners"
  >
    <template v-if="showDetail">
      <auto-image
        v-if="isImage"
        class="o-image--preview"
        :src="assetUrl"
        retry
      />
      <video
        v-if="isVideo"
        class="o-video--preview"
        :src="assetUrl"
        autoplay
        controls
        @click.stop
      />
      <audio
        v-if="isAudio"
        :src="assetUrl"
        autoplay
        controls
        @click.stop
      />
    </template>
  </el-dialog>
</template>

<script>
import { getAssetUrl } from '@/api/asset'
import { AssetType } from '@/constant'

export default {
  name: 'PreviewDialog',
  data () {
    return {
      showDetail: false,
      source: null
    }
  },
  computed: {
    isImage () {
      return this.source?.type === AssetType.IMAGE
    },
    isVideo () {
      return this.source?.type === AssetType.VIDEO
    },
    isAudio () {
      return this.source?.type === AssetType.AUDIO
    },
    assetUrl () {
      return this.source && getAssetUrl(this.source.url)
    }
  },
  methods: {
    show (source) {
      this.source = source
      this.showDetail = true
    }
  }
}
</script>

<style lang="scss" scoped>
.o-image--preview {
  min-width: 96px;
  max-width: 100%;
  min-height: 96px;
  max-height: 80vh;
  color: #fff;
  font-size: 32px;
  line-height: 0;
}

.o-video--preview {
  width: 100%;
  max-height: 80vh;
}
</style>
