let app = getApp();
let host = app.globalData.servsers; // 请求的url

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList: [],
    host: app.globalData.servsers,
    amount: '',
    address: {}, // 地址为空
  },

  onLoad() {
    let that = this;
    wx.request({
      url: host + '/adressapi/getdefaustAdress',
      method: 'get',
      data: {
        user_id: app.globalData.user_id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        console.log(res, "获取默认地址")
        that.setData({
          address: res.data
        })
      }


    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let goodsList = [],
      that = this;
    let address = wx.getStorageSync('accountAddress');
    console.log(address, "addressaddress")
    if (address) {
      that.setData({
        address: address
      })
    }
    goodsList = [...wx.getStorageSync('goodsList')];
    that.setData({
      goodsList: goodsList,
      amount: wx.getStorageSync('amount')
    })

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  submitOrder() {
    if (!this.data.address.consignee) {
      wx.showToast({
        title: '您还没有添加收货地址',
        icon: 'none'
      })
      return;
    }
    wx.showLoading({
      title: '结算中...'
    })
    let that = this,
      goodsList = that.data.goodsList,
      address = that.data.address;
    //生成订单
    goodsList.forEach(res => {
      res['userId'] = app.globalData.user_id; //'oovpNwjlsY6xx8ceCebFa1dOLd9E',
      res['adress'] = address.adress;
      res['area'] = address.area;
      res['city'] = address.city;
      res['province'] = address.province;
      res['consignee'] = address.consignee;
      res['phone'] = address.phone;
      res['openId'] = app.globalData.openid;
      delete res.imgArray
    });
    wx.request({
      url: host + '/createNewOrderCustomer',
      method: 'post',
      data: {
        shopCarList: JSON.stringify(goodsList)
      },
      success(el) {
        let res = el.data;
        console.log(res)
        wx.requestPayment({
          timeStamp: res.timeStamp,
          nonceStr: res.nonceStr,
          package: res.package,
          signType: res.signType,
          paySign: res.paySign,
          success(res) {
            console.log(res, "结算结果")
            if (res.errMsg === 'requestPayment:ok') {
              // wx.showToast({
              //   title: '支付成功',
              // })
              wx.redirectTo({
                url: '/customized/pages/result/result?state=true',
              })
            } else {
              wx.redirectTo({
                url: '/customized/pages/result/result?state=false',
              })
            }
          },
          fail(err) {
            wx.showToast({
              title: err.errMsg,
              icon: 'error'
            })
            console.log(err)
          },
          complete() {
            wx.hideLoading()
          }
        })
      }
    })
  },
  addressBtn() {
    wx.navigateTo({
      url: '/customized/pages/address/address',
    })
  }
})