// pages/change/change.js
var app = getApp()
Page({
  data: {
    feed: [],
    change1: '',
    change2: '',
    tempFilePaths: [],
     t:'',
     c:'',
    nowCount:0,//当前的图片上传个数
    index:0,
    url:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this
      var aid=options.aid
      console.log(options);
      console.log(options.aid);
      
      wx.cloud.database().collection('tiezi')
      .doc(aid)
      .get({
        success(res) {
          console.log("请求成功", res.data)
          console.log("请求成功", res.data.url.length)
          // 将查询返回的结果赋值给本地数组
          that.setData({
            feed: res.data,
            nowCount:res.data.url.length,
            tempFilePaths: res.data.url,
            url:res.data.url,
             t:res.data.title,
             c:res.data.content
          })
        },
        fail(res) {
          console.log("请求失败", res)
        }
      })
  },

  // 获取修改后的内容
  change1: function(e) {
    this.setData({
      change1: e.detail.value
    })
  },
  change: function(e) {
    this.setData({
      change2: e.detail.value
    })
  },
  //图片的上传
  chooseImage:function(e){
    let that = this;
    wx.chooseImage({
      count: 3, // 默认最多3张图片，可自行更改
      sizeType: ['original', 'compressed'],// 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: res => {
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 1000
        })
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let tempFilePath = res.tempFilePaths;
        console.log(tempFilePath);
        let nowCount = that.data.nowCount;
        let tempFilePaths = that.data.tempFilePaths;
        
        if(that.data.nowCount + tempFilePath.length >=3){
          let i = 0;
          while(nowCount<3){//还可以继续上传图片
            tempFilePaths.push(tempFilePath[i]);
            i++;
            nowCount++;
          }
          that.setData({
            nowCount:3,
            tempFilePaths:tempFilePaths
          })
        }else{
          let i = 0;
          while(i<tempFilePath.length){
            tempFilePaths.push(tempFilePath[i]);
            i++;
          }
          that.setData({
            nowCount:that.data.nowCount + tempFilePath.length,
            tempFilePaths:tempFilePaths
          })
        }

        console.log(that.data.nowCount,that.data.tempFilePaths);
      }
    })
  },
  //长按删除图片
  DeleteImg: function (e) {
    let that = this;
    let tempFilePaths = that.data.tempFilePaths;
    let index = e.currentTarget.dataset.index;//获取当前长按图片下标
    wx.showModal({
      title: '提示',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          //console.log('点击确定了');
          let nowCount = that.data.nowCount;
          that.setData({
            nowCount:nowCount-1
          })
          tempFilePaths.splice(index, 1);
        } else if (res.cancel) {
          //console.log('点击取消了');
          return false;
        }
        that.setData({
          tempFilePaths:tempFilePaths
        });
      }
    })
  },
  // 提交修改
  sure: function(event) {
      var that = this
       let content1
       let content
      //  if(this.data.change1==''){console.log("1")}
      //  if(this.data.change2==''){console.log("2")}
if(this.data.change1==''){
  content1=this.data.t
}else{
  content1=this.data.change1
}

if(this.data.change2==''){
  content=this.data.c
}else{
  content=this.data.change2
}


      console.log(content1,"1",content,"1")

      //图片修改
  let i;
  let count=0;
  let url=this.data.url
  let l=url.length
  console.log(l)
      if(that.data.nowCount==0){
      wx.cloud.init({
        env:"cloud1-8g5wmepxce8a3b8a",
      })
      let aid=wx.getStorageSync ("aid")
      console.log(aid);
      wx.cloud.database().collection('tiezi')
      .doc(aid)
      .update({
    data:{
      title:content1,
      content: content,
    }
      }).then(res=>{
        console.log(res);
        wx.showLoading({
          title: '修改成功...',
        })
        wx.redirectTo({
          url: '../answer/answer?aid='+aid,
        })
        wx.hideLoading()
      }).catch(res=>{
        console.log(res);
  })
  }else{
    //将所有的内容上传到云端去
    for(i=l;i<that.data.nowCount;i++){
      console.log(1);
      
      let extName = that.data.tempFilePaths[i].split(".").pop();
      let cloudPath = "love/" + new Date().getTime() + "." + extName;
      wx.cloud.uploadFile({
        cloudPath: cloudPath,
        filePath: that.data.tempFilePaths[i], // 文件路径
        success: res => {
          count++;
          url.push(res.fileID);
          console.log('上传图片');
          console.log(i,url);
          if(count==that.data.nowCount){
            console.log(url);
          }
        },fail:res=>{
          console.log(res);
        }
      })
     
    }
    wx.cloud.init({
      env:"cloud1-8g5wmepxce8a3b8a",
    })
    let aid=wx.getStorageSync ("aid")
    console.log(aid);
    wx.cloud.database().collection('tiezi')
    .doc(aid)
    .update({
  data:{
    title:content1,
    content: content,
    url:url
  }
    }).then(res=>{
      console.log(res);
      wx.showLoading({
        title: '修改成功...',
      })
      wx.redirectTo({
        url: '../answer/answer?aid='+aid,
      })
      wx.hideLoading()
    }).catch(res=>{
      console.log(res);
})
  }




  },


})
