const app = getApp();
import route_constant from '../../utils/route_constant.js'
import route_util from '../../utils/route_util.js';

Page({
  data: {
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '连接配网', //导航栏 中间的标题
    },
    scopeBluetooth: false,
    userFuzzyLocation: true,
    isNotruter: false
  },

  ///定位，蓝牙权限，蓝牙适配器
  onLoad(options) {
    var that = this;
    wx.offAppShow(that.getSetting);
    wx.onAppShow(that.getSetting);
    that.getSetting();
    if (options.isLognlink) {
      that.setData({
        isNotruter: true,
        nvabarData: {
          showCapsule: 1,
          title: '连接配网',
          callback: () => {
            route_util.goBackRoute(route_constant.indexRoot);
          }
        },
      })
    }
  },

  ///获取定位服务
  getSetting() {
    var that = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting["scope.userFuzzyLocation"]) {
          app.getBluetoothStatus();
          that.setData({
            userFuzzyLocation: true,
          });
        } else if (res.authSetting["scope.userFuzzyLocation"] === undefined) {
          ///请求定位权限
          wx.authorize({
            scope: "scope.userFuzzyLocation",
            success() {
              that.getSetting();
            }
          });
        } else {
          that.setData({
            userFuzzyLocation: false,
          });
          wx.showModal({
            title: '请打开系统位置获取',
            success(res) {
              if (res.confirm) {
                wx.openSetting({
                  complete() {
                    // that.getSetting();
                  }
                })
              } else if (res.cancel) {}
            }
          });
        }
      }
    })
  },

  ///检测蓝牙权限回调 true:false
  getBluetoothStatusCallck(v) {
    var that = this;
    that.setData({
      scopeBluetooth: v
    });
  },

  ///下一步
  next() {
    var that = this;
    ///初始化蓝牙
    wx.openBluetoothAdapter({
      success(res) {
        if (that.data.scopeBluetooth && that.data.userFuzzyLocation) {
          wx.navigateTo({
            url: './../deviceConnect1/deviceConnect1',
          });
        }
      },
      fail(res) {
        if (res.errCode === 10001) {
          wx.showToast({
            title: '蓝牙未打开',
            icon: "none",
            duration: 2000
          })
        }
      }
    });
  },

  ///手动开启定位权限
  setUserFuzzyLocation() {
    const that = this;
    wx.openSetting({
      complete() {
        // that.getSetting();
      }
    })
  },

  //手动开启蓝牙
  setBluetooth() {
    wx.openSetting({
      complete() {
        // app.getBluetoothStatus();
      }
    })
  },

  ///返回到首页
  notRoter() {
    route_util.goBackRoute(route_constant.indexRoot);
  },

  onUnload() {
    var that = this;
    ///传入的监听函数。不传此参数则移除所有监听函数
    wx.offAppShow(that.getSetting);
  },
})