// pages/order/index.js
const {
  api
} = require("../../utils/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    info: null,
    addr: null,
    checked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const id = options.id
    this.data.id = id

  },

  fetchAddr() {
    api('/personal/address/list', 'GET').then(res => {
      if (res.code === 200) {
        const tmps = res.data
        if (tmps.length > 0) {
          this.setData({
            addr: tmps[0]
          })
        }

      }
    })
  },

  chooseAddr() {
    const _this = this
    wx.chooseAddress({
      success(res) {
        const data = {
          isDefault: 1,
          phone: res.telNumber,
          name: res.userName,
          detail: res.provinceName + res.cityName + res.countyName + res.detailInfo
        }
        _this.saveAddr(data)
      }
    })
  },

  saveAddr(data) {
    api('/personal/address/add', 'POST', data, 'json').then(res => {
      if (res.code === 200) {
        this.fetchAddr()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.fetchInfo()
    this.fetchAddr()
  },

  change() {
    const checked = !this.data.checked
    this.setData({
      checked: checked
    })
  },

  pay() {
    if (this.data.checked === false) {
      wx.showModal({
        content: '请先勾选，我承诺会好好照顾它，不随意丢弃。',
      })
      return
    }
    const data = {
      addressId: this.data.addr.id,
      id: this.data.id
    }
    wx.showLoading({
      title: '请稍后',
    })
    api('/claim/claim', 'GET', data).then(res => {
      if (res.code === 200) {
        const orderId = res.data
        api('/pay/prePay', 'GET', {
          orderId: orderId
        }).then(res => {
          wx.hideLoading()
          if (res.code === 200) {
            const pay = res.data
            wx.requestPayment({
              appId: "wxb463820bf36dd5d6",
              nonceStr: pay.nonceStr,
              package: pay.package,
              signType: pay.signType,
              paySign: pay.paySign,
              timeStamp: pay.timeStamp,
              "success": res => {
                // 检查支付
                console.log(res);
              },
              "fail": function (res) {
                console.log(res);
              },
              "complete": function (res) {
                console.log(res);
              }
            })

          }
        })
      } else {
        wx.showModal({
          content: res.msg,
        })
      }
    })
  },

  fetchInfo() {
    api('/claim/detail', "GET", {
      id: this.data.id
    }).then(res => {
      if (res.code === 200) {
        const tmps = res.data
        tmps.content = tmps.content.trim()
        tmps.postage = tmps.postage.toFixed(2)
        this.setData({
          info: tmps
        })
      }
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})