// pages/hardwareDetails/hardwareDetails.js
const app = getApp();
const request = require('../../utils/request.js');
const tools = require('../../utils/tools.js');
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    elderId: '',
    dict: [],
    deviceArr: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  async getDetails() {
    let options = {
      url: 'resource/miniapp/user/getEquipList',
      data: {
        openId: wx.getStorageSync('openid'),
        type: app.globalData.type
      },
      method: 'post'
    }
    let res = await request.query(options);
    if (res.data.code === 200) {
      let arr = res.data.data.length > 0 ? res.data.data : []
      arr.map((item, index) => {
        item.regdateTime = this.toDate(item.regdateTime);
        item.kind = this.toKind(item.kind);
      })
      this.setData({
        deviceArr: arr
      })
      console.log(this.data.deviceArr)
    } else {
      Toast.fail(res.data.msg);
    }
  },
  async getDict() {
    let options = {
      url: 'system/app/home/dict/data/type/znyj_yj_kind',
      method: 'get'
    }
    let res = await request.query(options);
    if (res.data.code === 200) {
      this.setData({
        dict: res.data.data.length > 0 ? res.data.data : []
      })
      this.getDetails();
    } else {
      Toast.fail(res.data.msg);
    }
  },
  toDate(date_time) {
    let date = new Date(+new Date(date_time)).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
    return date
  },
  toKind(kind) {
    let a = this.data.dict.map(item => {
      console.log(item.dictValue)
      if (kind == item.dictValue) {
        console.log(item.dictLabel)
        return item.dictLabel
      }
    })
    return a[0];
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(!wx.getStorageSync('userId')){
      wx.showModal({
        title: '提示',
        content: '请扫描二维码进入小程序',
        showCancel: false,
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        }
      })
      return false;
    }
    tools.isLogin().then((res) => {
      this.getDict();
    }, (error) => {
      console.log(error);
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})