// pages/device_bind/device_bind.js
const {
  api,
  confirmTip,
  wxscan,
  rapi,
  userLocation,
  imgName,
  getUserInfo,
  wxLogin,
  alertTip,
  authAgainByUser,
  getProtocol
} = require('../../lib/util.js')
let timer = null
let pointTimer = null //网点解绑
const app = getApp()
Page({
  data: {
    imgTip: imgName('imgtip.png'),
    imgScan: imgName('imgscan.png'),
    modelScan: true,
    frameNo: null,
    vcuNo: null,
    agreeUserCar: false,
    userId: null,
    user: null
  },
  onLoad() {
    let user = wx.getStorageSync('user')
    let {
      userMapp
    } = user
    if (!userMapp || !userMapp.id) {
      wx.redirectTo({
        url: '/pages/welcome2/welcome2',
      })
      return
    }
  },
  onUnload() {
    clearInterval(timer)
  },
  onHide() {
    clearInterval(timer)
  },
  onShow() {
    // if (!this.options.from) {
    //   this.getCarList()
    //   clearInterval(timer)
    //   timer = setInterval(this.getCarList, 5000)
    // }

  },
  scanImg() {
    let This = this
    wxscan()
      .then(vcuNo => {
        This.setData({
          vcuNo
        })
        This.apiUseCar()
      })
      .catch(err => {
        alertTip(err)
      })
  },
  getCarList() {
    let that = this
    api.post('device/userBindingDeviceList', {}, true)
      .then(res => {
        app.socket.open = false
        if (res.items.length > 0) {
          clearInterval(timer)
          wx.switchTab({
            url: '/pages/user_index/user_index',
          })
        }
      })
      .catch(err => {
        alertTip(err)
      })
  },
  loginOut() {
    wx.clearStorage()
    wx.reLaunch({
      url: '/pages/welcome2/welcome2',
    })
  },
  toMy() {
    wx.redirectTo({
      url: '/pages/welcome2/welcome2',
    })
  },
  //同意租车协议
  hascheck(e) {
    this.setData({
      agreeUserCar: !this.data.agreeUserCar
    })
  },
  //进入租车协议
  toProtocol() {
    console.log(this.data.rentType)
    getProtocol(1)
      .then(res => {
        console.log(res)
      })
      .catch(err => {
        console.error(err)
        alertTip(err.errMsg || err)
      })
  },
  apiUseCar() {
    let This = this
    // if (!This.data.agreeUserCar) {
    //   alertTip('请同意租车协议')
    //   return false
    // }
    if (!this.data.vcuNo) {
      alertTip('请输入车辆编码')
      return false
    }
    let {
      vcuNo,
      userId
    } = This.data
    api.post('device/userBindingDevice', {
        vcuNo
      })
      .then(res => {
        //有使用的车辆 跳到用车
        console.log('bind:', res)
        let user = wx.getStorageSync('user')
        wx.setStorageSync('user', user)
        app.socket.open = false
        wx.switchTab({
          url: '/pages/user_index/user_index',
        })
        return
      })
      .catch(err => {
        alertTip(err)
        console.error(err)
      })

  },
  backScan() {
    this.setData({
      modelScan: true
    })
  },
  bindByHand() {
    this.setData({
      modelScan: false
    })
  },
  deviceInput(e) {
    this.setData({
      vcuNo: e.detail.value
    })
  }
})