// pages/ride_zige/ride_zige.js
const util = require('../../utils/utils');
var app = getApp()
var url = ''
Page({
  data: {
    currentItem: 0,
    cardList: [],//骑行卡列表
    deposit: '',//骑行卡
    depositMoney: '',//押金金额
    frame: false,//确认退还退押金弹框

  },
  onLoad: function (options) {

    this.setData({
      deposit: my.getStorageSync({ 'key': 'userState' }).data,
      depositMoney: my.getStorageSync({ 'key': 'setting' }).data.deposit
    })

    this.getCard()
    // this.getState()
  },

  //获取免押金卡列表
  getCard() {
    my.showLoading({})
    let data = {
      area_id: my.getStorageSync({ 'key': 'home' }).data.id
    }
    if (my.getStorageSync({ 'key': 'home' }).data.id == undefined) {
      my.hideLoading()
      my.showToast({
        content: '您附近没有运营区域',
        icon: 'none'
      })
    } else {
      //免押金卡
      app.request('/deposit_card/index', data, 'GET').then(res => {

        console.log(res.data);
        this.setData({
          cardList: res.data.data
        })
        my.hideLoading({
          complete: (res) => { },
        })
        console.log(this.data.cardList)
      })
    }

  },
  //选择骑行卡类型
  changeCard: function (e) {
    let that = this
    let id = e.currentTarget.dataset.id
    console.log(e, 'pppppp')
    that.setData({
      currentItem: id
    })
    // if(id==0){
    // }else if (that.data.deposit.is_deposit == 1 && that.data.deposit.deposit_type == 1) {
    //   my.showToast({
    //     content: '您已缴纳押金无需购买免押金卡',
    //     icon: 'none'
    //   })
    //   return;
    // }
  },
  //立即支付
  pay: util.throttle(function (e) {
    console.log('支付')
    var that = this;
    let url = '';
    var data = '';

    console.log(that.data.depShow)
    if (that.data.currentItem == 0) { //缴纳押金
      console.log(11111)
      url = "/deposit/pay";
      data = {
        area_id: my.getStorageSync({ 'key': 'home' }).data.id
      }
    } else { //购买免押金卡
      console.log(2222)
      url = "/deposit_card/pay";
      data = {
        area_id: my.getStorageSync({ 'key': 'home' }).data.id,
        id: that.data.currentItem
      }
    }
    if (my.getStorageSync({ 'key': 'setting' }).data == '') {
      my.alert({
        title: '提示',
        content: '您附近暂无运营区域~',
        success: function (res) {
          my.navigateBack()
        }
      })
    } else {
      app.request(url, data, 'POST', app.globalData.req).then(res => {
        console.log(res)
        if (res.status == 200) {
          console.log(res)
          my.tradePay({
            tradeNO: res.data.tradeNo,
            success(res) {
              console.log(res)
              if (res.resultCode == 9000) {
                let init = '';
                if (that.data.depShow) {
                  init = "购买成功"
                } else {
                  init = "支付成功"
                }
                my.showToast({
                  content: init,
                  icon: 'none',
                  duration: 1000,
                  success: function () {
                    my.reLaunch({
                      url: '/pages/pay_success/pay_success',
                    })
                  }
                })
                that.setData({
                  depoSuce: true
                })
              } else {
                return
              }
            },
            fail(err) {
              // console.log(err)
              // my.showToast({
              //   content: '支付失败',
              //   icon: 'none'
              // })
              // that.setData({
              //   depoSuce: true
              // })
            }
          })
        } else {
          console.log(res)
          that.setData({
            depoSuce: true
          })
        }
      })
    }


  }, 1000),
  //退还押金
  backDeposit: function () {
    this.setData({
      frame: true
    })
  },
  closeFrame: function () {
    this.setData({
      frame: false
    })
  },
  //立即退还
  sureBack: util.throttle(function (e) {
    app.request('/deposit/refund', '', 'POST').then(res => {
      console.log(res);
      if (res.status == 200) {
        my.showToast({
          content: '押金退回申请成功，请注意查收！',
          icon: 'none'
        })
        setTimeout(function () {
          my.reLaunch({
            url: '/pages/index/index',
          })
        }, 1500)
        // this.getState()
      }
    })
  }, 1000),
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})