// pages/deviceConnect0/deviceConnect0.ts
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scopeBluetooth: false,
    userFuzzyLocation: true,
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '连接配网', //导航栏 中间的标题
      callback() {
        wx.navigateBack({
          delta: 1,
        })
      }
    },
    isNotruter: false
  },
  onShow() {
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.offAppShow(this.getSetting);
    wx.onAppShow(this.getSetting);
    this.getSetting();
    if (options.isLognlink) {
      this.setData({
        isNotruter: true,
        nvabarData: {
          showCapsule: 1,
          title: '连接配网',
          callback: () => {
            wx.switchTab({
              url: "./../index/index"
            })
          }
        },
      })
    }
  },
  getBluetoothStatusCallck(v) {
    this.setData({
      scopeBluetooth: v
    })
  },
  getSetting() {
    const _this = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting["scope.userFuzzyLocation"]) {
          // 成功
          app.getBluetoothStatus();
          _this.setData({
            userFuzzyLocation: true,
          });
        } else if (res.authSetting["scope.userFuzzyLocation"] === undefined) {

          wx.authorize({
            scope: "scope.userFuzzyLocation",
            success() {
              _this.getSetting();
            }
          });
        } else {
          _this.setData({
            userFuzzyLocation: false,
          });
          wx.showModal({
            title: '请打开系统位置获取',
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.openSetting({
                  complete() {
                    // _this.getSetting();
                  }
                })
              } else if (res.cancel) {
                console.log('用户点击取消');
              }
            }
          })
        }
      }
    })
  },
  next() {
    const _this = this;
    wx.openBluetoothAdapter({
      success(res) {
        if (_this.data.scopeBluetooth && _this.data.userFuzzyLocation) {
          wx.navigateTo({
            url: './../deviceConnect1/deviceConnect1',
          });
        }
      },
      fail(res) {
        if (res.errCode === 10001) {
          wx.showToast({
            title: '蓝牙未打开',
            icon: "none",
            duration: 2000
          })
        }
      }
    });
  },
  setBluetooth() {
    wx.openSetting({
      complete() {
        // app.getBluetoothStatus();
      }
    })
  },
  setUserFuzzyLocation() {
    const _this = this;
    wx.openSetting({
      complete() {
        // _this.getSetting();
      }
    })
  },
  notRoter() {
    getCurrentPages()[0].onLoad();
    wx.switchTab({
      url: "./../index/index"
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    wx.offAppShow(this.getSetting);
  },

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

  },

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

  },

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

  }
})