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,
    currentCard: 1
  },
  onLoad: function (options) {
    var that = this;
    var area_id = ''
    my.showLoading({
      content: '加载中...',
    })
    if (my.getStorageSync({ 'key': 'home' }).data.id != undefined) {
      area_id = my.getStorageSync({ 'key': 'home' }).data.id
    }
    var data = {
      'area_id': area_id
    }
    app.request("/card_riding/index", data, "GET").then(res => {
      console.log(res)
      my.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,
        infoDetail: arr1,
        id:arr1.id
      })
    }).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 (my.getStorageSync({ 'key': 'home' }).data) {
      area_id = my.getStorageSync({ 'key': 'home' }).data.id
    } else {
      my.showToast({
        content: '当前地区暂无运营区域',
        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.status == 200) {
          that.setData({
            no: res.data.no
          })
          my.tradePay({
            tradeNO: res.data.tradeNo,
            success: function (res) {
              if (res.resultCode == 9000) {
                my.showToast({
                  content: '购买成功，请用车',
                  icon: 'none',
                  mask: true,
                  success: function () {
                    my.reLaunch({
                      url: '/pages/index/index',
                    })
                  }
                })
              } else {
                return
              }
            },
            fail(err) {
              console.log(err)
              // my.showToast({
              //   content: '支付失败',
              //   icon: 'none'
              // })
              // var data = {
              //   no: that.data.no
              // }
              // app.request("/card_riding/no_pay", data, "POST").then(res => {
              //   console.log(res)
              // })
            }
          })
        }
      })
    } else {
      my.showToast({
        content: '您的操作过于频繁，请稍后再试~',
        icon: 'none'
      })
    }

  }, 1000),
  //选择卡
  changeCard: function (e) {
    console.log(e.currentTarget.dataset.id.id)
    this.setData({
      id: e.currentTarget.dataset.id.id,
      // info: true,
      infoDetail: e.currentTarget.dataset.id,
      currentCard: e.currentTarget.dataset.id.id
    }
    )
  },
  buyButtom: function () {
    this.setData({
      info: true,
    })
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})