var app = getApp();
var that;
Page({
  data: {
    items: {
      work_id: '',
      state: 1,
      files: [],
      remark: ''
    },
    id: '',
    is: false,//是不是今日任务
  },
  onLoad(e) {
    that = this;
    dd.setNavigationBar({ title: "完成任务" });
    if (e.id) {
      this.setData({
        id: e.id,
        is: e.is,
        [`items.work_id`]: e.id,
      })
    }
  },
  //备注输入
  bindTextAreaBlur: function (e) {
    var remark = `items.remark`
    this.setData({
      [remark]: e.detail.value,
    });
  },
  formSubmit() {
    var page = getCurrentPages()[getCurrentPages().length - 2]//上一个页面的数据
    var data = this.data.items;
    data.files = data.files.toString();
    this.setData({ disabled: true })
    app.$post('api/integral/work', data).then(res => {
      app.globalData.showToast(res.data.msg);
      if (this.data.is == 'true') {
        page.filtrationList(1);
        setTimeout(() => {
          dd.navigateBack({ delta: 1 })
        }, 1000);
      } else if (this.data.is == 'false') {
        page.filtrationList(2, data.work_id);
        setTimeout(() => {
          dd.navigateBack({ delta: 1 })
        }, 1000);
      } else if (this.data.is == "detail") {
        page.getDetail(this.data.id, function () {
          setTimeout(() => {
            dd.navigateBack({ delta: 1 })
          }, 1000);
        })
      }
    }).finally(e => {
      this.setData({ disabled: false })
    })

  },
  //删除图片
  deteleImg(e) {
    var index = e.target.dataset.index;
    var files = that.data.items.files;//获取itemS集合中对应的files
    var files_box = `items.files`;//
    app.globalData.arrRemoveObj(files, files[index]);
    that.setData({
      [files_box]: files
    });

  },
  //图片选择
  openImg(e) {
    var files = that.data.items.files;
    if (files.length >= 3) {
      app.globalData.showToast("只能上传三张");
      return false;
    }
    dd.chooseImage({
      count: 1,
      success: (res) => {
        that.postImg(res.filePaths[0]);
      },
    });
  },
  postImg(img_url) {
    var files_box = `items.files`;
    var files = this.data.items.files;
    dd.httpRequest({
      url: app.globalData.imgHttpUrl,
      method: 'POST',
      data: {
        md5: that.random_string(32),
        'name': app.globalData.day
      },
      success: function (res) {
        if (app.globalData.userData.site_id) {
          var key = 'intesys/dd/' + app.globalData.userData.site_id + "/" + app.globalData.day + '/' + that.random_string(32) + '.png';
        } else {
          var key = 'intesys/dd/' + app.globalData.day + '/' + that.random_string(32) + '.png';
        }
        var obj = res.data.data;
        dd.uploadFile({
          url: app.globalData.ALIOSS_URL,
          fileType: 'image',
          fileName: 'file',
          filePath: img_url,
          formData: {
            key: key,
            policy: obj.policy,
            OSSAccessKeyId: obj.accessid,
            success_action_status: 200,
            signature: obj.signature
          },
          success: (res) => {
            if (res.statusCode == 200) {
              if (files.length > 0) {
                files.push('http://cdn.intesys.g107.com/' + key)
              } else {
                files = ['http://cdn.intesys.g107.com/' + key];
              }
              that.setData({
                [files_box]: files
              });
              app.globalData.showToast("已上传");
            }
          },
          fail: (err) => {
            console.log(err);
          }
        });
      },
      fail: function (res) {
        app.globalData.showToast("上传失败");
      }

    })
  },
  random_string(len) {
    len = len || 32
    var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
    var maxPos = chars.length
    var pwd = ''
    for (let i = 0; i < len; i++) {
      pwd += chars.charAt(Math.floor(Math.random() * maxPos))
    }
    return pwd
  },
});
