const app = getApp();
const util = require("../../../util/util.js")
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    loadModal:false,
    index:0,
    list:[
    ]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },
  getData: function(){
    let param = {  }
    util.http("/wx/getConfirmTask", param,  (err, list) =>{
      if( err != 0) return;
      this.setData({ list })
    });
  },
  doUpload( err, {url} ){
    if( err !=0 || !url ) {
      util.showMsg("图片上传失败")
      return
    }
    let {index,list} = this.data
    let id = list[index].id
    util.http( '/maker/addDeliverImg', {id, url}, (err,res)=>{
      if( err!= 0 ) return;
      list[index].deliver_img = url;
      this.setData({list});
    })
  },
  previewImage: function (e) {
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current, 
      urls: [current]
    })
  } ,
  uplodImg: function(e ){
    let index = e.currentTarget.dataset.index;
    console.log("uplodImg", e.currentTarget.dataset)
    this.setData({index});
    wx.chooseImage({  
      count: 1,  
      sizeType: ['compressed'], 
      sourceType: ['album', 'camera'],
      success:  (res) =>{
        var tempImagePath = res.tempFilePaths[0];
        util.uploadFile( tempImagePath, this.doUpload )
      },
      fail:()=>{
        app.showMsg('选择失败')
      }
    })
  }
})