const app = getApp();
const util = require("../../../util/util.js")
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    loadModal:false,
    from:0,
    size:6,
    list:[
    ]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function ( ) {
    app.checkLogin( ()=>{
      this.loadData()
    } )
  },
  bidTask: function( e ){
    let index = e.currentTarget.dataset.index;
    let item = this.data.list[index]
    util.http("/wx/applyWxTask", {id:item.id}, (err, res) =>{
      if( err != 0) return;
      item.is_bid = 1
      this.setData({ list: this.data.list})
      util.showMsg("抢单子成功");
    })
  },
  onReachBottom: function () {
    var that = this
    let newFrom = that.data.from
    if (newFrom == -1) {
      wx.stopPullDownRefresh();
      return;
    }
    this.getTaskList(newFrom, wx.stopPullDownRefresh);
  },
  getTaskList: function( newFrom, cb){
    let param = { from: newFrom, size:this.data.size }
    let list = this.data.list
    let that = this
    
    util.http("/wx/getSupplyCenter", param, function (err, res) {
      if( err != 0) return;
      if (newFrom == 0) list = [];
      let newlist = res.list.map( item =>{
        item.bid_end_at = item.bid_end_at.substr(0,10);
        return item
      });
      that.setData({
        from: res.from || -1,
        list: list.concat(newlist)
      })
      cb && cb()
    });
  },
  loadData :function( ){
    this.getTaskList(0)
  },
})