<template>
  <div class="simple-oss-upload">
    <template v-if="fileList.length < limit">
      <div class="button" @click="onChooseFile">本地上传</div>
      <input
        v-show="false"
        id="file"
        ref="ossFileInput"
        type="file"
        :accept="accept"
        multiple
        @change="onFileChange"
      />
    </template>
    <div class="file-list">
      <template v-for="(file, index) in fileList">
        <div class="file" :key="file.uuid">
          <div class="name">{{ file.name }}</div>
          <div class="control">
            <span class="play" @click="onPlay(file)">播放</span>
            <span class="remove" @click="onAbortUpload(file, index)">删除</span>
          </div>
          <div class="progress">
            <el-progress
              :percentage="file.percentage"
              :show-text="false"
              color="#F3920D"
            ></el-progress>
            <div class="status">
              <span>{{ file.percentage }}%</span>
              <span>上传中</span>
            </div>
          </div>
        </div>
      </template>
    </div>
    <!-- <template v-if="videoDialog">
      <div class="video-dialog">
        <video :src="currentVideoUrl"></video>
      </div>
      <div class="mask"></div>
    </template> -->
    <SimpleVideoPlayer
      :videoSrc="currentVideoUrl"
      ref="videoPlayer"
    ></SimpleVideoPlayer>
  </div>
</template>

<script>
import OssUploadUtils from './upload'
export default {
  props: {
    accept: {
      type: String,
      default: '.mp4',
    },
    limit: {
      type: Number,
      default: 9,
    },
  },
  data() {
    return {
      videoDialog: true,
      currentVideoUrl: '',
      ossUpload: null,
      fileList: [{ percentage: 90, name: 'a8b25605e0db0c9be8362c38d4.mp4' }],
    }
  },
  methods: {
    // 播放视频
    onPlay(rawFile) {
      this.currentVideoUrl = URL.createObjectURL(rawFile.file)
      this.$refs.videoPlayer.open()
    },
    // 选择文件
    onChooseFile() {
      this.$nextTick(() => {
        this.$refs.ossFileInput.click()
      })
    },
    // 文件选中
    onFileChange(e) {
      this.ossUpload = new OssUploadUtils(this, {
        success: this.onSuccess,
        progress: this.onProgress,
        faild: this.onFaild,
      })
      this.ossUpload.upload(e.target.files[0])
    },
    // 取消上传
    async onAbortUpload(file, index) {
      try {
        await this.$confirm('确定删除该视频?', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        })
        this.fileList.splice(index, 1)
        const { checkpoint } = file
        this.ossUpload.abortMultipartUpload(checkpoint)
      } catch (error) {
        console.log(error)
      }
    },
    // 上传进度
    onProgress(file, fileList) {
      this.fileList = fileList
      this.$emit('progress', { file, fileList })
    },
    // 上传成功
    onSuccess(file, fileList) {
      console.log(file)
      this.fileList = fileList
      this.$emit('success', { file, fileList })
    },
    // 上传失败
    onFaild(file, fileList) {
      this.fileList = fileList
      this.$emit('faild', { file, fileList })
    },
  },
}
</script>

<style lang="scss" scoped>
@mixin ellipsis($line: 1) {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: $line;
  -webkit-box-orient: vertical;
}

@media screen and (min-width: 768px) {
  .simple-oss-upload {
    .button {
      width: 104px;
      height: 36px;
      text-align: center;
      line-height: 36px;
      background: #f3920d;
      color: #fff;
      border-radius: 4px;
      cursor: pointer;
    }

    .video-dialog {
      position: fixed;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 800px;
      height: 600px;
      z-index: 11;
      background: #fff;
      border-radius: 4px;

      video {
        display: block;
        width: 100%;
        height: 100%;
      }
    }

    .mask {
      position: fixed;
      left: 0;
      top: 0;
      width: 100vw;
      height: 100vh;
      background: #000;
      opacity: 0.5;
      z-index: 10;
    }

    .file-list {
      line-height: initial;
      margin-top: 16px;
      .file {
        padding: 8px 0;
        position: relative;
        .name {
          font-size: 16px;
          margin-bottom: 18px;
          margin-right: 80px;
          color: #666;
        }
        .control {
          position: absolute;
          top: 8px;
          right: 0;
          span {
            font-size: 16px;
            cursor: pointer;

            &.play {
              color: #1890ff;
              margin-right: 12px;
            }

            &.remove {
              color: #f3920d;
            }
          }
        }
        .progress {
          padding-right: 120px;
          position: relative;
          .status {
            position: absolute;
            right: 0;
            top: 50%;
            transform: translateY(-53%);
            font-size: 16px;
            color: #f3920d;
          }
        }
      }
    }
  }
}
</style>
