<script setup lang="ts">
import VideoDetail from './VideoDetail.vue'
import { CircleClose } from '@element-plus/icons-vue'
import type { VideoUploadItem } from '@/utils/videoUpload'
import { createVideoUploader } from '@/utils/videoUpload'

const idShowMore = ref(false)
const emit = defineEmits(['upload', 'canClose'])

const { files: fileList, uploading, addFile, cancelUpload } = createVideoUploader({
  multiple: true,
  onUploadStarted() {
    emit('canClose', { closeStatus: false })
  },
  onUploadSucceed(file) {
    emit('upload', file)
  },
  onUploadEnd() {
    emit('canClose', { closeStatus: true })
  },
})

const fileChange = (event: Event) => {
  const element = event.currentTarget as HTMLInputElement
  const files = element.files
  if (!files) return
  for (const file of files) {
    addFile(file)
  }
}

const handleView = () => {
  idShowMore.value = true
}

const deleteFile = (item: VideoUploadItem) => {
  cancelUpload(item)
}

defineExpose({ files: fileList, uploading, cancelUpload })
</script>

<template>
  <div class="upload-video" style="display: flex; flex-direction: column; align-items: flex-start">
    <div class="upload-btn">
      本地文件
      <input accept=".mp4" type="file" id="fileUpload" multiple @change="fileChange" />
    </div>
    <div class="tips">推荐视频格式：帧率为25fps\输出码率为4M\输出格式为mp4，建议采用格式工厂等工具处理后上传。</div>
    <div v-for="(item, index) in fileList.slice(0, 3)" :key="index">
      <div class="video-info">
        <span class="name">{{ item.file?.name }}</span>
        <el-progress style="width: 200px" :percentage="item.progress" class="view" />
        <div v-if="item.progress === 100">上传成功</div>
        <el-icon v-else @click="deleteFile(item)"><CircleClose /></el-icon>
      </div>
    </div>
    <el-link style="padding-top: 3px" :underline="false" type="primary" v-if="fileList.length > 3" @click="handleView"
      >共{{ fileList.length }}个文件，查看更多</el-link
    >
  </div>
  <VideoDetail :videoList="fileList" v-model:modelValue="idShowMore" v-if="idShowMore === true" />
</template>

<style lang="scss">
.demo-progress {
  min-width: 350px;
  display: flex;
  span {
    color: #999999;
    font-size: 14px;
    transform: translateX(-20px);
  }
}
#fileUpload {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0.00001;
}
.tips {
  font-size: 12px;
  line-height: 100%;
  color: #999999;
  margin-top: 8px;
}
.upload-video {
  display: flex;
  align-items: center;
  .upload-btn {
    position: relative;
    width: 94px;
    line-height: 36px;
    background: rgba(250, 223, 230, 0.39);
    border: 1px solid #aa1941;
    border-radius: 20px;
    font-size: 14px;
    color: #aa1941;
    margin-right: 30px;
    text-align: center;
  }
  .video-info {
    display: flex;
    align-items: center;
    margin-top: 10px;
    .name {
      color: #333333;
      font-size: 14px;
      line-height: 1.5;
    }
    .view {
      font-size: 16px;
      line-height: 100%;
      margin-left: 20px;
      cursor: pointer;
    }
  }
}
</style>
