<template>
  <div :class="prefixCls">
    <Upload
      :action="uploadUrl"
      :headers="headers"
      :max-size="maxSize"
      :format="format"
      :accept="accept"
      :data="uploadDataDict"
      :before-upload="handleBeforeUpload"
      :on-format-error="handleFormatError"
      :on-exceeded-size="handleExceededSize"
      :on-error="handleError"
      :on-success="handleSuccess"
      :on-remove="handleRemove"
      :show-upload-list="false"
      ref="UploadComp"
    >
      <Button :type="btnType" :icon="btnIcon" :loading="loading">{{
        btnContent
      }}</Button>
    </Upload>
    <slot name="extra"></slot>
    <p
      :class="[
        {
          'cc-tips-success': res.success,
          'cc-tips-error': !res.success
        }
      ]"
      v-if="res.msg"
    >
      {{ res.msg }}
    </p>
  </div>
</template>

<script>
const prefixCls = "cc-upload-button";

export default {
  name: "upload-button",
  props: {
    btnIcon: {
      type: String
    },
    btnType: {
      type: String,
      default: "default"
    },
    btnContent: {
      type: String,
      default: "上传文件"
    },
    format: {
      type: Array,
      default() {
        return ["jpg", "png"];
      }
    },
    uploadUrl: {
      type: String,
      required: true
    },
    uploadData: {
      type: Object,
      default() {
        return {};
      }
    },
    headers: {
      type: Object,
      default() {
        return {};
      }
    },
    maxSize: {
      type: Number,
      default: 10 * 1024
    },
    addFilenameParam: {
      type: String,
      default: "fileName"
    },
    autoUpload: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      prefixCls,
      modalIsShow: false,
      loading: false,
      uploadDataDict: {},
      file: null,
      res: {
        success: true,
        msg: ""
      }
    };
  },
  computed: {
    accept() {
      return this.format.map(el => `.${el}`).join();
    }
  },
  methods: {
    visibleChange(visible) {
      if (!visible) {
        this.res = {
          success: true,
          msg: ""
        };
        this.$refs.UploadComp.clearFiles();
      }
    },
    startUpload() {
      if (this.file) {
        this.loading = true;
        this.$refs.UploadComp.post(this.file);
      }
    },
    handleBeforeUpload(file) {
      this.uploadDataDict = { ...this.uploadData };
      if (this.addFilenameParam)
        this.uploadDataDict[this.addFilenameParam] = file.name;
      this.$emit("file-change", file);

      if (!this.autoUpload) {
        this.file = file;
        return false;
      }

      this.loading = true;
      this.$refs.UploadComp.clearFiles();
    },
    handleError(error) {
      this.loading = false;
      this.res = {
        success: false,
        msg: error.message
      };
    },
    handleSuccess(response) {
      this.loading = false;
      if (response) {
        this.res = {
          success: true,
          msg: "上传成功！"
        };
        this.$emit("upload-success", response);
      } else {
        this.res = {
          success: false,
          msg: response.message || "上传失败"
        };
      }
    },
    handleFormatError() {
      this.res = {
        success: false,
        msg: "只支持文件格式为" + this.format.join("/")
      };
    },
    handleExceededSize() {
      this.res = {
        success: false,
        msg: "文件大小不能超过" + Math.floor(this.maxSize / 1024) + "M"
      };
    },
    handleRemove() {
      this.resData = "";
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    }
  }
};
</script>
