const {
  api,
  alertTip,
  confirmTip,
  imgName
} = require('../../lib/util.js')
const moment = require('../../lib/moment.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgViplogo: imgName('viplogo.png'),
    avator: "",
    imgHeadshot: imgName('headshot.png'),
    imgMore: imgName('more.png'),
    imgTip: imgName('tip.png'),
    address: '江苏省  常州市 武进区',
    userPhone: null,
    networkingStopTime: '0000-00-00',
    networkingStop: false, //到期
    vehicleName: null,
    menuItems1: [
      // {
      //   name: '电池信息',
      //   icon: 'https://ruiju.oss-cn-hangzhou.aliyuncs.com/guanyang/my_power.png',
      //   page: '/pages/battery/index'
      // },
      {
        name: '设备列表',
        icon: 'https://ruiju.oss-cn-hangzhou.aliyuncs.com/guanyang/my_device.png',
        page: '/pages/user_device_list/user_device_list'
      }, {
        name: '设置',
        icon: 'https://ruiju.oss-cn-hangzhou.aliyuncs.com/guanyang/my_setting.png',
        page: '/pages/setting/setting'
      }
    ],
    // menuItems2: [
    //   {
    //   name: '副账号',
    //   icon: 'https://ruiju.oss-cn-hangzhou.aliyuncs.com/guanyang/my_user.png',
    //   page: '/pages/carusers/carusers'
    // }, {
    //   name: '消息',
    //   icon: 'https://ruiju.oss-cn-hangzhou.aliyuncs.com/guanyang/my_msg.png',
    //   page: '/pages/user_message/user_message'
    // }, {
    //   name: '设置',
    //   icon: 'https://ruiju.oss-cn-hangzhou.aliyuncs.com/guanyang/my_setting.png',
    //   page: '/pages/setting/setting'
    // }]
  },
  onShow: function() {
    this.getUserInfo()
  }, //个人页导航pages/user_car_loc/user_car_loc
  getFirstPage() {
    let This = this
    let user = wx.getStorageSync('user').userMapp
    This.setData({
      userPhone: user.mobile.replace(/(\d{3})(\d{4})(\d{3})/, '$1****$3'),
      // networkingStopTime: moment(user.registerDate).format('YYYY-MM-DD HH:mm'),
      // networkingHasDay: Math.ceil((new Date(device.networkingStopTime) - Date.now()) / 1000 / 3600 / 24),
      // vehicleName: device.vehicleName
    })
  },
  toModifySelf() {
    wx.navigateTo({
      url: '/pages/modifySelf/modifySelf',
    })
  },
  getUserInfo() {
    let that = this
    api.get('user/myInfo', {}, false)
      .then(res => {
        console.log(res)
        let user = res.data
        this.setData({
          userPhone: user.mobile.replace(/(\d{3})(\d{4})(\d{3})/, '$1****$3'),
          networkingStopTime: moment(user.networkStop).format('YYYY-MM-DD HH:mm'),
          networkingStop: moment().isAfter(user.networkStop),
          // vehicleName: device.vehicleName
          avator: user.avatarUrl
        })
      })
      .catch(err => {
        alertTip(err)
      })
  },
  navPage(e) {
    let url = e.currentTarget.id
    if (url === '') return
    console.log(url)
    if (url == '/releaseDevice') {
      confirmTip('解绑设备后将无法收到报警和查看定位信息，是否确认解绑', () => {
        api
          .post("/message/returnCarTB")
          .then(res => {
            wx.reLaunch({
              url: '/pages/device_bind/device_bind',
            })
          })
          .catch(err => {
            alertTip(err)
          });
      })
      return
    }
    wx.navigateTo({
      url: e.currentTarget.id
    })

  },
  loginOut() {
    api.get('user/logout')
      .then(res => {
        wx.reLaunch({
          url: '/pages/welcome2/welcome2'
        })
      })
      .catch(err => {
        alertTip(err)
      })
  },
  serverPay() {
    wx.navigateTo({
      url: '/pages/user_buy_server/user_buy_server'
    })
  }
})