<template>
  <view class="content"style="background: white">
    <view class="text-area">
      <u--form style="width: 100%;"
               labelPosition="left"
               ref="uForm"
      >
        <u-form-item
            label="WIFI名称"
            borderBottom
            labelWidth="auto"
            @click="openChooseWifi"
            ref="item1"
        >
          <u--input
              v-model="SSID"
              disabled
              disabledColor="#ffffff"
              placeholder="请选择wifi"
              border="none"
          ></u--input>
          <u-icon
              slot="right"
              name="arrow-right"
          ></u-icon>
        </u-form-item>
        <u-form-item
            label="密码"
            borderBottom
            ref="item1"
        >
          <u--input
              v-model="password"
              border="none"
          ></u--input>
        </u-form-item>

      </u--form>

    </view>
    <view style="margin:10px">
      <u-button text="开始配网" @click="doConnect" size="small" type="primary"></u-button>

    </view>
    <u-picker @cancel="showWiftList=false" @confirm="chooseWifi"  :show="showWiftList" :columns="wifiList"></u-picker>
  </view>
</template>

<script>
  export default {
    data(){

      return{
        showWiftList:false,
        wifiList: [
          []
        ],
        SSID:"",
        password:""
      }
    },
    onLoad: function() {
      this.openWifi();
    },
    methods:{
		getAuth(){
			wx.getSetting({
			  success(res) {
			    if (!res.authSetting['scope.userLocation']) {
			      wx.authorize({
			        scope: 'scope.userLocation',
			        success () {
			          // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问

			        }
			      })
			    }
			  }
			})
		},
      chooseWifi(e){
        this.SSID = e.value[0];
        this.showWiftList= false;
      },
      openChooseWifi(){
        if(this.wifiList[0].length == 0){
            this.getWifiList();
        }else{
          this.showWiftList = true;
        }
      },
      openWifi(){
        let self = this;
        // #ifdef MP-WEIXIN
        wx.startWifi({
          success(res) {
            console.log(res);
            // self.getWifiList();
          },
          fail(res) {
            console.log(res)
            uni.showToast({
              title: '请打开WIFI',
              icon: 'none',
              duration: 3000
            });

          },
        })
        // #endif

      },
      getWifi(){
        // #ifdef MP-WEIXIN
        var that = this
        wx.getConnectedWifi({
          success(res) {
            console.log(res)
            that.BSSID = res.wifi.BSSID
            that.WIFIName = res.wifi.SSID
          },
          fail(res) {
            console.log(res)
            //报错的相关处理
          },
        })
        // #endif

      },
      getWifiList(){
        // #ifdef MP-WEIXIN
        var that = this
        uni.showLoading();
        wx.getWifiList({
          success(res) {
            console.log(res)
            wx.onGetWifiList(function(res) {
              that.showWiftList = true;
              uni.hideLoading();
              console.log("获取wifi列表");
              that.wifiList = [[]];
              console.log(res.wifiList); //在这里提取列表数据
              //通过遍历将WIFI名字存入集合，以便下卡框等组件使用
              for (var i = 0; i < res.wifiList.length; i++) {
                that.wifiList[0].push(res.wifiList[i].SSID)
              }
            })
          },
          fail(res) {
            console.log(res)
            uni.showToast({
              title: '获取wifi失败，请检查wifi',
              icon: 'none',
              duration: 2000
            });
          },
        })
        // #endif

      },
      doConnect(){
        // #ifdef MP-WEIXIN
        const airkiss = requirePlugin('airkiss');
        if (this.SSID != '' && this.password != '') {
          console.log(airkiss)
          uni.showLoading({
            title: '配网中请稍后..'
          });
          airkiss.startAirkiss(this.SSID, this.password, function(res) {
            console.log(res)
            switch (res.code) {
              case 0:
                uni.hideLoading();
                uni.showModal({
                  title: '初始化失败',
                  content: res.result,
                  showCancel: false,
                  confirmText: '收到',
                })
                break;
              case 1:
                uni.hideLoading();
                uni.showModal({
                  title: '配网成功',
                  content: '设备IP：' + res.ip + '\r\n 设备Mac：' + res.bssid,
                  showCancel: false,
                  confirmText: '好的',
                })
                break;
              case 2:
                uni.hideLoading();
                uni.showModal({
                  title: '配网失败',
                  content: '请检查密码是否正确',
                  showCancel: false,
                  confirmText: '收到',
                })
                break;

              default:
                uni.hideLoading();
                break;
            }

          })
        } else {
          uni.showToast({
            title: '请选择WIFI并输入密码',
            icon: 'none',
            duration: 3000
          });
        }
        // #endif

      }
    }
  }
</script>

<style>
  .content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }


  .text-area {
    display: flex;
    justify-content: center;
    background: white;
    width: 100%;
    height: 100%;
  }

  .title {
    font-size: 36rpx;
    color: #8f8f94;
  }
</style>
