<template>
  <view>
    <view class="flex justify-center flex-col pt-20">
      <image
        src="/static/images/icon_add_remote.png"
        mode="aspectFill"
        class="mx-[200rpx] w-350 h-350"
      ></image>
      <view class="mt-5">
        <up-loading-icon size="70rpx" text="" :vertical="true" textSize="32rpx"></up-loading-icon>
      </view>
      <view
        class="text-base rounded-xl mt-15 bg-black mx-[75rpx] w-600 h-80 text-align-center line-height-[80rpx] text-white shadow-sm"
        >{{ text }}</view
      >
    </view>
  </view>
</template>

<script setup>
  import { ref, onUnmounted, getCurrentInstance } from 'vue'
  import { onLoad } from '@dcloudio/uni-app'
  import { useBluetoothStore } from '@/stores/bluetooth'
  import { useBasicStore } from '@/stores/basic'
  import { useUserStore } from '@/stores/user'
  import { addRemoteRequest } from '@/api/remote'

  const instance = getCurrentInstance().proxy
  const eventChannel = instance.getOpenerEventChannel()

  const $bluetooth = useBluetoothStore()
  const $basic = useBasicStore()
  const $user = useUserStore()

  const bindFlag = ref(false)

  const text = ref('尝试连接设备…')

  onLoad(async options => {
    if (options.info) {
      const params = JSON.parse(options.info)
      const { code } = await $bluetooth.registerAuthentication(params)

      if (code === 0) {
        text.value = '已连接到锁，请按遥控'
      } else {
        $basic.backAndToast('操作失败，请重试')
      }

      uni.$on('registerRemoteConfirm', async data => {
        if (data.status === 0) {
          bindFlag.value = true
          const { code, message } = await addRemoteRequest({
            lockId: $bluetooth.currentLockInfo.lockId,
            startDate: params.startDate,
            endDate: params.endDate,
            remoteName: params.remoteName,
            remoteNumber: String(data.remoteNumber),
            remoteType: params.remoteType,
            addType: 1,
            remoteRight: params.isAdmin,
            isCoerced: params.isForce === 1 ? 2 : 1
          })
          if (code === 0) {
            eventChannel.emit('refresherList', {})
            $basic.backAndToast('添加成功', 2)
          } else {
            $basic.backAndToast(message)
          }
        } else if (data.status === 0xff) {
          $basic.backAndToast('添加失败，请重试')
        } else if (data.status === 0xfe) {
          uni.showToast({
            title: '管理员已满',
            icon: 'none'
          })
        } else if (data.status === 0xfd) {
          uni.showToast({
            title: '用户已满',
            icon: 'none'
          })
        } else if (data.status === 0xfc) {
          uni.showToast({
            title: '遥控已满',
            icon: 'none'
          })
        } else if (data.status === 0xfb) {
          uni.showToast({
            title: '遥控已存在',
            icon: 'none'
          })
        }
      })
    }
  })

  onUnmounted(async () => {
    uni.$off('registerRemoteConfirm')
    if (!bindFlag.value) {
      await $bluetooth.registerAuthenticationCancel({
        type: 'remote',
        keyId: $bluetooth.keyId.toString(),
        uid: $user.userInfo.uid.toString()
      })
    }
    $bluetooth.closeBluetoothConnection()
  })
</script>

<style lang="scss">
  page {
    background-color: $uni-bg-color-grey;
  }
</style>
