Page({
  data: {
    crowdfund_item_id: '',
    addressList: [],
    info: '',
    remark: '',
    crowdfund_id: '',
    addressDel:'',
  },

  onLoad(e) {
    wx.$api.companyModule.crowdfundsCreat(e.crowdfund_item_id).then(res => {
      if (res.address.length > 0) {
        this.setData({
          addressList: res.address,
          addressDel: res.address[0]
        })
        console.log(res);
        console.log(this.data.addressDel)
      }
      this.setData({
        crowdfund_item_id: e.crowdfund_item_id,
        info: res.info,
        crowdfund_id: e.crowdfund_id
      })
      console.log(this.data.info);
    }).catch(res => {
      wx.navigateBack({})
    })
  },
  onShow() {
    console.log(this.data.addressDel)
  },
  input(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  nowActed(e) {
    wx.showLoading({
      title: '创建中...',
      mask: 'true'
    })
    if (this.data.addressDel) {
      wx.$api.companyModule.crowdfundsCreatOrder(this.data.crowdfund_item_id, this.data.addressDel.address_id, this.data.remark).then(res => {
        var data = {};
        if (res.trade_no) {
          wx.login({
            success: res1 => {
              data = {
                trade_no: res.trade_no,
                code: res1.code
              }
              wx.$api.companyModule.wechat(data).then(res => {
                var ress = JSON.parse(res);
                wx.hideLoading({});
                wx.requestPayment({
                  timeStamp: ress.timeStamp,
                  nonceStr: ress.nonceStr,
                  package: ress.package,
                  signType: 'MD5',
                  paySign: ress.paySign,
                  success: res => {
                    wx.showToast({
                      title: '创建订单成功',
                    })
                    wx.navigateTo({
                      url: '/pages/home/projectSuccess/projectSuccess?crowdfund_id=' + this.data.crowdfund_id,
                    })
                  },
                  fail(res) {
                    wx.showToast({
                      title: '支付失败',
                    })
                  }
                })
              }).catch(res => {
                console.log(res)
              })
            }
          })
        }
      }).catch(res => {
        wx.navigateBack({})
      })
    } else {
      wx.hideLoading({
        success: (res) => {
          wx.showToast({
            icon: 'none',
            title: '请选择收货地址',
            duration: 2000
          })
        },
      })

    }
  },
  // 跳转到地址列表和新增地址页面
  goAddress() {
    wx.navigateTo({
      url: '/pages/mall/mall_address/mall_address?type=selectAddress',
    })
  }

})