<script setup>
import { ref, onMounted, onBeforeUnmount } from "vue";
import { getDeviceBatteryAPI } from "@/apis";

const power = ref(0);
const timer = ref(null);

onMounted(async () => {
  const data = await getDeviceBatteryAPI();
  power.value = data.battery;
  timer.value = setInterval(async () => {
    const data = await getDeviceBatteryAPI();
    power.value = data.battery;
  }, 1000 * 10);
});

onBeforeUnmount(() => {
  clearInterval(timer.value);
});
</script>

<template>
  <view class="container">
    <image src="../static/b-power.png" mode="widthFix" />
    <view>电量{{ power || 1 }}%</view>
  </view>
</template>

<style scoped>
.container {
  display: flex;
  align-items: center;
  color: #ffffffa8;
  font-size: 13px;
}
.container > image {
  width: 20px;
  margin-right: 5px;
}
</style>
