//index.js
//获取应用实例
import {
  getOpenidSessionKey
} from '../../utils/httpUtil';
import {
  formatDate
} from '../../utils/util';

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/APIClient';
import httputil from '../../utils/httpUtil';
import {
  GetQueryString
} from '../../utils/util';

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,
    myData: {},
    templates: 'hot',
    title: 'index中的title',
    jurisdictionFlag: true,
    hotInput: '12345',
    mineSettingInfo: '528',
    hide: true,
    isIOS: app.globalData.isIOS,
  },
  jurisdiction: function () {
    //隐藏弹框
    this.setData({
      hide: !this.data.hide
    })
    //登录页信息
    this.onShow();
  },
  //tab点击
  switcher: function ({
    currentTarget
  }) {
    if (currentTarget.dataset.index === this.data.myIndex) return;
    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) {
      this.setData({
        recommendPageNo: 1
      })
      hotInit(this);
    }
    if (myIndex == 2) {
      this.setData({
        followPageNo: 1
      })
      followInit(this);
    }
    if (myIndex == 3) {
      // console.log(this.data.isIOS)
      myInit(this);
    }
  },
  onLoad: function (options) {

    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 () {
    wx.setNavigationBarTitle({
      title: '小学课文朗读配音'
    })
    // this.init();
    getOpenidSessionKey((res) => {}, (error) => {
      // console.log(error)
      this.setData({
        hide: !this.data.hide
      })
    });
    if (this.data.myIndex === 3) {
      this.getUserWorksInfo();
    }
  },
  //初始化数据
  init: function () {
    httputil.getOpenidSessionKey((res) => {
      console.log('微信的用户信息', res)
      
    }, (error) => {
      console.log(error);
      this.jurisdiction()
      // this.setData({
      //   jurisdictionFlag: false
      // })
    });
  },
  // 获取用户信息
  getUserWorksInfo: function () {
    console.log(wx.getStorageSync('user'));
    httpRequestApi.getUserWorksInfo().success(res => {
      this.data.myData.user = res.data.data; //直接赋值 给 myData的user对象。
      this.data.myData.user.user.birthday = this.data.myData.user.user.birthday ? formatDate(res.data.data.user.birthday, 4) : '2018年1月1日';
      httpRequestApi.userIntoPage('pages/index/index', '首页我的').success((res) => {

      })
      this.setData({
        myData: this.data.myData,
      });
    }).fail(error => {
      console.log(error)
    })
  },
  // 触底加载
  onReachBottom: function () {

    // 当前在推荐页面 加载推荐
    if (this.data.myIndex === 1) {
      console.log(this.data.recommendPageNo)
      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.data.followPageNo, 5);

      } else {
        console.log('没有更多')
      }
    }
  },
  onPullDownRefresh: function () {
    //当前在团购页下拉加载
    if (this.data.myIndex === 0) {
      groupInit(this);
    }

    wx.showNavigationBarLoading() //在标题栏中显示加载
    //模拟加载
    setTimeout(function () {
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500);
  }

})