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

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

  /**
   * 生命周期函数--监听页面加载
   */
  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:7,
    //     userId:7
    //   },
    //   header: { 'content-type': 'application/json' },
    //   method: 'GET',
    //   dataType: 'json',
    //   success: function (res) {
    //     console.log(res)
    //   },
    //   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[0].path
    });
  },
  delete1(event) {
    const { index, name } = event.detail;
    const fileList1 = this.data[`fileList1`];
    fileList1.splice(index, 1);
    this.setData({ [`fileList1`]: fileList1 });
  },
  afterRead1(event) {
    const { file } = event.detail;
    const fileList1 = this.data[`fileList1`];

    this.setData({ 
      [`fileList1`]: fileList1.concat(file),
      frontImage: file[0].path
       });
  },
  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_new2.php',
            data: {
              unionId: res.data.unionId,
              circleId: that.data.circleId,
              comment: that.data.value
            },
            method: 'GET',
            success: function (resp) {
              if (resp.data.status=='OK'){
                wx.uploadFile({
                  url: app.url + 'ajax_circle_new2.php?unionId=' + res.data.unionId + '&circleId=' + that.data.circleId,
                  filePath: that.data.fileList[0].path,
                  name: 'frontImage',
                  success:function(resp1){
                    var result1=JSON.parse(resp1.data);
                    if (result1.status=='OK'){
                      if(that.data.fileList1.length>0){
                        for(var i in that.data.fileList1){
                          var file = that.data.fileList1[i];
                          setTimeout(function(){
                            wx.uploadFile({
                              url: app.url + 'ajax_circle_new2.php?unionId=' + res.data.unionId + '&circleId=' + that.data.circleId,
                              filePath: file.path,
                              name: 'image',
                              success: function (resp2) {
                                that.setData({
                                  uploadCount: that.data.uploadCount + 1
                                }, function () {
                                  that.finish();
                                })
                              }
                            })
                          },500);
                        }
                      }else{
                        wx.showToast({
                          title: '保存成功',
                        })
                        wx.redirectTo({
                          url: '/pages/circleDetails/circleDetails?circleId='+that.data.circleId,
                        })
                      }
                    }
                  },
                  fail:function(err){
                    console.log(err)
                  }
                })
              }
              console.log(res)
              // if (res.statusCode == 200) {
              //   that.setData({
              //     familyEventList: res.data.result.familyEventList
              //   })
              // }
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
      },
    })
  },
  finish(){
    var that=this;
    if(this.data.uploadCount==this.data.fileList1.length){
      wx.showToast({
        title: '保存成功',
      });
      wx.redirectTo({
        url: '/pages/circleDetails/circleDetails?circleId=' + that.data.circleId,
      })
    }
  }
})