<script setup lang="ts">
import VideoDetail from './VideoDetail.vue'
import { CircleClose } from '@element-plus/icons-vue'
import { useUpload } from '@/composables/useUpload'

const idShowMore = ref(false)

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

const { files, addFile, cancelUpload } = useUpload({
  multiple: true,
  onUploadSucceed(file) {
    emit('upload', file)
  }
})

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

const handleView = () => {
  idShowMore.value = true
}
// 删除上传文件
const deleteFile = function (index: number, item: any) {
  console.log(index, 'deleteFile', files)
  cancelUpload(item)
}
defineExpose({ files })
</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 files.slice(0, 3)" :key="index">
      <div class="video-info">
        <span class="name">{{ item.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(index, item)"><CircleClose /></el-icon>
      </div>
    </div>
    <el-link style="padding-top: 3px" :underline="false" type="primary" v-if="files.length > 3" @click="handleView"
      >共{{ files.length }}个文件，查看更多</el-link
    >
  </div>
  <VideoDetail :videoList="files" 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>
