<script setup lang="ts">
import type { File } from '../types'
import { VideoPlay } from '@element-plus/icons-vue'
import { isVideo } from '@/utils/index'

interface Props {
  preview?: boolean
  data: File[] | Record<string, File[]>
}
const props = defineProps<Props>()

const images = $computed(() => {
  if (!Array.isArray(props.data)) return []
  return props.data.filter(item => !isVideo(item.url))
})
const currentImages = $computed(() => {
  if (props.preview) return images
  return images.filter((item, index) => index < 3)
})
const imageSrcList = $computed(() => {
  return props.preview ? images.map(item => item.url) : []
})
const videos = $computed(() => {
  if (!Array.isArray(props.data)) return []
  return props.data.filter(item => isVideo(item.url))
})
</script>

<template>
  <div class="file-list">
    <div class="file-item" v-for="(item, index) in currentImages" :key="index">
      <p class="file-item__count" v-if="images.length > 3 && index === 0">共{{ images.length }}张</p>
      <el-image
        style="width: 100%; height: 100%"
        :src="item.url + '?x-oss-process=image/resize,m_fill,h_90,w_160'"
        :initial-index="index"
        :preview-src-list="imageSrcList"
        fit="cover" />
    </div>
    <template v-if="preview">
      <div class="video-item" v-for="(item, index) in videos" :key="index">
        <video :src="item.url" controls />
      </div>
    </template>
    <template v-else>
      <div class="file-item" v-for="(item, index) in videos" :key="index">
        <video :src="item.url" />
        <el-icon class="icon-video-play"><VideoPlay /></el-icon>
      </div>
    </template>
  </div>
</template>

<style lang="scss">
.file-list {
  margin-top: 20px;
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}
.file-item {
  position: relative;
  width: 160px;
  height: 90px;
  video {
    width: 100%;
    height: 100%;
  }
  .icon-video-play {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    font-size: 30px;
    color: #fff;
  }
}
.video-item {
  display: block;
  width: 320px;
  video {
    width: 100%;
    height: auto;
  }
}
.file-item__count {
  position: absolute;
  left: 0;
  top: 10px;
  padding: 0 10px;
  height: 18px;
  font-size: 12px;
  color: #fff;
  line-height: 18px;
  text-align: center;
  background: rgba(0, 0, 0, 0.8);
  z-index: 1000;
}
</style>
