import api from '../../api/api.js'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    source: {
      value: 'media',
      type: String
    },
    /* 文件夹ID */
    parentid: {
      type: String,
      value: wx.getStorageSync('siteP').appfolderid
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    originFiles: [

    ],
    attachmentids: [],
    gridConfig: {
      column: 5,
      width: 120,
      height: 120,
    },
    config: {
      count: 1,
    },
    timer: null,
    uploadCount: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleAdd(file) {
      let files = file.detail.files.map(e => {
        e.status = 'loading'
        return e
      })
      this.setData({
        uploadCount: files.length,
        originFiles: files,
        attachmentids: []
      })
    },
    handleRemove(data) {
      let file = data.detail.file
      this.setData({
        originFiles: this.data.originFiles.filter(e => {
          return e.name !== file.name
        })
      })
    },
    toSetFileData() {

      let files = this.data.originFiles

      for (let i = 0; i < files.length; i++) {
        // 初始化数据
        let that = this,
          data = this.requestType(files[i]);
        data.content.filename = data.content.filename ? data.content.filename : `${Date.now()}.${data.content.filetype}`;
        //发送请求
        wx.getFileSystemManager().readFile({
          filePath: files[i].url,
          success: result => {
            //返回临时文件路径
            const fileData = result.data;
            api._post(data).then(res => {
              if (res.msg == "成功") {
                that.uploadFile(res.data, fileData)
              }
            })
          },
          fail: console.error
        })
      }

      return new Promise((reslove, reject) => {
        this.data.timer = setInterval(e => {
          if (this.data.uploadCount === 0) {
            clearInterval(this.data.timer)
            reslove()
          }
        }, 1000)
      })
    },
    /* 请求类型 */
    requestType(file) {
      var index = file.url.lastIndexOf(".");
      var ext = file.url.substr(index + 1);
      //文件名称
      return {
        "classname": "system.attachment.huawei.OBS",
        "method": "getFileName",
        "content": {
          "filename": file.name,
          "filetype": ext,
          "parentid": this.data.parentid
        }
      }
    },
    /* 上传成功反馈 */
    uploadFile(res, data) {
      var that = this;
      wx.request({
        url: res.uploadurl,
        method: "PUT",
        data: data,
        header: {
          'content-type': 'application/octet-stream'
        },
        success(a) {
          api._post({
            "classname": "system.attachment.huawei.OBS",
            "method": "uploadSuccess",
            "content": {
              "serialfilename": res.serialfilename
            }
          }).then(rs => {

            that.setData({
              originFiles: that.data.originFiles.map(e => {
                if (e.name === res.filename) {
                  e.status = ''
                }
                return e
              })
            })

            that.data.attachmentids.push(rs.data.attachmentids[0])

            that.setData({
              uploadCount: that.data.uploadCount - 1
            })
          }).catch(err => {
            console.log(err)
          })
        }
      })
    },
  }
})