<script>
  import { useBluetoothStore } from '@/stores/bluetooth'
  import { useUserStore } from '@/stores/user'
  import { useBasicStore } from '@/stores/basic'
  import { mapState, mapActions } from 'pinia'
  let firstCheckSetting = true

	export default {
    globalData: {
      // 更新登录状态
      updateIsLogin(isLogin) {
        useUserStore().updateLoginStatus(isLogin)
      },
      // 账号信息
      appid: '',
      // 小程序版本
      envVersion: '',
      // 获取环境配置
      getEnvConfig() {
        const envVserionStorage = uni.getStorageSync('envVersion')
        if(envVserionStorage) {
          return envVserionStorage
        } else {
          if(this.envVersion === 'develop') {
            return 'DEV'
          } else if(this.envVersion === 'trial') {
            return 'PRE'
          } else {
            return 'PROD'
          }
        }
      }
    },
    computed: {
      ...mapState(useBluetoothStore, ['bluetoothStatus']),
    },
    onLaunch: async function() {
      // 检查强制升级
      this.updateMiniProgram()
      // 检查蓝牙权限
      const checkResult = await this.checkSetting()
      console.log(checkResult)
      if(checkResult === true) {
        this.initAndListenBluetooth(false)
      }
		},
		onShow() {
      if(firstCheckSetting) {
        firstCheckSetting = false
      } else {
        this.checkSetting()
      }
		},
    methods: {
      ...mapActions(useBluetoothStore, ['initBluetooth', 'onBluetoothState', 'updateBluetoothStatus', 'checkSetting',
      'onBluetoothConnectStatus', 'onBluetoothCharacteristicValueChange', 'updateInitBluetooth', 'initAndListenBluetooth']),
      ...mapActions(useUserStore, ['updateLoginStatus']),
      // 强制升级
      updateMiniProgram() {
        const updateManager = uni.getUpdateManager()
        updateManager.onUpdateReady(function () {
          uni.showModal({
            title: '更新提示',
            content: '新版本已经准备好，是否重启应用？',
            showCancel: false,
            success: function () {
              updateManager.applyUpdate()
            }
          })
        })
      }
    }
	}
</script>

<style lang="scss">
  @import "uview-plus/index.scss";
</style>
