// pages/groupPage/collage-details/collage-details.js
import httpRequestApi from '../../../utils/APIClient';
import util from '../../../utils/util';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    groupPurchaseInfo: {},
    organizer: {},
    surplusNum: '',
    joinUserList: [],
    surplusList: [],
    timeList: [],
    productId: '',
    orderId: '',
    groupId: '',
    content: [{
        text: "转发给自己的朋友们",
        color: "#000"
      },
      {
        text: " 分享好课，追随您一起学习进步",
        color: "#000"
      },
      {
        text: " 您的魅力不是吹的",
        color: "#000"
      },
      {
        text: "在限定时间内成功拼团，您将得到奖学金。",
        color: "#FF4600"
      }
    ],
    read: false,
    organizerUid: '',
    colonelName: '',
    rewardMoney: '',
    goBackHome: false,
    dataLoading: true
  },
  //显示分享页 
  openShare: function () {
    if (this.data.read) {
      const productId = this.data.productId;
      const id = this.data.orderId;
      const groupId = this.data.groupId;
      wx.navigateTo({
        url: `/pages/groupPage/grade-details/grade-details?productId=${productId}&id=${id}&groupId=${groupId}&share=true`
      })
    } else {
      this.shareDialog = this.selectComponent("#share-dialog");
      const data = {
        avatar: this.data.organizer.avatar,
        author: this.data.organizer.wechatName,
        iconImg: this.data.groupPurchaseInfo.bgImg,
        title: this.data.groupPurchaseInfo.productTitle,
        path: `pages/groupPage/group-details/group-details`,
        scene: `QR${this.data.orderId}`,
        groupPurchaseInfo: this.data.groupPurchaseInfo,
        groupType: this.data.groupOsType
      }
      this.shareDialog.share(data);
    }
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    this.setData({
      surplusList: []
    }, () => {
      this.getInfo();
    })
    wx.showNavigationBarLoading() //在标题栏中显示加载
    //模拟加载
    setTimeout(function () {
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // if (app.globalData.isIOS) {
    //   wx.redirectTo({
    //     url: '../../index/index'
    //   })
    //   return 
    // }
    if (options.read) {
      this.setData({
        read: options.read
      })
    }
    if (options.goBackHome == 'true') {
      this.setData({
        goBackHome: true
      })
    }
    // options.scene = 'QR1541862338777268';
    wx.setNavigationBarTitle({
      title: '拼团详情'
    })
    console.log(options);
    console.log(options.orderId);
    const orderId = options.orderId ? options.orderId : options.scene.replace('QR', '');
    // if (options.orderId) {
    //   return false;
    // }

    console.log(orderId)
    this.setData({
      orderId
    }, () => {
      this.getInfo()
    })
  },
  getInfo: function () {
    httpRequestApi.getMygroupInfo(this.data.orderId).success(res => {
      console.log(res);
      const groupOrder = res.data.data.groupPurchaseOrder;
      const surplusNum = groupOrder.headcount - groupOrder.joinCount;
      this.setData({
        organizerUid: groupOrder.organizer,
        read: groupOrder.status === 'SUCCESSED' ? true : false
      })
      for (var i = 0; i < surplusNum; i++) {
        this.data.surplusList.push(1);
      }
      //console.log(res.data.data.groupPurchaseOrder.closeTime - res.data.data.groupPurchaseOrder.gmtModified)
      //时间转换
      let timeList = [];
      const stillGoing = groupOrder.closeTime - Date.parse(new Date()) <= 0 ? false : true
      if (stillGoing) {
        timeList = util.lastHoursInGroup(groupOrder.closeTime - Date.parse(new Date()));
      }
      // const timeList = util.formatTime(groupOrder.closeTime - Date.parse(new Date()));
      // console.log(timeList)
      //res.data.data.joinUserList.unshift(res.data.data.organizer);
      this.setData({
        groupPurchaseInfo: res.data.data.groupPurchaseInfo,
        organizer: res.data.data.organizer,
        surplusNum,
        joinUserList: res.data.data.joinUserList,
        surplusList: this.data.surplusList,
        timeList,
        stillGoing,
        productId: groupOrder.productId,
        groupId: groupOrder.groupId,
        rewardMoney: res.data.data.groupPurchaseInfo.organizerPrice / 100,
        dataLoading: false,
        groupOsType: groupOrder.os
      })
      if (groupOrder.groupType === 'PROMOTION') {
        //this.data.joinUserList.unshift(res.data.data.organizer);
        this.setData({
          colonelName: res.data.data.organizer.wechatName
        })
        this.setData({
          joinUserList: this.data.joinUserList,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.shareDialog = this.selectComponent("#share-dialog");

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (ops) {
    if (ops.from === 'button') {
      console.log(ops.target)
    }

    const id = this.data.orderId;
    const data = {
      shareTypeEnum: 'LINK',
      shareContentEnum: 'GROUP',
      productId: this.data.productId
    }
    httpRequestApi.shareRecord(data).success((res) => {
      console.log(res)
    })
    return {
      title: '老师同学都在玩的配音show，你还在等什么？',
      path: `pages/groupPage/group-details/group-details?share=true&shareId=${id}`,
      imageUrl: `https://efunbox.lingjiao.cn/reader/resource/share/group_${this.data.productId}.jpg`,
    }
  }
})