<template>
  <view>
    <view class="flex justify-center flex-col pt-10 text-align-center text-base">
      <view>
        {{ text }}
      </view>
      <view v-if="showProcess" class="mt-5 font-bold">({{ process }}/{{ maxProcess }})</view>
      <image
        :src="`https://oss-lock.xhjcn.ltd/mp/icon_fingerprint_${process}.png`"
        mode="widthFix"
        class="mx-[250rpx] w-250 mt-5"
      ></image>
      <view class="mt-10">根据提示，抬起手指后再进行下一次指纹采集</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 { addFingerprintRequest } from '@/api/fingerprint'

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

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

  const bindFlag = ref(false)
  const showProcess = ref(false)
  const process = ref(0)
  const maxProcess = ref(0)

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

  onLoad(async options => {
    if (options.info) {
      const params = JSON.parse(options.info)
      uni.$on('registerFingerprintConfirm', async data => {
        if (data.status === 0) {
          bindFlag.value = true
          const { code, message } = await addFingerprintRequest({
            lockId: $bluetooth.currentLockInfo.lockId,
            startDate: params.startDate,
            endDate: params.endDate,
            weekDay: params.weekDays,
            startTime: params.startDate,
            endTime: params.endDate,
            fingerprintName: params.fingerprintName,
            fingerprintNumber: String(data.fingerprintNumber),
            fingerprintUserNo: String(data.fingerprintNumber),
            fingerprintType: params.fingerprintType,
            addType: 1,
            fingerRight: params.isAdmin,
            isCoerced: params.isForce === 1 ? 2 : 1
          })
          if (code === 0) {
            eventChannel.emit('refresherList', {})
            $basic.backAndToast('添加成功', 2)
          } else {
            $basic.backAndToast(message)
          }
        }
      })
      uni.$on('registerFingerprintProcess', data => {
        if (data.status === 0) {
          process.value = data.process
        } 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, data } = await $bluetooth.registerAuthentication(params)

      if (code === 0) {
        text.value = '请将您的手指按下'
        maxProcess.value = data.maxProcess
        showProcess.value = true
      } else if (code === -21) {
        $basic.backAndToast('请先打开蓝牙')
      }
    }
  })

  onUnmounted(() => {
    uni.$off('registerFingerprintConfirm')
    uni.$off('registerFingerprintProcess')
    if (!bindFlag.value) {
      $bluetooth.registerAuthenticationCancel({
        type: 'fingerprint',
        keyId: $bluetooth.keyId.toString(),
        uid: $user.userInfo.uid.toString()
      })
    }
  })
</script>
