const {
  api,
  mapApi,
  imgName,
  getUserInfo,
  wxLogin,
  alertTip,
  toastTip,
  authAgainByUser,
  userLocation,
  getRgbColor,
} = require('../../lib/util.js')
const moment = require('../../lib/moment.js')
let deviceTimer = null
Component({
  externalClasses: ['wr-nav'],
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  data: {
    navs: [{
      name: '主页',
      check: false,
      url: '/pages/user_index/user_index',
      img: './home.png',
    }, {
      name: '车况',
      check: false,
      url: '/pages/device/index',
      img: './device.png',
    }, {
      name: '服务点',
      check: false,
      url: '/pages/server_point/server_point',
      img: './server.png',
    }, {
      name: '我的',
      url: '/pages/my/my',
      check: false,
      img: './my.png',
    }],
    lock: true,
    user: {}
  },
  properties: {
    nowIndex: {
      type: Number,
      value: 0,
    }
  },
  attached() {
    let navs = this.data.navs.map(elt => {
      elt.check = false
      return elt
    })
    navs[this.data.nowIndex].check = true
    this.setData({
      navs
    })

    // ui定时器
    const user = wx.getStorageSync('user').userMapp
    if (!user || !user.nowVcuNo) {
      return
    }
    this.setData({
      user
    })
    clearInterval(deviceTimer)
    // this.getCarDetail()
    // deviceTimer = setInterval(() => {
    //   this.getCarDetail()
    // }, 5000)
  },
  detached() {
    clearInterval(deviceTimer)
  },
  methods: {
    toggleLockStatus() {
      if (this.data.lock) { //关
        this.netOpenCar()
      } else {
        this.netCloseCar()
      }
    },
    getCarDetail() {
      api.get('device/base/deviceDetail', {}, true)
        .then(res => {
          let {
            data,
            model: {
              deviceInfo
            }
          } = res
          let carData = {
            ...data,
            ...deviceInfo
          }
          this.triggerEvent('uidata', carData)
          this.setData({
            lock: carData.lock_state == '1' ? false : true //锁 1开锁状态
          })
        })
        .catch(err => {
          console.error(1, err)
        })
    },

    //关锁
    netCloseCar() {
      api.post('device/base/lockAndUnLock?state=2', {
        state: 2
      })
        .then(res => {
          alertTip('锁车成功')
          this.setData({
            'lock': true,
          })
        })
        .catch(err => {
          alertTip(err)
        })
    },
    //开锁
    netOpenCar() {
      api.post('device/base/lockAndUnLock?state=1', {
        state: 1
      })
        .then(res => {
          alertTip('开锁成功')
          this.setData({
            'lock': false,
          })
        })
        .catch(err => {
          alertTip(err)
        })
    },
  }
});