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
    }
  })
  //推荐团购
  that.recommend = function (start, end) {
    httpRequestApi.getGroupList().success( (res) => {
      console.log('推荐团购',res.data.data)
      that.data.groupData.listLength = res.data.data.length;
      that.data.groupData.recommendList = res.data.data.slice(start, end);
      that.data.groupData.recommendList.forEach( (item) => {
        if(item.groupPurchaseOrder.closeTime - Date.parse(new Date()) < 0 ) {
          that.data.groupData.timeList.push('时间到了,')
        } else {
          that.data.groupData.timeList.push(util.formatTime(item.groupPurchaseOrder.closeTime - Date.parse(new Date())).join(':'))
        }
      })
      console.log('截取之后', that.data.groupData.recommendList)
      that.setData({
        groupData: that.data.groupData
      })
      //util.formatTime(groupOrder.closeTime - Date.parse(new Date()))
    }).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(0, 3);

  //点击换一换
  that.change = function () {
    that.data.groupData.baseIndex++;
    if(that.data.groupData.listLength < 3 * (that.data.groupData.baseIndex)) {
      that.data.groupData.baseIndex = 0;
    }
    that.setData({
      groupData: that.data.groupData
    })
    that.recommend(3 * that.data.groupData.baseIndex, 3 * (that.data.groupData.baseIndex + 1));
  }
  //点击跳转
  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: '我的团购'
    })
  }
  //跳转到团购详情页
  that.groupDetail = function ({currentTarget}) {
    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;
      wx.navigateTo({
        url: `/pages/groupPage/group-details/group-details?productId=${productId}&id=${id}&groupId=${groupId}`
      })
    // 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}`
      })
  }
}