<template>
  <ui-upload
    v-model="updateFile"
    :disabled="uploading"
    @upload-success="handleUploadFileSuccess"
  >
    <template slot-scope="{loading}">
      <ui-button size="small" :loading="loading || uploading" type="primary">上传</ui-button>
    </template>
  </ui-upload>
</template>
<script>
import { getExtByFileSrc } from '../util'

export default {
  name: 'DiskManageUpload',
  props: {
    createDiskFileMethod: {
      type: Function
    },
    parentPathId: {
      type: String
    }
  },
  data() {
    return {
      uploading: false,
      updateFile: null
    }
  },
  methods: {
    handleUploadFileSuccess({ file }) {
      this.uploading = true
      this.createDiskFileMethod({
        absolutelyPath: file.url,
        fileName: file.name,
        fileSize: file.size,
        fileExtension: getExtByFileSrc(file.url),
        parentPathId: this.parentPathId
      }).then(() => {
        this.$emit('success')
      }).finally(() => {
        this.uploading = false
      })
    }
  }
}
</script>
