// pages/deviceDetail/detail.js

const BtHelper = require('../../devices/bt_helper');
const { deviceVersion } = require('../../request/deviceListRequest')
import EventManager from '../../utils/event_bus'

Page({
  data: {
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '设备详情', //导航栏 中间的标题
    },
    hasNew: true,
    // 页面数据
    device: getApp().globalData.device,
    btHelper: null,
  },
  checkOtaVersion(device) {
    let _this = this;
    deviceVersion(device.clienlType, device.connectType, device.version).then(res => {
      console.log(res);
      let updateData = res.data;
      let hasNewVersion = updateData.isEnforcement === 1 || updateData.isEnforcement === 2;
      _this.setData({
        hasNew: hasNewVersion,
      });
    })
  },


  goToWallpaper: function () {

    wx.navigateTo({
      url: '/pages/piano/wallpaper/wallpaper'
    });
  },

  goToOta: function () {

    wx.navigateTo({
      url: '/pages/piano/wallpaper/wallpaper'
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    let device = getApp().globalData.device;
    if (!device.connect) {
      // btHelper
      wx.showToast({
        title: '设备已经失去连接',
      })
      return;
    }

    const btHelper = BtHelper.getInstance();
    btHelper.getVersion()

    this.data.btHelper = btHelper;
    this.checkOtaVersion(device);
    EventManager.addNotification(CmdEvent,)
  },

})