// pages/mine/mine.js
var app = getApp()
Page({
  data: {
    userInfo: '',//用户信息
    deposit: '',//是否缴纳押金 未缴纳0 已缴纳1
    arr: [],
    show: false,
    setting: '',//设置
    src: '',//用户头像地址
  },
  onLoad: function (options) {
    // 页面加载完成
    my.getAuthCode({
      scopes: 'auth_user',
      success: (res) => {
        my.getAuthUserInfo({
          success: (userInfo) => {
            //my.alert({content: userInfo.avatar});
            this.setData({ src: userInfo.avatar });
            console.log(userInfo, 'userInfo')
            if (userInfo) {
              let sex = 0
              if (userInfo.gender == "m") {
                let sex = 1
              }
              else if (userInfo.gender == "f") {
                let sex = 2
              }
              else {
                let sex = 0
              }
              var data = {
                'area_id': userInfo.area_id,
                'avatar': userInfo.avatar,
                'city': userInfo.city,
                'gender': 0,
                'nickName': userInfo.nickName,
                'province': userInfo.province,
                'language': '中国'
              }
              this.userInfo(data)
            }

          }
        });
      },
    });
  },
  //同步用户信息
  userInfo: function (data) {
    let that = this
    app.request("/auth/weapp-userinfo-sync", data, "POST").then(res => {
      console.log(res, 'shouquan yonghuxinxi')
      if (res.status == 200) {
        // my.showToast({
        //   content: '授权成功',
        //   icon: 'none'
        // })
        my.setStorageSync({ 'key': 'userInfo', data: res.data.user })
        // wx.navigateBack()
        that.setData({ userInfo: res.data })
      } else {
        // my.showToast({
        //   content: '登陆失败请重试',
        //   icon: 'none'
        // })
      }
    })
  },
  getUser() {
    let token = my.getStorageSync({ 'key': 'token' }).data
    if (token != '') {
      app.request("/me", '', "GET").then(res => {
        console.log(res)
        200 == res.status && my.setStorageSync({ 'key': "userInfo", data: res.data })
        this.setData({
          userInfo: res.data,
          deposit: my.getStorageSync({ 'key': 'userState' }).data,
          setting: my.getStorageSync({ 'key': 'setting' }).data
        })
      }).catch(err => {
        console.log(err)
      })
    }
  },
  onReady: function () {

  },
  onShow: function () {
    let that = this
    this.getUser();
    var area_id = ''
    if (my.getStorageSync({ 'key': 'home' }).data= undefined) {
      area_id = my.getStorageSync({ 'key': 'home' }).data.id
    }
    var data = {
      'area_id': area_id
    }
    app.request("/card_riding/user_card", data, "GET").then(res => {
      console.log(res)
      that.setData({ arr: res.data.user_card, show: res.data.is_card_begin })
    })
  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  },
  adDetail: function () {
    my.navigateTo({
      url: '/pages/ride_zige/ride_zige',
    })
  },
  balance: function () {
    my.navigateTo({
      url: '/pages/balance/balance',
    })
  },
  coupon: function () {
    my.navigateTo({
      url: '/pages/coupon/coupon',
    })
  },
  myCard: function () {
    my.navigateTo({
      url: '/pages/mine_card/mine_card',
    })
  },
  rideZige: function () {
    console.log(my.getStorageSync({ 'key': 'home' }).data)
    if (!my.getStorageSync({ 'key': 'home' }).data) {
      my.alert({
        title: '提示',
        content: '您附近暂无运营区',
      })
    } else {
      my.navigateTo({
        url: '/pages/ride_zige/ride_zige',
      })
    }

  },
  setUp: function () {
    my.navigateTo({
      url: '/pages/set_up/set_up',
    })
  },
  mineData: function () {
    my.navigateTo({
      url: '/pages/mine_data/mine_data',
    })
  },
  trip: function () {
    my.navigateTo({
      url: '/pages/trip/trip',
    })
  },
  accountDetails: function () {
    my.navigateTo({
      url: '/pages/account_details/account_details',
    })
  },
  weizhang: function () {
    my.navigateTo({
      url: '/pages/weizhang/weizhang',
    })
  },
  buyCard: function () {
    my.navigateTo({
      // url: '/pages/ride_zige/ride_zige',
      url: '/pages/buy-card/buy-card'
    })
  },
  //退还押金
  backDeposit: function () {
    my.navigateTo({
      url: '/pages/ride_zige/ride_zige',
    })
  },
})