const util = require('../../../utils/util.js');
const app = getApp();
Page({
  data: {
    id:"",
    title:'',
    list:{},
    imgPath: util.config.imgPath,
    isLoadMore: true,
    pageIndex: 1,
    pageSize: 10
  },
  onLoad: function (options) {
    let title = decodeURIComponent(options.title);
    wx.setNavigationBarTitle({
      title: options.title
    });
    this.setData({
      id: options.id,
      title: options.title
    });
    this.getList(false);
  },
  getList(load){
    let self = this;
    util.ajax({
      func: "v2/categoryHomePageChallenge/articleList",
      data: { "id": self.data.id, "pageSize": self.data.pageSize, "pageIndex": self.data.pageIndex }
    }, function (res) {
      if (res.code == 0) {
        let isLoadMore = true;
        res.data.list.map(el => {
          if (!util.isEmpty(el.discount)) {
            let arr =[];
            if (el.discount.indexOf('+')==-1) {
              arr.push(el.discount);
            } else {
              arr=el.discount.split('+');
            }
            el.discount=arr;
          }
        });
        let newData = res.data.list;
        let oldData = !load ? newData : [...self.data.list, ...newData];
        console.log(oldData.length,self.data.pageSize)
        if (newData.length < self.data.pageSize) {
          isLoadMore = false;
        }
        self.setData({ list: oldData, isLoadMore: isLoadMore });
      }else
        util.showTips(res.reason);
    });
  },
  onPullDownRefresh: function () {
    var self = this;
    this.setData({pageIndex:1})
    wx.showNavigationBarLoading();
    setTimeout(function () {
      self.getList(false);
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1500);
  },
  onReachBottom: function () {
    var self = this;
    if (self.data.isLoadMore == false) {
      return false;
    }

    setTimeout(function () {
      self.setData({ pageIndex: self.data.pageIndex + 1 });
      self.getList(true);
    }, 1000);
  },
  onShareAppMessage() {
    return {
      title: this.data.title,
      path: '/pages/public/swiper/list?id='+this.data.id+('&title='+this.data.title)
    }
  }
})