<template>
  <div>
    <el-upload
      accept=".jpg,.jpeg,.png.JPG,.JPEG,.PNG"
      :class="{disabled:uploadDisabled}"
      action="http://111.203.232.175:8088/mall/file/image/upload"
      list-type="picture-card"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :on-success="handleAvatarSuccess"
      :headers="headers"
      :file-list="fileList"
      :limit="1"
      :multiple ="false"
    >
      <i class="el-icon-plus"></i>
      <div slot="tip" class="el-upload__tip color_red">只能上传.jpg/.jpeg/.png文件</div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt />
    </el-dialog>
  </div>
</template>

<script>
export default {
  // props:{
    // fileList:{
    //   type: Array,
    //   default: () => [],
    // }, 
  // },
  data() {
    return {
      fileList: [],
      dialogImageUrl: "",
      dialogVisible: false,
      imageUrl:"",
    };
  },
  computed: {
    headers() {
      return {
        Authorization: localStorage.getItem("backToken") || "",
      };
    },
    uploadDisabled:function() {
        return (this.fileList.length>0)||this.imageUrl
    },
  },
  methods: {
    // 图片上传成功的返回值
    handleAvatarSuccess(res, file) {
      if (res.resultCode == 200) {
        this.$emit("imgUrl", res.data.url);
        this.imageUrl = res.data.url;
      } else {
        this.$message.error(res.message);
      }
    },
    handleRemove(file, fileList) {
      this.$emit("imgUrl", "");
      this.imageUrl=""
      this.fileList=[]
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    showImg(cover) {
      let file = {
        name: "",
        url: cover,
      };
      this.fileList = [file]
    },
  },
};
</script>

<style lang="less">
.avatar {
  width: 100%;
  height: 100%;
}
.color_red{
  color:red;
}

.disabled .el-upload--picture-card {
    display: none;
}

</style>