//index.js
//获取应用实例
import {
  getOpenidSessionKey
} from '../../utils/httpUtil';
const app = getApp()

import {
  hotInit
} from '../../component/hot/hot';
import {
  groupInit
} from '../../component/group/group';
import {
  myInit
} from '../../component/my/my';
import {
  followInit
} from '../../component/follow/follow';
// import '../../utils/hls';
// import httpRequestApi from '../../utils/APIRequest';
import httputil from '../../utils/httpUtil';
Page({
  data: {
    tab: [{
        name: '热团',
        templates: 'group',
      },
      {
        name: '推荐',
        templates: 'hot',
      },
      {
        name: '关注',
        templates: 'follow',
      },
      {
        name: '我的',
        templates: 'my',
      }
    ],
    winH: 568,
    myIndex: 1,
    followData:[],
    recommendPageNo:1,
    recommendTotalNo:0,
    followPageNo:1,
    followPageTotalNo:0,
    templates: 'hot',
    title: 'index中的title',
    jurisdictionFlag: true,
    hotInput: '12345',
    mineSettingInfo: '528',
    hide: true
  },
  jurisdiction: function () {
    //隐藏弹框
    this.setData({
      hide: !this.data.hide
    })
    //登录页信息
    this.onShow();
  },
  //tab点击
  switcher: function ({
    currentTarget
  }) {
    this.updateData(currentTarget.dataset.index);
  },
  // 根据index 更新template
  updateData: function (index) {
    let myIndex = index;
    let templates = this.data.tab[myIndex].templates;
    this.setData({
      myIndex,
      templates
    });
    if (myIndex == 0) {
      groupInit(this);
    }
    if (myIndex == 1) {
      hotInit(this);
    }
    if (myIndex == 2) {
      followInit(this);
    }
    if (myIndex == 3) {
      myInit(this);
      // getOpenidSessionKey( res => {
      //   console.log(res);
      // }, (error) => {
      //   this.setData({
      //     hide: !this.data.hide
      //   });
      // });
    }
  },
  aa: function(){
    let a = new Promise((resolve, reject)=>{
      resolve(console.log('aa'))
    });
    return a;
  },
  bb: function(){
    let b = new Promise((resolve, reject)=>{
      resolve(console.log('bb'))
    });
    return b
  },
  cc: function(){
    let c = new Promise((resolve, reject)=>{
      console.log('cc')
    });
    return c
  },
  onLoad: function (options) {
    this.aa().then(()=>{
      console.log(1)
      return this.bb()
    }).then(()=>{
      console.log(2)
      return this.cc()
    })
    
    this.uid = wx.getStorageSync('uid');
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          winH: res.windowHeight
        });
      }
    });
    if (options.index) {
      this.updateData(options.index)
    }
    hotInit(this)
    // this.init();

  },
  onShow: function () {
    // this.init();
    getOpenidSessionKey((res) => {}, (error) => {
      // console.log(error)
      this.setData({
        hide: !this.data.hide
      })
    });
  },
  //初始化数据
  init: function () {
    httputil.getOpenidSessionKey((res) => {
      console.log('微信的用户信息', res);
      // this.setData({
      //   jurisdictionFlag: true
      // })
      // lookInit(this);
    }, (error) => {
      console.log(error);
      this.jurisdiction()
      // this.setData({
      //   jurisdictionFlag: false
      // })
    });
  },
  // 触底加载
  onReachBottom: function () {
    console.log(this.data.recommendPageNo)
    console.log('bottom')
    // 当前在推荐页面 加载推荐
    if (this.data.myIndex === 1) {
      this.setData({
        recommendPageNo: this.data.recommendPageNo+1
      })
      if(this.data.recommendPageNo <= this.data.recommendTotalNo ){
        this.getHotRecommend(this.uid, this.data.recommendPageNo, 3);
        
      } else {
        console.log('没有更多')
      }
    }
    if(this.data.myIndex === 2){
      console.log(this.data.followPageTotalNo)
      this.setData({
        followPageNo: this.data.followPageNo+1
      })
      if(this.data.followPageNo <= this.data.followPageTotalNo ){
        this.getWorks(this.uid, this.data.followPageNo, 5);
        
      } else {
        console.log('没有更多')
      }
    }
  }
})