// 图片上传
<template>
  <div class="component-upload-image">
    <el-upload
      :action="uploadImgUrl + '?prePath=' + prePath"
      list-type="picture-card"
      :on-success="handleUploadSuccess"
      :before-upload="handleBeforeUpload"
      :on-error="handleUploadError"
      name="file"
      :show-file-list="false"
      :headers="headers"
      style="display: inline-block; vertical-align: top"
      v-if="!isList"
    >
      <el-image
        v-if="!value"
        :src="value.indexOf('http') == -1 ? baseUrl + value : value"
      >
        <div slot="error" class="image-slot">
          <i class="el-icon-plus" />
        </div>
      </el-image>
      <div v-else class="image">
        <el-image
          :src="value.indexOf('http') == -1 ? baseUrl + value : value"
          :style="`width:150px;height:150px;`"
          fit="fill"
        />
        <div class="mask">
          <div class="actions">
            <span title="预览" @click.stop="dialogVisible = true">
              <i class="el-icon-zoom-in" />
            </span>
            <span title="移除" @click.stop="removeImage">
              <i class="el-icon-delete" />
            </span>
          </div>
        </div>
      </div>
    </el-upload>
    <el-upload
      :action="uploadImgUrl + '?prePath=' + prePath"
      list-type="picture-card"
      :on-remove="handleRemove"
      :on-success="handleUploadSuccess"
      :before-upload="handleBeforeUpload"
      :on-preview="handlePictureCardPreview"
      :file-list="imgList"
      accept=".jpeg,.png,.jpg,.bmp,.gif"
      :headers="headers"
      :limit="limit"
      v-else
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog
      :visible.sync="dialogVisible"
      title="预览"
      width="800"
      append-to-body
    >
      <img
        :src="dialogImageUrl"
        style="display: block; max-width: 100%; margin: 0 auto"
        v-if="isList"
      />
      <img
        :src="value"
        style="display: block; max-width: 100%; margin: 0 auto"
        v-else
      />
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      baseUrl: process.env.VUE_APP_API_BASE_URL + "/",
      dialogVisible: false,
      uploadImgUrl: "/enterprise/file/commonupload", // 上传的图片服务器地址
      imgList: [],
      dialogImageUrl: "",
    };
  },
  props: {
    value: {
      type: String,
      default: "",
    },
    //保存服务器路径前缀地址
    prePath: {
      type: String,
      default: "",
    },
    isList: {
      type: Boolean,
      default: false,
    },
    limit: {
      type: Number,
      default: undefined,
    },
  },
  mounted() {
    // 判断是否为多图上传
    if (this.isList) {
      if (this.value && this.value != "") {
        this.imgList = [];
        let arr = [];
        arr = this.value?.split(",");
        arr.forEach((v) => {
          let obj = {};
          obj.name = "轮播图";
          obj.uid = parseInt(Math.random(0, 100));
          v.indexOf("http") == -1
            ? (obj.url = this.baseUrl + v)
            : (obj.url = v);
          this.imgList.push(obj);
        });
      }
    }
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(fileList);
      this.imgList = fileList;
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    removeImage() {
      this.$emit("input", "");
    },
    handleUploadSuccess(res, file, fileList) {
      if (this.isList) {
        this.loading.close();
        this.imgList = fileList;
      } else {
        this.$emit("input", res.url);
        this.loading.close();
      }
    },
    handleBeforeUpload() {
      this.loading = this.$loading({
        lock: true,
        text: "上传中",
        background: "rgba(0, 0, 0, 0.7)",
      });
    },
    handleUploadError() {
      this.$message({
        type: "error",
        message: "上传失败",
      });
      this.loading.close();
    },
  },
  watch: {},
};
</script>

<style scoped lang="scss">
.image {
  position: relative;
  .mask {
    opacity: 0;
    position: absolute;
    top: 0;
    width: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    transition: all 0.3s;
  }
  &:hover .mask {
    opacity: 1;
  }
}
</style>
