const util = require('../../../utils/util.js');
const app = getApp();
Page({
  data: {
    tagActive:-1,//标签选中项
    scrollTop:0,
    dataList:[],
    pageIndex:1,
    typeTid:'',
    isHideLoadMore:false,
    showBack:false,
    currentPages:0,
    pageSize:8,
    tags:[],
    updateLoading:true,//加载中
    isAllLoaded:false
  },
  onLoad(options) {
    this.setData({currentPages:getCurrentPages().length});
    let that = this;
    this.setData({dataList:[]})
    this.getTagOption()
    if(!util.isObjEmpty(app.globalData.city)){
      this.getData(1,false);
    }else{
      // var getLocation = util.promise();
      // getLocation.then(function(value) {
        that.getData(1,false);
      // }, function(error) {
      // });
    }
  },
  onShow(){
    
  },
  onReachBottom() {
   
  },
  // onPullDownRefresh(){
  //   let that = this;
  //   wx.showNavigationBarLoading();
  //   this.setData({dataList:[]})
  //     that.getData(1,false,true)
  // },
  onPageScroll(e){
    var showBack = this.data.showBack;
    if(e.scrollTop>800){
      if(!showBack){
        this.setData({showBack:true})
      }
    }else{
      if(showBack){
        this.setData({showBack:false})
      }
    }
  },
  // 触底加载
  handleScrollLower(){
    if(!this.data.isHideLoadMore){
      let pageIndex = this.data.pageIndex;
      this.setData({updateLoading:true})
      this.getData(pageIndex+1,true)
    }
  },
  // 标签选择
  activeTag(e){
    let index=e.currentTarget.dataset.index,
    tid=e.currentTarget.dataset.tid;
    if (index!=this.data.tagActive) {
       this.setData({tagActive:index,typeTid:tid,dataList:[],updateLoading:true,scrollTop:0,isHideLoadMore:false})
    this.getData(1,false)
    }
  },
  // 获取标签分类
  getTagOption(){
    var that = this;
    util.ajax({
      func:"ebusiness/type/list",
      load:false
    },function(res){
      if(res.code == 0){
        that.setData({tags:res.data.list})
      }
    })
  },
  getData(pageIndex,load, PullDown){
    var that = this;
    util.ajax({
      func:"ebusiness/list",
      data: {
        pageIndex:pageIndex,
        pageSize:that.data.pageSize,
        tid:that.data.typeTid
      },
      load:false
    },function(res){
      if(res.code == 0){
        var datas = res.data.goods || [];
        var dataList = that.data.dataList;
        var isHideLoadMore = false;
        dataList = pageIndex == 1?datas:[...dataList , ...datas];
        if(datas.length < that.data.pageSize && dataList.length > 0){
          isHideLoadMore = true;
        }
        that.setData({dataList,isHideLoadMore,pageIndex,updateLoading:false})
      }else{
        util.showTips(res.reason);
      }

      if(PullDown){
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  toTop(){
    this.setData({showBack:false});
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
  },
  goDetails(e){
    let aid = e.currentTarget.dataset.id,title = e.currentTarget.dataset.title;
    wx.reportAnalytics('equip_details', {aid,title});
    wx.navigateTo({
      url: '/pages/product/goods/detail?aid='+aid
    })
  },
  onShareAppMessage() {
    return {
      title: '宝贝走天下电商商品',
      path: '/pages/product/goods/index'
    }
  },
})