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

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    fileList: [],
    imageList: [],
    headerImg: '',
    frontImage: '',
    circleId: 0,
    uploadCount: 0,
    deleteImages:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      circleId: options.circleId
    })
    var that = this;
    var config = wx.getStorageSync('config');
    // var location = wx.getStorageSync('location');
    wx: wx.request({
      url: app.url + 'ajax_get_circle_info.php',
      data: {
        unionId: config.unionId,
        circleId: options.circleId
      },
      success: function (res) {
        if(res.data.status=='OK'){
          if(res.data.result.circleDat.images){
            for (var i in res.data.result.circleDat.images){
              var data={
                url: res.data.result.circleDat.images[i]
              };
              that.data.imageList.push(data);
            }
          }
          that.setData({
            circleDat:res.data.result.circleDat,
            value:res.data.result.circleDat.comment,
            fileList: [{ url: res.data.result.circleDat.front_image}],
            imageList: that.data.imageList
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })

  },

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

  },

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

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

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

  },
  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),
      headerImg: file.path
    });
  },
  delete1(event) {
    const { index, name } = event.detail;
    const fileList1 = this.data.imageList;
    if(fileList1[index].url!==undefined){
      if(this.data.deleteImages!=''){
        this.data.deleteImages += '|' + fileList1[index].url;
      }else{
        this.data.deleteImages = fileList1[index].url;
      }
      this.setData({
        deleteImages: this.data.deleteImages
      })
    }
    fileList1.splice(index, 1);
    this.setData({ imageList: fileList1 });
  },
  afterRead1(event) {
    const { file } = event.detail;
    var imageList = this.data.imageList;
    this.setData({
      [`imageList`]: imageList.concat(file)
    });
  },
  onChange(event) {
    this.setData({
      value: event.detail.value
    })
  },
  btnCircle() {
    console.log(this.data.headerImg, this.data.frontImage)
    this.circleNew2();
  },
  circleNew2() {
    var that = this;
    if (that.data.fileList.length == 0) {
      Notify('请选择圈子头像');
      return;
    }
    if (that.data.value == '') {
      Notify('请填写圈子简介');
      return;
    }
    wx.getStorage({
      key: 'config',
      success: function (res) {
        if (res) {
          wx.request({
            url: app.url + 'ajax_circle_edit2.php',
            data: {
              unionId: res.data.unionId,
              circleId: that.data.circleId,
              comment: that.data.value,
              clear:false
            },
            method: 'GET',
            success: function (resp) {
              if (resp.data.status == 'OK') {
                if(that.data.fileList[0].path!==undefined){
                  wx.uploadFile({
                    url: app.url + 'ajax_circle_edit2.php?unionId=' + res.data.unionId + '&circleId=' + that.data.circleId +'&clear=false',
                    filePath: that.data.fileList[0].path,
                    name: 'frontImage',
                    success: function (resp1) {
                      var result1 = JSON.parse(resp1.data);
                      if (result1.status == 'OK') {
                        that.uploadImage();
                      }
                    },
                    fail: function (err) {
                      console.log(err)
                    }
                  })
                }else{
                  that.uploadImage();
                }
              }
              console.log(res)
              // if (res.statusCode == 200) {
              //   that.setData({
              //     familyEventList: res.data.result.familyEventList
              //   })
              // }
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
      },
    })
  },
  deleteImg(){
    var config=wx.getStorageSync('config');
    var that=this;
    if(that.data.deleteImages){
      wx.request({
        url: app.url + 'ajax_circle_edit2.php?unionId=' + config.unionId + '&circleId=' + that.data.circleId + '&clear=true',
        data:{
          deleteImages:that.data.deleteImages
        },
        success(){

        }
      })
    }
  },
  uploadImage(){
    var config=wx.getStorageSync('config');
    var that=this;
    if (that.data.imageList.length > 0) {
      for (var i in that.data.imageList) {
        var file = that.data.imageList[i];
        if (file.path !== undefined) {
          wx.uploadFile({
            url: app.url + 'ajax_circle_edit2.php?unionId=' + config.unionId + '&circleId=' + that.data.circleId + '&clear=false',
            filePath: file.path,
            name: 'image',
            success: function (resp2) {
              that.deleteImg();
              setTimeout(function () {
                that.setData({
                  uploadCount: that.data.uploadCount + 1
                }, function () {
                  that.finish();
                })
              }, 300)
            }
          })
        }
      }
    } else {
      wx.showToast({
        title: '保存成功',
      })
      wx.redirectTo({
        url: '/pages/circleDetails/circleDetails?circleId=' + that.data.circleId,
      })
    }
  },
  finish() {
    var that = this;
    var count=0;
    for(var i  in that.data.imageList){
      var file = that.data.imageList[i];
      if(file.path!==undefined){
        count++;
      }
    }
    if (this.data.uploadCount == count) {
      wx.showToast({
        title: '保存成功',
      });
      wx.redirectTo({
        url: '/pages/circleDetails/circleDetails?circleId=' + that.data.circleId,
      })
    }
  }
})