<template>
  <div>
    <input
      ref="inputer"
      :accept="accept"
      :multiple="multiple"
      class="fileUp"
      :class="{ miniInput: size === 'mini' }"
      type="file"
      name="file"
      :disabled="disabled"
      @change="handleChange($event)"
    />
  </div>
</template>

<script>
import urlConfig from "@/apis/url-config";
import { getToken } from "@/utils/auth";
//Uploadcondition 用于判断个性化文件 //返回 false 就不会上传文件
//UploadErrorEvent 上传成功
//UploadErrorEvent 上传失败

export default {
  name: "fileUploadPdf",
  props: {
    //@reject 默认值
    accept: String, //上传文件类型
    multiple: {
      //是否可以上传多张图片
      type: Boolean,
      default: false,
    },
    size: {
      type: String,
      default: "mini",
    },
    disabled: {
      //是否禁用
      type: Boolean,
      default: false,
    },
    uploadcondition: {
      type: Function,
      default: null,
    },
  },
  data() {
    return {
      loading: false,
      imgAPI: urlConfig.baseURL,
    };
  },
  methods: {
    async handleChange(event) {
      let that = this;
      let inputDOM1 = that.$refs.inputer;
      const files = inputDOM1.files;
      let length = files.length;
      if (length === 0) return;
      that.loading = that.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      for (let i = 0; i <= length; i++) {
        if (i === length) {
          this.loading.close();
          that.$refs.inputer.value = "";
        } else {
          let str = await that.httpupLoad(files, i);
          console.log(str)
          if (str === "error") {
            that.$emit("UploadErrorEvent", true);
          } else if (str === "break") {
            that.$emit("UploadErrorEvent", "break");
          } else {
            that.$emit("UploadSuccessEvent", str);
          }
        }
      }
    },
    async httpupLoad(files, i) {
      return new Promise((resolve, reject) => {
        let that = this;
        if (!this.uploadcondition(files[i])) {
          resolve("break");
        } else {
          let form = new FormData();
          form.append("file", files[i]);
          form.append("token", getToken());
          axios
            .post(`${that.imgAPI}admin/login/upload`, form)
            .then((res) => {
              const { status } = res;
              if (status === 200) {
                let item = res.data;
                const { code, data } = item;
                if (code === 1) {
                  const { file } = data;
                  const { full_url, name } = file
                  resolve({ url: full_url, name: name });
                }
              } else {
                reject();
              }
            })
            .catch((error) => {
              console.log(error)
              reject();
            });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.fileUp {
  opacity: 0;
  width: 100%;
  height: 100%;
  outline: none;
  position: absolute;
  top: 0;
  left: 0;
  &:hover {
    cursor: pointer;
  }
}
</style>
