// index.js
// 获取应用实例
const app = getApp()
const $api = require('../../utils/api.js').API;
const $push = require('../../utils/api.js').PUSH;

Page({
  data: {
    loading:0,
    notices:[],
    tabs:['今日留言','我的关注'],
    tabs1:['收益榜','盈利榜','亏损榜'],
    cur:0,
    cur1:0,
    date:'2022-04-27',
    explist:[],   //留言
    total1:0,
    page1:1,
    followList:[],  //关注
    total2:-1,
    page2:1,
    banners:[],
    winLost:{},
    baikeList:[],
    articleList1:[],
    articleList2:[],
    articleList3:[],
    articleList4:[],
    groups:{},
    role:0,
  },
  onLoad() {
    wx.showNavigationBarLoading()
    wx.getStorage({
      key: 'user',
      complete: res => {
        this.setData({
          role: res.data.role,
        })
      }
    })

    $api.getDate().then(res=>{
      this.setData({
        date:res.data.data
      })
      $api.getNotoice().then(res=>{
        this.setData({
          notices:res.data.data
        })
      })
    })
    this.getFollow()
    this.getIndexData()
    this.getExp()
  },
  fresh: function () {

  },
  // 获取首页数据
  getIndexData:function(){
    $api.getChampionlList({isbanner:1}).then(res=>{
      console.log(res)
      this.setData({
        banners:res.data.data.list
      })
    })
    $api.getWinList({new:1}).then(res=>{
      this.setData({
        winLost:res.data.data[0]
      })
      $api.getWinLose({match_id:res.data.data[0].match_id}).then(res1=>{
          this.setData({
            groups:res1.data.data
          })
      })
    })
    $api.getBaikeRandom().then(res=>{
      this.setData({
        baikeList:res.data.data
      })
    })
    $api.getChampionlList({category:'游资专场',page:1}).then(res=>{
      this.setData({
        articleList1:res.data.data.list
      })
    })
    $api.getChampionlList({category:'冠军交割',page:1}).then(res=>{
      this.setData({
        articleList2:res.data.data.list
      })
    })
    $api.getChampionlList({category:'牛人专场',page:1}).then(res=>{
      this.setData({
        articleList3:res.data.data.list
      })
    })
    $api.getChampionlList({category:'妖股列传',page:1}).then(res=>{
      this.setData({
        articleList4:res.data.data.list
      })
    })
  },
  //获取今日留言
  getExp:function(){
    var list = this.data.explist
    $api.getExperience({page:this.data.page1,page_size:20}).then(res=>{
      res.data.data.list.forEach(item => {
        list.push(item)
      })
      this.setData({
        explist: list,
        total1: res.data.data.total
      })
    })
  },
  //留言分页
  next1: function () {
    if (this.data.explist.length < this.data.total1) {
      const page = this.data.page1 + 1
      this.setData({
        page1: page
      })
      this.getExp()
    }
  },
  //我的关注
  getFollow(){
    var followList = this.data.followList
    if(this.data.loading){
      followList=[]
    }
    $api.getMyFollowV3({
      page: this.data.page2, page_size: 20
    }).then(res => {
      wx.hideNavigationBarLoading()
      res.data.data.list.forEach(item => {
        followList.push(item)
      })
      this.setData({
        followList: followList,
        total2:res.data.data.total,
        error:0,
        loading: 0
      })
      app.globalData.follow = 0
    })
    .catch(err=>{
      this.setData({
        error:1
      })
    })
  },
  //关注分页
  next2: function () {
    if (this.data.followList.length < this.data.total2) {
      const page = this.data.page2 + 1
      this.setData({
        page2: page
      })
      this.getFollow()
    }
  },
  tabChange(e){
      this.setData({
        cur:e.target.dataset.id
      })
      this.renew()
  },
  tabChange1(e){
    this.setData({
      cur1:e.target.dataset.id
    })
},
  //预览
  preview(e){
    wx.previewImage({
      urls: e.target.dataset.urls,
      current: e.target.dataset.src
    })
  },
  curChange(e){
    if (e.detail.source == "touch"){
        this.setData({
          cur: e.detail.current
        })
        this.renew()
    }
  },
  curChange1(e){
    if (e.detail.source == "touch"){
        this.setData({
          cur1: e.detail.current
        })
    }
  },
  renew(){
    this.setData({
      followList:[],
      explist:[],
      page1: 1,
      total1: 0,
      page2: 1,
      total2: -1
    })
    switch(this.data.cur){
      case 0:
        this.data.page1 = 1
        this.getExp()
        break;
      case 1:
        this.data.page2 = 1
        this.getFollow()
        break;
    }
  },
  pushToday(e){
    $push.pushToday(e.currentTarget.dataset)
  },

  onShow: function (e) {
    $api.initUser()
    
    $api.getsignup().then(res => {
      this.setData({
        info: res.data.data
      })
    })
    
    if(this.data.cur == 0 && app.globalData.follow == 1){
      this.data.loading = 1
      this.data.page2    = 1
      this.data.total2   = -1,
      this.getFollow()
    }
  },
})
