<template>
  <div>
    <div style="margin:100px auto 10px auto;text-align: center">
      <div>
        <Upload
          multiple
          :action="action"
          :on-success="_handelsuccess"
          :before-upload="_beupload"
          :data="dataObj"
          :headers="headers"
          :with-credentials="true"
          :show-upload-list="false"
        >
          <img src="../../../assets/file-add.png" height="100">
        </Upload>
      </div>
      <p style="margin:10px auto">请选择附件上传</p>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        planId: '',
        action: '',
        dataObj: {
          file: '',
        },
        headers: '',
      }
    },
    mounted() {
      this._getQuery();
    },
    methods: {
      _getQuery() {
        this.planId = this.$route.query.planId;
        this.action = this.$global.baseURL + '/drs/v1/sampling_plan_attachment/' + this.planId;
        this.headers = {'accessToken': localStorage.getItem('accessToken')};
      },
      //web
      _beupload(file) {
        this.dataObj.file = file.name;
        this.$toast.loading({
          message: '加载中...',
          forbidClick: true,
        });
      },
      _handelsuccess(response) { //上传成功
        this.$toast.clear();
        if (response.success) {
          this.$toast('上传成功');
          this.$router.go(-1);
        } else {
          this.$toast('上传失败');
        }
      },
    }
  }
</script>
