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

  /**
   * 页面的初始数据
   */
  data: {
    index: 1,
    imageurl: '',
    showpay: false,
    payWayarray: [],
    payWayindex: 0,
    paydate: ''
  },
  savepay() {
    if (!this.data.payAmount) {
      util.showToast('请输入金额')
      return
    }
    util.query('supplierpayback/wxapi/create', {
      supplier: {
        id: this.data.id,
        onlyID: this.data.onlyid
      },
      payAmount: this.data.payAmount,
      payWay: this.data.payWayarray[this.data.payWayindex].key,
      createTime: this.data.paydate + ' 00:00:00'
    }, function(res) {
      if (res.code === 10000) {
        util.showToast('保存成功')
        that.getpayback()
        that.getData()
        that.setData({
          showpay: false
        })
      } else {
        util.showToast('保存失败')
      }
    }, 'POST')
  },
  payback() {
    this.setData({
      showpay: true,
      paydate: util.formatShortTime(new Date()),
      payWayindex: 0,
      payAmount: ''
    })
  },
  paytimechange(e) {
    this.setData({
      paydate: e.detail.value
    })
  },
  setprice(e) {
    this.setData({
      payAmount: util.PointNum(e.detail.value)
    })
  },
  payWayChange(e) {
    this.setData({
      payWayindex: e.detail.value
    })
  },
  setindex(e) {
    this.setData({
      index: e.currentTarget.dataset.index
    })
  },
  del(e) {
    wx.showModal({
      title: '确认',
      content: '是否确定删除供应商',
      success(res) {
        if (res.confirm) {

          util.query("supplier/wxapi/delete", {
            id: that.data.id
          }, function(res) {
            if (res.code == 10000) {
              util.showToast('删除成功')
              setTimeout(function() {
                let pages = getCurrentPages()
                let prevPage = pages[pages.length - 2]

                wx.navigateBack({
                  delta: 1
                })
              }, 2000)
            } else {
              util.showToast(res.message)
            }
          }, "POST")
        } else if (res.cancel) {

        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    if (options.id) {
      this.setData({
        id: options.id,
        onlyid: options.onlyid
      })
      util.getPayWay(function(e) {
        that.setData({
          payWayarray: e
        })
        that.getData()
        that.getpayback()
        that.getbuyrecord()
      })

    }

  },
  hidepay() {
    this.setData({
      showpay: false,
    })
  },
  delpay(e) {
    wx.showModal({
      title: '确认',
      content: '是否确定删除付款记录',
      success(res) {
        if (res.confirm) {

          util.query("supplierpayback/wxapi/delete", {
            id: e.currentTarget.dataset.id
          }, function(res) {
            if (res.code == 10000) {
              util.showToast('删除成功')
              that.getpayback()
              that.getData()
            } else {
              util.showToast(res.message)
            }
          }, "POST")
        } else if (res.cancel) {

        }
      }
    })
  },
  getData() {
    util.query('/supplier/wxapi/findbyid', {
      id: this.data.id
    }, function(res) {
      if (res.code === 10000) {
        res.alreadyPayed = res.alreadyPayed.toFixed(2)
        res.needPay = res.needPay.toFixed(2)
        res.purchaseNum = res.purchaseNum.toFixed(2)
        res.stockNum = res.stockNum.toFixed(2)
        res.sumAmount = res.sumAmount.toFixed(2)
        res.totalAmount = res.totalAmount.toFixed(2)
        res.totalDiscount = res.totalDiscount.toFixed(2)
        that.setData(res)
      }

    })
  },
  getpayback() {
    util.query('supplierpayback/wxapi/listpage', {
      supplierOnlyId: this.data.onlyid,
      pageNo: 1,
      pageSize: 9999
    }, function(res) {
      if (res.code === 10000) {
        res.data.content.forEach(function(e) {
          var pyname = ''
          that.data.payWayarray.forEach(function(fe) {
            if (fe.key === e.payWay) {
              pyname = fe.name

            }
          })
          e.pyname = pyname
        })

        that.setData({
          plist: res.data.content
        })
      }

    })
  },
  getbuyrecord() {
    util.query('supplier/wxapi/findPurchaseRecord', {
      id: this.data.id
    }, function(res) {
      if (res.code === 10000) {
        that.setData({
          bdata: res.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

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

  },

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

  }
})