// pages/newCircleMoment/newCircleMoment.js
import Notify from '../../dist/notify/notify';
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isNewCircle: false,
    comment:'',
    fileList:[],
    circleId:0,
    uploadCount:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      circleId: options.circleId
    })
    if (options.isNewCircle != undefined) {
      this.setData({
        isNewCircle: options.isNewCircle,
      })
    }
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //判断上传的图片类型
  beforeRead(event) {
    const { file, callback } = event.detail;
    if (file[0].path.indexOf('jpg') < 0 && file[0].path.indexOf('png') < 0 && file[0].path.indexOf('gif') < 0) {
      wx.showToast({ title: '只能上传图片', icon: 'none' });
      callback(false);
      return;
    }
    callback(true);
  },
  delete(event) {
    const { index, name } = event.detail;
    const fileList = this.data[`fileList`];
    fileList.splice(index, 1);
    this.setData({ [`fileList`]: fileList });
  },
  afterRead(event) {
    const { file } = event.detail;
    const fileList = this.data[`fileList`];

    this.setData({ 
      [`fileList`]: fileList.concat(file),
       });
  },

  //获取输入文本框
  onInputComment(event){
    this.setData({
      comment: event.detail.value
    })
  },

  //添加动态
  circleMomentNew(){
    var that = this;
    if(that.data.comment==''){
      Notify('请输入圈子动态');
      return;
    }
    wx.getStorage({
      key: 'config',
      success: function(res) {
        if(res){
          wx.request({
            url: app.url + 'ajax_circle_moment_new.php',
            data: {
              unionId: res.data.unionId,
              circleId: that.data.circleId,
              comment: that.data.comment
            },
            method: 'GET',
            success: function (resp) {
              if (resp.data.status=='OK'){
                if(that.data.fileList.length>0){
                  for(var i in that.data.fileList){
                    var file = that.data.fileList[i];
                    wx.uploadFile({
                      url: app.url + 'ajax_circle_moment_new.php?unionId=' + res.data.unionId + '&circleId=' + that.data.circleId,
                      filePath: file.path,
                      name: 'image',
                      success: function (resp2) {
                        that.setData({
                          uploadCount: that.data.uploadCount + 1
                        }),
                        //调用结束上传
                        that.finish()
                      },
                      fail: function (err) {
                        console.log(err)
                      }
                    })
                  }
                } else {
                  wx.showToast({
                    title: '保存成功',
                  })
                  wx.redirectTo({
                    url: '/pages/circleDetails/circleDetails?circleId='+that.data.circleId,
                  })
                }
              }
            },
          })
        }
      },
    })
  },

  //取消添加动态
  circleMomentCancel() {
    wx.redirectTo({
      url: '/pages/circleDetails/circleDetails?circleId=' + this.data.circleId,
    })
  },

  //结束上传
  finish(){
    var that=this;
    if(this.data.uploadCount==this.data.fileList.length){
      wx.showToast({
        title: '保存成功',
      });
      wx.redirectTo({
        url: '/pages/circleDetails/circleDetails?circleId=' + that.data.circleId,
      })
    }
  }
})