const {
  api,
  formatTime,
  mapApi,
  imgName,
  getUserInfo,
  wxLogin,
  alertTip,
  authAgainByUser,
  userLocation
} = require('../../lib/util.js')
Page({
  data: {
    alarMsgShowRead: 0,
    page: 1, //当前页面
    hasMorePage: false, //更多页面
    alarmMsg: [], //消息列表
    imgDot: imgName('dot.png'),
    imgMore: imgName('seemore.png'),
    imgHasalarm: imgName('hasalarm.png'),
    imgHasmes: imgName('hasmes.png'),
    imgNoalarm: imgName('noalarm.png'),
    imgNomes: imgName('nomes.png'),
    msgType:null,
    promtTrue: {
      title: '当前没有报警记录!',
    },
    promtFalse: {
      title: '获取报警记录失败！',
      buttons: [{
        text: '重新获取',
      }],
    },
    success: true
  },
  onLoad: function() {
    this.data.msgType=new Map()
    this.getAlarmType()
      .then(() => {
        this.getMsgList()
      })
  },
  // 获取报警类型
  getAlarmType() {
    const msgType = this.data.msgType
    return api.get('alarmRecord/queryAlarmType')
      .then(res => {
        res.items.map(elt => {
          msgType.set(elt.alarmId, elt.alarmName)
          return elt
        })
      })
      .catch(err => {
        alertTip(err)
      })
  },
  //显示报警详细
  showAlarm(e) {
    console.log(e)
    let {
      value
    } = e.currentTarget.dataset
    let carLocAlarm = {
      latitude: value.lat,
      longitude: value.lng,
      content: value.alarmMsg,
      msgTime: value.showTime,
    }
    wx.setStorageSync('carLocAlarm', carLocAlarm)
    wx.navigateTo({
      url: '/pages/user_car_loc/user_car_loc',
    })
  },
  //拉取消息列表
  getMsgList() {
    let that = this
    const user = wx.getStorageSync('user')
    let curPage = this.data.page
    this.setData({
      success: true
    })
    api.get('alarmRecord/getList', {
        vcuNo: user.userMapp.nowVcuNo,
        curPage
      })
      .then(res => {
        let alarmMsg = res.items.map(item => {
          item.showTime = item.dbdate
          item.alarmMsg = this.data.msgType.get(item.messageType)
          return item
        })
        if (curPage == 1) {
          this.setData({
            alarmMsg,
            page: curPage + 1,
            hasMorePage: res.totalPage > curPage
          })
        } else {
          let newArrays = that.data.alarmMsg.concat(alarmMsg)
          this.setData({
            alarmMsg: newArrays,
            page: curPage + 1,
            hasMorePage: res.totalPage > curPage
          })
        }
      })
      .catch(err => {
        this.setData({
          success: false
        })
        alertTip(err)
      })
  }
})