import httpRequestApi from '../../utils/APIClient';
export const hotInit = (that) => {
  that.setData({
      hotData: {
        title: '热门',
        imgUrls: [
          '../../static/image/timg.jpg',
          '../../static/image/timg.jpg',
          '../../static/image/timg.jpg'
        ],
        indicatorDots: true,
        autoplay: true,
        interval: 5000,
        duration: 700,
        circular: true,
        hotWareCardFirst: 'hotWareCardFirst',
        hotWareCard: 'hotWareCard',
        hotSearch: '鹅鹅鹅',
        banner: [],
        recommend: [],
        hotWorks: [],
        winH: that.data.winH
      }
    }),
    // 搜索方法
    that.searchHandler = () => {
      if (that.data.hotInput.lenght !== 0) {
        wx.navigateTo({
          url: `../main/searchResult/searchResult?keyWords=${that.data.hotInput}`
        })
      }

    },
    // 输入框输入
    that.inputHandler = (e) => {
      that.setData({
        hotInput: e.detail.value
      });
    }
  // 打开全部课本
  that.openBooks = (e) => {
    wx.navigateTo({
      url: `../main/books/books`
    })
  }
  // 打开更多页面
  that.openMore = () => {
    wx.navigateTo({
      url: `../main/week/week`
    })
  }
  // 打开课程页面
  that.openClass = (e, lessonId) => {
    if (lessonId) {
      wx.navigateTo({
        url: `../main/class/class?id=${lessonId}`
      })
    } else {
      let id = e.currentTarget.dataset.classid;
      let title = e.currentTarget.dataset.title;
      wx.navigateTo({
        url: `../main/class/class?id=${id}&title=${title}`
      })
    }
  }
  // 打开用户作品页面
  that.openWorks = (e) => {
    let readId = e.currentTarget.dataset.readid;
    let title = e.currentTarget.dataset.title;
    wx.navigateTo({
      url: `../social/works/works?id=${readId}&title=${title}`
    })
  }
  that.getIndex = () => {
    const uid = wx.getStorageSync('uid');
    httpRequestApi.getIndex(
      uid
    ).success((res) => {

      const banner = res.data.data.banner;
      const recommend = res.data.data.recommendLesson;
      const bannerTemp = [];
      banner.forEach(item => {
        const temp = {};
        temp.url = item.boothContent;
        temp.type = item.operationType;
        temp.id = item.operationContent;

        bannerTemp.push(temp);
      });
      const recommendTemp = [];
      recommend.forEach(item => {
        const oTemp = {};
        oTemp.img = item.boothContent;
        oTemp.classId = item.operationContent;
        oTemp.title = item.title;
        oTemp.grade = item.summary;
        recommendTemp.push(oTemp);
      });
      let bannerIndex = 'hotData.banner';
      let recommendIndex = 'hotData.recommend';
      that.setData({
        [bannerIndex]: bannerTemp,
        [recommendIndex]: recommendTemp
      })
    })
  }
  that.getHotRecommend = (uid, pageNo, pageSize) => {
    httpRequestApi.getHotRecommend(
      uid,
      pageNo,
      pageSize
    ).success((res) => {
      console.log(res)
        const recommendRes = res.data.data.list;
        // const recommendWorks = [];
        recommendRes.forEach(item => {
          const temp = {};
          temp.title = item.userRead.title;
          temp.grade = item.userRead.title;
          temp.img = item.userRead.iconImg;
          temp.plays = item.userRead.playAmount ? item.userRead.playAmount : 0;
          temp.likes = item.userRead.likeAmount ? item.userRead.likeAmount : 0;
          temp.classId = item.userRead.id;
          // recommendWorks.push(temp);
          that.data.hotData.hotWorks.push(temp);
        });
        const hotStr = 'hotData.hotWorks'
        that.setData({
          [hotStr]: that.data.hotData.hotWorks,
          recommendTotalNo: res.data.data.totalNo
        })
    })
  }

  // 去全部课本
  that.goToBooks = () => {
    wx.navigateTo({
      url: `../main/books/books`
    })
  }
  that.bannerTap = (e) => {
    const type = e.currentTarget.dataset.type;
    const id = e.currentTarget.dataset.id;
    switch (type) {
      case 'LESSON':
        that.openClass('tap', id);
        break;
    }
  }
  // 下拉加载
  that.scrollUpdate = (e) => {
    console.log(e)
  }
  that.getIndex();
  that.getHotRecommend(that.uid, 1, 3)
}