const util = require('../../../utils/util.js');
Page({
  data: {
    columnsIndex:0,
    scrollLeft:0,
    list: [],
    columns:[],
    imgPath: util.config.imgPath,
    isLoadMore: true,
    pageIndex: 1,
    pageSize: 10
  },
  onLoad() {
    this.getColumns();
  },
  getColumns() {
    let that = this;
    util.ajax({
      func:"v3/column",
      data:{ ctype: 0 },
    },function(res){
      if(res.code == 0){
        that.setData({ columns:res.data });
        that.getList(false);
      }
    });
  },
  getList(load) {
    let self = this;
    util.ajax({
      func: "article/list",
      data: { "cid": self.data.columns[self.data.columnsIndex].id, "pageSize": self.data.pageSize, "pageIndex": self.data.pageIndex }
    }, function (res) {
      if (res.code == 0) {
        let isLoadMore = true;
        let newData = res.data.list;
        let oldData = !load ? newData : [...self.data.list, ...newData];
        if (newData.length < self.data.pageSize || oldData.length > self.data.pageSize) {
          isLoadMore = false;
        }
        self.setData({ list: oldData, remark: res.data.remark, isLoadMore });
      } else
        util.showTips(res.reason);
    });
  },
  columnChange(e){
    let index = e.currentTarget.dataset.index;
    this.setData({ columnsIndex: index, pageIndex: 1, scrollLeft: ((index - 1) * 90)});
    this.getList(false);
  },
  onPullDownRefresh: function () {
    var self = this;
    wx.showNavigationBarLoading();
    this.setData({pageIndex:1})
    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: '宝贝走天下-更安全的儿童户外教育专家',
      path: '/pages/product/activity/system'
    }
  }
})