// pages/groupPage/my-group/my-group.js
import httpRequestApi from '../../../utils/APIClient';
import util from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myGroupList: [],
    typeOf:'',
    title: '',
    headTextOne: '',
    headTextTwo: '',
    content: [],    
    productId: '',
    orderId: '',
    groupId: '',
    closeFlags: [],
    ind: 0,
    timeList: []
  },
    //跳转到团购详情页
  collageDetails: function ({currentTarget}) {
    const orderId = currentTarget.dataset.orderid;
    const productId = currentTarget.dataset.productid;
    const groupId = currentTarget.dataset.groupid;
    const uid = currentTarget.dataset.uid;
    const num = currentTarget.dataset.num;
    const ind = currentTarget.dataset.ind;
    this.setData({
      ind
    })
    console.log(num)
    if(!num) {
      wx.navigateTo({
        url: `/pages/groupPage/collage-details/collage-details?orderId=${orderId}`
      })
    } else {
      if(this.data.closeFlags[ind]) {
        this.popup.close();
        this.setData({
          productId,
          orderId,
          groupId
        })
        this.dialog(uid);
      }else {
        wx.navigateTo({
          url: `/pages/groupPage/collage-details/collage-details?orderId=${orderId}&read=true`
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    if (option && option.title) {
      wx.setNavigationBarTitle({
        title: option.title//页面标题为路由参数
      });
      this.setData({
        title: option.title
      });
    }
    httpRequestApi.getMygroup().success((res) => {
      if(!res.data.data.list){
        return
      }
      console.log('我的团', res.data.data.list)
      res.data.data.list.forEach(element => {
        if(element.groupPurchaseOrder.status == 'SUCCESSED') {
          this.data.closeFlags.push(true);
          this.data.timeList.push(util.formatDate(element.groupPurchaseOrder.gmtModified, 4))
        }else {
          this.data.closeFlags.push(false);
          if(element.groupPurchaseOrder.closeTime - Date.parse(new Date()) < 0 ) {
            this.data.timeList.push('时间到了,')
          } else {
            this.data.timeList.push(util.formatTime(element.groupPurchaseOrder.closeTime - Date.parse(new Date())).join(':'))
          }
        }
      });
      this.setData({
        closeFlags: this.data.closeFlags,
        myGroupList: res.data.data.list,
        timeList: this.data.timeList
      })
    }).fail( error => {
      console.log('错误', error)
    })
  },
  dialog: function (uid) {
      if(wx.getStorageSync('uid') == uid) {
        this.setData({
          typeOf: 'success',
          title: '恭喜 !',
          headTextOne: '您发起的团购拼团成功',
          content: [
            {
                text: "领袖体质魅力无穷!",
                color: "#000"
            },
            {
                text: " 您可以继续发起新的团购,",
                color: "#000"
            },
            {
              text: " 不再需要支付本课程费用,拼团成功,",
              color: "#FF9B00"
            },                          
            {
                text: " 您将得到xxx元奖励。",
                color: "#FF0000"
            }
          ],
        })
      }else {
        this.setData({
          typeOf: 'success',
          title: '拼团成功 ! ',
          headTextOne: '您参与的团购拼团成功',
          headTextTwo: '订单号:12344',
          content: [
            {
                text: "感谢团长的分享",
                color: "#000"
            },
            {
                text: " 您也可以发起新的团购分享给需要的朋友们,",
                color: "#000"
            },
            {
                text: "作为新的发起人,您无需在支付本课程费用;",
                color: "#FF0000"
            },
            {
              text: "团购成功,您将得到xxx元奖励。",
              color: "#FF0000"
            }                          
          ],
        })
      }
  },
  close: function () {
    const ind = this.data.ind;
    this.data.closeFlags[ind] = false;
    this.setData({
      closeFlags: this.data.closeFlags
    })
    console.log(this.data.closeFlags)
  },

  //再次发起团
  group: function () {
    debugger;
    const productId = this.data.productId;
    const groupId = this.data.groupId;
    const id = this.data.orderId;
    debugger;
    //开始发起团购
    httpRequestApi.SendGroupPurchas({
      productId,
      groupId
    }).success((res) => {
      console.log('发起团购', res);
      debugger;
      if (res.data.message) {
        wx.showModal({
          title: '提示',
          content: res.data.message,
          success:(res) => {
          }
        })
        return false;
      }
      wx.navigateTo({
        url: `/pages/groupPage/make-money/make-money?productId=${productId}&id=${res.data.data.groupPurchaseOrder.id}&groupId=${groupId}`
      })
      this.close();
    }).fail((error) => {

    })  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.popup = this.selectComponent("#popup");
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  onPullDownRefresh: function () {
    this.onLoad();
    
    wx.showNavigationBarLoading() //在标题栏中显示加载
    //模拟加载
    setTimeout(function()
    {
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    },1500);
  }
})