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

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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let trolleySku2Buy = this.options.trolleySku2Buy
    // 下单 && 计算优惠价
    if (trolleySku2Buy.length) {
      this.setData({ trolleySku2Buy }, () => {
        this.calPreferentialPrice(trolleySku2Buy)  // 计算优惠价
      })
    }
  },
  handelChangeSelectVip() {
    this.setData({
      isSelect: !this.data.isSelect
    })
  },
  // 计算优惠价
  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()
          this.setData({ currentOrderList: data })
        }
      }
    })
  },
  handelGobuy() {
    const { trolleySku2Buy } = this.data
    this.getBill(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) {
          delete data.appId
          wx.requestPayment(Object.assign({
            success(res) {
              console.log('000000',res)
              wx.hideLoading()
              // 支付成功页面
              wxService.router('/subPackage/page/pages/paymentStatus/paymentStatus')
            },
            fail(res) {
              wx.hideLoading()
              wx.showToast({
                title: '支付失败',
                icon: 'none',
                mask: true
              })
            }
          }, data))
        }
      }
    })
  },
  //获取微信地址
  chooseWxAddress() {
    wx.chooseAddress({
      success: (res) => {
        console.log(res)
        this.setData({
          addressInfo: res
        })
      },
      fail: function (err) {
        console.log(err)
      }
    })
  },

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


})