import { put as uploadFile } from '@/api/oss'
import { getFileName } from '@/utils/fileHandle'
import _ from 'lodash-es'

export default {
  data() {
    return {
      uploadMap: {},
      fileType: '',
      fileList: [],
      dialogImageUrl: '',
      dialogVisible: false
    };
  },
  computed: {
    uploadFiles() {
      const { uploadMap } = this
      const ret = {}

      _.forEach(uploadMap, (val, key) => {
        ret[key] = [{ name: key.replace(/Doc$/, ''), url: val, }]
      })

      return ret
    }
  },
  methods: {
    uploadedCallback(fileUrl) {
      this.uploadMap[this.fileType] = fileUrl
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    setFileType(fileType) {
      this.fileType = fileType
    },
    async handleUpload({ file }) {
      const fileName = await getFileName(file)

      const response = await uploadFile(fileName, file)

      const { res = {}, url } = response || {}

      if (res.status && res.status === 200) {
        if (_.isFunction(this.uploadedCallback)) {
          this.uploadedCallback(url)
        }
      }
    },
  }
}
