<template>
  <div v-loading="loading">
    <el-upload
      class="avatar-uploader"
      :action="uploadUrl"
      :headers="headers"
      :data="params"
      :name="name"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :on-remove="handleRemove"
      :limit="1"
    >
      <template v-if="fellUrl">
        <video v-if="isVideo" :src="fellUrl" class="video" controls>
          <source :src="fellUrl" type="video/mp4">
        </video>
        <img v-else :src="fellUrl" class="avatar">
      </template>

      <i v-else class="el-icon-plus avatar-uploader-icon"/>
      <i v-if="fellUrl" class="el-icon-delete avatar-uploader-icon" @click.stop="handleRemove"/>
    </el-upload>
  </div>
</template>
<script>
import { getToken } from '@/utils/auth'

export default {
  name: 'Index',

  props: {
    value: {
      type: String,
      default: ''
    },
    name: {
      type: String,
      default: 'file'
    }
  },
  data() {
    return {
      loading:false,
      params: {},
      labelWidth: '120px',
      isVideo: false,
      uploadUrl: window.global.baseURL + 'upload/uploadVideo',
      // uploadUrl: window.global.uploadURL + 'upload/uploadVideo',
      headers: {
        'Authorization': getToken()
      },
      fellUrl: this.value
    }
  },
  watch: {
    value(val) {
      this.fellUrl = Array.isArray(val) ? val[0] : val
      // 判断是否是视频类型
      this.isVideo = this.isVideoLink(val)
    }
  },
  methods: {
    isVideoLink(url) {
      const videoExtensions = /\.(mp4|m4v|webm|mov|avi|wmv)$/i
      return videoExtensions.test(url)
    },
    handleRemove(file, fileList) {
      this.fellUrl = ''
      this.$emit('input', '')
    },
    handleAvatarSuccess(res, file) {
      if (res.code === 0) {
        this.fellUrl = res.data.path
        this.$emit('input', res.data.path)
      } else {
        this.$message.error(res.msg)
      }
      this.loading=false
    },
    beforeAvatarUpload(file) {
      this.loading=true
    // console.log(file.type)
    // const isJPG =
    //   file.type === 'image/jpeg' ||
    //   file.type === 'image/png' ||
    //   file.type === 'image/jpeg'
    // // 上传头像图片大小不能超过2MB!
    // const isLt2M = file.size / 1024 / 1024 < 2
    // if (!isLt2M) {
    //   this.$message.error('上传头像图片大小不能超过 2MB!')
    // }
    // if (!isJPG) {
    //   this.$message.error('上传头像图片只能是 JPG/PNG/JPEG 格式!')
    // }
    // if (!isLt2M) {
    //   this.$message.error('上传头像图片大小不能超过 2MB!')
    // }
    // return isJPG && isLt2M
    }
  }
}
</script>

<style scoped lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 180px !important;
  height: 180px;
  line-height: 180px;
  text-align: center;
}

.avatar {
  z-index: 1;
  width: 180px !important;
  height: 180px !important;
  display: block !important;
  border-radius: 16px;
}

.video {
  z-index: 1;
  width: 180px !important;
  height: 180px !important;
  display: block !important;
  border-radius: 16px;
}

.el-icon-delete {
  position: absolute;
  z-index: 10;
  left: 0;
  top: 0;
  color: #fff;

  &:hover {
    color: #409EFF;
  }
}

.avatar-uploader-icon {
  border: 1px solid #ccc;
  border-radius: 16px;
}
</style>
