<template>
  <view>
    <view class="flex justify-center flex-col pt-20">
      <image
        src="/static/images/icon_add_palm_vein.png"
        mode="aspectFill"
        class="mx-[125rpx] w-500 h-500"
      ></image>
      <view
        class="text-base rounded-xl mt-15 bg-#5eb4ac mx-[32rpx] w-686 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 { addPalmVeinRequest } from '@/api/palmVein'

  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)

      uni.$on('registerPalmVeinConfirm', async data => {
        if (data.status === 0) {
          bindFlag.value = true
          const { code, message } = await addPalmVeinRequest({
            lockId: $bluetooth.currentLockInfo.lockId,
            startDate: params.startDate,
            endDate: params.endDate,
            startTime: params.startDate,
            endTime: params.endDate,
            palmVeinName: params.palmVeinName,
            palmVeinNumber: String(data.palmVeinNumber),
            palmVeinUserNo: String(data.palmVeinNumber),
            palmVeinType: params.palmVeinType,
            weekDay: params.weekDays,
            addType: 1,
            palmVeinRight: 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) {
          $basic.backAndToast('管理员已满')
        } else if (data.status === 0xfd) {
          $basic.backAndToast('用户已满')
        } else if (data.status === 0xfc) {
          $basic.backAndToast('掌静脉已满')
        } else if (data.status === 0xfb) {
          uni.showToast({
            title: '掌静脉已存在',
            icon: 'none'
          })
        }
      })

      const { code } = await $bluetooth.registerAuthentication(params)

      if (code === 0) {
        text.value = '已连接到锁，请自然张开手掌，掌心对准摄像头'
      } else if (code === -21) {
        $basic.backAndToast('请先打开蓝牙')
      }
    }
  })

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