const {
  api,
  mockApi,
  formatTime,
  imgName,
  getUserInfo,
  wxLogin,
  alertTip,
  authAgainByUser,
  userLocation
} = require('../../lib/util.js')
const moment = require('../../lib/moment.js')
let months = {
  '01': '一月',
  '02': '二月',
  '03': '三月',
  '04': '四月',
  '05': '五月',
  '06': '六月',
  '07': '七月',
  '08': '八月',
  '09': '九月',
  '10': '十月',
  '11': '十一月',
  '12': '十二月',
}
Page({
  data: {
    msg: 'welcome',
    er: imgName('erwei.png'),
    imgHead: imgName('journey.jpg'),
    imgLogo: imgName('logo.png'),
    imgArrowUp: imgName('arrowup.png'),
    imgArrowDown: imgName('arrowdown.png'),
    imgLoction: imgName('showloc.png'),
    imgRoad: imgName('showroad.png'),
    imgRightarr: imgName('right_arr.png'),
    imgAlarm: imgName('timeline_remind_black.png'),
    detailList: [],
    page: 1, //当前页面
    hasMorePage: false, //更多页面
    promtTrue: {
      title: '当前没有行程记录！',
    },
    promtFalse: {
      title: '获取行程记录失败！',
      buttons: [{
        text: '重新获取',
      }],
    },
    successs: false
  },
  onLoad: function() {
    // this.getTotalMileage()
    // this.getDaysMileage()
    this.getList(1)
  },
  onShow: function() {

  },
  //获取行程记录
  getList() {
    let that = this
    let curPage = this.data.page
    let user = wx.getStorageSync('user').userMapp
    this.setData({
      success: true
    })
    api.get('tripRecord/getTripRecordList', {
        vcuNo: user.nowVcuNo,
        curPage
      })
      .then(res => {
        let detailList = res.items.map(item => {
          item.startPosition = String(item.startPosition).slice(0, 5)
          item.endPosition = String(item.endPosition).slice(0, 5)
          const du = moment.duration(moment(item.endTime) - moment(item.startTime), 'ms')
          const hours = du.get('hours')
          const mins = du.get('minutes')
          const ss = du.get('seconds')
          const totalMins = du.as('minutes');
          // item.useTime = hours + '时' + mins + '分' + ss + '秒'
          item.useTime = Math.ceil(totalMins) + '分'
          item.startDate = formatTime(new Date(item.startTime))
          item.endDate = formatTime(new Date(item.endTime))
          item.useMileage = ((item.endMileage - item.startMileage) / 1000).toFixed(2)
          return item
        })
        if (curPage == 1) {
          this.setData({
            detailList,
            page: curPage + 1,
            hasMorePage: res.totalPage > curPage,
          })
        } else {
          let newArrays = that.data.detailList.concat(detailList)
          this.setData({
            detailList: newArrays,
            page: curPage + 1,
            hasMorePage: res.totalPage > curPage,
          })
        }
      })
      .catch(err => {
        this.setData({
          successs: false
        })
        alertTip(err)
      })
  },
  //显示行程
  getTrack(e) {
    let id = e.currentTarget.id
    wx.navigateTo({
      url: `/pages/path_detail/path_detail?id=${id}`,
    })
  }
})