// pages/stock/suppliermanage/suppliermanage.js
let common = require('../../../utils/util.js');
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: '',
    data: {
      total: 0,
      payedMoney: 0,
      totalMoney: 0,
      needPayMoney: 0
    },
    searchText: '',
    isfirst: true
  },
  setSearchText(e) {
    this.setData({
      searchText: e.detail.value
    })
    this.getListData()
  },
  clearText() {
    this.setData({
      searchText: ''
    })
    this.getListData()
  },
  toaddsupplier() {
    wx.navigateTo({
      url: '/pages/stock/addsupplier/addsupplier',
    })
  },
  edite(e) {
    wx.navigateTo({
      url: '/pages/stock/addsupplier/addsupplier?id=' + e.currentTarget.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    this.getListData()
  },
  todetail(e) {
    wx.navigateTo({
      url: '/pages/stock/supplierdetail/supplierdetail?id=' + e.currentTarget.dataset.id + '&onlyid=' + e.currentTarget.dataset.onlyid
    })
  },
  getListData() {
    common.query("supplier/wxapi/list", {
      tag: this.data.searchText
    }, function(res) {
      if (res.code === 10000) {
        res.alreadyPayed = res.alreadyPayed.toFixed(2)
        res.needPay = res.needPay.toFixed(2)
        res.totalAmount = res.totalAmount.toFixed(2)


        that.setData({
          data: res,
          datalength: res.data.length
        })
      } else {
        that.setData({
          data: {
            total: 0,
            alreadyPayed: 0,
            totalAmount: 0,
            needPay: 0

          },
          datalength: 0
        })
      }

    })
  },
  onPullDownRefresh() {
    this.getListData()
    wx.stopPullDownRefresh()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (!this.data.isfirst) {
      this.getListData()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      isfirst: false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})