<template>
    <div class="input-container">
      <el-input v-model="imgUrl" :placeholder="placeholder" >

        <el-popover
          slot="append"
          placement="left"
          width="500"
          trigger="click">
          <div style="text-align: center">
            <el-image v-if="imgUrl" style="width: 100%" fit="contain" :src="imgUrl"></el-image>
            <p v-else>资源不存在</p>
          </div>
          <el-button slot="reference" icon="el-icon-view"></el-button>
        </el-popover>
      </el-input>
      <el-upload
        class="upload"
        :action="action"
        :show-file-list="false"
        :headers="headers"
        :on-success="handleSuccess"
        :before-upload="beforeUpload"
        :on-error="handleUploadError"
      >
        <el-button size="small" type="primary">{{btnName}}</el-button>
      </el-upload>
    </div>
</template>

<script>
import { getToken } from '@/utils/auth'

export default {
  name: "uploadPicInput",
  props: {
    value: {
      type: String
    },
    // 大小限制(MB)
    fileSize: {
      type: Number,
      default: 5,
    },
    placeholder: {
      type: String,
      default: '请输入图片链接'
    },
    btnName: {
      type: String,
      default: '上传图片'
    },
    // 文件类型, 例如['png', 'jpg', 'jpeg']
    fileType: {
      type: Array,
      default: () => ["png", "jpg", "jpeg"],
    },
    // 文件上传地址
    action: {
      type: String,
      default: process.env.VUE_APP_BASE_API + "/common/oss/upload"
    }
  },
  data() {
    return {
      limit: 1,
      imgUrl: this.value,
      headers: {
        Authorization: "Bearer " + getToken(),
      },
    };
  },
  methods: {
    // 上传失败
    handleUploadError() {
      this.$modal.msgError("上传图片失败，请重试");
      this.$modal.closeLoading();
    },
    // 上传成功回调
    handleSuccess(res, file) {
      if (res.code === 200) {
        this.imgUrl = res.data.url;
        this.$emit("input", this.imgUrl);
      } else {
        this.$modal.msgError(res.msg);
      }
      this.$modal.closeLoading();
    },
    // 上传前loading加载
    beforeUpload(file) {
      let isImg = false;
      if (this.fileType.length) {
        let fileExtension = "";
        if (file.name.lastIndexOf(".") > -1) {
          fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
        }
        isImg = this.fileType.some(type => {
          if (file.type.indexOf(type) > -1) return true;
          if (fileExtension && fileExtension.indexOf(type) > -1) return true;
          return false;
        });
      } else {
        isImg = file.type.indexOf("image") > -1;
      }

      if (!isImg) {
        this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}图片格式文件!`);
        return false;
      }
      if (this.fileSize) {
        const isLt = file.size / 1024 / 1024 < this.fileSize;
        if (!isLt) {
          this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`);
          return false;
        }
      }
      this.$modal.loading("正在上传图片，请稍候...");
    },
  },
};
</script>

<style lang="less" scoped>
.input-container {
  display: flex;
}
.upload {
  display: inline-block;
  margin-left: 10px;
}
</style>
