<style lang="less">
.component-location {
  border-radius: 16rpx;
  background: rgba(255, 255, 255, 0.9);
  width: 96rpx;
  height: 96rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin-bottom: 16rpx;
  .icon {
    width: 56rpx;
    height: 56rpx;
  }
}
</style>
<template>
  <view class="component-location" v-on:click.stop="position">
    <image class="icon" src="{{h5StaticPath+'icon-location.svg'}}" />
  </view>
</template>
<script>
import wepy from '@wepy/core';
import config from '@/config';
let hasInit = false;
let h5StaticPath = config.h5StaticPath + '/page-intelligent-control/';
wepy.component({
  data: {
    h5StaticPath
  },
  methods: {
    position() {
      return this.startBeaconDiscovery().then(res => {
        let devices = [];
        wx.showLoading({
          title: '正在定位..', //提示的内容,
          mask: true, //显示透明蒙层，防止触摸穿透,
          success: res => {}
        });
        
        wx.onBeaconUpdate(res => {
          if (res && res.beacons && res.beacons.length > 0) {
            res.beacons.forEach(beacon => {
              devices.push(beacon);
            });
          } 
        });
        setTimeout(() => {
          wx.hideLoading();
          wx.offBeaconUpdate();
          this.stopBeaconDiscovery();
          if(!devices.length){
            wx.showToast({
              title: '未搜索到蓝牙信标',
              icon: 'none'
            });
            return;
          }
          this.$emit('component-location-position', devices);
        }, 4*1000);
      });
    },
    getBeacons() {
      return new Promise((resolve, reject) => {
        wx.getBeacons({
          success: res => {
            resolve(res);
          },
          fail: res => {
            wx.showToast({
              title: res.errorMessage,
              duration: 1000,
              icon: 'none'
            });
            reject(res);
          }
        });
      });
    },
    startBeaconDiscovery() {
      return new Promise((resolve, reject) => {
        wx.startBeaconDiscovery({
          uuids: ['00000000-0000-0000-0000-000000000006'],
          success: res => {
            resolve(res);
          },
          fail: res => {
            let msg = res.errMsg;
            switch (res.errCode) {
              case 11000:
                msg = '未开启蓝牙权限';
                break;
              case 11001:
                msg = '未开启蓝牙权限';
                break;
              case 11003:
                msg = '定位中，请稍候';
                break;
            }
            wx.showToast({
              title: msg,
              duration: 1000,
              icon: 'none'
            });
            reject(res);
          }
        });
      });
    },
    stopBeaconDiscovery() {
      return new Promise((resolve, reject) => {
        wx.stopBeaconDiscovery({
          success: res => {
            resolve(res);
          },
          fail: res => {
            dd.showToast({
              content: res.errorMessage,
              duration: 1000
            });
            reject(res);
          }
        });
      });
    },
    connectBLEDevice(devicedId) {
      return new Promise((resolve, reject) => {
        wx.startBeaconDiscovery({
          deviceId: devicedId,
          success: res => {
            resolve(res);
          },
          fail: res => {
            wx.showToast({
              title: res.errorMessage,
              duration: 1000,
              icon: 'none'
            });
            reject(res);
          }
        });
      });
    }
  }
});
</script>