// pages/confirmOrder/confirmOrder.js
const wxService = require('../../utils/wxService')

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    addressInfo: '',
    showSelectCoupon: false,
    skuIds: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let trolleySku2Buy = this.options.trolleySku2Buy
    console.log('thtrolleySku2Bu', JSON.parse(trolleySku2Buy))
    // 下单 && 计算优惠价
    if(trolleySku2Buy.length){
      this.getBill(trolleySku2Buy) // 下单
      // this.calPreferentialPrice(trolleySku2Buy)  // 计算优惠价
    }
  },
  //  下单
  getBill(trolleySku2Buy) {
    wx.showLoading({
      title: '加载中'
    })
    wxService.post(`/sale/trade/buyer/bill`,trolleySku2Buy).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          console.log('data', data)
        }
      }
    })
  },
  // 计算优惠价
  calPreferentialPrice(trolleySku2Buy) {
    wx.showLoading({
      title: '加载中'
    })
    wxService.post(`/sale/trade/buyer/preview`,trolleySku2Buy).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          console.log('data', data)
        }
      }
    })
  },
  //获取微信地址
  chooseWxAddress() {
    wx.chooseAddress({
      success: (res) => {
        console.log(res)
        this.setData({
          addressInfo: res
        })
      },
      fail: function (err) {
        console.log(err)
      }
    })
  },

  //选择优惠券
  selectCoupon() {
    this.setData({
      showSelectCoupon: true
    })
  }


})