<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { useUpload } from '@/composables/useUpload'

const emit = defineEmits(['upload'])

const { files, uploading, addFile } = useUpload({
  onUploadSucceed(file) {
    emit('upload', file)
  }
})
const file = computed(() => {
  return files.value[0]
})

const fileChange = async (event: Event) => {
  const element = event.currentTarget as HTMLInputElement
  const files = element.files
  if (!files) return
  const [file] = files
  if (file.name.indexOf('.mp4') === -1) {
    ElMessage('请上传mp4格式视频')
    return
  }
  addFile(file)
}
</script>
<template>
  <div>
    <div class="upload-video">
      <div class="upload-btn">
        本地文件
        <input accept=".mp4" type="file" id="fileUpload" @change="fileChange" />
      </div>
      <div class="demo-progress" v-if="uploading">
        <el-progress style="width: 340px" :percentage="file.progress" />
      </div>
      <!-- <div class="error video-info" v-if="uploadData.code === 2">
        <div class="name">上传失败（请重新选择文件进行上传）</div>
      </div> -->
      <div class="video-info" v-else>
        <div class="name">{{ file?.name }}</div>
      </div>
    </div>
    <div class="tips">推荐视频格式：帧率为25fps\输出码率为4M\输出格式为mp4，建议采用格式工厂等工具处理后上传。</div>
  </div>
</template>
<style lang="scss">
.demo-progress {
  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;
    .name {
      color: #333333;
      font-size: 16px;
      line-height: 100%;
    }
    .view {
      font-size: 16px;
      line-height: 100%;
      color: #399ee8;
      margin-left: 20px;
      cursor: pointer;
    }
  }
}
</style>
