<template>
  <view>
    <view class="mx-4 pt-5 text-base">
      <view class="text-sm">
        <view> 本操作将上传锁内数据到服务器，过程可能需要几分钟，请耐心等待</view>
      </view>
      <view
        @click="asyncData"
        class="w-full bg-#63b8af text-white line-height-80rpx h-80 rounded-40rpx text-center mt-4 text-lg font-bold"
      >
        开始
      </view>
    </view>
  </view>
</template>

<script setup>
  import { ref } from 'vue'
  import { useBluetoothStore } from '@/stores/bluetooth'
  import { useUserStore } from '@/stores/user'
  import { getLockSettingRequest, lockDataUploadRequest } from '@/api/setting'
  import { useBasicStore } from '@/stores/basic'

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

  const progress = ref(0)
  const page = ref(0)

  const list = ref([])

  const pending = ref(false)

  const typeList = ['password', 'card', 'fingerprint', 'face', 'palmVein', 'remote', 'setting']

  const asyncData = async (flag = false) => {
    if (pending.value && !flag) return
    const netWork = await $basic.getNetworkType()
    if (!netWork) {
      return
    }
    pending.value = true
    uni.showLoading({
      title: `上传中${progress.value}/7`
    })
    const { code, data } = await $bluetooth.getLockDataList({
      type: typeList[progress.value],
      page: page.value,
      lockId: $bluetooth.currentLockInfo.lockName.toString(),
      uid: $user.userInfo.uid.toString(),
      countReq: 10
    })
    console.log('code value :', code)
    if (code === 0) {
      if (typeList[progress.value] === 'setting') {
        const { code: resultCode } = await lockDataUploadRequest({
          lockId: $bluetooth.currentLockInfo.lockId,
          uploadType: 1,
          records: data.list
        })
        uni.hideLoading()
        pending.value = false
        $bluetooth.closeBluetoothConnection()
        if (resultCode === 0) {
          const { code, data } = await getLockSettingRequest({
            lockId: $bluetooth.currentLockInfo.lockId
          })
          if (code === 0) {
            if (data.lockSettingInfo.catEyeConfig[0]) {
              data.lockSettingInfo.catEyeConfig[0].catEyeModeConfig.recordTime = Number(
                data.lockSettingInfo.catEyeConfig[0].catEyeModeConfig.recordTime
              )
              data.lockSettingInfo.catEyeConfig[0].catEyeModeConfig.detectionDistance = Number(
                data.lockSettingInfo.catEyeConfig[0].catEyeModeConfig.detectionDistance
              )
            }
            $bluetooth.updateCurrentLockSetting(data)
          }
          uni.showToast({
            title: '上传成功',
            icon: 'none'
          })
        } else {
          uni.showToast({
            title: '上传失败，请重试',
            icon: 'none'
          })
        }
        return
      }
      list.value = list.value.concat(data.list)
      if (data.size === 10) {
        page.value++
      } else {
        const { code: resultCode } = await lockDataUploadRequest({
          lockId: $bluetooth.currentLockInfo.lockId,
          uploadType: 2,
          recordType: progress.value + 2,
          records: list.value
        })
        if (resultCode !== 0) {
          pending.value = false
          $bluetooth.closeBluetoothConnection()
          uni.hideLoading()
          uni.showToast({
            title: '上传失败，请重试',
            icon: 'none'
          })
          return
        }
        page.value = 0
        list.value = []
        progress.value++
      }
      await asyncData(true)
    } else if (code === -21) {
      pending.value = false
      $bluetooth.closeBluetoothConnection()
      uni.hideLoading()
    } else {
      pending.value = false
      $bluetooth.closeBluetoothConnection()
      uni.hideLoading()
      uni.showToast({
        title: '上传失败，请保持在锁附近',
        icon: 'none'
      })
    }
  }
</script>

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