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: {
    logs: [],
    sf: ''
  },
  goMydevice() {
    wx.navigateTo({
      url: '/pages/hardwareDetails/hardwareDetails',
    })
  },
  goinfo() {
    wx.navigateTo({
      url: '/pages/info/info',
    })
  },
  async unbindOpenid() {
    let options = {
      url: 'resource/miniapp/user/unbindOpenid',
      data: {
        type: app.globalData.type,
        openId: app.globalData.openid
      },
      method: 'post'
    }
    let res = await request.query(options);
    console.log(res)
    if (res.data.code === 200) {
      wx.removeStorageSync('openid')
      wx.navigateTo({
        url: '/pages/login/login',
      })
    } else {
      Toast.fail(res.data.msg);
    }
  },
  async exitLogin() {
    wx.showModal({
      showCancel: true,
      content: "是否确认退出",
      success: (res) => {
        if (res.confirm) {
          this.unbindOpenid();
        }
      }
    })
  },
  bind() {
    wx.scanCode({
      success: (res) => {
        console.log(res.result, '我成功的参数')
        wx.navigateTo({
          url: '/pages/hardwareBind/hardwareBind?code=' + res.result,
        })
      },
      fail: (res) => {
        console.log(res)
      }
    })
    // wx.navigateTo({
    //   url: '/pages/hardwareBind/hardwareBind',
    // })
  },
  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) => {
      console.log(app.globalData.type)
      this.setData({
        sf: app.globalData.type === 'jhr' ? '监护人' : app.globalData.type === 'jhdx' ? '监护对象' : ''
      })
    }, (error) => {
      console.log(error);
    })
  }
})