import httpRequestApi from '../../utils/APIClient';
export const groupInit = (that) => {
  that.setData({
    groupData: {
      recommendList: [],
      bookList: [],
      sendGroupFlag: true,
      selectFlag: []
    }
  })
  //请求数据封装
  that.getGroupList = function () {
    httpRequestApi.getGroupList().success( (res) => {
      console.log('推荐团购',res.data.data)
      that.data.groupData.recommendList = res.data.data;
      that.setData({
        groupData: that.data.groupData
      })
    }).fail((error) => {
      console.log('错误',error)
    })
    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.more = function ({currentTarget}) {
    let type;
    if(currentTarget.dataset.type == 'group') {
      wx.navigateTo({
        url: `/pages/groupPage/discount-group/discount-group`
      })
    }else {
      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;
    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}`
      })
  }
}