// subPackage/page/pages/grassCommunity/grassCommunity.js
const app = getApp()
const wxService = require('../../../../utils/wxService')
const utils = require('../../../../utils/util')
const { memberId } = wx.getStorageSync('_baseUserInfo')


wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    pics:[],
    showSelectRelatedGoods: false,
    params:{
      article: "",
      describe: "",
      headline: "",
      id: 0,
      sspList: [
        {
          cover: 0, 
          url: ""
        }
      ],
      stcList: [
        {
          commodityId: 0
        }
      ],
      userId: memberId
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  // 关联商品
  handleRelatedGoods(){
    console.log('关联商品')
    this.setData({
      showSelectRelatedGoods: true
    })
  },
  uploadImg(){
    this.chooseImg()
  },
  chooseImg(){
    let that = this;
    let pics = this.data.pics;
    wx.chooseImage({
      count: 5 - pics.length, // 最多可以选择的图片5张，
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        var imgsrc = res.tempFilePaths;
        pics = pics.concat(imgsrc);
        that.setData({
          pics: pics
        });
        console.log(that.data.pics)
        that.moreImgUpload({
          url: 'http://buyer.devapi.bigaka.net/merchant/upload/pic',//这里是你图片上传的接口
          path: that.data.pics//这里是选取的图片的地址数组        
        });
      },
      fail: function () {
        // fail      
      },
      complete: function () {
        // complete      
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  //多张图片上传
  moreImgUpload(data) {
    var that = this,
      i = data.i ? data.i : 0,//当前上传的哪张图片
      success = data.success ? data.success : 0,//上传成功的个数
      fail = data.fail ? data.fail : 0;//上传失败的个数
      wx.uploadFile({
        url: data.url,
        filePath: data.path[i],
        header : {
          'buyer-token' : wx.getStorageSync('token')
        },
        name: 'file',//这里根据自己的实际情况改
        formData: null,//这里是上传图片时一起上传的数据
        success: (resp) => {
          success++;//图片上传成功，图片上传成功的变量+1
          console.log(resp)
          console.log(i);
          //这里可能有BUG，失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时，这里的success才+1 
        },
        fail: (res) => {
          fail++;//图片上传失败，图片上传失败的变量+1
          console.log('fail:' + i + "fail:" + fail);
        },
        complete: () => {
          console.log(i);
          i++;//这个图片执行完上传后，开始上传下一张            
          if (i == data.path.length) {   //当图片传完时，停止调用          
            console.log('执行完毕');
            console.log('成功：' + success + " 失败：" + fail);
          } else {//若图片还没有传完，则继续调用函数                
            console.log(i);
            data.i = i;
            data.success = success;
            data.fail = fail;
            that.moreImgUpload(data);
          }
        }
      })
  },

  // 发布笔记
  formSubmit(e){
    console.log(e)
  }

})