var app = getApp();
const util = require('../../utils/utils.js');
Page({
  data: {
    hot: false,
    index1: null,
    money: '',
    arr: [],
    info: false,
    id: '',
    no: '',
    infoDetail: [],
    hotInfo: [],
    img:app.globalData.imgUrl,
    setting:{}
  },
  onLoad: function (options) {
    var that = this;
    var area_id = ''
    wx.showLoading({
      title: '加载中...',
    })
    if (wx.getStorageSync('home').id != undefined) {
      area_id = wx.getStorageSync('home').id
    }
    var data = {
      'area_id': area_id
    }
    this.setData({
      setting: wx.getStorageSync('setting')
    })

    app.request("/card_riding/index", data, "GET").then(res => {
      console.log(res)
      wx.hideLoading()
      if (res.data.data == '') {
        return;
      }
      var arr1 = res.data.data.shift()
      console.log(arr1)
      console.log(res.data.data)
      that.setData({
        hotInfo: arr1,
        arr: res.data.data,
        hot: true
      })
    }).catch(err => {
      console.log(err)
    })
  },
  background: function () {
    this.setData({
      info: false
    })
  },
  buy: function (e) {
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var arr = this.data.arr;
    this.setData({
      id,
      info: true,
      infoDetail: arr[index]
    })
  },
  buy1: function () {
    this.setData({
      id: this.data.hotInfo.id,
      info: true,
      infoDetail: this.data.hotInfo
    })
  },
  form: util.throttle(function () {
    var that = this;
    that.setData({
      info: false
    })
    var area_id = ''
    if (wx.getStorageSync('home').id != undefined) {
      area_id = wx.getStorageSync('home').id
    } else {
      wx.showToast({
        title: '当前地区暂无运营区域',
        icon: 'none'
      })
      return;
    }
    if (app.globalData.req) {
      var data = {
        'card_riding_id': that.data.id,
        'area_id': area_id
      }
      app.request("/card_riding/pay", data, "POST", app.globalData.req).then(res => {
        console.log(res)
        if (res.statusCode == 200) {
          that.setData({
            no: res.data.no
          })
          wx.requestPayment({
            timeStamp: res.data.timeStamp.toString(),
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            success(resp) {
              console.log(resp)
              wx.showToast({
                title: '购买成功，请用车',
                icon: 'none',
                mask: true,
                success: function () {
                  if(app.globalData.compatible){
                    wx.reLaunch({
                      url: '/pages/compatible/index/index',
                    })
                  }else{
                    wx.reLaunch({
                      url: '/pages/index/index',
                    })
                  }
                }
              })
            },
            fail(err) {
              console.log(err)
              wx.showToast({
                title: '支付失败',
                icon: 'none'
              })
              var data = {
                no: that.data.no
              }
              app.request("/card_riding/no_pay", data, "POST").then(res => {
                console.log(res)
              })
            }
          })
        }
      })
    } else {
      wx.showToast({
        title: '您的操作过于频繁，请稍后再试~',
        icon: 'none'
      })
    }

  }, 1000),
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})