import httpRequestApi from '../../utils/APIClient';
import util from '../../utils/util';
const app = getApp();
export const groupInit = (that) => {
  that.setData({
    groupData: {
      recommendList: [],
      bookList: [],
      sendGroupFlag: true,
      selectFlag: [],
      isIPX: app.globalData.isIPX,
      timeList: [],
      listLength: '',
      baseIndex: 0,
      isIOS: app.globalData.isIOS,
      alertFlag: false
    },
    groupIndex: 1
  })
  //推荐团购
  that.recommend = function () {
    console.log(that.data.listLength)
    httpRequestApi.getGroupList(that.data.groupIndex).success((res) => {
      
      const recommendListTemp = [];
      res.data.data.list.forEach(item => {
        const temp = {};
        temp.avatar = item.organizer.avatar;
        temp.wechatName = item.organizer.wechatName;
        temp.groupTitle = item.groupPurchaseOrder.groupTitle;
        temp.headCount = item.groupPurchaseOrder.headcount;
        temp.joinCount = item.groupPurchaseOrder.joinCount;
        temp.lastTime = item.groupPurchaseOrder.closeTime - item.currentTime <= 0 ? '时间不足,' : util.formatTime(item.groupPurchaseOrder.closeTime - item.currentTime).join(':');
        temp.id = item.groupPurchaseOrder.id;
        // that.data.groupData.recommendList.push(temp);
        recommendListTemp.push(temp);
      })
      const recommendListStr = "groupData.recommendList";
      // const recommendListThreeStr = "groupData.recommendListThree";
      that.setData({
        [recommendListStr]: recommendListTemp,
        // [recommendListThreeStr]: that.data.groupData.recommendList.slice(that.data.groupData.baseIndex, that.data.groupData.baseIndex + 3),
        listLength: res.data.data.totalNo
      })
    }).fail((error) => {
      console.log('错误', error)
    })
  }

  //请求数据封装
  that.getGroupList = function () {
    httpRequestApi.getAllBooks(1, 10).success((res) => {
      console.log('全部课', res.data.data.list)
      that.data.groupData.bookList = res.data.data.list;
      console.log(that.data.groupData.bookList)
      res.data.data.list.forEach(element => {
        that.data.groupData.selectFlag.push(true);
      });
      that.setData({
        groupData: that.data.groupData
      })
    }).fail((error) => {
      console.log('错误', error)
    })
  }();

  that.recommend();

  //点击换一换
  that.change = function () {
    that.data.groupIndex++
    if (that.data.groupIndex >= that.data.listLength) {
      that.setData({
        groupIndex: 1
      })
    } else {
      that.setData({
        groupIndex: that.data.groupIndex
      })
    }
    that.recommend(that.data.groupIndex)
  }
  //点击跳转
  that.more = function ({
    currentTarget
  }) {
    wx.navigateTo({
      url: `../main/books/books`
    })
  }
  //发起团购
  that.sendGroup = function () {
    that.data.groupData.sendGroupFlag = !that.data.groupData.sendGroupFlag;
    that.setData({
      groupData: that.data.groupData
    })
  }
  //选中团购课程
  that.selectImg = function ({
    currentTarget
  }) {
    const ind = currentTarget.dataset.ind;
    //判断单选
    that.data.groupData.selectFlag.forEach((item, index) => {
      if (index == ind) {
        that.data.groupData.selectFlag[ind] = !that.data.groupData.selectFlag[ind];
      } else {
        that.data.groupData.selectFlag[index] = true;
      }
    })
    that.setData({
      groupData: that.data.groupData
    })
  }
  //点击确定
  that.sure = function () {
    that.data.groupData.selectFlag.forEach((item, index) => {
      if (!item) {
        const productId = that.data.groupData.bookList[index].id;
        const title = that.data.groupData.bookList[index].title
        wx.navigateTo({
          url: `/pages/groupPage/grade-details/grade-details?productId=${productId}&title=${title}`
        })
      }
    })

  }
  //跳转到我的团购
  that.myGroup = function () {
    wx.navigateTo({
      url: `/pages/groupPage/my-group/my-group`
    })
    wx.setNavigationBarTitle({
      title: '我的团购'
    })
  }
  // IOS提示不能参团
  that.showAlert = function () {
    let str = "groupData.alertFlag"
    that.setData({
      [str]: !that.data.groupData.alertFlag
    })
    console.log(that.data.groupData.alertFlag)
  }
  //跳转到团购详情页
  that.groupDetail = function ({
    currentTarget
  }) {
    if (that.data.groupData.isIOS) {
      that.showAlert();
      return;
    }
    const productId = currentTarget.dataset.productid;
    const id = currentTarget.dataset.id;
    const groupId = currentTarget.dataset.groupid;
    const ind = currentTarget.dataset.ind;
    const groupType = that.data.groupData.recommendList[ind].groupType;
    console.log(id)
    wx.navigateTo({
      // url: `/pages/groupPage/group-details/group-details?productId=${productId}&id=${id}&groupId=${groupId}`
      url: `/pages/groupPage/group-details/group-details?productId=${id}`
    })
    // if(groupType === "PROMOTION") {
    //   wx.navigateTo({
    //     url: `/pages/groupPage/make-money/make-money?productId=${productId}&id=${id}&groupId=${groupId}`
    //   })
    // }else  {
    //   wx.navigateTo({
    //     url: `/pages/groupPage/group-details/group-details?productId=${productId}&id=${id}&groupId=${groupId}`
    //   })
    // }

  }
  //跳转到课程详情
  that.goToBook = function (e) {
    console.log(e.currentTarget.dataset)
    let id = e.currentTarget.dataset.id;
    let title = e.currentTarget.dataset.title;
    console.log(id)
    wx.navigateTo({
      url: `/pages/groupPage/grade-details/grade-details?productId=${id}&title=${title}`
    })
  }
}