<template>
  <view>
    <view class="mx-4 pt-5 text-base">
      <view>电量信息可以通过网关远程更新，或通过手机蓝牙在锁旁边更新</view>
      <view class="mt-5"
        >电池1电量：{{ $bluetooth.currentLockSetting.lockBasicInfo.electricQuantity }}%</view
      >
      <view class="mt-2" v-if="$bluetooth.currentLockSetting?.lockFeature?.isSupportBackupBattery"
        >电池2电量：{{ $bluetooth.currentLockSetting.lockBasicInfo.electricQuantityStandby }}%</view
      >
      <view class="mt-2">
        电量更新时间：{{
          timeFormat(
            $bluetooth.currentLockSetting.lockBasicInfo.electricQuantityDate,
            'yyyy-mm-dd h:M'
          )
        }}
      </view>
      <view
        @click="updateElectricQuantity"
        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 { timeFormat } from 'uview-plus'
  import { ref } from 'vue'
  import { useBluetoothStore } from '@/stores/bluetooth'
  import { useUserStore } from '@/stores/user'
  import { updateElectricQuantityRequest } from '@/api/room'
  import { useLockStore } from '@/stores/lock'

  const $bluetooth = useBluetoothStore()
  const $user = useUserStore()
  const $lock = useLockStore()

  const pending = ref(false)

  const updateElectricQuantity = async () => {
    if (pending.value) return
    uni.showLoading({
      title: '更新中'
    })
    pending.value = true
    const { code } = await $bluetooth.updateServerTimestamp()
    if (code === 0) {
      const date = new Date()
      const timestamp = $bluetooth.serverTimestamp - date.getTimezoneOffset() * 60
      const { code: lockStatus, data } = await $bluetooth.getLockStatus({
        name: $bluetooth.currentLockInfo.lockId.toString(),
        uid: $user.userInfo.uid.toString(),
        nowTime: $bluetooth.serverTimestamp,
        localTime: timestamp
      })
      $bluetooth.closeBluetoothConnection()
      if (lockStatus === 0) {
        const { code: resultCode, data: resultData } = await updateElectricQuantityRequest({
          lockId: $bluetooth.currentLockInfo.lockId,
          electricQuantity: data.lockConfig.electricQuantity,
          electricQuantityStandby: data.lockConfig.electricQuantityStandby
        })
        uni.hideLoading()
        pending.value = false
        if (resultCode === 0) {
          $bluetooth.updateCurrentLockInfo({
            ...$bluetooth.currentLockInfo,
            electricQuantity: data.lockConfig.electricQuantity,
            electricQuantityStandby: data.lockConfig.electricQuantityStandby,
            electricQuantityDate: resultData.electricQuantityDate
          })
          $bluetooth.updateCurrentLockSetting({
            ...$bluetooth.currentLockSetting,
            lockBasicInfo: {
              ...$bluetooth.currentLockSetting.lockBasicInfo,
              electricQuantity: data.lockConfig.electricQuantity,
              electricQuantityStandby: data.lockConfig.electricQuantityStandby,
              electricQuantityDate: resultData.electricQuantityDate
            }
          })
          $lock.updateLockSearch({
            ...$lock.lockSearch,
            pageNo: 1
          })
          $lock.getLockList($lock.lockSearch)
          uni.showToast({
            title: '更新成功',
            icon: 'none'
          })
        } else {
          uni.showToast({
            title: '更新失败',
            icon: 'none'
          })
        }
      } else {
        uni.hideLoading()
        pending.value = false
        uni.showToast({
          title: '更新失败',
          icon: 'none'
        })
      }
    } else {
      uni.hideLoading()
      pending.value = false
      uni.showToast({
        title: '更新失败',
        icon: 'none'
      })
    }
  }
</script>
