import httpRequestApi from '../../utils/APIRequest';
export function myInit(that) {
  that.setData({
    myData: {
      nav: ['我的娃', '收藏', '历史'],
      myInd: 0,
      favoritesList: [],
      playLogList: [],
      userName: '',
      avatar: ''
    }
  })
  //点击切换
  that.myChoice = ({ currentTarget }) => {
    that.data.myData.myInd = currentTarget.dataset.index;
    that.setData({
      myData: that.data.myData
    })
    //获取个人信息
    if(currentTarget.dataset.index == 0) {
     that.getUserInfo();
    }
    //获取收藏列表
    if(currentTarget.dataset.index == 1) {
      that.getFavoritesList();
    }
    //获取播放记录
    if(currentTarget.dataset.index == 2) {
      that.getPlayLogList();
    }
  }
  //滑动切换
  that.mySlide = ({detail}) => {
    that.data.myData.myInd = detail.current;
    // console.log(detail.current);
    that.setData({
      myData: that.data.myData
    })
    //获取个人信息
    if(detail.current == 0) {
      that.getUserInfo();
    }
    //获取收藏列表
    if(detail.current == 1) {
      that.getFavoritesList();
    }
    //获取播放记录
    if(detail.current == 2) {
      that.getPlayLogList();
    }
  }
  //点击跳转页面
  that.setName = () => {
    wx.navigateTo({
      url: '/pages/setName/setName'
    })
  }
  //获取收藏列表
  that.getFavoritesList = () => {
    httpRequestApi.getFavoritesList({
      pageNo: 1,
      pageSize: 10
    }).success((res)=>{
      console.log('收藏列表', res);
      that.data.myData.favoritesList = res.data.data.list;
      that.setData({
        myData: that.data.myData
      })
    })
  }
  //获取播放记录
  that.getPlayLogList = () => {
    httpRequestApi.getPlayLogList({
      pageNo: 1,
      pageSize: 10
    }).success((res)=>{
      console.log('播放记录', res);
      that.data.myData.playLogList = res.data.data.list;
      that.setData({
        myData: that.data.myData
      })
    })
  }
  //跳转到相册
  that.album = ({ currentTarget }) => {
    //const id = currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/album/album' 
    })
  }
  //跳转到详情页
  that.detail = ({ currentTarget }) => {
    const id = currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/details/details?id=' + id 
    })
  }
  //获取个人信息
  that.getUserInfo = () => {
    httpRequestApi.getUserInfo().success(res => {
      console.log('七彩童年的用户信息', res);
      //wx.setStorage('photoBox', res.data.data.photoBox)
      wx.setStorageSync('photoBox', res.data.data.photoBox)
      that.data.myData.userName = res.data.data.nickName;
      that.data.myData.avatar = res.data.data.avatar
      that.setData({
        myData: that.data.myData
      })
    });
  }
  //初始化调用个人信息
  that.getUserInfo();
}