const app = getApp();
Page({
  data: {
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '设置网络', //导航栏 中间的标题
    },
    scopeBluetooth: app.globalData.scopeBluetooth,
    isShowPwd: false,
    is5GWifi: false,
    ssid: "",
    pwdData: "",
    pwdData1: "",
  },

  onShow() {
    var that = this;
    that.getWifiStatus();
  },

  getBluetoothStatus() {
    app.getBluetoothStatus();
  },

  ///Wifi名称
  setSsid: function (e) {
    var that = this;
    let str = false;
    if (e.detail.value.indexOf("5G") !== -1) {
      str = true;
    };
    that.setData({
      ssid: e.detail.value,
      is5GWifi: str
    });
  },

  ///Wifi名称
  setPwd: function (e) {
    var that = this;
    app.globalData.pwdData = e.detail.value;
    that.setData({
      pwdData: e.detail.value,
    });
  },

  ///获取网络状态
  getWifiStatus() {
    var that = this;
    wx.getNetworkType({
      success(res) {
        if (res.networkType === "wifi") {
          wx.startWifi({
            success(res) {
              //获取当前已连接wifi名
              wx.getConnectedWifi({
                success: function (res) {
                  var ssid = res.wifi.SSID;
                  // 5Gwifi
                  if (res.wifi.SSID.indexOf("5G") !== -1) {
                    that.setData({
                      is5GWifi: true,
                      ssid: ssid,
                    });
                  } else {
                    that.setData({
                      is5GWifi: false,
                      ssid: ssid,
                    });
                  };

                  app.globalData.ssid = ssid;
                  wx.getStorage({
                    key: 'wifiInfo',
                    success(wifiInfo) {
                      const resData = JSON.parse(wifiInfo.data);
                      if (resData.ssid === res.wifi.SSID) {
                        that.setData({
                          pwdData: resData.password,
                        });
                      }
                    }
                  })
                },
              })
            }
          })
        }
      }
    })
  },

  ///下一步
  next() {
    var that = this;
    if (that.data.ssid === "") {
      wx.showToast({
        title: '请输入WIFI名称',
        icon: 'none',
        duration: 2000
      })
      return;
    };

    if (that.data.pwdData === "") {
      wx.showToast({
        title: '请输入WIFI密码',
        icon: 'none',
        duration: 2000
      })
      return;
    };

    app.globalData.ssid = that.data.ssid;
    app.globalData.pwdData = that.data.pwdData;
    wx.navigateTo({
      url: './../deviceConnect2/deviceConnect2',
    })
  },

  notRoter() {
    wx.navigateBack({
      delta: 1
    });
  },

  showPwd() {
    var that = this;
    that.setData({
      isShowPwd: !that.data.isShowPwd
    })
  },

})